Modulating Doctrine Objects with PHPUnit

I am starting to try and validate Doctrine objects using PHPUnit and would like to reload the database from the model objects each time.

My first attempt looks something like this:

class Tests_User extends PHPUnit_Framework_TestCase { public function setUp() { Doctrine_Manager::connection('mysql://user: pass@localhost /testdb'); Doctrine::createDatabases(); Doctrine::createTablesFromModels('../../application/models'); } public function testSavingWorks() { $user = new User(); $user->save(); } public function testSavingWorksAgain() { $user = new User(); $user->save(); } public function tearDown() { Doctrine::dropDatabases(); } } 

The problem is that when setUp () is called again for the second test, createTablesFromModels () fails, so I get an error because none of the tables are present.

I would really like the example of someone else re-initializing the Doctrine connection for PHPUnit or other testing purposes.

+4
source share
3 answers

So it turns out that createTablesFromModels includes files and then compares lists of specific classes before and after, so it doesn't work twice.

After repeating, the following sequence is repeated:

 Doctrine::loadModels($path); Doctrine::createTablesFromArray(Doctrine::getLoadedModels()); 
+1
source

If you have not found this yet, Jani has published an approach to help automate the installation / break process.

http://codeutopia.net/blog/2008/08/27/database-helper-for-phpunit/

+1
source

Check out this Ocramius Gist, which shows that you can test data usage: https://gist.github.com/Ocramius/3994325

0
source

All Articles