1

Topic: ZC39 – Writing Doctrine Unit Tests with Zend_Test

Building on the Introduction to Doctrine 1.2 video, this video will show how you can easily test the persistence of Doctrine models within the Zend_Test environment.

I also touch briefly on how to setup the latest version of MAMP with phpunit.

Enjoy!

2

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

You can show how setup UTF8 for another languages in Doctrine ?

I try replace in UserTest with:

$u->name = "Jon čertův kopeček";

And in database (phpmyadmin) have mishmash characters.

SOLVED by change in bootstrap.php

        $conn = Doctrine_Manager::connection($doctrine['dsn'], 'doctrine');
        $conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
+      $conn->setCharset('utf8');

I try setCharset to manager, isn't work. This solution works!

Thanks for your tutorial, i'm waiting for next episode.

Last edited by abtris (2009-12-02 14:40:23)

3

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

thanks for mentioning this abtris!

4

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

this is more related to phpunit actually.

what does

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

mean? isit ... :

* type="coverage-html" -> i want a report in html
* yui="true" -> whats this? using yahoo UI?
* highlight="true" -> whats this?
* lowUpperBound="35" highLowerBound="70" -> and these?

* <log type="testdox-html" target="/log/testdox.html"/> -> a 2nd report in testdox-html? whats this abt?

5

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

when i try to run phpunit i get

D:\Projects\ZF\doctrine\tests>phpunit
create-db - Successfully created database for connection named 'Doctrine'
PHPUnit 3.4.3 by Sebastian Bergmann.

PHP Warning:  fopen(/log/testdox.html): failed to open stream: No such file or directory in C:\wamp\bin\php\php5.3.0\PEA
R\PHPUnit\Util\Printer.php on line 107

Warning: fopen(/log/testdox.html): failed to open stream: No such file or directory in C:\wamp\bin\php\php5.3.0\PEAR\PHP
Unit\Util\Printer.php on line 107
PHP Warning:  fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P
rinter.php on line 163

Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe
r.php on line 163
PHP Warning:  fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P
rinter.php on line 163

Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe
r.php on line 163
PHP Warning:  fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P
rinter.php on line 163

Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe
r.php on line 163
PHP Warning:  fclose() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P
rinter.php on line 123

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe
r.php on line 123


Time: 1 second

←[30;42m←[2KOK (0 tests, 0 assertions)
←[0m←[2K
D:\Projects\ZF\doctrine\tests>

seems like i dont have some pear library installed?

6

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

jiewmeng wrote:

when i try to run phpunit i get

D:\Projects\ZF\doctrine\tests>phpunit
create-db - Successfully created database for connection named 'Doctrine'
PHPUnit 3.4.3 by Sebastian Bergmann.

PHP Warning:  fopen(/log/testdox.html): failed to open stream: No such file or directory in C:\wamp\bin\php\php5.3.0\PEA
R\PHPUnit\Util\Printer.php on line 107

Warning: fopen(/log/testdox.html): failed to open stream: No such file or directory in C:\wamp\bin\php\php5.3.0\PEAR\PHP
Unit\Util\Printer.php on line 107
PHP Warning:  fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P
rinter.php on line 163

Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe
r.php on line 163
PHP Warning:  fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P
rinter.php on line 163

Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe
r.php on line 163
PHP Warning:  fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P
rinter.php on line 163

Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe
r.php on line 163
PHP Warning:  fclose() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\P
rinter.php on line 123

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\wamp\bin\php\php5.3.0\PEAR\PHPUnit\Util\Printe
r.php on line 123


Time: 1 second

←[30;42m←[2KOK (0 tests, 0 assertions)
←[0m←[2K
D:\Projects\ZF\doctrine\tests>

seems like i dont have some pear library installed?

i got it working already ... its because i copied code from the phpunit manual ...

<log type="testdox-html" target="/log/testdox.html"/>

vs

<log type="testdox" target="/log/testdox.html"/>

but when do i use various options eg. testdox  vs testdox-html vs testdox-txt etc etc. the manual did not specify whats what

7

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

Hi, I tried to follow the tutorial but I allways get a

Cannot redeclare class PHPUnit_Util_FilterIterator in /path/to/library/PHPUnit/Util/FilterIterator.php on line 163

I use Zend Framework 1.9 and PHPUnit 3.4.4 could you give me a tip what I doing wrong?

best regards
aruss

8

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

Hello,

I have followed your tutorial. Preveiously I have created my own form validator in the Zend Framework which was working correctly. But now by using phpunit i get the following error:

Time: 0 seconds, Memory: 4.50Mb

OK (0 tests, 0 assertions)

Generating code coverage report, this may take a moment.
Fatal error: Class 'Zend_Validate_Abstract' not found in /var/www/nrka/library/JS/Validate/CompareDates.php on line 2

Call Stack:
    0.0004      63840   1. {main}() /usr/bin/phpunit:0
    0.2052    4486164   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:52
    0.2053    4486896   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:147
    0.2082    4493296   4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:214
    0.3762    4668380   5. PHPUnit_Util_Report::render() /usr/share/php/PHPUnit/TextUI/TestRunner.php:479
    0.3763    4669256   6. PHPUnit_Framework_TestResult->getCodeCoverageInformation() /usr/share/php/PHPUnit/Util/Report.php:98
    0.3763    4669368   7. PHPUnit_Util_Filter::getFilteredCodeCoverage() /usr/share/php/PHPUnit/Framework/TestResult.php:624
    0.6731    5975076   8. include_once('/var/www/nrka/library/JS/Validate/CompareDates.php') /usr/share/php/PHPUnit/Util/Filter.php:382


Any idea's how to solve this?

9

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

I've been watching your vids the last couple of days. My compliments, they contain just enough detail for me.

I was just wondering, I'm going to use your setup for my new project. But should it be easy to convert this setup to the definitive setup from Zend+Doctrine once they release it in Q1 2010?

Thanks!

10

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

Hi, great casts, very helpful!!!

I just wanted to let you now that I managed to speed up the tests in my application, by more than a minute by deleting the lines that exclude the zend library and the .phtml files in the filter in the phpunit.xml file, I don't think they are necessary because you exclude them with the main filter.

Hope this helps

11

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

Hi, in this example the database is recreated at the begining of the tests, shouldn't it be recreated for each test? so that tests aren't affected by previous ones.

thanks

12

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

Hey Sebastian,

you can certainly do it in that manner, however as you start writing hundreds of tests,  you might find nuking your db every time to slow you down. To do it though, you could just modify your startUp and tearDown methods for the model tests.

13

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

Thank you for your reply.

And how about using doctrine mock drivers or sqlite memory database? to solve that problem.

Thanks!

14

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

Hey,
When I run the tests, I get the following error:

PHPUnit 3.4.8 by Sebastian Bergmann.

.

Time: 46 seconds, Memory: 32.00Mb

OK (1 test, 6 assertions)

Generating code coverage report, this may take a moment.
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 35 bytes) in /usr/share/php/PHPUnit/Util/Report/Node/File.php on line 644

Call Stack:
    0.0006      62840   1. {main}() /usr/bin/phpunit:0
    0.1008    4498284   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:54
    0.1008    4499016   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:146
   42.4558   21921616   4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:213
   46.2234   30628640   5. PHPUnit_Util_Report::render() /usr/share/php/PHPUnit/TextUI/TestRunner.php:478
   46.2948   31323220   6. PHPUnit_Util_Report::addItems() /usr/share/php/PHPUnit/Util/Report.php:118
   46.2986   31358240   7. PHPUnit_Util_Report::addItems() /usr/share/php/PHPUnit/Util/Report.php:154
   46.3014   31385764   8. PHPUnit_Util_Report::addItems() /usr/share/php/PHPUnit/Util/Report.php:154
   46.5328   33319972   9. PHPUnit_Util_Report_Node_Directory->addFile() /usr/share/php/PHPUnit/Util/Report.php:146
   46.5328   33321944  10. PHPUnit_Util_Report_Node_File->__construct() /usr/share/php/PHPUnit/Util/Report/Node/Directory.php:147
   46.5329   33322732  11. PHPUnit_Util_Report_Node_File->loadFile() /usr/share/php/PHPUnit/Util/Report/Node/File.php:168
   46.5332   33365436  12. token_get_all() /usr/share/php/PHPUnit/Util/Report/Node/File.php:644


Any idea??

15

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

hey boodito, if its a memory issue, try bumping up your limit in the php.ini file!

16

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

in one of your first videos about zf and doctrine you had a models folder and then the models files and a Base directory with the generated classes for the models.

in the application.ini you defined some settings:

doctrine.generate_models_options.pearStyle = true
doctrine.generate_models_options.generateTableClasses = false
doctrine.generate_models_options.generateBaseClasses = true
doctrine.generate_models_options.baseClassPrefix = "Base_"
doctrine.generate_models_options.baseClassesDirectory =
doctrine.generate_models_options.classPrefixFiles = false
doctrine.generate_models_options.classPrefix = "Model_"

If I try to integrate phpunit testing with the setup explained in previous tutorials and the one you provide in this video
example:

class ArticleTest extends Zend_Test_PHPUnit_ControllerTestCase {
.
.
.
public function testFindAll() {
        $article = new Model_Article();
        $obj = $article->findAll($this->lang);
        $this->assertTrue( count($obj) > 0);
    }

I get this error:

PHPUnit 3.4.9 by Sebastian Bergmann.

PHP Fatal error:  Class 'Model_Article' not found in C:\server\www\zend_site\t
ests\application\models\ArticleTest.php on line 23

What am I doing wrong?

17

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

hey Yusuf,

I've read several reports a silly bug where models that start with letters before "D" aren't loaded... I'm not sure if that's your issue though...

18

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

you're joking wright? and if not, do you know if there are ways to fix this besides renaming the models? tnx for ur time on this and your excellent screencasts wink

It seems like this is the issue, I've created another table, generated the model for it and tested it successfully.
by the way, the new class name is: 'Project'

Last edited by yusufdestina (2010-04-16 21:57:22)

19

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

ridiculous indeed...