Laravel documentation for testing? - php

Is there a thorough documentation for different commands for writing phpunit tests with Laravel?
All I can find are introduction-level docs like https://laravel.com/docs/5.3/application-testing, but nothing that would give more info about the available methods and their parameters.
Also, I haven't found a way to extend the test files beyond the default
php artisan make:test SomeTest
At least I would like to change the testExample() function name to something more describing and add several functions in the test file.

Related

How to execute a PHP script who depends on project classes?

I have to execute a script which is in the project's root directory.
My script is instancing some classes, but unfortunately, it says that my class isn't found when I do a "PHP myscript.php".
I can try with includes, but I have several errors when a class extends another (external abstract class). Is there a way to make this script "part of the project", in order to not include each class?
I read a similar question but it does not answer exactly to my question: How do you execute a method in a class from the command line
Thanks,
If you want a script that is reusable I would recommend creating a command and executing that command when needed.
See: The laravel docs
EDIT
Sorry, I falsely pressumend you were using the Laravel Framework, for Zend see: The Zend/console docs

Symfony 3 setting up a test database

I'm trying to set up a testing suite for my Symfony 3 app, and I'm wondering what the correct method for setting up the test database is. I've found this article, but it doesn't actually explain how to add fixtures programmatically.
Also, it appears their example sets up the test database for every test.
Is there a way to setup a test database which is automatically loaded with fixtures when phpunit is run? The official documentation is kind of sparse
Symfony has different environments you can operate in. By default those are prod(production), dev(developement) and test. Although it may not be exactly what you want, you can configure different config, paramaters, routes and so on for each environment. Read the official documentation for more info but yeah, you can setup your parameters.yml file for test mode which could have a different database configured there.
https://symfony.com/doc/current/configuration/environments.html
If you extend your TestCases from KernelTestCase or WebTestCase, basically extend from \PHPUnit_Framework_TestCase you have methods like setUpBeforeClass() or setUp() which are run by PhpUnit before your test/test cases is/are executed.
Use this to e.g. create your fixtures, load your SQL file or whatever and however you might require your prerequisites for your tests.

How to call CLI from a controller on ZF2

Our system is written in ZF2. I'm trying to call a ZFTool console command programmatically, if there's a way to do that. All documentation I have found so far points to call a Controller action from CLI, instead of CLI programmatically from a controller.
If there's no default way to do this, a workaround would be fine as long as it's testable. Thanks in advance.
PS: I'm new to ZF2, I come from Laravel where you have a facade class to execute commands from a controller class Artisan::call('my:command').

Can we use Symfony\Bundle\FrameworkBundle\Test\WebTestCase for symfony 2 console commands testing?

Using Symfony\Bundle\FrameworkBundle\Test\WebTestCase, we get easy access to container, entity manager etc. We can use it for functional testing by automatic manual HTTP requests.
Can we use it to test Symfony2 console commands as well so that we can have easy access to container and all services?
I want to test my costum Symfony2 console commands which uses many services which in turn uses doctrine entity manager to access data.
PHPunit documentation suggest to extend the test class with PHPUnit_Extensions_Database_TestCase,
Can we extend WebTestCase instead of test instead to test console commands ?
I have already refereed
How to test Doctrine Repositories Cookbook
How to test code that interacts with the Database Cookbook
The Console Component Docs
WebTestCase is meant for functional testing your web applications. Nothing will stop you from using it to test commands, but it doesn't feel right (hence your question).
Testing commands
Remember, that command tests (as well as controller tests) shouldn't be complex, just like the code you're putting in there shouldn't be complex either.
Treat your commands as controllers, make them slim and put your business logic where it belongs - to the model.
Accessing the container
Having said that, you can implement your own KernelAwareTestCase (or ContainerAwareTestCase) yourself. Here's a base class I'm using occasionally: jakzal / KernelAwareTest.php Gist
Also, note that next to Symfony\Component\Console\Application there's a Symfony\Bundle\FrameworkBundle\Console\Application which can actually work with the Symfony kernel.
Final note
Remember, that the most extensive testing should be done on a unit level.

yii phpunit testing with views in coverage

My Yii app getting phpunit coverage in controllers, models, components, modules but not any views. The problem is that code coverage loader in phpunit includes the view file during preparing a coverage report. Views with forms have calls $this->beginWidget which causes a crash since there is no $this context.
Views dont really have important code or logic but still they have some conditions and even loops to call renderPartial so it would be good to get a view code also covered.
Is there a solution to this problem?
Have you tried extending CWebTestCase? Generally when writing unit tests, you have fixtures and things to provide the necessary data - but with tests on views and 'functional' tests, for web-apps, it's generally easiest to mimic a browser and have it perform actions on the web app as if a user was actually using it. Currently, this mimicking is most easily done with Selenium (in my opinion).
The Yii Guide on Functional Testing is a good place to start as well as the Selenium Documentation. There's also this book that goes over using selenium (I'm not sure if the newest edition does, but I know the previous release with Publication Date: August 11, 2010 did), and Larry Ullman's Yii Book will have chapters on testing and the usage of Selenium in functional tests when he's completed that chapter.
Hope this helps!
Update to further explain CWebTestCase
CWebTestCase extends PHPUnit_Extensions_SeleniumTestCase which directly implements a functional testing foundation that you can use within phpunit in order to test views, test widget creation inside of views, assert that text exists, 'click' on links, etc. These tests are still run from the command line though they require that Selenium-RC server be started upon the test being run and they require a valid browser being configured. A valid browser can be configured with as little code as the following placed inside of the setUp() function:
$this->setBrowser('*firefox /usr/lib/firefox/firefox-bin');
Stating that code coverage cannot be provided by CWebTestCase is not true, as CWebTestCase extends PHPUnit_Extensions_SeleniumTestCase, which provides the following as quoted from the documentation:
PHPUnit_Extensions_SeleniumTestCase can collect code coverage
information for tests run through Selenium:
Copy PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php into
your webserver's document root directory. In your webserver's php.ini
configuration file, configure
PHPUnit/Extensions/SeleniumTestCase/prepend.php and
PHPUnit/Extensions/SeleniumTestCase/append.php
as the auto_prepend_file and auto_append_file, respectively. In your test
case class that extends PHPUnit_Extensions_SeleniumTestCase, use
protected $coverageScriptUrl = 'http://host/phpunit_coverage.php';
to configure the URL for the phpunit_coverage.php script.

Categories