Created
February 27, 2012 18:02
-
-
Save JCook21/1925881 to your computer and use it in GitHub Desktop.
Abstract base class for writing Doctrine test cases using MySQL. Extends OrmTestCase to allow for truncating tables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use DoctrineExtensions\PHPUnit\OrmTestCase, | |
Doctrine\ORM\Configuration, | |
Doctrine\Common\Cache\ArrayCache, | |
Doctrine\ORM\EntityManager; | |
/** | |
* Base class for test cases that need an entity manager to work with. | |
* Defines an entity manager for the tests to work with and truncates tables in a way that is compatible with MySQL | |
* @author Jeremy Cook | |
* @version 1.0 | |
*/ | |
abstract class MySQLOrmTestCase extends OrmTestCase | |
{ | |
/** | |
* Entity manager for the class to work with. | |
* Has to be set here as a static property as it is marked as a private property in the parent class. | |
* @var \Doctrine\ORM\EntityManager | |
*/ | |
static protected $_em; | |
/** | |
* Creates an entity manager that an be shared as a fixture between tests. | |
* @return void | |
*/ | |
public static function setUpBeforeClass() | |
{ | |
$config = new Configuration; | |
$cache = new ArrayCache(); | |
//Setup options here | |
$conn = array(); | |
self::$_em = EntityManager::create($conn, $config); | |
} | |
/** | |
* Method to create the entity manager, used by parent methods. | |
* @return \Doctrine\ORM\EntityManager | |
* @see OrmTestCase::createEntityManager() | |
*/ | |
protected function createEntityManager () | |
{ | |
return self::$_em; | |
} | |
/** | |
* Overrides the parent method to allow foreign key checks to be disabled and re-enabled. | |
* This fixes problems with MySQL truncating tables with foreign keys. | |
* (non-PHPdoc) | |
* @see DoctrineExtensions\PHPUnit.OrmTestCase::setUp() | |
*/ | |
protected function setUp() | |
{ | |
//Disable foreign key checks to allow tables to be truncated. | |
self::$_em->getConnection()->exec('SET @PREV_foreign_key_checks = @@foreign_key_checks'); | |
self::$_em->getConnection()->exec('SET foreign_key_checks = 0'); | |
parent::setUp(); | |
self::$_em->getConnection()->exec('SET @@foreign_key_checks = @PREV_foreign_key_checks'); | |
} | |
/** | |
* Destroys the entity manager at the end of a test run. | |
* @return void | |
*/ | |
public static function tearDownAfterClass() | |
{ | |
self::$_em = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment