DBTestCase.php 13.9 KB
Newer Older
1 2 3
<?php
require_once("../draft/DB.php");

zYne's avatar
zYne committed
4
class Doctrine_Db_TestLogger implements Doctrine_Overloadable {
zYne's avatar
zYne committed
5 6 7 8 9 10 11 12 13 14 15 16
    private $messages = array();
    
    public function __call($m, $a) {
        $this->messages[] = $m;
    }
    public function pop() {
        return array_pop($this->messages);
    }
    public function getAll() {
        return $this->messages;
    }
}
zYne's avatar
zYne committed
17 18
class Doctrine_Db_TestValidListener extends Doctrine_Db_EventListener { }
class Doctrine_Db_TestInvalidListener { }
zYne's avatar
zYne committed
19

zYne's avatar
zYne committed
20
class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
zYne's avatar
zYne committed
21 22
    protected $dbh;

23 24 25
    public function prepareData() { }
    public function prepareTables() { }
    public function init() { }
zYne's avatar
zYne committed
26

27
    public function testFetchAll() {
zYne's avatar
zYne committed
28 29
        $this->dbh = Doctrine_Db2::getConnection('sqlite::memory:');
        $this->dbh->connect();
30 31


zYne's avatar
zYne committed
32
        $this->dbh->query('CREATE TABLE entity (id INTEGER, name TEXT)');
33

zYne's avatar
zYne committed
34 35
        $this->dbh->query("INSERT INTO entity (id, name) VALUES (1, 'zYne')");
        $this->dbh->query("INSERT INTO entity (id, name) VALUES (2, 'John')");
36

zYne's avatar
zYne committed
37
        $a = $this->dbh->fetchAll('SELECT * FROM entity');
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53


        $this->assertEqual($a, array (
                            0 =>
                            array (
                              'id' => '1',
                              'name' => 'zYne',
                            ),
                            1 =>
                            array (
                              'id' => '2',
                              'name' => 'John',
                            ),
                          ));
    }
    public function testFetchOne() {
zYne's avatar
zYne committed
54
        $c = $this->dbh->fetchOne('SELECT COUNT(1) FROM entity');
55 56 57
        
        $this->assertEqual($c, 2);
        
zYne's avatar
zYne committed
58
        $c = $this->dbh->fetchOne('SELECT COUNT(1) FROM entity WHERE id = ?', array(1));
59 60 61 62 63 64 65 66
        
        $this->assertEqual($c, 1);
    }
    
    public function testFetchAssoc() {

    }
    public function testFetchColumn() {
zYne's avatar
zYne committed
67
        $a = $this->dbh->fetchColumn('SELECT * FROM entity');
68 69 70 71 72 73

        $this->assertEqual($a, array (
                              0 => '1',
                              1 => '2',
                            ));

zYne's avatar
zYne committed
74
        $a = $this->dbh->fetchColumn('SELECT * FROM entity WHERE id = ?', array(1));
75 76 77 78 79 80

        $this->assertEqual($a, array (
                              0 => '1',
                            ));
    }
    public function testFetchArray() {
zYne's avatar
zYne committed
81
        $a = $this->dbh->fetchArray('SELECT * FROM entity');
82 83 84 85 86 87

        $this->assertEqual($a, array (
                              0 => '1',
                              1 => 'zYne',
                            ));

zYne's avatar
zYne committed
88
        $a = $this->dbh->fetchArray('SELECT * FROM entity WHERE id = ?', array(1));
89 90 91 92 93 94 95

        $this->assertEqual($a, array (
                              0 => '1',
                              1 => 'zYne',
                            ));
    }
    public function testFetchRow() {
zYne's avatar
zYne committed
96
        $c = $this->dbh->fetchRow('SELECT * FROM entity');
97 98 99 100 101 102

        $this->assertEqual($c, array (
                              'id' => '1',
                              'name' => 'zYne',
                            ));
                            
zYne's avatar
zYne committed
103
        $c = $this->dbh->fetchRow('SELECT * FROM entity WHERE id = ?', array(1));
104 105 106 107 108 109 110 111 112
        
        $this->assertEqual($c, array (
                              'id' => '1',
                              'name' => 'zYne',
                            ));
    }
    public function testFetchPairs() {
                                   	
    }
zYne's avatar
zYne committed
113
    public function testAddValidEventListener() {
zYne's avatar
zYne committed
114 115 116
        $this->dbh->setListener(new Doctrine_Db_EventListener());

        $this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener);
zYne's avatar
zYne committed
117
        try {
zYne's avatar
zYne committed
118
            $ret = $this->dbh->addListener(new Doctrine_Db_TestLogger());
zYne's avatar
zYne committed
119
            $this->pass();
zYne's avatar
zYne committed
120 121
            $this->assertTrue($ret instanceof Doctrine_Db2);
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
122 123
            $this->fail();
        }
zYne's avatar
zYne committed
124 125 126
        $this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
        $this->assertTrue($this->dbh->getListener()->get(0) instanceof Doctrine_Db_TestLogger);

zYne's avatar
zYne committed
127
        try {
zYne's avatar
zYne committed
128
            $ret = $this->dbh->addListener(new Doctrine_Db_TestValidListener());
zYne's avatar
zYne committed
129
            $this->pass();
zYne's avatar
zYne committed
130 131
            $this->assertTrue($ret instanceof Doctrine_Db2);
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
132 133
            $this->fail();
        }
zYne's avatar
zYne committed
134 135 136
        $this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
        $this->assertTrue($this->dbh->getListener()->get(0) instanceof Doctrine_Db_TestLogger);
        $this->assertTrue($this->dbh->getListener()->get(1) instanceof Doctrine_Db_TestValidListener);
zYne's avatar
zYne committed
137 138
        
        try {
zYne's avatar
zYne committed
139
            $ret = $this->dbh->addListener(new Doctrine_Db_EventListener_Chain(), 'chain');
zYne's avatar
zYne committed
140
            $this->pass();
zYne's avatar
zYne committed
141 142
            $this->assertTrue($ret instanceof Doctrine_Db2);
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
143 144
            $this->fail();
        }
zYne's avatar
zYne committed
145 146 147 148
        $this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
        $this->assertTrue($this->dbh->getListener()->get(0) instanceof Doctrine_Db_TestLogger);
        $this->assertTrue($this->dbh->getListener()->get(1) instanceof Doctrine_Db_TestValidListener);
        $this->assertTrue($this->dbh->getListener()->get('chain') instanceof Doctrine_Db_EventListener_Chain);
zYne's avatar
zYne committed
149 150 151 152
        
        // replacing

        try {
zYne's avatar
zYne committed
153
            $ret = $this->dbh->addListener(new Doctrine_Db_EventListener_Chain(), 'chain');
zYne's avatar
zYne committed
154
            $this->pass();
zYne's avatar
zYne committed
155 156
            $this->assertTrue($ret instanceof Doctrine_Db2);
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
157 158
            $this->fail();
        }
zYne's avatar
zYne committed
159 160 161 162
        $this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
        $this->assertTrue($this->dbh->getListener()->get(0) instanceof Doctrine_Db_TestLogger);
        $this->assertTrue($this->dbh->getListener()->get(1) instanceof Doctrine_Db_TestValidListener);
        $this->assertTrue($this->dbh->getListener()->get('chain') instanceof Doctrine_Db_EventListener_Chain);
zYne's avatar
zYne committed
163
    }
zYne's avatar
zYne committed
164

zYne's avatar
zYne committed
165
    public function testListeningEventsWithSingleListener() {
zYne's avatar
zYne committed
166 167 168
        $this->dbh->setListener(new Doctrine_Db_TestLogger());
        $listener = $this->dbh->getListener();
        $stmt = $this->dbh->prepare('INSERT INTO entity (id) VALUES(?)');
zYne's avatar
zYne committed
169 170 171 172 173 174 175 176 177

        $this->assertEqual($listener->pop(), 'onPrepare');
        $this->assertEqual($listener->pop(), 'onPrePrepare');
        
        $stmt->execute(array(1));

        $this->assertEqual($listener->pop(), 'onExecute');
        $this->assertEqual($listener->pop(), 'onPreExecute');
        
zYne's avatar
zYne committed
178
        $this->dbh->exec('DELETE FROM entity');
zYne's avatar
zYne committed
179 180 181 182

        $this->assertEqual($listener->pop(), 'onExec');
        $this->assertEqual($listener->pop(), 'onPreExec');
        
zYne's avatar
zYne committed
183
        $this->dbh->beginTransaction();
zYne's avatar
zYne committed
184 185 186 187

        $this->assertEqual($listener->pop(), 'onBeginTransaction');
        $this->assertEqual($listener->pop(), 'onPreBeginTransaction');

zYne's avatar
zYne committed
188
        $this->dbh->query('INSERT INTO entity (id) VALUES (1)');
zYne's avatar
zYne committed
189

zYne's avatar
zYne committed
190
        $this->dbh->commit();
zYne's avatar
zYne committed
191 192 193 194 195 196 197
        
        $this->assertEqual($listener->pop(), 'onCommit');
        $this->assertEqual($listener->pop(), 'onPreCommit');
        
        $this->assertEqual($listener->pop(), 'onQuery');
        $this->assertEqual($listener->pop(), 'onPreQuery');
        
zYne's avatar
zYne committed
198

zYne's avatar
zYne committed
199 200
    }
    public function testListeningEventsWithListenerChain() {
zYne's avatar
zYne committed
201 202 203 204
        $this->dbh->query('DROP TABLE entity');

        $this->dbh->addListener(new Doctrine_Db_TestLogger());
        $this->dbh->addListener(new Doctrine_Db_TestLogger());
zYne's avatar
zYne committed
205

zYne's avatar
zYne committed
206
        $this->dbh->query('CREATE TABLE entity (id INT)');
zYne's avatar
zYne committed
207

zYne's avatar
zYne committed
208 209
        $listener = $this->dbh->getListener()->get(0);
        $listener2 = $this->dbh->getListener()->get(1);
zYne's avatar
zYne committed
210 211 212 213 214 215 216
        $this->assertEqual($listener->pop(), 'onQuery');
        $this->assertEqual($listener->pop(), 'onPreQuery');

        $this->assertEqual($listener2->pop(), 'onQuery');
        $this->assertEqual($listener2->pop(), 'onPreQuery');


zYne's avatar
zYne committed
217
        $stmt = $this->dbh->prepare('INSERT INTO entity (id) VALUES(?)');
zYne's avatar
zYne committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

        $this->assertEqual($listener->pop(), 'onPrepare');
        $this->assertEqual($listener->pop(), 'onPrePrepare');

        $this->assertEqual($listener2->pop(), 'onPrepare');
        $this->assertEqual($listener2->pop(), 'onPrePrepare');

        $stmt->execute(array(1));

        $this->assertEqual($listener->pop(), 'onExecute');
        $this->assertEqual($listener->pop(), 'onPreExecute');
        
        $this->assertEqual($listener2->pop(), 'onExecute');
        $this->assertEqual($listener2->pop(), 'onPreExecute');
        
zYne's avatar
zYne committed
233
        $this->dbh->exec('DELETE FROM entity');
zYne's avatar
zYne committed
234 235 236 237 238 239 240

        $this->assertEqual($listener->pop(), 'onExec');
        $this->assertEqual($listener->pop(), 'onPreExec');

        $this->assertEqual($listener2->pop(), 'onExec');
        $this->assertEqual($listener2->pop(), 'onPreExec');

zYne's avatar
zYne committed
241
        $this->dbh->beginTransaction();
zYne's avatar
zYne committed
242 243 244 245 246 247 248

        $this->assertEqual($listener->pop(), 'onBeginTransaction');
        $this->assertEqual($listener->pop(), 'onPreBeginTransaction');

        $this->assertEqual($listener2->pop(), 'onBeginTransaction');
        $this->assertEqual($listener2->pop(), 'onPreBeginTransaction');

zYne's avatar
zYne committed
249
        $this->dbh->query('INSERT INTO entity (id) VALUES (1)');
zYne's avatar
zYne committed
250

zYne's avatar
zYne committed
251
        $this->dbh->commit();
zYne's avatar
zYne committed
252 253 254 255 256 257 258

        $this->assertEqual($listener->pop(), 'onCommit');
        $this->assertEqual($listener->pop(), 'onPreCommit');
        
        $this->assertEqual($listener->pop(), 'onQuery');
        $this->assertEqual($listener->pop(), 'onPreQuery');
        
zYne's avatar
zYne committed
259
        $this->dbh->query('DROP TABLE entity');
zYne's avatar
zYne committed
260 261 262
    }
    public function testSetValidEventListener() {
        try {
zYne's avatar
zYne committed
263
            $this->dbh->setListener(new Doctrine_Db_TestLogger());
zYne's avatar
zYne committed
264
            $this->pass();
zYne's avatar
zYne committed
265
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
266 267
            $this->fail();
        }
zYne's avatar
zYne committed
268
        $this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_TestLogger);
zYne's avatar
zYne committed
269
        try {
zYne's avatar
zYne committed
270
            $this->dbh->setListener(new Doctrine_Db_TestValidListener());
zYne's avatar
zYne committed
271
            $this->pass();
zYne's avatar
zYne committed
272
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
273 274
            $this->fail();
        }
zYne's avatar
zYne committed
275
        $this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_TestValidListener);
zYne's avatar
zYne committed
276
        try {
zYne's avatar
zYne committed
277
            $this->dbh->setListener(new Doctrine_Db_EventListener_Chain());
zYne's avatar
zYne committed
278 279
            $this->pass();

zYne's avatar
zYne committed
280
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
281 282
            $this->fail();
        }
zYne's avatar
zYne committed
283
        $this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
zYne's avatar
zYne committed
284
        try {
zYne's avatar
zYne committed
285
            $this->dbh->setListener(new Doctrine_Db_EventListener());
zYne's avatar
zYne committed
286
            $this->pass();
zYne's avatar
zYne committed
287
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
288 289
            $this->fail();
        }
zYne's avatar
zYne committed
290
        $this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener);
zYne's avatar
zYne committed
291 292 293
    }
    public function testSetInvalidEventListener() {
        try {
zYne's avatar
zYne committed
294
            $this->dbh->setListener(new Doctrine_Db_TestInvalidListener());
zYne's avatar
zYne committed
295
            $this->fail();
zYne's avatar
zYne committed
296
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
297 298 299
            $this->pass();
        }
    }
300 301
    public function testInvalidDSN() {
        try {
zYne's avatar
zYne committed
302
            $this->dbh = Doctrine_Db2::getConnection('');
303
            $this->fail();
zYne's avatar
zYne committed
304
        } catch(Doctrine_Db_Exception $e) {
305 306 307
            $this->pass();
        }
        try {
zYne's avatar
zYne committed
308
            $this->dbh = Doctrine_Db2::getConnection('unknown');
309
            $this->fail();
zYne's avatar
zYne committed
310
        } catch(Doctrine_Db_Exception $e) {
311 312 313
            $this->pass();
        }   
        try {
zYne's avatar
zYne committed
314
            $this->dbh = Doctrine_Db2::getConnection(0);
315
            $this->fail();
zYne's avatar
zYne committed
316
        } catch(Doctrine_Db_Exception $e) {
317 318 319 320 321
            $this->pass();
        }
    }
    public function testInvalidScheme() {
        try {
zYne's avatar
zYne committed
322
            $this->dbh = Doctrine_Db2::getConnection('unknown://:memory:');
323
            $this->fail();
zYne's avatar
zYne committed
324
        } catch(Doctrine_Db_Exception $e) {
325 326 327 328 329
            $this->pass();
        }
    }
    public function testInvalidHost() {
        try {
zYne's avatar
zYne committed
330
            $this->dbh = Doctrine_Db2::getConnection('mysql://user:password@');
331
            $this->fail();
zYne's avatar
zYne committed
332
        } catch(Doctrine_Db_Exception $e) {
333 334 335 336 337
            $this->pass();
        }
    }
    public function testInvalidDatabase() {
        try {
zYne's avatar
zYne committed
338
            $this->dbh = Doctrine_Db2::getConnection('mysql://user:password@host/');
339
            $this->fail();
zYne's avatar
zYne committed
340
        } catch(Doctrine_Db_Exception $e) {
341 342 343
            $this->pass();
        }
    }
zYne's avatar
zYne committed
344
    public function testGetConnectionPdoLikeDSN() {
zYne's avatar
zYne committed
345 346 347 348
        $this->dbh = Doctrine_Db2::getConnection('mysql:host=localhost;dbname=test', 'root', 'password');
        $this->assertEqual($this->dbh->getOption('dsn'), 'mysql:host=localhost;dbname=test');
        $this->assertEqual($this->dbh->getOption('username'), 'root');
        $this->assertEqual($this->dbh->getOption('password'), 'password');
zYne's avatar
zYne committed
349

350

zYne's avatar
zYne committed
351
        $this->dbh = Doctrine_Db2::getConnection('sqlite::memory:');
zYne's avatar
zYne committed
352

zYne's avatar
zYne committed
353 354 355
        $this->assertEqual($this->dbh->getOption('dsn'), 'sqlite::memory:');
        $this->assertEqual($this->dbh->getOption('username'), false);
        $this->assertEqual($this->dbh->getOption('password'), false);
zYne's avatar
zYne committed
356
    }
zYne's avatar
zYne committed
357 358 359 360
    public function testDriverName() {

    }

zYne's avatar
zYne committed
361
    public function testGetConnectionWithPearLikeDSN() {
zYne's avatar
zYne committed
362 363 364 365
        $this->dbh = Doctrine_Db2::getConnection('mysql://zYne:password@localhost/test');
        $this->assertEqual($this->dbh->getOption('dsn'), 'mysql:host=localhost;dbname=test');
        $this->assertEqual($this->dbh->getOption('username'), 'zYne');
        $this->assertEqual($this->dbh->getOption('password'), 'password');
366

zYne's avatar
zYne committed
367

zYne's avatar
zYne committed
368
        $this->dbh = Doctrine_Db2::getConnection('sqlite://:memory:');
zYne's avatar
zYne committed
369

zYne's avatar
zYne committed
370 371 372
        $this->assertEqual($this->dbh->getOption('dsn'), 'sqlite::memory:');
        $this->assertEqual($this->dbh->getOption('username'), false);
        $this->assertEqual($this->dbh->getOption('password'), false);
373
    }
zYne's avatar
zYne committed
374

375
}