* @license PHP License * @package WB * @subpackage unittest */ WBClass::load('WBObserver'); /** * Unit Test Event Processor * * Test Event system * * @version 0.1.1 * @package WB * @subpackage unittest */ class TestCaseEventProcessor extends UnitTestCase implements WBObserver { /** * table * @var WBDatasource_Table */ protected $table; /** * Event processor * @var WBEvent_Processor */ protected $proc; /** * update counter * @var int */ protected $updates = 0; /** * constructor */ public function __construct() { WBClass::load('WBUnitTest_SQLUte' , 'WBEvent_Handler_UnitTest'); WBUnitTest_SQLUte::source(__FILE__, 'construct'); $this->table = WBClass::create('WBDatasource_Table'); $this->proc = WBClass::create('WBEvent_Processor'); } /** * destructor */ public function __destruct() { // WBUnitTest_SQLUte::source( __FILE__, 'destruct' ); } /** * check classes */ public function testClasses() { WBClass::load('WBEvent_Processor'); $this->assertTrue(class_exists( 'WBEvent_Processor', false)); $this->assertTrue(class_exists( 'WBEvent_Queue', false)); $this->assertIsA($this->proc, 'WBEvent_Processor'); } public function testProcAllSingle() { $this->addEvents(1); $actual = $this->proc->executeAll(); $this->verifyExecuteAll($actual, 1); $actual = $this->proc->executeAll(); $this->verifyExecuteAll($actual, 0); } public function testProcAllMulti() { $this->addEvents(5); $actual = $this->proc->executeAll(); $this->verifyExecuteAll($actual, 5); $actual = $this->proc->executeAll(); $this->verifyExecuteAll($actual, 0); } public function testProcAllSingleObserver() { // attache observer $this->updates = 0; $this->proc->attach($this); $this->addEvents(1); $actual = $this->proc->executeAll(); $this->verifyExecuteAll($actual, 1); $actual = $this->proc->executeAll(); $this->verifyExecuteAll($actual, 0); $this->assertEqual($this->updates, 1); // remove observer $this->proc->detach($this); $this->addEvents(1); $actual = $this->proc->executeAll(); $this->verifyExecuteAll($actual, 1); // verify observer updates $this->assertEqual($this->updates, 1); $this->updates = 0; } public function testProcAllMultiObserver() { $this->updates = 0; $this->proc->attach($this); $this->addEvents(5); $actual = $this->proc->executeAll(); $this->verifyExecuteAll($actual, 5); $actual = $this->proc->executeAll(); $this->verifyExecuteAll($actual, 0); $this->assertEqual($this->updates, 5); $this->proc->detach($this); $this->addEvents(5); $actual = $this->proc->executeAll(); $this->verifyExecuteAll($actual, 5); // verify observer updates $this->assertEqual($this->updates, 5); $this->updates = 0; } protected function addEvents($count = 1) { // mock event data $e = array( 'action' => 'unittest', 'name' => 'unittest:processor:all', 'message' => 'foo bar', 'data' => serialize(array()), 'created' => '1980-01-01', ); for ($i = 0; $i <$count; ++$i) { $this->table->save('eventqueue', '__new', $e); } } protected function verifyExecuteAll($actual, $count = 1) { $this->assertEqual($actual, $count); $list = WBEvent_Handler_UnitTest::getProclist(); WBEvent_Handler_UnitTest::flushList(); $this->assertEqual($count, count($list)); foreach ($list as $name) { $this->assertEqual('unittest:processor:all', $name); } } public function update($obj) { ++$this->updates; $this->assertIsA($obj, 'WBEvent_Processor'); $this->assertEqual($this->updates, $obj->getPosition()); } public function getObjectId() { return spl_object_hash($this); } } ?>