UnitTestCase.php 10 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */

/**
 * Doctrine_UnitTestCase
 *
 * @package     Doctrine
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
zYne's avatar
zYne committed
33 34
class Doctrine_UnitTestCase extends UnitTestCase 
{
doctrine's avatar
doctrine committed
35
    protected $manager;
zYne's avatar
zYne committed
36
    protected $connection;
doctrine's avatar
doctrine committed
37 38 39 40 41
    protected $objTable;
    protected $new;
    protected $old;
    protected $dbh;
    protected $listener;
zYne's avatar
zYne committed
42

doctrine's avatar
doctrine committed
43
    protected $users;
44
    protected $valueHolder;
doctrine's avatar
doctrine committed
45
    protected $tables = array();
46
    protected $unitOfWork;
zYne's avatar
zYne committed
47 48 49 50 51
    protected $driverName = false;
    protected $generic = false;
    protected $conn;
    protected $adapter;
    protected $export;
52
    protected $expr;
zYne's avatar
zYne committed
53 54
    protected $dataDict;
    protected $transaction;
zYne's avatar
zYne committed
55

doctrine's avatar
doctrine committed
56

zYne's avatar
zYne committed
57
    protected $init = false;
doctrine's avatar
doctrine committed
58

zYne's avatar
zYne committed
59 60
    public function init() 
    {
doctrine's avatar
doctrine committed
61 62 63
        $name = get_class($this);

        $this->manager   = Doctrine_Manager::getInstance();
zYne's avatar
zYne committed
64
        $this->manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL);
doctrine's avatar
doctrine committed
65

doctrine's avatar
doctrine committed
66
        $this->tables = array_merge($this->tables, 
zYne's avatar
zYne committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
                        array('entity',
                              'entityReference',
                              'email',
                              'phonenumber',
                              'groupuser',
                              'album',
                              'song',
                              'element',
                              'error',
                              'description',
                              'address',
                              'account',
                              'task',
                              'resource',
                              'assignment',
                              'resourceType',
                              'resourceReference')
doctrine's avatar
doctrine committed
84
                              );
85 86


zYne's avatar
zYne committed
87 88 89 90
        $class = get_class($this);
        $e     = explode('_', $class);


zYne's avatar
zYne committed
91 92 93 94 95 96 97 98 99 100
        if ( ! $this->driverName) {
            $this->driverName = 'main';
    
            switch($e[1]) {
                case 'Export':
                case 'Import':
                case 'Transaction':
                case 'DataDict':
                case 'Sequence':
                    $this->driverName = 'Sqlite';
zYne's avatar
zYne committed
101 102
                break;
            }
zYne's avatar
zYne committed
103 104
            
            $module = $e[1];
zYne's avatar
zYne committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    
            if(count($e) > 3) {
                $driver = $e[2];
                switch($e[2]) {
                    case 'Firebird':
                    case 'Informix':
                    case 'Mysql':
                    case 'Mssql':
                    case 'Oracle':
                    case 'Pgsql':
                    case 'Sqlite':
                        $this->driverName = $e[2];
                    break;
                }
            }
zYne's avatar
zYne committed
120
        }
121

122
        try {
zYne's avatar
zYne committed
123
            $this->conn = $this->connection = $this->manager->getConnection($this->driverName);
zYne's avatar
zYne committed
124 125
            $this->manager->setCurrentConnection($this->driverName);

zYne's avatar
zYne committed
126
            $this->connection->evictTables();
zYne's avatar
zYne committed
127
            $this->dbh      = $this->adapter = $this->connection->getDbh();
doctrine's avatar
doctrine committed
128
            $this->listener = $this->manager->getAttribute(Doctrine::ATTR_LISTENER);
129

130
            $this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
zYne's avatar
zYne committed
131

132
        } catch(Doctrine_Manager_Exception $e) {
zYne's avatar
zYne committed
133
            if($this->driverName == 'main') {
zYne's avatar
zYne committed
134
                $this->dbh = new PDO('sqlite::memory:');
135
                $this->dbh->sqliteCreateFunction('trim', 'trim', 1);
zYne's avatar
zYne committed
136
            } else {
zYne's avatar
zYne committed
137
                $this->dbh = $this->adapter = new Doctrine_Adapter_Mock($this->driverName);
zYne's avatar
zYne committed
138
            }
zYne's avatar
zYne committed
139

zYne's avatar
zYne committed
140
            $this->conn = $this->connection = $this->manager->openConnection($this->dbh, $this->driverName);
zYne's avatar
zYne committed
141 142 143 144 145 146 147 148

            if($this->driverName !== 'main') {
                $exc  = 'Doctrine_Connection_' . ucwords($this->driverName) . '_Exception';

                $this->exc = new $exc();

            } else {
            }
zYne's avatar
zYne committed
149

zYne's avatar
zYne committed
150
            $this->listener = new Doctrine_EventListener();
doctrine's avatar
doctrine committed
151 152
            $this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
        }
zYne's avatar
zYne committed
153
        if ($this->driverName !== 'main') {
zYne's avatar
zYne committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

            if (isset($module)) {
                switch($module) {
                    case 'Export':
                    case 'Import':
                    case 'Transaction':
                    case 'Sequence':
                    case 'Expression':
                        $lower = strtolower($module);
    
                        $this->$lower = $this->connection->$lower;
                    break;
                    case 'DataDict':
                        $this->dataDict = $this->connection->dataDict;
                    break;
                }
            }
zYne's avatar
zYne committed
171
        }
172
        $this->unitOfWork = $this->connection->unitOfWork;
173
        $this->connection->setListener(new Doctrine_EventListener());
zYne's avatar
zYne committed
174
        $this->query = new Doctrine_Query($this->connection);
175

zYne's avatar
zYne committed
176 177 178 179
        if ($this->driverName === 'main') {
            $this->prepareTables();
            $this->prepareData();
        }
180
    }
doctrine's avatar
doctrine committed
181
    public function prepareTables() {
182
        foreach($this->tables as $name) {
183 184 185
            $name = ucwords($name);
            $table = $this->connection->getTable($name);
            $query = 'DROP TABLE ' . $table->getTableName();
186
            try {
187 188
                $this->conn->exec($query);
            } catch(Doctrine_Connection_Exception $e) {
189 190

            }
doctrine's avatar
doctrine committed
191
        }
zYne's avatar
zYne committed
192 193
        $this->conn->export->exportClasses($this->tables);
        /**
194
        foreach($this->tables as $name) {
195
            $name = ucwords($name);
zYne's avatar
zYne committed
196
            $table = $this->connection->getTable($name);
197

zYne's avatar
zYne committed
198
            $table->clear();
doctrine's avatar
doctrine committed
199
        }
zYne's avatar
zYne committed
200
        */
doctrine's avatar
doctrine committed
201

zYne's avatar
zYne committed
202
        $this->objTable = $this->connection->getTable('User');
doctrine's avatar
doctrine committed
203
    }
zYne's avatar
zYne committed
204 205
    public function prepareData() 
    {
zYne's avatar
zYne committed
206
        $groups = new Doctrine_Collection($this->connection->getTable('Group'));
doctrine's avatar
doctrine committed
207

zYne's avatar
zYne committed
208
        $groups[0]->name = 'Drama Actors';
doctrine's avatar
doctrine committed
209

zYne's avatar
zYne committed
210
        $groups[1]->name = 'Quality Actors';
211

doctrine's avatar
doctrine committed
212

zYne's avatar
zYne committed
213 214
        $groups[2]->name = 'Action Actors';
        $groups[2]['Phonenumber'][0]->phonenumber = '123 123';
215
        $groups->save();
doctrine's avatar
doctrine committed
216

zYne's avatar
zYne committed
217
        $users = new Doctrine_Collection('User');
doctrine's avatar
doctrine committed
218 219


zYne's avatar
zYne committed
220 221 222
        $users[0]->name = 'zYne';
        $users[0]['Email']->address = 'zYne@example.com';
        $users[0]['Phonenumber'][0]->phonenumber = '123 123';
doctrine's avatar
doctrine committed
223

zYne's avatar
zYne committed
224 225 226 227 228
        $users[1]->name = 'Arnold Schwarzenegger';
        $users[1]->Email->address = 'arnold@example.com';
        $users[1]['Phonenumber'][0]->phonenumber = '123 123';
        $users[1]['Phonenumber'][1]->phonenumber = '456 456';
        $users[1]->Phonenumber[2]->phonenumber = '789 789';
doctrine's avatar
doctrine committed
229 230
        $users[1]->Group[0] = $groups[2];

zYne's avatar
zYne committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
        $users[2]->name = 'Michael Caine';
        $users[2]->Email->address = 'caine@example.com';
        $users[2]->Phonenumber[0]->phonenumber = '123 123';

        $users[3]->name = 'Takeshi Kitano';
        $users[3]->Email->address = 'kitano@example.com';
        $users[3]->Phonenumber[0]->phonenumber = '111 222 333';

        $users[4]->name = 'Sylvester Stallone';
        $users[4]->Email->address = 'stallone@example.com';
        $users[4]->Phonenumber[0]->phonenumber = '111 555 333';
        $users[4]['Phonenumber'][1]->phonenumber = '123 213';
        $users[4]['Phonenumber'][2]->phonenumber = '444 555';

        $users[5]->name = 'Kurt Russell';
        $users[5]->Email->address = 'russell@example.com';
        $users[5]->Phonenumber[0]->phonenumber = '111 222 333';

        $users[6]->name = 'Jean Reno';
        $users[6]->Email->address = 'reno@example.com';
        $users[6]->Phonenumber[0]->phonenumber = '111 222 333';
        $users[6]['Phonenumber'][1]->phonenumber = '222 123';
        $users[6]['Phonenumber'][2]->phonenumber = '123 456';

        $users[7]->name = 'Edward Furlong';
        $users[7]->Email->address = 'furlong@example.com';
        $users[7]->Phonenumber[0]->phonenumber = '111 567 333';
doctrine's avatar
doctrine committed
258 259

        $this->users = $users;
zYne's avatar
zYne committed
260
        $this->users->save(); 
doctrine's avatar
doctrine committed
261
    }
zYne's avatar
zYne committed
262 263
    public function getConnection() 
    {
zYne's avatar
zYne committed
264
        return $this->connection;
doctrine's avatar
doctrine committed
265
    }
zYne's avatar
zYne committed
266 267
    public function assertDeclarationType($type, $type2) 
    {
268
        $dec = $this->getDeclaration($type);
zYne's avatar
zYne committed
269 270
        
        if ( ! is_array($type2)) {
271
            $type2 = array($type2);
zYne's avatar
zYne committed
272 273 274
        }

        $this->assertEqual($dec['type'], $type2);
275
    }
zYne's avatar
zYne committed
276 277
    public function getDeclaration($type) 
    {
278 279
        return $this->dataDict->getPortableDeclaration(array('type' => $type, 'name' => 'colname', 'length' => 1, 'fixed' => true));
    }
zYne's avatar
zYne committed
280 281
    public function clearCache() 
    {
doctrine's avatar
doctrine committed
282
        foreach($this->tables as $name) {
zYne's avatar
zYne committed
283
            $table = $this->connection->getTable($name);
doctrine's avatar
doctrine committed
284 285 286
            $table->getCache()->deleteAll();
        }
    }
zYne's avatar
zYne committed
287 288
    public function setUp()
    {
zYne's avatar
zYne committed
289 290 291
        if ( ! $this->init) {
            $this->init();
        }
zYne's avatar
zYne committed
292 293 294
        if (isset($this->objTable)) {
            $this->objTable->clear();
        }
zYne's avatar
zYne committed
295 296

        $this->init = true;
doctrine's avatar
doctrine committed
297 298
    }
}