EventsServiceTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Elgg;
  3. class EventsServiceTest extends \PHPUnit_Framework_TestCase {
  4. public $counter = 0;
  5. public function setUp() {
  6. $this->counter = 0;
  7. }
  8. public function testTriggerCallsRegisteredHandlersAndReturnsTrue() {
  9. $events = new \Elgg\EventsService();
  10. $events->registerHandler('foo', 'bar', array($this, 'incrementCounter'));
  11. $events->registerHandler('foo', 'bar', array($this, 'incrementCounter'));
  12. $this->assertTrue($events->trigger('foo', 'bar'));
  13. $this->assertEquals($this->counter, 2);
  14. }
  15. public function testFalseStopsPropagationAndReturnsFalse() {
  16. $events = new \Elgg\EventsService();
  17. $events->registerHandler('foo', 'bar', 'Elgg\Values::getFalse');
  18. $events->registerHandler('foo', 'bar', array($this, 'incrementCounter'));
  19. $this->assertFalse($events->trigger('foo', 'bar'));
  20. $this->assertEquals($this->counter, 0);
  21. }
  22. public function testNullDoesNotStopPropagation() {
  23. $events = new \Elgg\EventsService();
  24. $events->registerHandler('foo', 'bar', 'Elgg\Values::getNull');
  25. $events->registerHandler('foo', 'bar', array($this, 'incrementCounter'));
  26. $this->assertTrue($events->trigger('foo', 'bar'));
  27. $this->assertEquals($this->counter, 1);
  28. }
  29. public function testUnstoppableEventsCantBeStoppedAndReturnTrue() {
  30. $events = new \Elgg\EventsService();
  31. $events->registerHandler('foo', 'bar', 'Elgg\Values::getFalse');
  32. $events->registerHandler('foo', 'bar', array($this, 'incrementCounter'));
  33. $this->assertTrue($events->trigger('foo', 'bar', null, array(
  34. \Elgg\EventsService::OPTION_STOPPABLE => false
  35. )));
  36. $this->assertEquals($this->counter, 1);
  37. }
  38. public function testUncallableHandlersAreLogged() {
  39. $events = new \Elgg\EventsService();
  40. $loggerMock = $this->getMock('\Elgg\Logger', array(), array(), '', false);
  41. $events->setLogger($loggerMock);
  42. $events->registerHandler('foo', 'bar', array(new \stdClass(), 'uncallableMethod'));
  43. $expectedMsg = 'handler for event [foo, bar] is not callable: (stdClass)->uncallableMethod';
  44. $loggerMock->expects($this->once())->method('warn')->with($expectedMsg);
  45. $events->trigger('foo', 'bar');
  46. }
  47. public function incrementCounter() {
  48. $this->counter++;
  49. return true;
  50. }
  51. }