#native_company# #native_desc#
#native_cta#

PHPUnit Fundamentals: Setting Up and Tearing Down a Doctrine 2 Database

By W. Jason Gilmore
on August 17, 2011

Like many members of the Zend Framework community, I’ve been integrating Doctrine 2 into my projects for quite some time, despite the popular ORM not yet being officially supported by the Zend Framework. The recently released Doctrine 2 presents significant advantages over its 1.X predecessor, advantages which are even more pronounced when compared to the Zend Framework’s default database solution, namely the Zend_Db component. It really does make database integration fun, and I highly recommend checking it out if you haven’t already (to get a head start on the initially confusing integration process, check out my z2d2 project, hosted on GitHub).
One of many cool Doctrine 2 features is the Doctrine Console, a command-line utility you can use to easily carry out a variety of administration tasks such as generating the database schema based on a set of map files, converting Doctrine 1.X schema’s to 2.X schemas, and dropping a database. For instance, given a series of annotated classes, generating the associated table schemas is as easy as executing the following command:

$ doctrine orm:schema-tool:create

So what does all of this have to do with the purpose of this installment? When running unit tests which vet various aspects of your database you’ll want to setup (create) and teardown (destroy) the test database, ensuring that a pristine and repeatable version of the database is available to each test. This logically involves repeatedly recreating the database, schema, and potentially data.
While there exist many programmatic approaches to setting up and tearing down a database, when using Doctrine 2 it’s possible to use the SchemaTool class, which presents a programmatic interface to many of the features made available through the Doctrine Console. Using this class you can retrieve the mapping metadata, using it to repeatedly rebuild the database schema. The following setUp() method uses this class (complete with code comments) to perform these tasks:

<?php

class ModelTestCase extends PHPUnit_Framework_TestCase
{
  
  protected $em;
  
  public function setUp()
  {
    
    // Make sure we are using the test environment
    $application = new Zend_Application(
      'testing', 
      APPLICATION_PATH . '/configs/application.ini'
    );

    // Bootstrap the application
    $bootstrap = $application->bootstrap()->getBootstrap();

    // Retrieve the Doctrine 2 entity manager
    $this->em = $bootstrap->getResource('entityManager'); 
    
    // Instantiate the schema tool
    $tool = new DoctrineORMToolsSchemaTool($this->em);

    // Retrieve all of the mapping metadata
    $classes = $this->em->getMetadataFactory()->getAllMetadata();

    // Delete the existing test database schema
    $tool->dropSchema($classes);

    // Create the test database schema
    $tool->createSchema($classes);

    parent::setUp();
    
  }
  
  public function tearDown()
  {
    parent::tearDown();
  }
  
}

You might be wondering why nothing specific to the database destruction process is found in the tearDown() method. This is because it’s easier to avoid additional work by essentially carrying out both steps in the setUp() method; you’ll see that the dropSchema() method is used directly prior to executing the createSchema() method.
After implementing this revised setUp() method, go ahead and run your tests, and you’ll see that the database will successfully regenerate with each test.