UnitTestCase.php 8.12 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

zYne's avatar
zYne committed
25
    protected $init = false;
doctrine's avatar
doctrine committed
26 27 28 29 30

    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);
zYne's avatar
zYne committed
32
        $this->manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL);
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 89
            $this->manager->setCurrentConnection($this->driverName);

zYne's avatar
zYne committed
90
            $this->connection->evictTables();
zYne's avatar
zYne committed
91
            $this->dbh      = $this->adapter = $this->connection->getDbh();
doctrine's avatar
doctrine committed
92
            $this->listener = $this->manager->getAttribute(Doctrine::ATTR_LISTENER);
93

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

96
        } catch(Doctrine_Manager_Exception $e) {
zYne's avatar
zYne committed
97
            if($this->driverName == 'main') {
zYne's avatar
zYne committed
98
                $this->dbh = Doctrine_Db::getConnection('sqlite::memory:');
zYne's avatar
zYne committed
99 100 101
            } else {
                $this->dbh = $this->adapter = new AdapterMock($this->driverName);
            }
zYne's avatar
zYne committed
102

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

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

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

            } else {
            }
zYne's avatar
zYne committed
112

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

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

            }
doctrine's avatar
doctrine committed
141 142
        }

143
        foreach($this->tables as $name) {
144
            $name = ucwords($name);
zYne's avatar
zYne committed
145
            $table = $this->connection->getTable($name);
146

doctrine's avatar
doctrine committed
147
            $table->clear(); 
doctrine's avatar
doctrine committed
148 149
        }

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

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

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

doctrine's avatar
doctrine committed
159

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

zYne's avatar
zYne committed
164
        $users = new Doctrine_Collection('User');
doctrine's avatar
doctrine committed
165 166


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

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

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

        $this->users = $users;
zYne's avatar
zYne committed
207
        $this->connection->flush();
doctrine's avatar
doctrine committed
208
    }
zYne's avatar
zYne committed
209 210
    public function getConnection() {
        return $this->connection;
doctrine's avatar
doctrine committed
211
    }
212 213
    public function assertDeclarationType($type, $type2) {
        $dec = $this->getDeclaration($type);
zYne's avatar
zYne committed
214 215
        
        if ( ! is_array($type2)) {
216
            $type2 = array($type2);
zYne's avatar
zYne committed
217 218 219
        }

        $this->assertEqual($dec['type'], $type2);
220 221 222 223
    }
    public function getDeclaration($type) {
        return $this->dataDict->getPortableDeclaration(array('type' => $type, 'name' => 'colname', 'length' => 1, 'fixed' => true));
    }
doctrine's avatar
doctrine committed
224 225
    public function clearCache() {
        foreach($this->tables as $name) {
zYne's avatar
zYne committed
226
            $table = $this->connection->getTable($name);
doctrine's avatar
doctrine committed
227 228 229 230
            $table->getCache()->deleteAll();
        }
    }
    public function setUp() {
zYne's avatar
zYne committed
231 232 233 234
        if ( ! $this->init) {
            $this->init();
        }
        $this->objTable->clear();
235
        
doctrine's avatar
doctrine committed
236 237 238 239
        $this->init    = true;
    }
}
?>