From 5e4604933d7921b8ea8a0c3c4148293732bde02b Mon Sep 17 00:00:00 2001 From: Eimantas Kateiva Date: Sun, 24 Dec 2017 17:17:41 +0200 Subject: [PATCH] add assertDispatchedTimes for event fakes --- .../Support/Testing/Fakes/EventFake.php | 21 +++++- tests/Support/SupportTestingEventFakeTest.php | 65 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 tests/Support/SupportTestingEventFakeTest.php diff --git a/src/Illuminate/Support/Testing/Fakes/EventFake.php b/src/Illuminate/Support/Testing/Fakes/EventFake.php index 258c21192ca6..edbb90eb1b96 100644 --- a/src/Illuminate/Support/Testing/Fakes/EventFake.php +++ b/src/Illuminate/Support/Testing/Fakes/EventFake.php @@ -47,17 +47,36 @@ public function __construct(Dispatcher $dispatcher, $eventsToFake = []) * Assert if an event was dispatched based on a truth-test callback. * * @param string $event - * @param callable|null $callback + * @param callable|int|null $callback * @return void */ public function assertDispatched($event, $callback = null) { + if (is_int($callback)) { + return $this->assertDispatchedTimes($event, $callback); + } + PHPUnit::assertTrue( $this->dispatched($event, $callback)->count() > 0, "The expected [{$event}] event was not dispatched." ); } + /** + * Assert if a event was dispatched a number of times. + * + * @param string $event + * @param int $times + * @return void + */ + public function assertDispatchedTimes($event, $times = 1) + { + PHPUnit::assertTrue( + ($count = $this->dispatched($event)->count()) === $times, + "The expected [{$event}] event was dispatched {$count} times instead of {$times} times." + ); + } + /** * Determine if an event was dispatched based on a truth-test callback. * diff --git a/tests/Support/SupportTestingEventFakeTest.php b/tests/Support/SupportTestingEventFakeTest.php new file mode 100644 index 000000000000..74c974ec643c --- /dev/null +++ b/tests/Support/SupportTestingEventFakeTest.php @@ -0,0 +1,65 @@ +fake = new EventFake(m::mock(Dispatcher::class)); + } + + public function testAssertDispacthed() + { + $this->fake->dispatch(EventStub::class); + + $this->fake->assertDispatched(EventStub::class); + } + + /** + * @expectedException PHPUnit\Framework\ExpectationFailedException + * @expectedExceptionMessage The expected [Illuminate\Tests\Support\EventStub] event was dispatched 2 times instead of 1 times. + */ + public function testAssertDispatchedWithCallbackInt() + { + $this->fake->dispatch(EventStub::class); + $this->fake->dispatch(EventStub::class); + + $this->fake->assertDispatched(EventStub::class, 2); + $this->fake->assertDispatched(EventStub::class, 1); + } + + /** + * @expectedException PHPUnit\Framework\ExpectationFailedException + * @expectedExceptionMessage The expected [Illuminate\Tests\Support\EventStub] event was dispatched 2 times instead of 1 times. + */ + public function testAssertDispatchedTimes() + { + $this->fake->dispatch(EventStub::class); + $this->fake->dispatch(EventStub::class); + + $this->fake->assertDispatchedTimes(EventStub::class, 2); + $this->fake->assertDispatchedTimes(EventStub::class, 1); + } + + /** + * @expectedException PHPUnit\Framework\ExpectationFailedException + * @expectedExceptionMessage The unexpected [Illuminate\Tests\Support\EventStub] event was dispatched. + */ + public function testAssertNotDispatched() + { + $this->fake->dispatch(EventStub::class); + + $this->fake->assertNotDispatched(EventStub::class); + } +} + +class EventStub +{ +}