DBTestCase.php 12.1 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 28 29
    
    public function testInitialize() {
        $this->dbh = new Doctrine_Db('sqlite::memory:');
        $this->dbh->exec('CREATE TABLE entity (id INTEGER, name TEXT)');
zYne's avatar
zYne committed
30

zYne's avatar
zYne committed
31 32
        $this->dbh->exec("INSERT INTO entity (id, name) VALUES (1, 'zYne')");
        $this->dbh->exec("INSERT INTO entity (id, name) VALUES (2, 'John')");
33 34
        
        
zYne's avatar
zYne committed
35
        $this->assertEqual($this->dbh->getAttribute(PDO::ATTR_DRIVER_NAME), 'sqlite');
36 37
    }

zYne's avatar
zYne committed
38
    public function testAddValidEventListener() {
zYne's avatar
zYne committed
39 40 41
        $this->dbh->setListener(new Doctrine_Db_EventListener());

        $this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener);
zYne's avatar
zYne committed
42
        try {
zYne's avatar
zYne committed
43
            $ret = $this->dbh->addListener(new Doctrine_Db_TestLogger());
zYne's avatar
zYne committed
44
            $this->pass();
zYne's avatar
zYne committed
45
            $this->assertTrue($ret instanceof Doctrine_Db);
zYne's avatar
zYne committed
46
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
47 48
            $this->fail();
        }
zYne's avatar
zYne committed
49 50 51
        $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
52
        try {
zYne's avatar
zYne committed
53
            $ret = $this->dbh->addListener(new Doctrine_Db_TestValidListener());
zYne's avatar
zYne committed
54
            $this->pass();
zYne's avatar
zYne committed
55
            $this->assertTrue($ret instanceof Doctrine_Db);
zYne's avatar
zYne committed
56
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
57 58
            $this->fail();
        }
zYne's avatar
zYne committed
59 60 61
        $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
62 63
        
        try {
zYne's avatar
zYne committed
64
            $ret = $this->dbh->addListener(new Doctrine_Db_EventListener_Chain(), 'chain');
zYne's avatar
zYne committed
65
            $this->pass();
zYne's avatar
zYne committed
66
            $this->assertTrue($ret instanceof Doctrine_Db);
zYne's avatar
zYne committed
67
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
68 69
            $this->fail();
        }
zYne's avatar
zYne committed
70 71 72 73
        $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
74 75 76 77
        
        // replacing

        try {
zYne's avatar
zYne committed
78
            $ret = $this->dbh->addListener(new Doctrine_Db_EventListener_Chain(), 'chain');
zYne's avatar
zYne committed
79
            $this->pass();
zYne's avatar
zYne committed
80
            $this->assertTrue($ret instanceof Doctrine_Db);
zYne's avatar
zYne committed
81
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
82 83
            $this->fail();
        }
zYne's avatar
zYne committed
84 85 86 87
        $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
88
    }
zYne's avatar
zYne committed
89

zYne's avatar
zYne committed
90
    public function testListeningEventsWithSingleListener() {
zYne's avatar
zYne committed
91 92 93
        $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
94 95 96 97 98 99 100 101 102

        $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
103
        $this->dbh->exec('DELETE FROM entity');
zYne's avatar
zYne committed
104 105 106 107

        $this->assertEqual($listener->pop(), 'onExec');
        $this->assertEqual($listener->pop(), 'onPreExec');
        
zYne's avatar
zYne committed
108
        $this->dbh->beginTransaction();
zYne's avatar
zYne committed
109 110 111 112

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

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

zYne's avatar
zYne committed
115
        $this->dbh->commit();
zYne's avatar
zYne committed
116 117 118 119 120 121 122
        
        $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
123

zYne's avatar
zYne committed
124
    }
125
    public function testListeningQueryEventsWithListenerChain() {
zYne's avatar
zYne committed
126 127 128 129
        $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
130

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

zYne's avatar
zYne committed
133 134
        $listener = $this->dbh->getListener()->get(0);
        $listener2 = $this->dbh->getListener()->get(1);
zYne's avatar
zYne committed
135 136 137 138 139
        $this->assertEqual($listener->pop(), 'onQuery');
        $this->assertEqual($listener->pop(), 'onPreQuery');

        $this->assertEqual($listener2->pop(), 'onQuery');
        $this->assertEqual($listener2->pop(), 'onPreQuery');
140 141
    }
    public function testListeningPrepareEventsWithListenerChain() {
zYne's avatar
zYne committed
142

zYne's avatar
zYne committed
143
        $stmt = $this->dbh->prepare('INSERT INTO entity (id) VALUES(?)');
144 145
        $listener = $this->dbh->getListener()->get(0);
        $listener2 = $this->dbh->getListener()->get(1);
zYne's avatar
zYne committed
146 147 148 149 150 151 152 153 154 155
        $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');
156

zYne's avatar
zYne committed
157 158
        $this->assertEqual($listener2->pop(), 'onExecute');
        $this->assertEqual($listener2->pop(), 'onPreExecute');
159 160
    }
    public function testListeningExecEventsWithListenerChain() {
zYne's avatar
zYne committed
161
        $this->dbh->exec('DELETE FROM entity');
162 163
        $listener = $this->dbh->getListener()->get(0);
        $listener2 = $this->dbh->getListener()->get(1);
zYne's avatar
zYne committed
164 165 166 167 168
        $this->assertEqual($listener->pop(), 'onExec');
        $this->assertEqual($listener->pop(), 'onPreExec');

        $this->assertEqual($listener2->pop(), 'onExec');
        $this->assertEqual($listener2->pop(), 'onPreExec');
169 170
    }
    public function testListeningTransactionEventsWithListenerChain() {
zYne's avatar
zYne committed
171
        $this->dbh->beginTransaction();
172 173
        $listener = $this->dbh->getListener()->get(0);
        $listener2 = $this->dbh->getListener()->get(1);
zYne's avatar
zYne committed
174 175 176 177 178 179
        $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
180
        $this->dbh->query('INSERT INTO entity (id) VALUES (1)');
zYne's avatar
zYne committed
181

zYne's avatar
zYne committed
182
        $this->dbh->commit();
zYne's avatar
zYne committed
183 184 185 186 187 188 189

        $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
190
        $this->dbh->query('DROP TABLE entity');
zYne's avatar
zYne committed
191 192 193
    }
    public function testSetValidEventListener() {
        try {
zYne's avatar
zYne committed
194
            $this->dbh->setListener(new Doctrine_Db_TestLogger());
zYne's avatar
zYne committed
195
            $this->pass();
zYne's avatar
zYne committed
196
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
197 198
            $this->fail();
        }
zYne's avatar
zYne committed
199
        $this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_TestLogger);
zYne's avatar
zYne committed
200
        try {
zYne's avatar
zYne committed
201
            $this->dbh->setListener(new Doctrine_Db_TestValidListener());
zYne's avatar
zYne committed
202
            $this->pass();
zYne's avatar
zYne committed
203
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
204 205
            $this->fail();
        }
zYne's avatar
zYne committed
206
        $this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_TestValidListener);
zYne's avatar
zYne committed
207
        try {
zYne's avatar
zYne committed
208
            $this->dbh->setListener(new Doctrine_Db_EventListener_Chain());
zYne's avatar
zYne committed
209 210
            $this->pass();

zYne's avatar
zYne committed
211
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
212 213
            $this->fail();
        }
zYne's avatar
zYne committed
214
        $this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
zYne's avatar
zYne committed
215
        try {
zYne's avatar
zYne committed
216
            $this->dbh->setListener(new Doctrine_Db_EventListener());
zYne's avatar
zYne committed
217
            $this->pass();
zYne's avatar
zYne committed
218
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
219 220
            $this->fail();
        }
zYne's avatar
zYne committed
221
        $this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener);
zYne's avatar
zYne committed
222 223 224
    }
    public function testSetInvalidEventListener() {
        try {
zYne's avatar
zYne committed
225
            $this->dbh->setListener(new Doctrine_Db_TestInvalidListener());
zYne's avatar
zYne committed
226
            $this->fail();
zYne's avatar
zYne committed
227
        } catch(Doctrine_Db_Exception $e) {
zYne's avatar
zYne committed
228 229 230
            $this->pass();
        }
    }
231 232
    public function testInvalidDSN() {
        try {
zYne's avatar
zYne committed
233
            $this->dbh = Doctrine_Db::getConnection('');
234
            $this->fail();
zYne's avatar
zYne committed
235
        } catch(Doctrine_Db_Exception $e) {
236 237 238
            $this->pass();
        }
        try {
zYne's avatar
zYne committed
239
            $this->dbh = Doctrine_Db::getConnection('unknown');
240
            $this->fail();
zYne's avatar
zYne committed
241
        } catch(Doctrine_Db_Exception $e) {
242 243 244
            $this->pass();
        }   
        try {
zYne's avatar
zYne committed
245
            $this->dbh = Doctrine_Db::getConnection(0);
246
            $this->fail();
zYne's avatar
zYne committed
247
        } catch(Doctrine_Db_Exception $e) {
248 249 250 251 252
            $this->pass();
        }
    }
    public function testInvalidScheme() {
        try {
zYne's avatar
zYne committed
253
            $this->dbh = Doctrine_Db::getConnection('unknown://:memory:');
254
            $this->fail();
zYne's avatar
zYne committed
255
        } catch(Doctrine_Db_Exception $e) {
256 257 258 259 260
            $this->pass();
        }
    }
    public function testInvalidHost() {
        try {
zYne's avatar
zYne committed
261
            $this->dbh = Doctrine_Db::getConnection('mysql://user:password@');
262
            $this->fail();
zYne's avatar
zYne committed
263
        } catch(Doctrine_Db_Exception $e) {
264 265 266 267 268
            $this->pass();
        }
    }
    public function testInvalidDatabase() {
        try {
zYne's avatar
zYne committed
269
            $this->dbh = Doctrine_Db::getConnection('mysql://user:password@host/');
270
            $this->fail();
zYne's avatar
zYne committed
271
        } catch(Doctrine_Db_Exception $e) {
272 273 274
            $this->pass();
        }
    }
zYne's avatar
zYne committed
275
    public function testGetConnectionPdoLikeDSN() {
zYne's avatar
zYne committed
276
        $this->dbh = Doctrine_Db::getConnection('mysql:host=localhost;dbname=test', 'root', 'password');
zYne's avatar
zYne committed
277 278 279
        $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
280

281

zYne's avatar
zYne committed
282
        $this->dbh = Doctrine_Db::getConnection('sqlite::memory:');
zYne's avatar
zYne committed
283

zYne's avatar
zYne committed
284 285 286
        $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
287
    }
zYne's avatar
zYne committed
288 289 290 291
    public function testDriverName() {

    }

zYne's avatar
zYne committed
292
    public function testGetConnectionWithPearLikeDSN() {
zYne's avatar
zYne committed
293
        $this->dbh = Doctrine_Db::getConnection('mysql://zYne:password@localhost/test');
zYne's avatar
zYne committed
294 295 296
        $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');
297

zYne's avatar
zYne committed
298

zYne's avatar
zYne committed
299
        $this->dbh = Doctrine_Db::getConnection('sqlite://:memory:');
zYne's avatar
zYne committed
300

zYne's avatar
zYne committed
301 302 303
        $this->assertEqual($this->dbh->getOption('dsn'), 'sqlite::memory:');
        $this->assertEqual($this->dbh->getOption('username'), false);
        $this->assertEqual($this->dbh->getOption('password'), false);
304
    }
zYne's avatar
zYne committed
305

306
}