PluginHooksServiceTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Elgg;
  3. class PluginHooksServiceTest extends \PHPUnit_Framework_TestCase {
  4. public function testTriggerCallsRegisteredHandlers() {
  5. $hooks = new \Elgg\PluginHooksService();
  6. $this->setExpectedException('InvalidArgumentException');
  7. $hooks->registerHandler('foo', 'bar', array('\Elgg\PluginHooksServiceTest', 'throwInvalidArg'));
  8. $hooks->trigger('foo', 'bar');
  9. }
  10. public function testCanPassParamsAndChangeReturnValue() {
  11. $hooks = new \Elgg\PluginHooksService();
  12. $hooks->registerHandler('foo', 'bar', array('\Elgg\PluginHooksServiceTest', 'changeReturn'));
  13. $returnval = $hooks->trigger('foo', 'bar', array(
  14. 'testCase' => $this,
  15. ), 1);
  16. $this->assertEquals(2, $returnval);
  17. }
  18. public function testNullReturnDoesntChangeValue() {
  19. $hooks = new \Elgg\PluginHooksService();
  20. $hooks->registerHandler('foo', 'bar', 'Elgg\Values::getNull');
  21. $returnval = $hooks->trigger('foo', 'bar', array(), 1);
  22. $this->assertEquals(1, $returnval);
  23. }
  24. public function testUncallableHandlersAreLogged() {
  25. $hooks = new \Elgg\PluginHooksService();
  26. $loggerMock = $this->getMock('\Elgg\Logger', array(), array(), '', false);
  27. $hooks->setLogger($loggerMock);
  28. $hooks->registerHandler('foo', 'bar', array(new \stdClass(), 'uncallableMethod'));
  29. $expectedMsg = 'handler for plugin hook [foo, bar] is not callable: (stdClass)->uncallableMethod';
  30. $loggerMock->expects($this->once())->method('warn')->with($expectedMsg);
  31. $hooks->trigger('foo', 'bar');
  32. }
  33. public static function changeReturn($foo, $bar, $returnval, $params) {
  34. $testCase = $params['testCase'];
  35. $testCase->assertEquals(1, $returnval);
  36. return 2;
  37. }
  38. public static function throwInvalidArg() {
  39. throw new \InvalidArgumentException();
  40. }
  41. }