Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I in PHP re-create my Database (for Unit Testing for example)

How can I from PHP, re-create my Database, maybe inserting default data. Currently, I am intending to use the behavior for Unit Tests.

I am using Doctrine 2, Zend Framework 1.11, Zend_Test for unit tests

I could use the CLI

doctrine orm:schema-tool:update --force

Or

doctrine orm:schema-tool:drop --force
doctrine orm:schema-tool:create

I am looking for a PHP replacement, so far found this

but it will look something like

$tool = new \Doctrine\ORM\Tools\SchemaTool($em);
$classes = array(
  $em->getClassMetadata('Entities\User'),
  $em->getClassMetadata('Entities\Profile')
);
$tool->dropSchema($classes, \Doctrine\ORM\Tools\SchemaTool::DROP_DATABASE);
$tool->createSchema($classes);

And I don't really want having to specify the model classes, esp in development when they can change. It should just read from the all classes specified in ... below ... just like with the CLI, you don't need to specify the classes you want?

$driverImpl = $config->newDefaultAnnotationDriver(array(realpath('../models')));
$config->setMetadataDriverImpl($driverImpl);
like image 447
Jiew Meng Avatar asked Dec 17 '10 11:12

Jiew Meng


People also ask

Can database be mocked for unit testing?

Unit tests are incredibly important to us as developers because they allow us to demonstrate the correctness of the code we've written. More importantly, unit tests allow us to make updates to our code base with confidence that we haven't broken anything.


2 Answers

You can use

  • Benjamin Eberlei's Doctrine Extension to PHPUnit at GitHub

The PHPUnit Extension for Doctrine offers several hooks into PHPUnits Database extension and offers a very convenient way to test your Doctrine 2 code against a Database.

There is some examples in the Readme, including an example that shows how to create the database schema on the fly. Benjamin Eberlei's is a Doctrine 2 core contributor.

Also see B. Eberlei's Ultimate Guide to DB Testing with PHPUnit

  • http://www.phpunit.de/manual/dbunit.txt
like image 139
Gordon Avatar answered Sep 29 '22 05:09

Gordon


Because you have set the path to the models when setting up the EntityManager you can create the schema in code without having to redeclare this path (and without having to declare each class). To do this you need to get a reference to your configured instance of EntityManager and get a reference to ClassMetadataFactory which you can then call ClassMetadataFactory#getAllMedata() from.

Here is an example where I have a static Bootstrap class that allows me to get a reference to the EntityManager from anywhere and I recreate the schema on the setUp() call in the unit tests:

class ModelTestCase extends PHPUnit_Framework_TestCase {

    public function setUp() {
        $em = Bootstrap::getEntityManager();
        $tool = new \Doctrine\ORM\Tools\SchemaTool($em);

        $mdFactory = $em->getMetadataFactory();
        $tool->createSchema($mdFactory->getAllMetadata());
        parent::setUp();
    }
}
like image 22
Tim Avatar answered Sep 29 '22 06:09

Tim