|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the CleverAge/ProcessBundle package. |
| 7 | + * |
| 8 | + * Copyright (c) Clever-Age |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CleverAge\ProcessBundle\Tests\Task\Event; |
| 15 | + |
| 16 | +use CleverAge\ProcessBundle\Configuration\ProcessConfiguration; |
| 17 | +use CleverAge\ProcessBundle\Configuration\TaskConfiguration; |
| 18 | +use CleverAge\ProcessBundle\Context\ContextualOptionResolver; |
| 19 | +use CleverAge\ProcessBundle\Event\EventDispatcherTaskEvent; |
| 20 | +use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; |
| 21 | +use CleverAge\ProcessBundle\Model\ProcessHistory; |
| 22 | +use CleverAge\ProcessBundle\Model\ProcessState; |
| 23 | +use CleverAge\ProcessBundle\Task\Event\EventDispatcherTask; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 26 | +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
| 27 | + |
| 28 | +#[\PHPUnit\Framework\Attributes\CoversClass(EventDispatcherTask::class)] |
| 29 | +#[\PHPUnit\Framework\Attributes\UsesClass(EventDispatcherTaskEvent::class)] |
| 30 | +#[\PHPUnit\Framework\Attributes\UsesClass(AbstractConfigurableTask::class)] |
| 31 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessConfiguration::class)] |
| 32 | +#[\PHPUnit\Framework\Attributes\UsesClass(TaskConfiguration::class)] |
| 33 | +#[\PHPUnit\Framework\Attributes\UsesClass(ContextualOptionResolver::class)] |
| 34 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessHistory::class)] |
| 35 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessState::class)] |
| 36 | +class EventDispatcherTaskTest extends TestCase |
| 37 | +{ |
| 38 | + /** @var list<string> */ |
| 39 | + private array $calledListeners = []; |
| 40 | + |
| 41 | + /** @var list<string> */ |
| 42 | + private array $deprecations = []; |
| 43 | + |
| 44 | + public function testEventIsDispatchedUnderEventName(): void |
| 45 | + { |
| 46 | + $dispatcher = $this->createDispatcher(['myapp.myevent']); |
| 47 | + |
| 48 | + $this->execute($dispatcher, ['event_name' => 'myapp.myevent']); |
| 49 | + |
| 50 | + self::assertSame(['myapp.myevent'], $this->calledListeners); |
| 51 | + self::assertSame([], $this->deprecations); |
| 52 | + } |
| 53 | + |
| 54 | + public function testEventIsDispatchedUnderClassNameWithoutEventName(): void |
| 55 | + { |
| 56 | + $dispatcher = $this->createDispatcher(['myapp.myevent', EventDispatcherTaskEvent::class]); |
| 57 | + |
| 58 | + $this->execute($dispatcher, []); |
| 59 | + |
| 60 | + self::assertSame([EventDispatcherTaskEvent::class], $this->calledListeners); |
| 61 | + self::assertSame([], $this->deprecations); |
| 62 | + } |
| 63 | + |
| 64 | + public function testEventIsDispatchedOnceWhenEventNameIsTheClassName(): void |
| 65 | + { |
| 66 | + $dispatcher = $this->createDispatcher([EventDispatcherTaskEvent::class]); |
| 67 | + |
| 68 | + $this->execute($dispatcher, ['event_name' => EventDispatcherTaskEvent::class]); |
| 69 | + |
| 70 | + self::assertSame([EventDispatcherTaskEvent::class], $this->calledListeners); |
| 71 | + self::assertSame([], $this->deprecations); |
| 72 | + } |
| 73 | + |
| 74 | + public function testClassNameListenersAreStillCalledWithDeprecation(): void |
| 75 | + { |
| 76 | + $dispatcher = $this->createDispatcher(['myapp.myevent', EventDispatcherTaskEvent::class]); |
| 77 | + |
| 78 | + $this->execute($dispatcher, ['event_name' => 'myapp.myevent']); |
| 79 | + |
| 80 | + self::assertSame(['myapp.myevent', EventDispatcherTaskEvent::class], $this->calledListeners); |
| 81 | + self::assertCount(1, $this->deprecations); |
| 82 | + self::assertStringContainsString('listen to "myapp.myevent" instead', $this->deprecations[0]); |
| 83 | + } |
| 84 | + |
| 85 | + public function testPassiveTaskOutputsInput(): void |
| 86 | + { |
| 87 | + $state = $this->execute($this->createDispatcher([]), [], 'input'); |
| 88 | + |
| 89 | + self::assertSame('input', $state->getOutput()); |
| 90 | + } |
| 91 | + |
| 92 | + public function testInvalidEventNameThrows(): void |
| 93 | + { |
| 94 | + $this->expectException(InvalidOptionsException::class); |
| 95 | + $this->execute($this->createDispatcher([]), ['event_name' => 123]); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param list<string> $eventNames |
| 100 | + */ |
| 101 | + private function createDispatcher(array $eventNames): EventDispatcher |
| 102 | + { |
| 103 | + $dispatcher = new EventDispatcher(); |
| 104 | + foreach ($eventNames as $eventName) { |
| 105 | + $dispatcher->addListener($eventName, function (EventDispatcherTaskEvent $event) use ($eventName): void { |
| 106 | + $this->calledListeners[] = $eventName; |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + return $dispatcher; |
| 111 | + } |
| 112 | + |
| 113 | + private function execute(EventDispatcher $dispatcher, array $options, mixed $input = null): ProcessState |
| 114 | + { |
| 115 | + $processConfiguration = new ProcessConfiguration('test', []); |
| 116 | + $state = new ProcessState($processConfiguration, new ProcessHistory($processConfiguration)); |
| 117 | + $state->setContextualOptionResolver(new ContextualOptionResolver()); |
| 118 | + $state->setContext([]); |
| 119 | + $state->setTaskConfiguration(new TaskConfiguration('dispatch', EventDispatcherTask::class, $options)); |
| 120 | + $state->setInput($input); |
| 121 | + |
| 122 | + set_error_handler(function (int $errno, string $errstr): bool { |
| 123 | + $this->deprecations[] = $errstr; |
| 124 | + |
| 125 | + return true; |
| 126 | + }, \E_USER_DEPRECATED); |
| 127 | + try { |
| 128 | + $task = new EventDispatcherTask($dispatcher); |
| 129 | + $task->initialize($state); |
| 130 | + $task->execute($state); |
| 131 | + } finally { |
| 132 | + restore_error_handler(); |
| 133 | + } |
| 134 | + |
| 135 | + return $state; |
| 136 | + } |
| 137 | +} |
0 commit comments