1
0

SymfonyAdapterTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace lib\EventDispatcher;
  24. use OC\EventDispatcher\EventDispatcher;
  25. use OC\EventDispatcher\GenericEventWrapper;
  26. use OC\EventDispatcher\SymfonyAdapter;
  27. use OCP\EventDispatcher\Event;
  28. use OCP\EventDispatcher\GenericEvent;
  29. use PHPUnit\Framework\MockObject\MockObject;
  30. use Psr\Log\LoggerInterface;
  31. use Symfony\Component\EventDispatcher\Event as SymfonyEvent;
  32. use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher;
  33. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  34. use Symfony\Component\EventDispatcher\GenericEvent as SymfonyGenericEvent;
  35. use Test\TestCase;
  36. class SymfonyAdapterTest extends TestCase {
  37. /** @var EventDispatcher|MockObject */
  38. private $eventDispatcher;
  39. /** @var LoggerInterface|MockObject */
  40. private $logger;
  41. /** @var EventDispatcherInterface */
  42. private $adapter;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->eventDispatcher = $this->createMock(EventDispatcher::class);
  46. $this->logger = $this->createMock(LoggerInterface::class);
  47. $this->adapter = new SymfonyAdapter(
  48. $this->eventDispatcher,
  49. $this->logger
  50. );
  51. }
  52. public function testDispatchTypedEvent(): void {
  53. $event = new Event();
  54. $eventName = 'symfony';
  55. $this->eventDispatcher->expects(self::once())
  56. ->method('dispatch')
  57. ->with(
  58. $eventName,
  59. $event
  60. )
  61. ->willReturnArgument(0);
  62. $this->adapter->dispatch($eventName, $event);
  63. }
  64. public function testDispatchSymfonyGenericEvent(): void {
  65. $eventName = 'symfony';
  66. $event = new SymfonyGenericEvent();
  67. $wrapped = new GenericEventWrapper(
  68. $this->logger,
  69. $eventName,
  70. $event
  71. );
  72. $symfonyDispatcher = $this->createMock(SymfonyDispatcher::class);
  73. $this->eventDispatcher->expects(self::once())
  74. ->method('getSymfonyDispatcher')
  75. ->willReturn($symfonyDispatcher);
  76. $symfonyDispatcher->expects(self::once())
  77. ->method('dispatch')
  78. ->with(
  79. self::equalTo($wrapped),
  80. $eventName
  81. )
  82. ->willReturnArgument(0);
  83. $result = $this->adapter->dispatch($eventName, $event);
  84. self::assertEquals($result, $wrapped);
  85. }
  86. public function testDispatchOldSymfonyEventWithFlippedArgumentOrder(): void {
  87. $event = new SymfonyEvent();
  88. $eventName = 'symfony';
  89. $symfonyDispatcher = $this->createMock(SymfonyDispatcher::class);
  90. $this->eventDispatcher->expects(self::once())
  91. ->method('getSymfonyDispatcher')
  92. ->willReturn($symfonyDispatcher);
  93. $symfonyDispatcher->expects(self::once())
  94. ->method('dispatch')
  95. ->with(
  96. $event,
  97. $eventName
  98. )
  99. ->willReturnArgument(0);
  100. $result = $this->adapter->dispatch($event, $eventName);
  101. self::assertSame($result, $event);
  102. }
  103. public function testDispatchOldSymfonyEvent(): void {
  104. $event = new SymfonyEvent();
  105. $eventName = 'symfony';
  106. $symfonyDispatcher = $this->createMock(SymfonyDispatcher::class);
  107. $this->eventDispatcher->expects(self::once())
  108. ->method('getSymfonyDispatcher')
  109. ->willReturn($symfonyDispatcher);
  110. $symfonyDispatcher->expects(self::once())
  111. ->method('dispatch')
  112. ->with(
  113. $event,
  114. $eventName
  115. )
  116. ->willReturnArgument(0);
  117. $result = $this->adapter->dispatch($eventName, $event);
  118. self::assertSame($result, $event);
  119. }
  120. public function testDispatchCustomGenericEventWithFlippedArgumentOrder(): void {
  121. $event = new GenericEvent();
  122. $eventName = 'symfony';
  123. $this->eventDispatcher->expects(self::once())
  124. ->method('dispatch')
  125. ->with(
  126. $eventName,
  127. $event
  128. )
  129. ->willReturnArgument(0);
  130. $result = $this->adapter->dispatch($event, $eventName);
  131. self::assertSame($result, $event);
  132. }
  133. public function testDispatchCustomGenericEvent(): void {
  134. $event = new GenericEvent();
  135. $eventName = 'symfony';
  136. $this->eventDispatcher->expects(self::once())
  137. ->method('dispatch')
  138. ->with(
  139. $eventName,
  140. $event
  141. );
  142. $result = $this->adapter->dispatch($eventName, $event);
  143. self::assertSame($result, $event);
  144. }
  145. public function testDispatchEventWithoutPayload(): void {
  146. $eventName = 'symfony';
  147. $symfonyDispatcher = $this->createMock(SymfonyDispatcher::class);
  148. $this->eventDispatcher->expects(self::once())
  149. ->method('getSymfonyDispatcher')
  150. ->willReturn($symfonyDispatcher);
  151. $symfonyDispatcher->expects(self::once())
  152. ->method('dispatch')
  153. ->with(
  154. $eventName
  155. )
  156. ->willReturnArgument(0);
  157. $result = $this->adapter->dispatch($eventName);
  158. self::assertNotNull($result);
  159. }
  160. }