Skip to content

Instantly share code, notes, and snippets.

@JCook21
Created February 27, 2012 18:52
Show Gist options
  • Save JCook21/1926215 to your computer and use it in GitHub Desktop.
Save JCook21/1926215 to your computer and use it in GitHub Desktop.
Work on trying to get a Doctrine/PHPUnit connection working that allows truncates.
<?php
namespace Tests\Models\Db;
use DoctrineExtensions\PHPUnit\OrmTestCase,
Doctrine\ORM\Configuration,
Doctrine\Common\Cache\ArrayCache,
Doctrine\ORM\EntityManager,
Doctrine\DBAL\Types\Type,
Doctrine\Spatial\ORM\SchemaEventSubscriber,
Doctrine\Common\EventManager,
PHPUnit_Extensions_Database_Operation_Composite,
PHPUnit_Extensions_Database_Operation_Factory;
require_once __DIR__ . '/../../bootstrap.php';
/**
* Base class for test cases that need an entity manager to work with.
* Defines an entity manager for the tests to work with.
* @author Jeremy Cook
* @version 1.0
* @package Veridis
* @todo: Look at how to put config into application.ini
*/
abstract class Base 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();
//Config stuff here
$eventManager = new EventManager();
$eventManager->addEventListener(array("preTestSetUp"), new SchemaSetupListener());
self::$_em = EntityManager::create($conn, $config, $eventManager);
$platform = self::$_em->getConnection()->getDatabasePlatform();
Type::addType('point', 'Doctrine\\Spatial\\DBAL\\Types\\PointType');
$platform->registerDoctrineTypeMapping('Point', 'point');
self::$_em->getConnection()
->getEventManager()
->addEventSubscriber(new SchemaEventSubscriber());
}
/**
* Method to create the entity manager, mostly used by parent methods.
* @return \Doctrine\ORM\EntityManager
* @see OrmTestCase::createEntityManager()
*/
protected function createEntityManager ()
{
return self::$_em;
}
public function getSetUpOperation()
{
$truncate = new MySQLTruncate();
$truncate->setCascade();
return new \PHPUnit_Extensions_Database_Operation_Composite(array(
$truncate,
PHPUnit_Extensions_Database_Operation_Factory::INSERT()
));
}
/**
* Destroys the entity manager at the end of a test run.
* @return void
*/
public static function tearDownAfterClass()
{
self::$_em = null;
}
}
<?php
namespace Tests\Models\Db;
/**
*
* @author Jeremy
*
*/
class MySQLTruncate extends \PHPUnit_Extensions_Database_Operation_Truncate
{
public function execute(\PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, \PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
{
$connection->getConnection()->exec("SET @PHAKE_PREV_foreign_key_checks = @@foreign_key_checks");
$connection->getConnection()->exec("SET @@foreign_key_checks = 0");
parent::execute($connection, $dataSet);
$connection->getConnection()->exec("SET @@foreign_key_checks = @PHAKE_PREV_foreign_key_checks");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment