From 0cf141ca6a3c282dc20ae82cf4fe33b051cfde15 Mon Sep 17 00:00:00 2001 From: Danilo Polani Date: Sat, 9 Jan 2021 16:18:59 +0100 Subject: [PATCH 1/3] Add ::assertNothingDispatched() to events fake --- .../Support/Testing/Fakes/EventFake.php | 15 +++++++++++++++ tests/Support/SupportTestingEventFakeTest.php | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Illuminate/Support/Testing/Fakes/EventFake.php b/src/Illuminate/Support/Testing/Fakes/EventFake.php index 84f67482ebe3..d4f052e4ed2f 100644 --- a/src/Illuminate/Support/Testing/Fakes/EventFake.php +++ b/src/Illuminate/Support/Testing/Fakes/EventFake.php @@ -106,6 +106,21 @@ public function assertNotDispatched($event, $callback = null) ); } + /** + * Assert if no event was dispatched. + * + * @return void + */ + public function assertNothingDispatched() + { + $count = count(Arr::flatten($this->events)); + + PHPUnit::assertSame( + 0, $count, + "Unexpected {$count} events were dispatched." + ); + } + /** * Get all of the events matching a truth-test callback. * diff --git a/tests/Support/SupportTestingEventFakeTest.php b/tests/Support/SupportTestingEventFakeTest.php index 3a9ccc6cdd3b..d8db3ad41ff0 100644 --- a/tests/Support/SupportTestingEventFakeTest.php +++ b/tests/Support/SupportTestingEventFakeTest.php @@ -118,6 +118,21 @@ function ($event, $payload) { $fake->assertDispatched('Bar'); $fake->assertNotDispatched('Baz'); } + + public function testAssertNothingDispatched() + { + $this->fake->assertNothingDispatched(); + + $this->fake->dispatch(EventStub::class); + $this->fake->dispatch(EventStub::class); + + try { + $this->fake->assertNothingDispatched(); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertThat($e, new ExceptionMessage('Unexpected 2 events were dispatched.')); + } + } } class EventStub From 436f7bfc62089b4320697a8ce87e6b8bc456aae4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 11 Jan 2021 08:56:44 -0600 Subject: [PATCH 2/3] Update EventFake.php --- src/Illuminate/Support/Testing/Fakes/EventFake.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Testing/Fakes/EventFake.php b/src/Illuminate/Support/Testing/Fakes/EventFake.php index d4f052e4ed2f..90f30212ef7b 100644 --- a/src/Illuminate/Support/Testing/Fakes/EventFake.php +++ b/src/Illuminate/Support/Testing/Fakes/EventFake.php @@ -107,7 +107,7 @@ public function assertNotDispatched($event, $callback = null) } /** - * Assert if no event was dispatched. + * Assert that no events were dispatched. * * @return void */ @@ -117,7 +117,7 @@ public function assertNothingDispatched() PHPUnit::assertSame( 0, $count, - "Unexpected {$count} events were dispatched." + "{$count} unexpected events were dispatched." ); } From 46b64fc236bd1c6dbe888e49d54d3bbbc08de0aa Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 11 Jan 2021 08:56:57 -0600 Subject: [PATCH 3/3] Update SupportTestingEventFakeTest.php --- tests/Support/SupportTestingEventFakeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/SupportTestingEventFakeTest.php b/tests/Support/SupportTestingEventFakeTest.php index d8db3ad41ff0..d51562d10c58 100644 --- a/tests/Support/SupportTestingEventFakeTest.php +++ b/tests/Support/SupportTestingEventFakeTest.php @@ -130,7 +130,7 @@ public function testAssertNothingDispatched() $this->fake->assertNothingDispatched(); $this->fail(); } catch (ExpectationFailedException $e) { - $this->assertThat($e, new ExceptionMessage('Unexpected 2 events were dispatched.')); + $this->assertThat($e, new ExceptionMessage('2 unexpected events were dispatched.')); } } }