Writing Doctrine Unit Tests with Zend_Test
December 2nd, 2009Building 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.
Edit: I spoke to Guilherme Blanco (one of the core developers behind Doctrine) and he kindly brought up some things I should bear in mind for my Doctrine / Zend set.
Doctrine’s autoloader configuration can be scaled back by doing PEAR style Model Loading (new to 1.2). Also, you can use Doctrine_Core instead of Doctrine since everything has been moved to Doctrine_Core in order to follow Zend-style namespacing.
lastly, you can register your own CLI tasks as part of Doctrine’s CLI script or just write them in the scripts/doctrine.php file. With this, you could have your newly-generated models placed in the necessary folders. I’ll be experimenting with this in the coming week.
Enjoy!

Awesome stuff! I’m super exited about the next one too! Just keep ‘em comming:D
Love the whole idea of zendcasts.com. Just what I needed. Keep it up!
[...] still have Jon Lebensold, the creator of ZendCasts. Jon just made another great podcast on “Writing Doctrine Unit Tests with Zend_Test” yesterday. I downloaded the source code from Jon’s episode. I started merging it with [...]
For your assertions, I’d do $this->assertEquals(1, $u->id), or, if typing is a check you want, assertSame(). Easier to read, shorter to type, and makes more semantic sense as you read through the tests.
Otherwise, really nice webcast. I’d love to see the sourcecode posted somewhere, as it’d make it easier to “look it up” later.
Hi Matthew,
thanks for the feedback. I still struggle with remembering the differences between assertSame / assertEquals. in terms of the code, you can grab it (and most of the recent casts) here: http://code.google.com/p/zendcasts/source/browse/#svn/trunk/zc39-writing-zend-tests-with-doctrine-models
I would really like to see an example of Doctrine integrated as a module
Hmm I dont think it makes sense to test for ID values. Surrogate key generators should not be used for expecting specific values .. their entire job is to ensure that they will never generate the same ID twice not that they are necessarily sequential or start at a specific value.
So it makes more sense to use the exists() method to check if the model has been persisted:
http://www.doctrine-project.org/Doctrine_Record/1_2#method_exists
Or if you do not trust Doctrine, then you would need to run an SQL query, but its probably wise to trust Doctrine (or ensure trust by making their the Doctrine unit tests are sufficiently complete).
Hi Lukas,
you’re absolutely right. Thanks for bringing this up. I’ve usually ended up writing my own existence checking code, but I imagine that a tool like Doctrine would have this down to an art.
Thank you for for your really good presentations !
A question though:
I followed your video, but the code coverage for my generated model is not ‘covered’ by my tests.
Any one has any ideas why ? Thanks
Hi Stephane,
I imagine this has something to do with your node in the phpunit XML configuration file.
I followed the tutorial, but i permanently get ‘Could not create and run test suite: ../application/Bootsrtap.php does not exist’ error message. PHPUnit-3.3.17/PHPUnit/TextUI/Command.php:132 line causing problems
r. Sandor
never mind, I’ve found iy, it was the xml entry was a bit misspelled
My code is getting stuck at @$cli->run(“doctrine”, “rebuild-all-reload”, “force”); =/
Any tips?
Thanks!
Ok!!!I got it! =)
But my phpunit logs aren’t beeing generated =/
Thanks for some great casts!
Küpper: I maybe got stuck on the same thing..
When I run phpunit I get a “Couldn’t locate driver named mysql” error.. but when I run Doctrine manually from /application/scripts it works just fine.
Any ideas??
Thanks!