UnitTestCase.php 8 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
    protected $objTable;
    protected $new;
    protected $old;
    protected $dbh;
    protected $listener;
zYne's avatar
zYne committed
10

doctrine's avatar
doctrine committed
11
    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
    protected $driverName = false;
    protected $generic = false;
    protected $conn;
    protected $adapter;
    protected $export;
20
    protected $expr;
zYne's avatar
zYne committed
21 22
    protected $dataDict;
    protected $transaction;
zYne's avatar
zYne committed
23

doctrine's avatar
doctrine committed
24 25 26 27 28 29 30

    private $init = false;

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

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

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


zYne's avatar
zYne committed
55 56 57 58 59 60 61 62 63 64 65
        $class = get_class($this);
        $e     = explode('_', $class);

        $this->driverName = 'main';

        switch($e[1]) {
            case 'Export':
            case 'Import':
            case 'Expression':
            case 'Transaction':
            case 'DataDict':
zYne's avatar
zYne committed
66
            case 'Sequence':
zYne's avatar
zYne committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
                $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;
            }
        }
85

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

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

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

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

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

            } else {
            }
zYne's avatar
zYne committed
109

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

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

129
    }
doctrine's avatar
doctrine committed
130
    public function prepareTables() {
131
        foreach($this->tables as $name) {
zYne's avatar
zYne committed
132
            $query = 'DROP TABLE ' . Doctrine::tableize($name);
133 134 135 136 137
            try {
                $this->dbh->query($query);
            } catch(PDOException $e) {

            }
doctrine's avatar
doctrine committed
138 139
        }

140
        foreach($this->tables as $name) {
141
            $name = ucwords($name);
zYne's avatar
zYne committed
142
            $table = $this->connection->getTable($name);
143

doctrine's avatar
doctrine committed
144
            $table->clear(); 
doctrine's avatar
doctrine committed
145 146
        }

zYne's avatar
zYne committed
147
        $this->objTable = $this->connection->getTable('User');
doctrine's avatar
doctrine committed
148 149
    }
    public function prepareData() {
zYne's avatar
zYne committed
150
        $groups = new Doctrine_Collection($this->connection->getTable('Group'));
doctrine's avatar
doctrine committed
151

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

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

doctrine's avatar
doctrine committed
156

zYne's avatar
zYne committed
157 158
        $groups[2]->name = 'Action Actors';
        $groups[2]['Phonenumber'][0]->phonenumber = '123 123';
159
        $groups->save();
doctrine's avatar
doctrine committed
160

zYne's avatar
zYne committed
161
        $users = new Doctrine_Collection('User');
doctrine's avatar
doctrine committed
162 163


zYne's avatar
zYne committed
164 165 166
        $users[0]->name = 'zYne';
        $users[0]['Email']->address = 'zYne@example.com';
        $users[0]['Phonenumber'][0]->phonenumber = '123 123';
doctrine's avatar
doctrine committed
167

zYne's avatar
zYne committed
168 169 170 171 172
        $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
173 174
        $users[1]->Group[0] = $groups[2];

zYne's avatar
zYne committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        $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
202 203

        $this->users = $users;
zYne's avatar
zYne committed
204
        $this->connection->flush();
doctrine's avatar
doctrine committed
205
    }
zYne's avatar
zYne committed
206 207
    public function getConnection() {
        return $this->connection;
doctrine's avatar
doctrine committed
208
    }
209 210 211 212 213 214 215 216 217
    public function assertDeclarationType($type, $type2) {
        $dec = $this->getDeclaration($type);
        if( ! is_array($type2))
            $type2 = array($type2);
        $this->assertEqual($dec[0], $type2);
    }
    public function getDeclaration($type) {
        return $this->dataDict->getPortableDeclaration(array('type' => $type, 'name' => 'colname', 'length' => 1, 'fixed' => true));
    }
doctrine's avatar
doctrine committed
218 219
    public function clearCache() {
        foreach($this->tables as $name) {
zYne's avatar
zYne committed
220
            $table = $this->connection->getTable($name);
doctrine's avatar
doctrine committed
221 222 223 224 225 226
            $table->getCache()->deleteAll();
        }
    }
    public function setUp() {
        if( ! $this->init) $this->init(); 
        
227 228 229
        if(isset($this->objTable))
            $this->objTable->clear();
        
doctrine's avatar
doctrine committed
230 231 232 233
        $this->init    = true;
    }
}
?>