diff --git a/src/Testing/Laravel/Contracts/Events/DispatcherAssert.php b/src/Testing/Laravel/Contracts/Events/DispatcherAssert.php new file mode 100644 index 00000000..7a43f2f8 --- /dev/null +++ b/src/Testing/Laravel/Contracts/Events/DispatcherAssert.php @@ -0,0 +1,177 @@ + $listen + * @param array $hasListeners + * @param array $subscribe + * @param array $until + * @param array $dispatch + * @param array $push + * @param array $flush + * @param array $forget + * @param array $forgetPushed + */ + public function __construct( + array $listen = [], + array $hasListeners = [], + array $subscribe = [], + array $until = [], + array $dispatch = [], + array $push = [], + array $flush = [], + array $forget = [], + array $forgetPushed = [], + ) { + $this->setExpectations(DispatcherListenExpectation::class, array_values(array_filter($listen))); + $this->setExpectations(DispatcherHasListenersExpectation::class, array_values(array_filter($hasListeners))); + $this->setExpectations(DispatcherSubscribeExpectation::class, array_values(array_filter($subscribe))); + $this->setExpectations(DispatcherUntilExpectation::class, array_values(array_filter($until))); + $this->setExpectations(DispatcherDispatchExpectation::class, array_values(array_filter($dispatch))); + $this->setExpectations(DispatcherPushExpectation::class, array_values(array_filter($push))); + $this->setExpectations(DispatcherFlushExpectation::class, array_values(array_filter($flush))); + $this->setExpectations(DispatcherForgetExpectation::class, array_values(array_filter($forget))); + $this->setExpectations(DispatcherForgetPushedExpectation::class, array_values(array_filter($forgetPushed))); + } + + /** + * Register an event listener with the dispatcher. + * + * @param Closure|string|array $events + * @param Closure|string|array|null $listener + */ + public function listen($events, $listener = null) + { + $expectation = $this->getExpectation(DispatcherListenExpectation::class); + $message = $this->getDebugMessage(); + + Assert::assertEquals($expectation->events, $events, $message); + Assert::assertEquals($expectation->listener, $listener, $message); + } + + /** + * Determine if a given event has listeners. + * + * @param string $eventName + * @return bool + */ + public function hasListeners($eventName) + { + $expectation = $this->getExpectation(DispatcherHasListenersExpectation::class); + $message = $this->getDebugMessage(); + + Assert::assertEquals($expectation->eventName, $eventName, $message); + + return $expectation->return; + } + + /** + * Register an event subscriber with the dispatcher. + * + * @param object|string $subscriber + */ + public function subscribe($subscriber) + { + $expectation = $this->getExpectation(DispatcherSubscribeExpectation::class); + $message = $this->getDebugMessage(); + + Assert::assertEquals($expectation->subscriber, $subscriber, $message); + } + + /** + * Dispatch an event until the first non-null response is returned. + * + * @param string|object $event + * @param mixed $payload + * @return array|null + */ + public function until($event, $payload = []) + { + $expectation = $this->getExpectation(DispatcherUntilExpectation::class); + $message = $this->getDebugMessage(); + + Assert::assertEquals($expectation->event, $event, $message); + Assert::assertEquals($expectation->payload, $payload, $message); + + return $expectation->return; + } + + /** + * Dispatch an event and call the listeners. + * + * @param string|object $event + * @param mixed $payload + * @param bool $halt + * @return array|null + */ + public function dispatch($event, $payload = [], $halt = false) + { + $expectation = $this->getExpectation(DispatcherDispatchExpectation::class); + $message = $this->getDebugMessage(); + + Assert::assertEquals($expectation->event, $event, $message); + Assert::assertEquals($expectation->payload, $payload, $message); + Assert::assertEquals($expectation->halt, $halt, $message); + + return $expectation->return; + } + + /** + * Register an event and payload to be fired later. + * + * @param string $event + * @param array $payload + */ + public function push($event, $payload = []) + { + $expectation = $this->getExpectation(DispatcherPushExpectation::class); + $message = $this->getDebugMessage(); + + Assert::assertEquals($expectation->event, $event, $message); + Assert::assertEquals($expectation->payload, $payload, $message); + } + + /** + * Flush a set of pushed events. + * + * @param string $event + */ + public function flush($event) + { + $expectation = $this->getExpectation(DispatcherFlushExpectation::class); + $message = $this->getDebugMessage(); + + Assert::assertEquals($expectation->event, $event, $message); + } + + /** + * Remove a set of listeners from the dispatcher. + * + * @param string $event + */ + public function forget($event) + { + $expectation = $this->getExpectation(DispatcherForgetExpectation::class); + $message = $this->getDebugMessage(); + + Assert::assertEquals($expectation->event, $event, $message); + } + + /** + * Forget all of the queued listeners. + */ + public function forgetPushed() + { + $expectation = $this->getExpectation(DispatcherForgetPushedExpectation::class); + } +} diff --git a/src/Testing/Laravel/Contracts/Events/DispatcherDispatchExpectation.php b/src/Testing/Laravel/Contracts/Events/DispatcherDispatchExpectation.php new file mode 100644 index 00000000..1f42544a --- /dev/null +++ b/src/Testing/Laravel/Contracts/Events/DispatcherDispatchExpectation.php @@ -0,0 +1,16 @@ + new DispatcherAssert( + listen: [new DispatcherListenExpectation(events: [TestEvent::class], listener: TestListener::class)] + ), + call: static fn (DispatcherAssert $assert) => $assert->listen( + events: [TestEvent::class], + listener: TestListener::class + ), + ), + new AssertExpectationEntity( + methodName: 'listen', + createAssert: static fn () => new DispatcherAssert( + listen: [new DispatcherListenExpectation(events: [], listener: null)] + ), + call: static fn (DispatcherAssert $assert) => $assert->listen(events: []), + ), + new AssertExpectationEntity( + methodName: 'listen', + createAssert: static fn () => new DispatcherAssert( + listen: [new DispatcherListenExpectation(events: [TestEvent::class], listener: null)] + ), + call: static fn (DispatcherAssert $assert) => $assert->listen(events: [TestEvent::class]), + ), + new AssertExpectationEntity( + methodName: 'hasListeners', + createAssert: static fn () => new DispatcherAssert( + hasListeners: [new DispatcherHasListenersExpectation(return: true, eventName: 'test')] + ), + call: static fn (DispatcherAssert $assert) => $assert->hasListeners(eventName: 'test'), + checkResult: true, + expectedResult: true + ), + new AssertExpectationEntity( + methodName: 'hasListeners', + createAssert: static fn () => new DispatcherAssert( + hasListeners: [new DispatcherHasListenersExpectation(return: false, eventName: TestEvent::class)] + ), + call: static fn (DispatcherAssert $assert) => $assert->hasListeners(eventName: TestEvent::class), + checkResult: true, + expectedResult: false + ), + new AssertExpectationEntity( + methodName: 'subscribe', + createAssert: static fn () => new DispatcherAssert( + subscribe: [new DispatcherSubscribeExpectation(subscriber: TestListener::class)] + ), + call: static fn (DispatcherAssert $assert) => $assert->subscribe(subscriber: TestListener::class), + ), + new AssertExpectationEntity( + methodName: 'until', + createAssert: static fn () => new DispatcherAssert( + until: [new DispatcherUntilExpectation(return: null, event: TestEvent::class, payload: ['test'])] + ), + call: static fn (DispatcherAssert $assert) => $assert->until( + event: TestEvent::class, + payload: ['test'] + ), + checkResult: true, + expectedResult: null + ), + new AssertExpectationEntity( + methodName: 'until', + createAssert: static fn () => new DispatcherAssert( + until: [new DispatcherUntilExpectation(return: [], event: TestEvent::class)] + ), + call: static fn (DispatcherAssert $assert) => $assert->until(event: TestEvent::class), + checkResult: true, + expectedResult: [] + ), + new AssertExpectationEntity( + methodName: 'dispatch', + createAssert: static fn () => new DispatcherAssert( + dispatch: [ + new DispatcherDispatchExpectation(return: [], event: TestEvent::class, payload: [ + 'test', + ], halt: true), + ] + ), + call: static fn (DispatcherAssert $assert) => $assert->dispatch( + event: TestEvent::class, + payload: ['test'], + halt: true + ), + checkResult: true, + expectedResult: [] + ), + new AssertExpectationEntity( + methodName: 'dispatch', + createAssert: static fn () => new DispatcherAssert( + dispatch: [ + new DispatcherDispatchExpectation(return: null, event: TestEvent::class, payload: ['test']), + ] + ), + call: static fn (DispatcherAssert $assert) => $assert->dispatch( + event: TestEvent::class, + payload: ['test'] + ), + checkResult: true, + expectedResult: null + ), + new AssertExpectationEntity( + methodName: 'push', + createAssert: static fn () => new DispatcherAssert( + push: [new DispatcherPushExpectation(event: TestEvent::class, payload: ['test'])] + ), + call: static fn (DispatcherAssert $assert) => $assert->push(event: TestEvent::class, payload: ['test']), + ), + new AssertExpectationEntity( + methodName: 'push', + createAssert: static fn () => new DispatcherAssert( + push: [new DispatcherPushExpectation(event: TestEvent::class)] + ), + call: static fn (DispatcherAssert $assert) => $assert->push(event: TestEvent::class), + ), + new AssertExpectationEntity( + methodName: 'flush', + createAssert: static fn () => new DispatcherAssert( + flush: [new DispatcherFlushExpectation(event: TestEvent::class)] + ), + call: static fn (DispatcherAssert $assert) => $assert->flush(event: TestEvent::class), + ), + new AssertExpectationEntity( + methodName: 'forget', + createAssert: static fn () => new DispatcherAssert( + forget: [new DispatcherForgetExpectation(event: TestEvent::class)] + ), + call: static fn (DispatcherAssert $assert) => $assert->forget(event: TestEvent::class), + ), + new AssertExpectationEntity( + methodName: 'forgetPushed', + createAssert: static fn () => new DispatcherAssert( + forgetPushed: [new DispatcherForgetPushedExpectation()] + ), + call: static fn (DispatcherAssert $assert) => $assert->forgetPushed(), + ), + ]; + } + + protected function createEmptyAssert(): AbstractExpectationCallsMap + { + return new DispatcherAssert(); + } +} diff --git a/tests/Feature/Testing/Laravel/Contracts/Events/TestEvent.php b/tests/Feature/Testing/Laravel/Contracts/Events/TestEvent.php new file mode 100644 index 00000000..b1b564f0 --- /dev/null +++ b/tests/Feature/Testing/Laravel/Contracts/Events/TestEvent.php @@ -0,0 +1,9 @@ +