Zendcasts Forum

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

You are not logged in.

#1 2009-12-02 05:33:59

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

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!

Offline

#2 2009-12-02 12:40:06

abtris
New member
From: Prague
Registered: 2009-11-13
Posts: 5
Website

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)

Offline

#3 2009-12-02 18:48:31

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

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

thanks for mentioning this abtris!

Offline

#4 2009-12-12 03:37:40

jiewmeng
Guest

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 2009-12-12 03:47:36

jiewmeng
Guest

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 2009-12-12 04:50:09

jiewmeng
Guest

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 2009-12-16 17:19:12

aruss
Guest

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 2010-01-01 10:04:21

Sanders
Guest

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 2010-01-04 15:54:49

neographikal
New member
Registered: 2010-01-04
Posts: 2

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!

Offline

#10 2010-01-20 21:48:10

Sebastian Montero
Guest

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 2010-01-21 01:16:34

Sebastian Montero
Guest

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 2010-01-22 18:25:14

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

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.

Offline

#13 2010-01-22 20:43:35

Sebastian Montero
Guest

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 2010-02-18 11:05:16

boodito
New member
Registered: 2010-02-18
Posts: 1

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??

Offline

#15 2010-02-18 23:48:22

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

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!

Offline

#16 2010-04-16 13:54:57

yusufdestina
New member
Registered: 2010-04-16
Posts: 2

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?

Offline

#17 2010-04-16 15:36:24

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

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...

Offline

#18 2010-04-16 17:11:59

yusufdestina
New member
Registered: 2010-04-16
Posts: 2

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)

Offline

#19 2010-04-17 21:26:01

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

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

ridiculous indeed...

Offline

#20 2010-11-10 05:14:42

Wallgate
New member
Registered: 2010-11-09
Posts: 4

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

Hi.

I'm trying to integrate ZF and Doctrine in a testable way, and I have some trouble which comes back to what Sebastian Montero wrote. What I fail to see is a way of bootstrapping my test suite so it could be used for testing both controllers and models.

In this screencast Zend_Application is instantiated in /tests/application/bootstrap.php so the CLI code runs only once. Yet in ZC-25 which covers controller testing it's instantiated in setUp method of a ControllerTestCase class.

The problem is that if you create an instance of Zend_Application in bootstrap.php and then you try to test your controllers it's all messed up. Though everything works fine in browser, running $this->dispatch('/uri') ends up with "No default module" error, and when you fix that it cannot find your controller plugins and then it has trouble finding layout resource and so on and so forth. So you basically have to bootstrap your application twice which is no good.

On the other hand, you can move everything into your setUp method. But then you'll have 'build-all-reload' CLI command running for every single test method which also is a bad thing.

What I do is I actually run 'build-all-reload' before instantiating the application:

<?php

define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));

define('APPLICATION_ENV', 'testing');

set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    realpath(APPLICATION_PATH . '/models'),
    get_include_path(),
)));


require_once 'Zend/Application.php';
require_once 'Zend/Config/Ini.php';
require_once 'Doctrine.php';
require_once 'ControllerTestCase.php';


$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', APPLICATION_ENV);
$dbConfig       = $config->db->toArray();
$doctrineConfig = $config->doctrine->toArray();

spl_autoload_register(array('Doctrine', 'autoload'));

$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_AGGRESSIVE);
$manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);
$manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL);
$manager->setAttribute(Doctrine::ATTR_QUOTE_IDENTIFIER, true);

$dsn = sprintf("mysql://%s:%s@%s/%s",
    $dbConfig['username'],
    $dbConfig['password'],
    $dbConfig['host'],
    $dbConfig['dbname']
);

$connection = $manager->connection($dsn);
$connection->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);

$cli = new Doctrine_Cli($doctrineConfig);
@$cli->run(array('Doctrine', 'build-all-reload', 'force'));

And then I create an instance of Zend_Application normally:

<?php

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

abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    protected $application = NULL;

    public function setUp()
    {
        $this->bootstrap = array($this, 'appBootstrap');
        parent::setUp();
    }

    public function appBootstrap()
    {
        $this->application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
        );
        $this->application->bootstrap();
    }
}

I wonder what you guys think about it and if there's a better way doing this.

Offline

#21 2010-11-11 09:34:05

StefC30
New member
Registered: 2010-11-11
Posts: 2

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

Hello,

This source code doesn't work.

When I set up a new Zend Framework project from these sources and run PHPUnit test with Zend Studio, I get several errors :

First, the Zend_Test_PHPUnit_ControllerTestCase class is not known : “Fatal error: Class ‘Zend_Test_PHPUnit_ControllerTestCase’ not found in D:\…\tests\application\models\UserTest.php”.
Ok, I add a require_once ‘Zend/Test/PHPUnit/ControllerTestCase.php’; in my UserTest.php file.

So, I no longer have this error, but a new error for the model on $u = new User(); : “Class ‘User’ not found in D:\…\tests\application\models\UserTest.php”.

I can still not add a require_once for each class of my project.
The bootstrap does not even use.

Do you have any idea of the origin of this problem ?
Is this related to the use of Zend Studio ?
Is there anything else to add or modify to run this project ?

Thank you in advance for your help.

Best regards.

Stéphan.

Last edited by StefC30 (2010-11-11 09:36:34)

Offline

#22 2010-11-11 16:41:02

Wallgate
New member
Registered: 2010-11-09
Posts: 4

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

Sorry, I never worked with Zend Studio. But technically there are all the neccesary require_once statements if you of course put your classes in right places. Have you tried running your tests from the command line?

Offline

#23 2010-11-11 22:42:51

StefC30
New member
Registered: 2010-11-11
Posts: 2

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

No, just from Zend Studio.

Offline

#24 2010-11-12 09:17:25

Wallgate
New member
Registered: 2010-11-09
Posts: 4

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

I think I found out what's wrong with my source code. Everything works fine if your testcases extend ControllerTestCase class. But if you try to extend PHPUnit_Framework_TestCase it ends up crushing since you've got no Zend_Application instantiated and therefore no autoloader initialized (apart from the Doctrine's one). Thanks for pointing me at a bug like this.

Actually there's a solution shown in the last of Zend Casts (which deals with creating two instances of Zend_Application). Gotta stick to that until there is a better way doing it.

Offline

#25 2010-11-21 12:55:20

boarder212
New member
Registered: 2009-09-29
Posts: 3

Re: ZC39 – Writing Doctrine Unit Tests with Zend_Test

@Wallgate
a better solution for this is to create a loader class where u do the
Zend_Application bootstrapping.
Then u can create a testhelper class for unittests witch extends PHPUnit_Framework_TestCase  and another testhelper
for controllertests  witch extends Zend_Test_PHPUnit_ControllerTestCase.
In your setUp method just use something like this.

        $loader = new Loader();
        $this->application = $loader->loadApplication();
        $this->bootstrap = $loader->getBootstrap();

Best regards
Christian

Offline

Board footer

Powered by FluxBB