HooksRegistrationServiceTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Elgg;
  3. class HooksRegistrationServiceTest extends \PHPUnit_Framework_TestCase {
  4. /**
  5. * @var HooksRegistrationService
  6. */
  7. public $mock;
  8. public function setUp() {
  9. parent::setUp();
  10. $this->mock = $this->getMockForAbstractClass('\Elgg\HooksRegistrationService');
  11. }
  12. public function testCanRegisterHandlers() {
  13. $f = function () {};
  14. $this->assertTrue($this->mock->registerHandler('foo', 'bar', 'callback1'));
  15. $this->assertTrue($this->mock->registerHandler('foo', 'bar', $f));
  16. $this->assertTrue($this->mock->registerHandler('foo', 'baz', 'callback3', 100));
  17. $expected = array(
  18. 'foo' => array(
  19. 'bar' => array(
  20. 500 => 'callback1',
  21. 501 => $f,
  22. ),
  23. 'baz' => array(
  24. 100 => 'callback3'
  25. )
  26. )
  27. );
  28. $this->assertSame($expected, $this->mock->getAllHandlers());
  29. // check possibly invalid callbacks
  30. $this->assertFalse($this->mock->registerHandler('foo', 'bar', 1234));
  31. }
  32. public function testCanUnregisterHandlers() {
  33. $o = new HooksRegistrationServiceTest_invokable();
  34. $this->mock->registerHandler('foo', 'bar', 'callback1');
  35. $this->mock->registerHandler('foo', 'bar', 'callback2', 100);
  36. $this->mock->registerHandler('foo', 'bar', 'callback2', 150);
  37. $this->mock->registerHandler('foo', 'bar', [$o, '__invoke'], 300);
  38. $this->mock->registerHandler('foo', 'bar', [$o, '__invoke'], 300);
  39. $this->mock->registerHandler('foo', 'bar', [$o, '__invoke'], 300);
  40. $this->assertTrue($this->mock->unregisterHandler(
  41. 'foo', 'bar', 'callback2'));
  42. $this->assertTrue($this->mock->unregisterHandler(
  43. 'foo', 'bar', HooksRegistrationServiceTest_invokable::KLASS . '::__invoke'));
  44. $this->assertTrue($this->mock->unregisterHandler(
  45. 'foo', 'bar', [HooksRegistrationServiceTest_invokable::KLASS, '__invoke']));
  46. $this->assertTrue($this->mock->unregisterHandler(
  47. 'foo', 'bar', [$o, '__invoke']));
  48. $expected = array(
  49. 'foo' => array(
  50. 'bar' => array(
  51. // only one removed
  52. 150 => 'callback2',
  53. 500 => 'callback1',
  54. )
  55. )
  56. );
  57. $this->assertSame($expected, $this->mock->getAllHandlers());
  58. // check unregistering things that aren't registered
  59. $this->assertFalse($this->mock->unregisterHandler('foo', 'bar', 'not_valid'));
  60. }
  61. public function testOnlyOneHandlerUnregistered() {
  62. $this->mock->registerHandler('foo', 'bar', 'callback');
  63. $this->mock->registerHandler('foo', 'bar', 'callback');
  64. $this->assertTrue($this->mock->unregisterHandler('foo', 'bar', 'callback'));
  65. $this->assertSame([501 => 'callback'], $this->mock->getAllHandlers()['foo']['bar']);
  66. }
  67. public function testGetOrderedHandlers() {
  68. $this->mock->registerHandler('foo', 'bar', 'callback1');
  69. $this->mock->registerHandler('foo', 'bar', 'callback2');
  70. $this->mock->registerHandler('foo', 'baz', 'callback3', 100);
  71. $expected_foo_bar = array(
  72. 'callback1',
  73. 'callback2'
  74. );
  75. $expected_foo_baz = array(
  76. 'callback3'
  77. );
  78. $this->assertSame($expected_foo_bar, $this->mock->getOrderedHandlers('foo', 'bar'));
  79. $this->assertSame($expected_foo_baz, $this->mock->getOrderedHandlers('foo', 'baz'));
  80. }
  81. }
  82. class HooksRegistrationServiceTest_invokable {
  83. const KLASS = __CLASS__;
  84. function __invoke() {}
  85. }