OrmFunctionalTestCase.php 6.94 KB
Newer Older
romanb's avatar
romanb committed
1
<?php
romanb's avatar
romanb committed
2

3
namespace Doctrine\Tests;
romanb's avatar
romanb committed
4

romanb's avatar
romanb committed
5
/**
6
 * Base testcase class for all functional ORM testcases.
romanb's avatar
romanb committed
7
 *
8
 * @since 2.0
romanb's avatar
romanb committed
9
 */
10
class OrmFunctionalTestCase extends OrmTestCase
romanb's avatar
romanb committed
11
{
12 13
    /* The metadata cache shared between all functional tests. */
    private static $_metadataCacheImpl = null;
14 15
    /* The query cache shared between all functional tests. */
    private static $_queryCacheImpl = null;
16

17 18 19
    /* Shared connection when a TestCase is run alone (outside of it's functional suite) */
    private static $_sharedConn;
    
20
    /** The EntityManager for this testcase. */
21 22
    protected $_em;

23 24
    /** The SchemaTool. */
    protected $_schemaTool;
25

26 27 28 29 30 31 32 33 34 35 36 37
    /** 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',
38 39
            'Doctrine\Tests\Models\CMS\CmsGroup',
            'Doctrine\Tests\Models\CMS\CmsArticle'
40 41
        ),
        'forum' => array(),
42 43 44
        'company' => array(
            'Doctrine\Tests\Models\Company\CompanyPerson',
            'Doctrine\Tests\Models\Company\CompanyEmployee',
romanb's avatar
romanb committed
45 46 47 48 49
            'Doctrine\Tests\Models\Company\CompanyManager',
            'Doctrine\Tests\Models\Company\CompanyOrganization',
            'Doctrine\Tests\Models\Company\CompanyEvent',
            'Doctrine\Tests\Models\Company\CompanyAuction',
            'Doctrine\Tests\Models\Company\CompanyRaffle'
50
        ),
piccoloprincipe's avatar
piccoloprincipe committed
51 52
        'ecommerce' => array(
            'Doctrine\Tests\Models\ECommerce\ECommerceCart',
53 54
            'Doctrine\Tests\Models\ECommerce\ECommerceCustomer',
            'Doctrine\Tests\Models\ECommerce\ECommerceProduct',
55
            'Doctrine\Tests\Models\ECommerce\ECommerceShipping',
56 57
            'Doctrine\Tests\Models\ECommerce\ECommerceFeature',
            'Doctrine\Tests\Models\ECommerce\ECommerceCategory'
piccoloprincipe's avatar
piccoloprincipe committed
58
        ),
59 60 61
        'generic' => array(
            'Doctrine\Tests\Models\Generic\DateTimeModel'
        )
62 63 64
    );

    protected function useModelSet($setName)
romanb's avatar
romanb committed
65
    {
66
        $this->_usedModelSets[$setName] = true;
romanb's avatar
romanb committed
67 68 69
    }
    
    /**
70
     * Sweeps the database tables and clears the EntityManager.
romanb's avatar
romanb committed
71 72 73
     */
    protected function tearDown()
    {
74
        $conn = $this->sharedFixture['conn'];
75
        if (isset($this->_usedModelSets['cms'])) {
76 77 78 79 80 81
            $conn->executeUpdate('DELETE FROM cms_users_groups');
            $conn->executeUpdate('DELETE FROM cms_groups');
            $conn->executeUpdate('DELETE FROM cms_addresses');
            $conn->executeUpdate('DELETE FROM cms_phonenumbers');
            $conn->executeUpdate('DELETE FROM cms_articles');
            $conn->executeUpdate('DELETE FROM cms_users');
romanb's avatar
romanb committed
82
        }
piccoloprincipe's avatar
piccoloprincipe committed
83
        if (isset($this->_usedModelSets['ecommerce'])) {
84 85 86 87 88 89 90 91
            $conn->executeUpdate('DELETE FROM ecommerce_carts_products');
            $conn->executeUpdate('DELETE FROM ecommerce_products_categories');
            $conn->executeUpdate('DELETE FROM ecommerce_products_related');
            $conn->executeUpdate('DELETE FROM ecommerce_carts');
            $conn->executeUpdate('DELETE FROM ecommerce_customers');
            $conn->executeUpdate('DELETE FROM ecommerce_features');
            $conn->executeUpdate('DELETE FROM ecommerce_products');
            $conn->executeUpdate('DELETE FROM ecommerce_shippings');
92
            $conn->executeUpdate('UPDATE ecommerce_categories SET parent_id = NULL');
93
            $conn->executeUpdate('DELETE FROM ecommerce_categories');
piccoloprincipe's avatar
piccoloprincipe committed
94
        }
95
        if (isset($this->_usedModelSets['company'])) {
96 97 98
            $conn->executeUpdate('DELETE FROM company_persons_friends');
            $conn->executeUpdate('DELETE FROM company_managers');
            $conn->executeUpdate('DELETE FROM company_employees');
99
            $conn->executeUpdate('UPDATE company_persons SET spouse_id = NULL');
100
            $conn->executeUpdate('DELETE FROM company_persons');
romanb's avatar
romanb committed
101 102 103 104
            $conn->executeUpdate('DELETE FROM company_raffles');
            $conn->executeUpdate('DELETE FROM company_auctions');
            $conn->executeUpdate('DELETE FROM company_events');
            $conn->executeUpdate('DELETE FROM company_organizations');
105
        }
106
        if (isset($this->_usedModelSets['generic'])) {
107
            $conn->executeUpdate('DELETE FROM date_time_model');
108
        }
109
        $this->_em->clear();
romanb's avatar
romanb committed
110
    }
111

112 113 114 115
    /**
     * Creates a connection to the test database, if there is none yet, and
     * creates the necessary tables.
     */
116 117
    protected function setUp()
    {
118
        $forceCreateTables = false;
119
        
120
        if ( ! isset($this->sharedFixture['conn'])) {
121 122 123 124
            if ( ! isset(self::$_sharedConn)) {
                self::$_sharedConn = TestUtil::getConnection();
            }
            $this->sharedFixture['conn'] = self::$_sharedConn;
125 126 127
            if ($this->sharedFixture['conn']->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) {
                $forceCreateTables = true;
            }
128
        }
129 130
        if ( ! $this->_em) {
            $this->_em = $this->_getEntityManager();
131 132 133 134
            $this->_schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->_em);
        }

        $classes = array();
135
        foreach ($this->_usedModelSets as $setName => $bool) {
romanb's avatar
romanb committed
136
            if ( ! isset(self::$_tablesCreated[$setName])/* || $forceCreateTables*/) {
137 138 139 140 141 142 143
                foreach (self::$_modelSets[$setName] as $className) {
                    $classes[] = $this->_em->getClassMetadata($className);
                }
                self::$_tablesCreated[$setName] = true;
            }
        }
        if ($classes) {
romanb's avatar
romanb committed
144
            $this->_schemaTool->createSchema($classes);
145
        }
146 147
    }

148 149 150 151 152 153 154
    /**
     * 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
     */
155
    protected function _getEntityManager($config = null, $eventManager = null) {
156 157 158 159
        // 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)) {
160
            self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
161
        }
162
        if (is_null(self::$_queryCacheImpl)) {
163
        	self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache;
164
        }
165
        $config = new \Doctrine\ORM\Configuration();
166
        $config->setMetadataCacheImpl(self::$_metadataCacheImpl);
167
        $config->setQueryCacheImpl(self::$_queryCacheImpl);
168
        $conn = $this->sharedFixture['conn'];
169
        
170
        return \Doctrine\ORM\EntityManager::create($conn, $config);
171
    }
piccoloprincipe's avatar
piccoloprincipe committed
172
}