You are not logged in.
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
episode! Thank you!
Offline
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
Hi Jon,
this is clear now.
Thanks, Udo
P.S.: I like your approach how to structure code.
Offline
hi jon,
have another one for you ![]()
how would you unit test a captcha field? ![]()
thanks, udo
Offline
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
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
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.
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
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
hmm... you might need to make the folder /log and also ensure it's writable.
Offline
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?
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
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
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
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
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
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 ![]()
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
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 ? ![]()
btw thanks for tutorializing ![]()
Offline