2006-09-23 01:20:21 +04:00
|
|
|
<?php
|
|
|
|
require_once("../draft/DB.php");
|
|
|
|
|
2006-11-06 20:56:14 +03:00
|
|
|
class Doctrine_Db_TestLogger implements Doctrine_Overloadable {
|
2006-09-23 14:44:39 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2006-11-06 20:56:14 +03:00
|
|
|
class Doctrine_Db_TestValidListener extends Doctrine_Db_EventListener { }
|
|
|
|
class Doctrine_Db_TestInvalidListener { }
|
2006-09-23 14:44:39 +04:00
|
|
|
|
2006-11-06 20:56:14 +03:00
|
|
|
class Doctrine_Db_TestCase extends Doctrine_UnitTestCase {
|
2006-11-08 13:18:15 +03:00
|
|
|
protected $dbh;
|
|
|
|
|
2006-09-23 01:20:21 +04:00
|
|
|
public function prepareData() { }
|
|
|
|
public function prepareTables() { }
|
|
|
|
public function init() { }
|
2007-01-12 14:38:03 +03:00
|
|
|
|
|
|
|
public function testInitialize() {
|
|
|
|
$this->dbh = new Doctrine_Db('sqlite::memory:');
|
|
|
|
$this->dbh->exec('CREATE TABLE entity (id INTEGER, name TEXT)');
|
2006-09-23 14:44:39 +04:00
|
|
|
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->dbh->exec("INSERT INTO entity (id, name) VALUES (1, 'zYne')");
|
|
|
|
$this->dbh->exec("INSERT INTO entity (id, name) VALUES (2, 'John')");
|
2006-09-27 14:55:02 +04:00
|
|
|
|
|
|
|
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->assertEqual($this->dbh->getAttribute(PDO::ATTR_DRIVER_NAME), 'sqlite');
|
2006-09-27 14:55:02 +04:00
|
|
|
}
|
|
|
|
|
2006-09-23 14:44:39 +04:00
|
|
|
public function testAddValidEventListener() {
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->setListener(new Doctrine_Db_EventListener());
|
|
|
|
|
|
|
|
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener);
|
2006-09-23 14:44:39 +04:00
|
|
|
try {
|
2006-11-08 13:18:15 +03:00
|
|
|
$ret = $this->dbh->addListener(new Doctrine_Db_TestLogger());
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->pass();
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->assertTrue($ret instanceof Doctrine_Db);
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->fail();
|
|
|
|
}
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
|
|
|
|
$this->assertTrue($this->dbh->getListener()->get(0) instanceof Doctrine_Db_TestLogger);
|
|
|
|
|
2006-09-23 14:44:39 +04:00
|
|
|
try {
|
2006-11-08 13:18:15 +03:00
|
|
|
$ret = $this->dbh->addListener(new Doctrine_Db_TestValidListener());
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->pass();
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->assertTrue($ret instanceof Doctrine_Db);
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->fail();
|
|
|
|
}
|
2006-11-08 13:18:15 +03:00
|
|
|
$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);
|
2006-09-23 14:44:39 +04:00
|
|
|
|
|
|
|
try {
|
2006-11-08 13:18:15 +03:00
|
|
|
$ret = $this->dbh->addListener(new Doctrine_Db_EventListener_Chain(), 'chain');
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->pass();
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->assertTrue($ret instanceof Doctrine_Db);
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->fail();
|
|
|
|
}
|
2006-11-08 13:18:15 +03:00
|
|
|
$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);
|
2006-09-23 14:44:39 +04:00
|
|
|
|
|
|
|
// replacing
|
|
|
|
|
|
|
|
try {
|
2006-11-08 13:18:15 +03:00
|
|
|
$ret = $this->dbh->addListener(new Doctrine_Db_EventListener_Chain(), 'chain');
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->pass();
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->assertTrue($ret instanceof Doctrine_Db);
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->fail();
|
|
|
|
}
|
2006-11-08 13:18:15 +03:00
|
|
|
$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);
|
2006-09-23 14:44:39 +04:00
|
|
|
}
|
2006-11-08 13:18:15 +03:00
|
|
|
|
2006-09-23 14:44:39 +04:00
|
|
|
public function testListeningEventsWithSingleListener() {
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->setListener(new Doctrine_Db_TestLogger());
|
|
|
|
$listener = $this->dbh->getListener();
|
|
|
|
$stmt = $this->dbh->prepare('INSERT INTO entity (id) VALUES(?)');
|
2006-09-23 14:44:39 +04:00
|
|
|
|
|
|
|
$this->assertEqual($listener->pop(), 'onPrepare');
|
|
|
|
$this->assertEqual($listener->pop(), 'onPrePrepare');
|
|
|
|
|
|
|
|
$stmt->execute(array(1));
|
|
|
|
|
|
|
|
$this->assertEqual($listener->pop(), 'onExecute');
|
|
|
|
$this->assertEqual($listener->pop(), 'onPreExecute');
|
|
|
|
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->exec('DELETE FROM entity');
|
2006-09-23 14:44:39 +04:00
|
|
|
|
|
|
|
$this->assertEqual($listener->pop(), 'onExec');
|
|
|
|
$this->assertEqual($listener->pop(), 'onPreExec');
|
|
|
|
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->beginTransaction();
|
2006-09-23 14:44:39 +04:00
|
|
|
|
|
|
|
$this->assertEqual($listener->pop(), 'onBeginTransaction');
|
|
|
|
$this->assertEqual($listener->pop(), 'onPreBeginTransaction');
|
|
|
|
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->query('INSERT INTO entity (id) VALUES (1)');
|
2006-09-23 14:44:39 +04:00
|
|
|
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->commit();
|
2006-09-23 14:44:39 +04:00
|
|
|
|
|
|
|
$this->assertEqual($listener->pop(), 'onCommit');
|
|
|
|
$this->assertEqual($listener->pop(), 'onPreCommit');
|
|
|
|
|
|
|
|
$this->assertEqual($listener->pop(), 'onQuery');
|
|
|
|
$this->assertEqual($listener->pop(), 'onPreQuery');
|
|
|
|
|
2006-11-08 13:18:15 +03:00
|
|
|
|
2006-09-23 14:44:39 +04:00
|
|
|
}
|
2006-11-16 15:45:34 +03:00
|
|
|
public function testListeningQueryEventsWithListenerChain() {
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->query('DROP TABLE entity');
|
|
|
|
|
|
|
|
$this->dbh->addListener(new Doctrine_Db_TestLogger());
|
|
|
|
$this->dbh->addListener(new Doctrine_Db_TestLogger());
|
2006-09-23 14:44:39 +04:00
|
|
|
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->query('CREATE TABLE entity (id INT)');
|
2006-09-23 14:44:39 +04:00
|
|
|
|
2006-11-08 13:18:15 +03:00
|
|
|
$listener = $this->dbh->getListener()->get(0);
|
|
|
|
$listener2 = $this->dbh->getListener()->get(1);
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->assertEqual($listener->pop(), 'onQuery');
|
|
|
|
$this->assertEqual($listener->pop(), 'onPreQuery');
|
|
|
|
|
|
|
|
$this->assertEqual($listener2->pop(), 'onQuery');
|
|
|
|
$this->assertEqual($listener2->pop(), 'onPreQuery');
|
2006-11-16 15:45:34 +03:00
|
|
|
}
|
|
|
|
public function testListeningPrepareEventsWithListenerChain() {
|
2006-09-23 14:44:39 +04:00
|
|
|
|
2006-11-08 13:18:15 +03:00
|
|
|
$stmt = $this->dbh->prepare('INSERT INTO entity (id) VALUES(?)');
|
2006-11-16 15:45:34 +03:00
|
|
|
$listener = $this->dbh->getListener()->get(0);
|
|
|
|
$listener2 = $this->dbh->getListener()->get(1);
|
2006-09-23 14:44:39 +04:00
|
|
|
$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');
|
2006-11-16 15:45:34 +03:00
|
|
|
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->assertEqual($listener2->pop(), 'onExecute');
|
|
|
|
$this->assertEqual($listener2->pop(), 'onPreExecute');
|
2006-11-16 15:45:34 +03:00
|
|
|
}
|
|
|
|
public function testListeningExecEventsWithListenerChain() {
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->exec('DELETE FROM entity');
|
2006-11-16 15:45:34 +03:00
|
|
|
$listener = $this->dbh->getListener()->get(0);
|
|
|
|
$listener2 = $this->dbh->getListener()->get(1);
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->assertEqual($listener->pop(), 'onExec');
|
|
|
|
$this->assertEqual($listener->pop(), 'onPreExec');
|
|
|
|
|
|
|
|
$this->assertEqual($listener2->pop(), 'onExec');
|
|
|
|
$this->assertEqual($listener2->pop(), 'onPreExec');
|
2006-11-16 15:45:34 +03:00
|
|
|
}
|
|
|
|
public function testListeningTransactionEventsWithListenerChain() {
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->beginTransaction();
|
2006-11-16 15:45:34 +03:00
|
|
|
$listener = $this->dbh->getListener()->get(0);
|
|
|
|
$listener2 = $this->dbh->getListener()->get(1);
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->assertEqual($listener->pop(), 'onBeginTransaction');
|
|
|
|
$this->assertEqual($listener->pop(), 'onPreBeginTransaction');
|
|
|
|
|
|
|
|
$this->assertEqual($listener2->pop(), 'onBeginTransaction');
|
|
|
|
$this->assertEqual($listener2->pop(), 'onPreBeginTransaction');
|
|
|
|
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->query('INSERT INTO entity (id) VALUES (1)');
|
2006-09-23 14:44:39 +04:00
|
|
|
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->commit();
|
2006-09-23 14:44:39 +04:00
|
|
|
|
|
|
|
$this->assertEqual($listener->pop(), 'onCommit');
|
|
|
|
$this->assertEqual($listener->pop(), 'onPreCommit');
|
|
|
|
|
|
|
|
$this->assertEqual($listener->pop(), 'onQuery');
|
|
|
|
$this->assertEqual($listener->pop(), 'onPreQuery');
|
|
|
|
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->query('DROP TABLE entity');
|
2006-09-23 14:44:39 +04:00
|
|
|
}
|
|
|
|
public function testSetValidEventListener() {
|
|
|
|
try {
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->setListener(new Doctrine_Db_TestLogger());
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->pass();
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->fail();
|
|
|
|
}
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_TestLogger);
|
2006-09-23 14:44:39 +04:00
|
|
|
try {
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->setListener(new Doctrine_Db_TestValidListener());
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->pass();
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->fail();
|
|
|
|
}
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_TestValidListener);
|
2006-09-23 14:44:39 +04:00
|
|
|
try {
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->setListener(new Doctrine_Db_EventListener_Chain());
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->pass();
|
|
|
|
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->fail();
|
|
|
|
}
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener_Chain);
|
2006-09-23 14:44:39 +04:00
|
|
|
try {
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->setListener(new Doctrine_Db_EventListener());
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->pass();
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->fail();
|
|
|
|
}
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->assertTrue($this->dbh->getListener() instanceof Doctrine_Db_EventListener);
|
2006-09-23 14:44:39 +04:00
|
|
|
}
|
|
|
|
public function testSetInvalidEventListener() {
|
|
|
|
try {
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->dbh->setListener(new Doctrine_Db_TestInvalidListener());
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->fail();
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 14:44:39 +04:00
|
|
|
$this->pass();
|
|
|
|
}
|
|
|
|
}
|
2006-09-23 01:20:21 +04:00
|
|
|
public function testInvalidDSN() {
|
|
|
|
try {
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->dbh = Doctrine_Db::getConnection('');
|
2006-09-23 01:20:21 +04:00
|
|
|
$this->fail();
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 01:20:21 +04:00
|
|
|
$this->pass();
|
|
|
|
}
|
|
|
|
try {
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->dbh = Doctrine_Db::getConnection('unknown');
|
2006-09-23 01:20:21 +04:00
|
|
|
$this->fail();
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 01:20:21 +04:00
|
|
|
$this->pass();
|
|
|
|
}
|
|
|
|
try {
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->dbh = Doctrine_Db::getConnection(0);
|
2006-09-23 01:20:21 +04:00
|
|
|
$this->fail();
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 01:20:21 +04:00
|
|
|
$this->pass();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function testInvalidScheme() {
|
|
|
|
try {
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->dbh = Doctrine_Db::getConnection('unknown://:memory:');
|
2006-09-23 01:20:21 +04:00
|
|
|
$this->fail();
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 01:20:21 +04:00
|
|
|
$this->pass();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function testInvalidHost() {
|
|
|
|
try {
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->dbh = Doctrine_Db::getConnection('mysql://user:password@');
|
2006-09-23 01:20:21 +04:00
|
|
|
$this->fail();
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 01:20:21 +04:00
|
|
|
$this->pass();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function testInvalidDatabase() {
|
|
|
|
try {
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->dbh = Doctrine_Db::getConnection('mysql://user:password@host/');
|
2006-09-23 01:20:21 +04:00
|
|
|
$this->fail();
|
2006-11-06 20:56:14 +03:00
|
|
|
} catch(Doctrine_Db_Exception $e) {
|
2006-09-23 01:20:21 +04:00
|
|
|
$this->pass();
|
|
|
|
}
|
|
|
|
}
|
2006-09-23 02:29:06 +04:00
|
|
|
public function testGetConnectionPdoLikeDSN() {
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->dbh = Doctrine_Db::getConnection('mysql:host=localhost;dbname=test', 'root', 'password');
|
2006-11-08 13:18:15 +03:00
|
|
|
$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');
|
2006-09-23 02:29:06 +04:00
|
|
|
|
2006-09-23 01:20:21 +04:00
|
|
|
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->dbh = Doctrine_Db::getConnection('sqlite::memory:');
|
2006-09-23 02:29:06 +04:00
|
|
|
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->assertEqual($this->dbh->getOption('dsn'), 'sqlite::memory:');
|
|
|
|
$this->assertEqual($this->dbh->getOption('username'), false);
|
|
|
|
$this->assertEqual($this->dbh->getOption('password'), false);
|
2006-09-23 02:29:06 +04:00
|
|
|
}
|
2006-09-23 14:44:39 +04:00
|
|
|
public function testDriverName() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-09-23 02:29:06 +04:00
|
|
|
public function testGetConnectionWithPearLikeDSN() {
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->dbh = Doctrine_Db::getConnection('mysql://zYne:password@localhost/test');
|
2006-11-08 13:18:15 +03:00
|
|
|
$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');
|
2006-09-23 01:20:21 +04:00
|
|
|
|
2006-09-23 14:44:39 +04:00
|
|
|
|
2007-01-12 14:38:03 +03:00
|
|
|
$this->dbh = Doctrine_Db::getConnection('sqlite://:memory:');
|
2006-09-23 14:44:39 +04:00
|
|
|
|
2006-11-08 13:18:15 +03:00
|
|
|
$this->assertEqual($this->dbh->getOption('dsn'), 'sqlite::memory:');
|
|
|
|
$this->assertEqual($this->dbh->getOption('username'), false);
|
|
|
|
$this->assertEqual($this->dbh->getOption('password'), false);
|
2006-09-23 01:20:21 +04:00
|
|
|
}
|
2006-09-23 02:29:06 +04:00
|
|
|
|
2006-09-23 01:20:21 +04:00
|
|
|
}
|