Zendcasts Forum

A community of developers who work with the Zend Framework and other enterprise PHP technologies

You are not logged in.

#1 2009-08-23 17:25:14

udo
Administrator
From: Switzerland
Registered: 2009-08-06
Posts: 62

ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

Hello Jon,

at 17:55 you create a method

public function setUp() {

...

}

Wouldn't it be better to use

public function init() {

...

}

which is called automatically when instantiating the class?

Thanks, Udo

P.S.: Another cool cool episode! Thank you!

Offline

#2 2009-08-23 23:30:20

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

Hey Udo,

setUp() is a convention used in the unit testing framework. the setUp() method gets called every a unit test runs (same with tearDown()), whereas the init() method is specific to the Zend_Controller architecture. I use the ControllerTestCase as a super class only because it simplifies testing controllers and I assume that the code being tested will always be associated with the Zend Framework. In most cases, however, I'm not actually testing the request model, however I maintain this architectural choice for the sake of consistency.

my 2 cents,

j

Offline

#3 2009-08-24 06:55:02

udo
Administrator
From: Switzerland
Registered: 2009-08-06
Posts: 62

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

Hi Jon,

this is clear now.

Thanks, Udo

P.S.: I like your approach how to structure code.

Offline

#4 2009-08-24 18:38:32

udo
Administrator
From: Switzerland
Registered: 2009-08-06
Posts: 62

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

hi jon,

have another one for you wink

how would you unit test a captcha field? roll

thanks, udo

Offline

#5 2009-08-25 03:32:12

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

hmm...

well my understanding is that a captcha relies on either a one-way hash or a session variable for verification. I would try and decouple the validation of the hash from the actual page request and test the validation engine without the page. I would then write tests that assume that the validation engine is functioning correctly and simulate a user posting back the form with the captcha.

my 2 cents

Offline

#6 2009-08-25 04:52:16

marsbomber
New member
Registered: 2009-08-06
Posts: 6

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

Hi Jon,

it's great to watch a video cast about php unit testing.

i'm at the moment developing a web based administration system, which means auth and acl is required. this means except my login and error controller, all other controllers are protected.

The way i implemented this is i created a cust controller action, which extends the Zend_Controller_Action class. and i have a user identity check in the init

class MyApp_Controller_Action extends Zend_Controller_Action {
    public function init() {
        $myAuth = Zend_Auth::getInstance();
        if(!$myAuth->hasIdentity()) {
            // no good, redirect user to login controller
            $this->_redirect('/login');
        }
        else {
            // good, do some other stuff
        }
    }
}

and all my protected controllers will extend MyApp_Controller_Action, so that they all have the same user identity check behaviour.

Now ... my question is ... how should I do my unit test on the protected controllers? should i setup a user identity in setup()?

I'm new to unit testing (i guess a lot of the php guys are ...), so please bare with me stupidity ...

thanks

Offline

#7 2009-09-02 13:09:34

John Nunez
Guest

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

Hi Jon,

Do you know why phpunit loses the Autoloader when generating the reports?  I get an error about not being about to find Zend_Application_Bootstrap_Bootstrap.  If I turn off the reports it continues with no errors.  If I add the require_once('Zend/Application/Bootstrap/Bootstrap.php'); the reports work with no errors.

#8 2009-09-02 13:12:13

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

This is probably happening due to the include paths not being set properly for the command-line version of the application.... When you run phpunit, you need to have some way of bootstrapping it (similar to what you have in your index.php file).

Offline

#9 2009-11-29 15:53:56

Max Bucknell
Guest

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

Hi, I got phpunit working (finally) and it's all happy, bar one thing. You show that code coverage html stuff in the log/report. And I don't know why. I get this output in terminal:

conquistador:tests Max$ phpunit --configuration phpunit.xml
PHPUnit 3.3.17 by Sebastian Bergmann.

.

Time: 0 seconds

OK (1 test, 3 assertions)
conquistador:tests Max$


and I checked for the line in phpunit.xml:

<log type="coverage-html" target="./log/report" charset="UTF-8"
            yui="true" highlight="true" lowUpperBound="50" highLowerBound="80"/>

it's there, so what am I missing? Any help greatly appreciated

#10 2009-12-02 03:41:57

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

hmm... you might need to make the folder /log and also ensure it's writable.

Offline

#11 2010-05-11 09:41:37

Fivelow
New member
Registered: 2010-04-27
Posts: 4

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

I'm getting:

Fatal error: Class 'Zend_Test_PHPUnit_ControllerTestCase' not found in /Applications/XAMPP/xamppfiles/htdocs/jakeaikens.com/tests/application/ControllerTestCase.php on line 12

when I run phpunit from the command line. I looked in application/library/Zend/Test/PHPUnit and ControllerTestCase DOES exist. I'm completely baffled. Is there a way to unit test the unit testing? smile

Here is the code from phpunit.xml:

<phpunit bootstrap="./application/bootstrap.php" colors="true">
    <testsuite name="Jake's Blog">
        <directory>./</directory>
    </testsuite>
    <filter>
        <whitelist>
            <directory suffix=".php">../application</directory>
            <exclude>
                <directory suffix=".phtml">../application</directory>
            </exclude>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="./log/report" charset="UTF-8"
        yui="true" highlight="true" lowUpperBoubd="50" highLowerBound="80" />
        <log type="testdox" target="./log/testdox.html" />
    </logging>

</phpunit>


Here is the code from ControllerTestCase.php:

<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of ControllerTestCase
*
* @author Jake
*/
class App_ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {
    protected $application;
    public function setUp()
    {
        $this->bootstrap = array($this, 'appBootstrap');
        parent::setUp();
    }
    public function appBootstrap()
    {
        $this->application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/config/aplication.ini');
    }
}

Anyone? Bueller? Bueller?

Last edited by Fivelow (2010-05-11 10:18:20)

Offline

#12 2010-07-02 20:40:30

miguelp
New member
From: Portugal
Registered: 2010-05-11
Posts: 8
Website

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

Hi,

I'm using Zend studio 7.2 and i followed the tutorial "to the letter" just the project name is diferent, and when i click the "debug" button or click in "run" in the PHPUnit panel, it throws the following error:

Fatal error: Class 'ControllerTestCase' not found in H:\www\mudarcasa\tests\application\controllers\IndexControllerTest.php on line 3

but the class is there, i tried to just grab the code from google and run it and the same happens.

Offline

#13 2010-07-12 13:49:08

georgeenciu
New member
From: Bucharest, Romania
Registered: 2010-02-18
Posts: 7
Website

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

miguelp wrote:

Hi,

I'm using Zend studio 7.2 and i followed the tutorial "to the letter" just the project name is diferent, and when i click the "debug" button or click in "run" in the PHPUnit panel, it throws the following error:

Fatal error: Class 'ControllerTestCase' not found in H:\www\mudarcasa\tests\application\controllers\IndexControllerTest.php on line 3

but the class is there, i tried to just grab the code from google and run it and the same happens.

had the same problem ... and came to the conclusion that the run as php unit or debug as php unit option in zend studio will not work with phpunit.xml and resolved that with including the files (the bootstrap & the ControllerTestCase)

Offline

#14 2010-07-25 21:55:53

miguelp
New member
From: Portugal
Registered: 2010-05-11
Posts: 8
Website

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

yes, i've found that zendstudio 7.2 dosent work with phpunit.xml, i've solved the problem by including an testHelper.php that will load the bootstrap for the tests and all the other classes.

Offline

#15 2010-12-18 20:57:33

Testbaudson
New member
Registered: 2010-12-18
Posts: 1

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

Fivelow wrote:

I'm getting:

Fatal error: Class 'Zend_Test_PHPUnit_ControllerTestCase' not found in /Applications/XAMPP/xamppfiles/htdocs/jakeaikens.com/tests/application/ControllerTestCase.php on line 12

when I run phpunit from the command line.

I had the same problem. I assume this is because the application is instantiated within this class, so autoloading is not yet available.

Workaround: I required the class manually in ControllerTestCase.php, now it works.

require_once "Zend/Test/PHPUnit/ControllerTestCase.php";

Offline

#16 2011-01-02 22:19:40

samuelherzog
Member
From: Eugendorf, Austria
Registered: 2009-08-18
Posts: 27

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

Today i stumbled upon a little detail which is a potential pain in your ass:

When using the ControllerTestCase class as provided, the dataProvider annotation from phpunit is broken.

To solve this nifty little detail you need the same constructor signature accordingly to the PHPUnit_Framework_TestCase.
I uploaded my personal ControllerTestCase to GitHub, so everyone can enjoy it smile

Another detail I added: I changed the bootstrap method just because it seems to be more DRY to me, its just an alternative approach.

Offline

#17 2011-08-14 12:35:50

whisher
New member
Registered: 2010-11-27
Posts: 1

Re: ZC25 - Unit Testing with the Zend Framework with Zend_Test and PHPUnit

Hi Jon,

I'm new at unit test so please be patient.
I realized that the testdox.html is auto generated but in the tutorial code there is also a lot of files in log/report (not auto generated) so I'm wondering how that stuff works and how to set it up and above all where it comes from ? smile

btw thanks for tutorializing smile

Offline

Board footer

Powered by FluxBB