2008-05-24 21:37:26 +04:00
|
|
|
<?php
|
2009-01-04 19:15:32 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\Tests;
|
2009-01-04 19:15:32 +03:00
|
|
|
|
2008-05-24 21:37:26 +04:00
|
|
|
/**
|
2009-03-30 23:43:05 +04:00
|
|
|
* Base testcase class for all functional ORM testcases.
|
2008-05-24 21:37:26 +04:00
|
|
|
*
|
2009-02-04 21:03:05 +03:00
|
|
|
* @since 2.0
|
2008-05-24 21:37:26 +04:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class OrmFunctionalTestCase extends OrmTestCase
|
2008-05-24 21:37:26 +04:00
|
|
|
{
|
2009-02-04 21:03:05 +03:00
|
|
|
/* The metadata cache shared between all functional tests. */
|
|
|
|
private static $_metadataCacheImpl = null;
|
2009-05-21 23:18:14 +04:00
|
|
|
/* The query cache shared between all functional tests. */
|
|
|
|
private static $_queryCacheImpl = null;
|
2009-02-04 21:03:05 +03:00
|
|
|
|
2009-05-30 14:30:05 +04:00
|
|
|
/* Shared connection when a TestCase is run alone (outside of it's functional suite) */
|
|
|
|
private static $_sharedConn;
|
|
|
|
|
2009-02-04 21:03:05 +03:00
|
|
|
/** The EntityManager for this testcase. */
|
2009-01-12 16:34:41 +03:00
|
|
|
protected $_em;
|
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
/** The SchemaTool. */
|
|
|
|
protected $_schemaTool;
|
2009-03-28 23:59:07 +03:00
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
/** The names of the model sets used in this testcase. */
|
|
|
|
private $_usedModelSets = array();
|
|
|
|
|
|
|
|
/** Whether the database schema has already been created. */
|
|
|
|
private static $_tablesCreated = array();
|
|
|
|
|
|
|
|
/** List of model sets and their classes. */
|
|
|
|
private static $_modelSets = array(
|
|
|
|
'cms' => array(
|
|
|
|
'Doctrine\Tests\Models\CMS\CmsUser',
|
|
|
|
'Doctrine\Tests\Models\CMS\CmsPhonenumber',
|
|
|
|
'Doctrine\Tests\Models\CMS\CmsAddress',
|
2009-04-09 22:12:48 +04:00
|
|
|
'Doctrine\Tests\Models\CMS\CmsGroup',
|
|
|
|
'Doctrine\Tests\Models\CMS\CmsArticle'
|
2009-03-30 23:43:05 +04:00
|
|
|
),
|
|
|
|
'forum' => array(),
|
2009-05-21 12:53:40 +04:00
|
|
|
'company' => array(
|
|
|
|
'Doctrine\Tests\Models\Company\CompanyPerson',
|
|
|
|
'Doctrine\Tests\Models\Company\CompanyEmployee',
|
|
|
|
'Doctrine\Tests\Models\Company\CompanyManager'
|
|
|
|
),
|
2009-07-01 13:18:08 +04:00
|
|
|
'ecommerce' => array(
|
|
|
|
'Doctrine\Tests\Models\ECommerce\ECommerceCart',
|
2009-07-01 16:03:41 +04:00
|
|
|
'Doctrine\Tests\Models\ECommerce\ECommerceCustomer',
|
|
|
|
'Doctrine\Tests\Models\ECommerce\ECommerceProduct',
|
2009-07-01 17:11:45 +04:00
|
|
|
'Doctrine\Tests\Models\ECommerce\ECommerceShipping',
|
2009-07-02 13:37:59 +04:00
|
|
|
'Doctrine\Tests\Models\ECommerce\ECommerceFeature',
|
|
|
|
'Doctrine\Tests\Models\ECommerce\ECommerceCategory'
|
2009-07-01 13:18:08 +04:00
|
|
|
),
|
2009-06-20 16:59:33 +04:00
|
|
|
'generic' => array(
|
|
|
|
'Doctrine\Tests\Models\Generic\DateTimeModel'
|
|
|
|
)
|
2009-03-30 23:43:05 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
protected function useModelSet($setName)
|
2008-05-24 21:37:26 +04:00
|
|
|
{
|
2009-05-21 12:53:40 +04:00
|
|
|
$this->_usedModelSets[$setName] = true;
|
2008-05-24 21:37:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-03-30 23:43:05 +04:00
|
|
|
* Sweeps the database tables and clears the EntityManager.
|
2008-05-24 21:37:26 +04:00
|
|
|
*/
|
|
|
|
protected function tearDown()
|
|
|
|
{
|
2009-01-07 20:46:02 +03:00
|
|
|
$conn = $this->sharedFixture['conn'];
|
2009-05-21 12:53:40 +04:00
|
|
|
if (isset($this->_usedModelSets['cms'])) {
|
2009-03-30 23:43:05 +04:00
|
|
|
$conn->exec('DELETE FROM cms_users_groups');
|
|
|
|
$conn->exec('DELETE FROM cms_groups');
|
|
|
|
$conn->exec('DELETE FROM cms_addresses');
|
|
|
|
$conn->exec('DELETE FROM cms_phonenumbers');
|
2009-04-09 22:12:48 +04:00
|
|
|
$conn->exec('DELETE FROM cms_articles');
|
2009-03-30 23:43:05 +04:00
|
|
|
$conn->exec('DELETE FROM cms_users');
|
2008-05-24 21:37:26 +04:00
|
|
|
}
|
2009-07-01 13:18:08 +04:00
|
|
|
if (isset($this->_usedModelSets['ecommerce'])) {
|
|
|
|
$conn->exec('DELETE FROM ecommerce_carts');
|
|
|
|
$conn->exec('DELETE FROM ecommerce_customers');
|
2009-07-01 16:03:41 +04:00
|
|
|
$conn->exec('DELETE FROM ecommerce_products');
|
2009-07-02 18:36:47 +04:00
|
|
|
$conn->exec('DELETE FROM ecommerce_carts_products');
|
2009-07-01 16:03:41 +04:00
|
|
|
$conn->exec('DELETE FROM ecommerce_shippings');
|
2009-07-01 17:11:45 +04:00
|
|
|
$conn->exec('DELETE FROM ecommerce_features');
|
2009-07-02 13:37:59 +04:00
|
|
|
$conn->exec('DELETE FROM ecommerce_categories');
|
2009-07-02 15:48:44 +04:00
|
|
|
$conn->exec('DELETE FROM ecommerce_products_categories');
|
2009-07-06 16:18:04 +04:00
|
|
|
$conn->exec('DELETE FROM ecommerce_products_related');
|
2009-07-01 13:18:08 +04:00
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
if (isset($this->_usedModelSets['company'])) {
|
2009-06-14 21:34:28 +04:00
|
|
|
$conn->exec('DELETE FROM company_persons_friends');
|
2009-05-21 12:53:40 +04:00
|
|
|
$conn->exec('DELETE FROM company_managers');
|
|
|
|
$conn->exec('DELETE FROM company_employees');
|
|
|
|
$conn->exec('DELETE FROM company_persons');
|
|
|
|
}
|
2009-06-20 16:59:33 +04:00
|
|
|
if (isset($this->_usedModelSets['generic'])) {
|
|
|
|
$conn->exec('DELETE FROM date_time_model');
|
|
|
|
}
|
2009-01-12 16:34:41 +03:00
|
|
|
$this->_em->clear();
|
2008-05-24 21:37:26 +04:00
|
|
|
}
|
2009-01-07 20:46:02 +03:00
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
/**
|
|
|
|
* Creates a connection to the test database, if there is none yet, and
|
|
|
|
* creates the necessary tables.
|
|
|
|
*/
|
2009-01-07 20:46:02 +03:00
|
|
|
protected function setUp()
|
|
|
|
{
|
2009-03-30 23:43:05 +04:00
|
|
|
$forceCreateTables = false;
|
2009-05-30 14:30:05 +04:00
|
|
|
|
2009-01-07 20:46:02 +03:00
|
|
|
if ( ! isset($this->sharedFixture['conn'])) {
|
2009-05-30 14:30:05 +04:00
|
|
|
if ( ! isset(self::$_sharedConn)) {
|
|
|
|
self::$_sharedConn = TestUtil::getConnection();
|
|
|
|
}
|
|
|
|
$this->sharedFixture['conn'] = self::$_sharedConn;
|
2009-03-30 23:43:05 +04:00
|
|
|
if ($this->sharedFixture['conn']->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) {
|
|
|
|
$forceCreateTables = true;
|
|
|
|
}
|
2009-01-07 20:46:02 +03:00
|
|
|
}
|
2009-01-12 16:34:41 +03:00
|
|
|
if ( ! $this->_em) {
|
|
|
|
$this->_em = $this->_getEntityManager();
|
2009-03-30 23:43:05 +04:00
|
|
|
$this->_schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->_em);
|
|
|
|
}
|
|
|
|
|
|
|
|
$classes = array();
|
2009-05-21 12:53:40 +04:00
|
|
|
foreach ($this->_usedModelSets as $setName => $bool) {
|
2009-06-14 21:34:28 +04:00
|
|
|
if ( ! isset(self::$_tablesCreated[$setName])/* || $forceCreateTables*/) {
|
2009-03-30 23:43:05 +04:00
|
|
|
foreach (self::$_modelSets[$setName] as $className) {
|
|
|
|
$classes[] = $this->_em->getClassMetadata($className);
|
|
|
|
}
|
|
|
|
self::$_tablesCreated[$setName] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($classes) {
|
2009-05-30 16:08:15 +04:00
|
|
|
$this->_schemaTool->createSchema($classes);
|
2009-01-12 16:34:41 +03:00
|
|
|
}
|
2009-01-07 20:46:02 +03:00
|
|
|
}
|
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
/**
|
|
|
|
* Gets an EntityManager for testing purposes.
|
|
|
|
*
|
|
|
|
* @param Configuration $config The Configuration to pass to the EntityManager.
|
|
|
|
* @param EventManager $eventManager The EventManager to pass to the EntityManager.
|
|
|
|
* @return EntityManager
|
|
|
|
*/
|
2009-01-07 20:46:02 +03:00
|
|
|
protected function _getEntityManager($config = null, $eventManager = null) {
|
2009-02-04 21:03:05 +03:00
|
|
|
// NOTE: Functional tests use their own shared metadata cache, because
|
|
|
|
// the actual database platform used during execution has effect on some
|
|
|
|
// metadata mapping behaviors (like the choice of the ID generation).
|
|
|
|
if (is_null(self::$_metadataCacheImpl)) {
|
2009-07-07 00:34:54 +04:00
|
|
|
self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
|
2009-02-04 21:03:05 +03:00
|
|
|
}
|
2009-05-21 23:18:14 +04:00
|
|
|
if (is_null(self::$_queryCacheImpl)) {
|
2009-07-07 00:34:54 +04:00
|
|
|
self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
|
2009-05-21 23:18:14 +04:00
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
$config = new \Doctrine\ORM\Configuration();
|
2009-02-04 21:03:05 +03:00
|
|
|
$config->setMetadataCacheImpl(self::$_metadataCacheImpl);
|
2009-05-21 23:18:14 +04:00
|
|
|
$config->setQueryCacheImpl(self::$_queryCacheImpl);
|
2009-01-22 22:38:10 +03:00
|
|
|
$eventManager = new \Doctrine\Common\EventManager();
|
2009-01-07 20:46:02 +03:00
|
|
|
$conn = $this->sharedFixture['conn'];
|
2009-05-30 13:37:56 +04:00
|
|
|
|
2009-02-17 14:02:11 +03:00
|
|
|
return \Doctrine\ORM\EntityManager::create($conn, $config, $eventManager);
|
2009-01-07 20:46:02 +03:00
|
|
|
}
|
2009-07-01 13:18:08 +04:00
|
|
|
}
|