UnitTestCase.php 7.35 KB
Newer Older
doctrine's avatar
doctrine committed
1 2 3
<?php
class Doctrine_UnitTestCase extends UnitTestCase {
    protected $manager;
zYne's avatar
zYne committed
4
    protected $connection;
doctrine's avatar
doctrine committed
5 6 7 8 9 10 11
    protected $objTable;
    protected $new;
    protected $old;
    protected $dbh;
    protected $listener;
    protected $cache;
    protected $users;
12
    protected $valueHolder;
doctrine's avatar
doctrine committed
13
    protected $tables = array();
14
    protected $unitOfWork;
zYne's avatar
zYne committed
15 16 17 18 19 20 21 22
    protected $driverName = false;
    protected $generic = false;
    protected $conn;
    protected $adapter;
    protected $export;
    protected $dataDict;
    protected $transaction;
    
doctrine's avatar
doctrine committed
23 24 25 26 27 28 29

    private $init = false;

    public function init() {
        $name = get_class($this);

        $this->manager   = Doctrine_Manager::getInstance();
doctrine's avatar
doctrine committed
30
        $this->manager->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_IMMEDIATE);
31
        
doctrine's avatar
doctrine committed
32

doctrine's avatar
doctrine committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
        $this->tables = array_merge($this->tables, 
                        array("entity",
                              "entityReference",
                              "email",
                              "phonenumber",
                              "groupuser",
                              "album",
                              "song",
                              "element",
                              "error",
                              "description",
                              "address",
                              "account",
                              "task",
                              "resource",
                              "assignment",
                              "resourceType",
                              "resourceReference")
                              );
52 53


zYne's avatar
zYne committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        $class = get_class($this);
        $e     = explode('_', $class);

        $this->driverName = 'main';

        switch($e[1]) {
            case 'Export':
            case 'Import':
            case 'Expression':
            case 'Transaction':
            case 'DataDict':
                $this->driverName = 'Sqlite';
            break;
        }

        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;
            }
        }
83

84
        try {
zYne's avatar
zYne committed
85
            $this->connection = $this->manager->getConnection($this->driverName);
zYne's avatar
zYne committed
86
            $this->connection->evictTables();
zYne's avatar
zYne committed
87
            $this->dbh      = $this->adapter = $this->connection->getDbh();
doctrine's avatar
doctrine committed
88
            $this->listener = $this->manager->getAttribute(Doctrine::ATTR_LISTENER);
89

90
            $this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
91
        } catch(Doctrine_Manager_Exception $e) {
zYne's avatar
zYne committed
92 93 94 95 96
            if($this->driverName == 'main') {
                $this->dbh = Doctrine_Db::getConnection("sqlite::memory:");
            } else {
                $this->dbh = $this->adapter = new AdapterMock($this->driverName);
            }
zYne's avatar
zYne committed
97

zYne's avatar
zYne committed
98 99 100 101 102 103 104 105 106
            $this->connection  = $this->manager->openConnection($this->dbh, $this->driverName);

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

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

            } else {
            }
zYne's avatar
zYne committed
107

doctrine's avatar
doctrine committed
108
            $this->listener = new Doctrine_EventListener_Debugger();
doctrine's avatar
doctrine committed
109 110
            $this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
        }
zYne's avatar
zYne committed
111 112 113 114 115
        if($this->driverName !== 'main') {
            $this->export       = $this->connection->export;
            $this->transaction  = $this->connection->transaction;
            $this->dataDict     = $this->connection->dataDict;
        }
116
        $this->unitOfWork = $this->connection->unitOfWork;
117
        $this->connection->setListener(new Doctrine_EventListener());
zYne's avatar
zYne committed
118
        $this->query = new Doctrine_Query($this->connection);
119 120
        $this->prepareTables();
        $this->prepareData();
121

zYne's avatar
zYne committed
122
        $this->valueHolder = new Doctrine_ValueHolder($this->connection->getTable('User'));
123

124
    }
doctrine's avatar
doctrine committed
125
    public function prepareTables() {
126
        foreach($this->tables as $name) {
zYne's avatar
zYne committed
127
            $query = 'DROP TABLE ' . Doctrine::tableize($name);
128 129 130 131 132
            try {
                $this->dbh->query($query);
            } catch(PDOException $e) {

            }
doctrine's avatar
doctrine committed
133 134
        }

135
        foreach($this->tables as $name) {
136
            $name = ucwords($name);
zYne's avatar
zYne committed
137
            $table = $this->connection->getTable($name);
138

doctrine's avatar
doctrine committed
139
            $table->clear(); 
doctrine's avatar
doctrine committed
140 141
        }

zYne's avatar
zYne committed
142
        $this->objTable = $this->connection->getTable('User');
doctrine's avatar
doctrine committed
143 144
    }
    public function prepareData() {
zYne's avatar
zYne committed
145
        $groups = new Doctrine_Collection($this->connection->getTable('Group'));
doctrine's avatar
doctrine committed
146

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

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

doctrine's avatar
doctrine committed
151

zYne's avatar
zYne committed
152 153
        $groups[2]->name = 'Action Actors';
        $groups[2]['Phonenumber'][0]->phonenumber = '123 123';
154
        $groups->save();
doctrine's avatar
doctrine committed
155

zYne's avatar
zYne committed
156
        $users = new Doctrine_Collection('User');
doctrine's avatar
doctrine committed
157 158


zYne's avatar
zYne committed
159 160 161
        $users[0]->name = 'zYne';
        $users[0]['Email']->address = 'zYne@example.com';
        $users[0]['Phonenumber'][0]->phonenumber = '123 123';
doctrine's avatar
doctrine committed
162

zYne's avatar
zYne committed
163 164 165 166 167
        $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
168 169
        $users[1]->Group[0] = $groups[2];

zYne's avatar
zYne committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
        $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
197 198

        $this->users = $users;
zYne's avatar
zYne committed
199
        $this->connection->flush();
doctrine's avatar
doctrine committed
200
    }
zYne's avatar
zYne committed
201 202
    public function getConnection() {
        return $this->connection;
doctrine's avatar
doctrine committed
203
    }
doctrine's avatar
doctrine committed
204 205
    public function clearCache() {
        foreach($this->tables as $name) {
zYne's avatar
zYne committed
206
            $table = $this->connection->getTable($name);
doctrine's avatar
doctrine committed
207 208 209 210 211 212
            $table->getCache()->deleteAll();
        }
    }
    public function setUp() {
        if( ! $this->init) $this->init(); 
        
213 214 215
        if(isset($this->objTable))
            $this->objTable->clear();
        
doctrine's avatar
doctrine committed
216 217 218 219
        $this->init    = true;
    }
}
?>