ForwardingEmitterTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Hooks;
  9. use OC\Hooks\PublicEmitter;
  10. class DummyForwardingEmitter extends \OC\Hooks\ForwardingEmitter {
  11. public function emitEvent($scope, $method, $arguments = array()) {
  12. $this->emit($scope, $method, $arguments);
  13. }
  14. /**
  15. * @param \OC\Hooks\Emitter $emitter
  16. */
  17. public function forward(\OC\Hooks\Emitter $emitter) {
  18. parent::forward($emitter);
  19. }
  20. }
  21. /**
  22. * Class ForwardingEmitter
  23. *
  24. * allows forwarding all listen calls to other emitters
  25. *
  26. * @package OC\Hooks
  27. */
  28. class ForwardingEmitterTest extends BasicEmitterTest {
  29. public function testSingleForward() {
  30. $baseEmitter = new PublicEmitter();
  31. $forwardingEmitter = new DummyForwardingEmitter();
  32. $forwardingEmitter->forward($baseEmitter);
  33. $hookCalled = false;
  34. $forwardingEmitter->listen('Test', 'test', function () use (&$hookCalled) {
  35. $hookCalled = true;
  36. });
  37. $baseEmitter->emit('Test', 'test');
  38. $this->assertTrue($hookCalled);
  39. }
  40. public function testMultipleForwards() {
  41. $baseEmitter1 = new PublicEmitter();
  42. $baseEmitter2 = new PublicEmitter();
  43. $forwardingEmitter = new DummyForwardingEmitter();
  44. $forwardingEmitter->forward($baseEmitter1);
  45. $forwardingEmitter->forward($baseEmitter2);
  46. $hookCalled = 0;
  47. $forwardingEmitter->listen('Test', 'test1', function () use (&$hookCalled) {
  48. $hookCalled++;
  49. });
  50. $forwardingEmitter->listen('Test', 'test2', function () use (&$hookCalled) {
  51. $hookCalled++;
  52. });
  53. $baseEmitter1->emit('Test', 'test1');
  54. $baseEmitter1->emit('Test', 'test2');
  55. $this->assertEquals(2, $hookCalled);
  56. }
  57. public function testForwardExistingHooks() {
  58. $baseEmitter = new PublicEmitter();
  59. $forwardingEmitter = new DummyForwardingEmitter();
  60. $hookCalled = false;
  61. $forwardingEmitter->listen('Test', 'test', function () use (&$hookCalled) {
  62. $hookCalled = true;
  63. });
  64. $forwardingEmitter->forward($baseEmitter);
  65. $baseEmitter->emit('Test', 'test');
  66. $this->assertTrue($hookCalled);
  67. }
  68. }