2006-04-14 00:37:28 +04:00
|
|
|
<?php
|
2006-06-07 00:37:56 +04:00
|
|
|
require_once("UnitTestCase.php");
|
2006-09-12 14:15:58 +04:00
|
|
|
class EventListenerTest extends Doctrine_Record {
|
|
|
|
public function setTableDefinition() {
|
|
|
|
$this->hasColumn("name", "string", 100);
|
|
|
|
$this->hasColumn("password", "string", 8);
|
|
|
|
}
|
|
|
|
public function setUp() {
|
|
|
|
$this->setAttribute(Doctrine::ATTR_LISTENER, new Doctrine_EventListener_AccessorInvoker());
|
|
|
|
}
|
|
|
|
public function getName($name) {
|
|
|
|
return strtoupper($name);
|
|
|
|
}
|
|
|
|
public function setPassword($password) {
|
|
|
|
return md5($password);
|
|
|
|
}
|
|
|
|
}
|
2006-09-24 23:38:45 +04:00
|
|
|
class Doctrine_EventListener_TestLogger implements Doctrine_Overloadable, Countable {
|
|
|
|
private $messages = array();
|
2006-04-14 00:37:28 +04:00
|
|
|
|
2006-09-24 23:38:45 +04:00
|
|
|
public function __call($m, $a) {
|
|
|
|
|
|
|
|
$this->messages[] = $m;
|
|
|
|
}
|
|
|
|
public function pop() {
|
|
|
|
return array_pop($this->messages);
|
|
|
|
}
|
|
|
|
public function clear() {
|
|
|
|
$this->messages = array();
|
2006-04-14 00:37:28 +04:00
|
|
|
}
|
2006-09-24 23:38:45 +04:00
|
|
|
public function getAll() {
|
|
|
|
return $this->messages;
|
|
|
|
}
|
|
|
|
public function count() {
|
|
|
|
return count($this->messages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Doctrine_EventListenerTestCase extends Doctrine_UnitTestCase {
|
|
|
|
private $logger;
|
|
|
|
|
2006-09-12 14:15:58 +04:00
|
|
|
|
2006-09-24 23:38:45 +04:00
|
|
|
public function testSetListener() {
|
|
|
|
$this->logger = new Doctrine_EventListener_TestLogger();
|
|
|
|
|
|
|
|
$e = new EventListenerTest;
|
|
|
|
|
|
|
|
$e->getTable()->setListener($this->logger);
|
|
|
|
|
2006-10-06 20:50:00 +04:00
|
|
|
$e->name = 'listener';
|
|
|
|
$e->save();
|
|
|
|
|
2006-09-24 23:38:45 +04:00
|
|
|
$this->assertEqual($e->getTable()->getListener(), $this->logger);
|
|
|
|
}
|
|
|
|
public function testOnLoad() {
|
|
|
|
$this->logger->clear();
|
|
|
|
$this->assertEqual($this->connection->getTable('EventListenerTest')->getListener(), $this->logger);
|
|
|
|
$this->connection->clear();
|
|
|
|
|
|
|
|
$e = $this->connection->getTable('EventListenerTest')->find(1);
|
|
|
|
|
2006-10-06 20:50:00 +04:00
|
|
|
|
2006-09-24 23:38:45 +04:00
|
|
|
$this->assertEqual($e->getTable()->getListener(), $this->logger);
|
|
|
|
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onLoad');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreLoad');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOnCreate() {
|
|
|
|
$e = new EventListenerTest;
|
|
|
|
|
|
|
|
|
|
|
|
$e->setListener($this->logger);
|
|
|
|
$this->logger->clear();
|
|
|
|
$e = new EventListenerTest;
|
|
|
|
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onCreate');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreCreate');
|
|
|
|
$this->assertEqual($this->logger->count(), 0);
|
2006-09-12 14:15:58 +04:00
|
|
|
}
|
2006-09-24 23:38:45 +04:00
|
|
|
public function testOnSleepAndOnWakeUp() {
|
|
|
|
$e = new EventListenerTest;
|
|
|
|
|
|
|
|
$this->logger->clear();
|
|
|
|
|
|
|
|
$s = serialize($e);
|
|
|
|
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onSleep');
|
|
|
|
$this->assertEqual($this->logger->count(), 0);
|
|
|
|
|
|
|
|
$e = unserialize($s);
|
|
|
|
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onWakeUp');
|
|
|
|
$this->assertEqual($this->logger->count(), 0);
|
|
|
|
}
|
|
|
|
public function testTransaction() {
|
|
|
|
$e = new EventListenerTest();
|
|
|
|
$e->name = "test 1";
|
|
|
|
|
|
|
|
$this->logger->clear();
|
|
|
|
|
|
|
|
$e->save();
|
|
|
|
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onSave');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onInsert');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreInsert');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreSave');
|
|
|
|
|
|
|
|
$e->name = "test 2";
|
|
|
|
|
|
|
|
$e->save();
|
|
|
|
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onSave');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onUpdate');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreUpdate');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreSave');
|
|
|
|
|
|
|
|
$this->logger->clear();
|
|
|
|
|
|
|
|
$e->delete();
|
|
|
|
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onDelete');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreDelete');
|
|
|
|
}
|
|
|
|
public function testTransactionWithConnectionListener() {
|
|
|
|
$e = new EventListenerTest();
|
|
|
|
$e->getTable()->getConnection()->setListener($this->logger);
|
|
|
|
|
|
|
|
$e->name = "test 2";
|
|
|
|
|
|
|
|
$this->logger->clear();
|
|
|
|
|
|
|
|
$e->save();
|
2006-09-30 16:36:03 +04:00
|
|
|
|
2006-09-24 23:38:45 +04:00
|
|
|
$this->assertEqual($this->logger->pop(), 'onTransactionCommit');
|
2006-09-30 16:36:03 +04:00
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreTransactionCommit');
|
2006-09-24 23:38:45 +04:00
|
|
|
$this->assertEqual($this->logger->pop(), 'onSave');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onInsert');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreInsert');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreSave');
|
2006-09-30 16:36:03 +04:00
|
|
|
|
2006-09-24 23:38:45 +04:00
|
|
|
$this->assertEqual($this->logger->pop(), 'onTransactionBegin');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreTransactionBegin');
|
|
|
|
|
|
|
|
$e->name = "test 1";
|
|
|
|
|
|
|
|
$e->save();
|
2006-09-30 16:36:03 +04:00
|
|
|
|
2006-09-24 23:38:45 +04:00
|
|
|
$this->assertEqual($this->logger->pop(), 'onTransactionCommit');
|
2006-09-30 16:36:03 +04:00
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreTransactionCommit');
|
2006-09-24 23:38:45 +04:00
|
|
|
$this->assertEqual($this->logger->pop(), 'onSave');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onUpdate');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreUpdate');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreSave');
|
2006-09-30 16:36:03 +04:00
|
|
|
|
2006-09-24 23:38:45 +04:00
|
|
|
$this->assertEqual($this->logger->pop(), 'onTransactionBegin');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreTransactionBegin');
|
|
|
|
|
|
|
|
$this->logger->clear();
|
|
|
|
|
|
|
|
$e->delete();
|
|
|
|
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onTransactionCommit');
|
2006-09-30 16:36:03 +04:00
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreTransactionCommit');
|
2006-09-24 23:38:45 +04:00
|
|
|
$this->assertEqual($this->logger->pop(), 'onDelete');
|
2006-09-30 16:36:03 +04:00
|
|
|
|
2006-09-24 23:38:45 +04:00
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreDelete');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onTransactionBegin');
|
|
|
|
$this->assertEqual($this->logger->pop(), 'onPreTransactionBegin');
|
2006-09-24 23:54:55 +04:00
|
|
|
|
|
|
|
$this->connection->setListener(new Doctrine_EventListener());
|
2006-09-24 23:38:45 +04:00
|
|
|
}
|
|
|
|
|
2006-09-24 23:54:55 +04:00
|
|
|
|
|
|
|
|
2006-06-03 13:10:43 +04:00
|
|
|
public function prepareData() { }
|
2006-09-12 14:15:58 +04:00
|
|
|
public function prepareTables() {
|
|
|
|
$this->tables = array('EventListenerTest');
|
|
|
|
parent::prepareTables();
|
|
|
|
}
|
2006-04-14 00:37:28 +04:00
|
|
|
}
|
|
|
|
?>
|