Setting up unit testing in the Zend Framework - php

I'm just getting started with unit testing in the zend framework. I've read lots of the docs ZF and PhpUnit but there are a few things I can't figure out.
I have Zend Framework set up. Do I need to install PHPUnit too or is it all sorted within the framework?
Zend Framework has created files for the unit tests under /tests/application/controllers/ControllerNameTest.php. I assume I create my tests here.
How do I run a test? I'm sure this is really simple because the docs I have read assume I should know how to do this. Do I do it from the command line? If so, how?
Any help appreciated.
Thanks

You need to install PHPUnit, too. Zend Framework and PHPUnit are two different things. You find the installation instructions for PHPUnit here: https://github.com/sebastianbergmann/phpunit.
Basically, You can put the tests where you want. But the tests-Folder is a good place for them.
After you have installed phpunit, you can call your unit tests from the command line. Just enter the folder where you put your tests on command line and type "phpunit", this will run all tests in the folder. You can also use the --filter option to run a single test.

You need to install the phpunit. The easiest way in my opinion is using PEAR (http://www.phpunit.de/manual/current/en/installation.html#installation.pear).
For testing controllers you can replicate the same code which you found in the ControllerNameTest.php file. For testing models you can create a PHPUnit test case.
<?php
class YourApp_Model_YourModel extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap('config')
->bootstrap('defaultModuleAutoloader')
->bootstrap('autoloader');
// You might need to add few more bootstrap, depends on your needs
}
public function testSomeMethod(){}
I hope that helps.

I have found a great guide to setting everything up here with PHPUnit and Zend Framework here:
http://grover.open2space.com/content/unit-testing-zend-framework-111-and-phpunit

Related

Is there a way to test Symfony application other than PHPUnit?

During an interview, project manager asked me the following question:
I see in your resume that you are using PHPUnit 4 for tests? Why don't
you use the built-in testing of Symfony?
I answered:
What I know is that Symfony app testing is done via PHPUnit, which is
recommended from the docs of 2.3 and 2.7. I don't know if Symfony3
comes with a testing component!
Was my answer correct? Thanks for the explanations.
Yes, you are correct. Symfony has a WebTestCase (extending the KernelTestCase where you can find that it extends PhpUnit) for functional testing. Additionally Symfony provides a PhpUnitBridge that provides helpful stuff for writing PHPUnit-Tests for a Symfony app, e.g. to make tests fail that use deprecated components. Both rely on PHPUnit and provide additional capabalities on top of "vanilla" phpunit.
There are alternative testing tools, such as phpspec for writing unit tests and Behat for higher level tests (functional and acceptance testing), but both are not used inside Symfony and therefore don't really seem to be what was being asked about.
Additional information to your answer:
In my experience, WebTestCase also bootstrap kernel as static method in all test. Due to that, is not so good to Unit testing by that tool, as this test should be fast. They design this with functional testing controllers and actions but to not need start HTTP server.
Then for:
unit testing, build-in in is not good option
functional testing, it was original intention, but i think, Behat is better solution
system testing - not so good.
Summary, that is why i don't use this tool.

Use PEAR-Package inside Symfony

I am writing an PHPUnit WebTestCase for a Symfony repository. In some tests I want to print out time measurements. For that case I found the PEAR Console\Table package. Via Composer I get it installed, but how can I use it within my test?
The Console_Table.php doesn't have a namespace. So I think I could not use
use Console\Table;
or?
So when I must use the require_once statement, which is the right relativ path, when Composer installed the Table.php into vendor/pear/console_table/.
At this point I would say I am really new to PHP.
Thanks for your replies.
Ok,
I get it. absalon.valdes was right. $table = \Console_Table(); is the solution.
Important was to do a clean build of the project in Eclipse. After that, the compiler recognizes the class.
My bad.
Thanks.

Use two composer autoloaders in phing to run phpunit

I have a situation, that all app code comes from one source already compiled and in read only access. I need to run tests, but code that comes to me do not have phpunit installed.
Would it be possible to pass 2 autoloaders to phpunit using phing? One with app dependencies and other with phpunit?
phpunit is a command line program that already comes with it's own autoloading, and it only needs the directory with test classes (applying it's own autodetection/autoloading to them), and a bootstrap script which allows to instantiate all the classes that at some point need to be tested.
So the answer is "yes, expect to be able to use two autoloaders", but there most certainly is no need to fiddle with the autoloader coming with PHPUnit.

How to integrate Symfony 1.4 with Codeception

I want to be able to integrate Symfony 1.4 with Codeception unit tests.
I created init files but was unable to include Symfony library classes to be used for testing.
This page Symfony 1.4 Unit testing explains a little but it doesn't show an example of instantiating a class even with Lime test. I tried this with Lime and it didn't work. Is there a way to auto include classes using Symfony Core which allows me to instantiate classes for my needs when creating unit tests?
I tried including unit.php file from Symfony test folder but got the same result of not being able to instantiate existing library classes.
If it is too hard to do it with Codeception then I'm looking for options in Lime test also. I just want to be able to unit test...
How would I do this?
Running these will load all your classes:
require_once __DIR__.'/lib/autoload/sfCoreAutoload.class.php';
sfCoreAutoload::register();
Of course you should set the paths properly.
You can also try to use the Symfony 1.5. maintained by L'express (https://github.com/LExpress/symfony1). It uses composer and have the autoloading prepared for you. (you just need to include autoload.php)

PHPUnit Path problems

I am writing a ZF application. I had some abstract parent classes in a library directory. Netbeans could generate test skeletons for the child classes no problems, and I could run the tests no problem.
Later on I decided to move the abstract parents out of the library and in to the application directory (to improve readability). I updated my application code accordingly, and it runs no problem.
However, now when I use netbeans to generate test skeletons for the child classes, it gives a fatal error saying it can not find the parent. I then constructed a test class manually and ran it from the command line, and PHPUnit gave the same error.
What do I need to do to get this pathing working correctly? In the PHPUnit bootstrap I tried adding the Application directory to the include path, and registering Application as a namespace with the Zend Autoloader. I don't think this is a recommended practice, and it failed anyhow...
I really do struggle with path issues, finding files, etc...
Any assistance is much appreciated.
OK, so I can move the classes from the library to the core code directories, and the core code finds them no problems because they conform to the standard naming conventions autoladed by ZF.
Other applications (e.g. PHPUnit) won't be able to find them though. I could get around this by setting their autoloading the same ZF. Alternatively, I could incorporate required files with require_once (which would negate the use of autoloading in the first place).
So, it seems that my best shot is to put the shared classes back in the library. My original PHPUnit bootstrap adds the library prefix to the ZF autoloader, so I'm good to go.
All-in-all, I've just learned another good use for libraries.
[EDIT:] Another Alternative...
My test classes are boostrapping Zend Application which sets up the autoloading for PHPUnit. The only drawback doing it this way is that PHPUnit (via my netbeans IDE) can't create the skeleton tests for me. I guess I'll have to live with that.

Categories