From 8382ad7c2d9120fb547c5cbbd71e3db5ed97cb4f Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Tue, 4 Jul 2023 12:35:12 -0400 Subject: [PATCH 1/2] Assertions for counting outgoing mailables --- .../Support/Testing/Fakes/MailFake.php | 50 +++++++++++++++++++ tests/Support/SupportTestingMailFakeTest.php | 48 ++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/src/Illuminate/Support/Testing/Fakes/MailFake.php b/src/Illuminate/Support/Testing/Fakes/MailFake.php index d47373ffae12..a2a715e3130f 100644 --- a/src/Illuminate/Support/Testing/Fakes/MailFake.php +++ b/src/Illuminate/Support/Testing/Fakes/MailFake.php @@ -100,6 +100,56 @@ protected function assertSentTimes($mailable, $times = 1) ); } + /** + * Assert the total number of mailables were sent. + * + * @param int $count + * @return void + */ + public function assertSentCount($count) + { + $total = collect($this->mailables)->count(); + + PHPUnit::assertSame( + $count, $total, + "The total number of mailables sent was {$total} instead of {$count}." + ); + } + + /** + * Assert the total number of mailables were queued. + * + * @param int $count + * @return void + */ + public function assertQueuedCount($count) + { + $total = collect($this->queuedMailables)->count(); + + PHPUnit::assertSame( + $count, $total, + "The total number of mailables queued was {$total} instead of {$count}." + ); + } + + /** + * Assert the total number of mailables were sent or queued. + * + * @param int $count + * @return void + */ + public function assertOutgoingCount($count) + { + $total = collect($this->mailables) + ->concat($this->queuedMailables) + ->count(); + + PHPUnit::assertSame( + $count, $total, + "The total number of outgoing mailables was {$total} instead of {$count}." + ); + } + /** * Determine if a mailable was not sent or queued to be sent based on a truth-test callback. * diff --git a/tests/Support/SupportTestingMailFakeTest.php b/tests/Support/SupportTestingMailFakeTest.php index 72f4e3b323da..4052ee6c2622 100644 --- a/tests/Support/SupportTestingMailFakeTest.php +++ b/tests/Support/SupportTestingMailFakeTest.php @@ -133,6 +133,21 @@ public function testAssertSentTimes() $this->fake->assertSent(MailableStub::class, 2); } + public function testAssertSentCount() + { + $this->fake->to('taylor@laravel.com')->send($this->mailable); + $this->fake->to('taylor@laravel.com')->send($this->mailable); + + try { + $this->fake->assertSentCount(1); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertStringContainsString('The total number of mailables sent was 2 instead of 1.', $e->getMessage()); + } + + $this->fake->assertSentCount(2); + } + public function testAssertQueued() { try { @@ -162,6 +177,21 @@ public function testAssertQueuedTimes() $this->fake->assertQueued(MailableStub::class, 2); } + public function testAssertQueuedCount() + { + $this->fake->to('taylor@laravel.com')->queue($this->mailable); + $this->fake->to('taylor@laravel.com')->queue($this->mailable); + + try { + $this->fake->assertQueuedCount(1); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertStringContainsString('The total number of mailables queued was 2 instead of 1.', $e->getMessage()); + } + + $this->fake->assertQueuedCount(2); + } + public function testSendQueuesAMailableThatShouldBeQueued() { $this->fake->to('taylor@laravel.com')->send(new QueueableMailableStub); @@ -204,6 +234,24 @@ public function testAssertNothingQueued() } } + public function testAssertOutgoingCount() + { + $this->fake->assertNothingOutgoing(); + + $this->fake->to('taylor@laravel.com')->queue($this->mailable); + + try { + $this->fake->assertOutgoingCount(2); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertStringContainsString('The total number of outgoing mailables was 1 instead of 2.', $e->getMessage()); + } + + $this->fake->to('taylor@laravel.com')->send($this->mailable); + + $this->fake->assertOutgoingCount(2); + } + public function testAssertQueuedWithClosure() { $this->fake->to($user = new LocalizedRecipientStub)->queue($this->mailable); From f20ad2f1c9dc7ea583d09f644cb8310005ae2601 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 4 Jul 2023 20:23:27 +0200 Subject: [PATCH 2/2] Update MailFake.php --- .../Support/Testing/Fakes/MailFake.php | 100 +++++++++--------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/src/Illuminate/Support/Testing/Fakes/MailFake.php b/src/Illuminate/Support/Testing/Fakes/MailFake.php index a2a715e3130f..a13998cfdd87 100644 --- a/src/Illuminate/Support/Testing/Fakes/MailFake.php +++ b/src/Illuminate/Support/Testing/Fakes/MailFake.php @@ -100,56 +100,6 @@ protected function assertSentTimes($mailable, $times = 1) ); } - /** - * Assert the total number of mailables were sent. - * - * @param int $count - * @return void - */ - public function assertSentCount($count) - { - $total = collect($this->mailables)->count(); - - PHPUnit::assertSame( - $count, $total, - "The total number of mailables sent was {$total} instead of {$count}." - ); - } - - /** - * Assert the total number of mailables were queued. - * - * @param int $count - * @return void - */ - public function assertQueuedCount($count) - { - $total = collect($this->queuedMailables)->count(); - - PHPUnit::assertSame( - $count, $total, - "The total number of mailables queued was {$total} instead of {$count}." - ); - } - - /** - * Assert the total number of mailables were sent or queued. - * - * @param int $count - * @return void - */ - public function assertOutgoingCount($count) - { - $total = collect($this->mailables) - ->concat($this->queuedMailables) - ->count(); - - PHPUnit::assertSame( - $count, $total, - "The total number of outgoing mailables was {$total} instead of {$count}." - ); - } - /** * Determine if a mailable was not sent or queued to be sent based on a truth-test callback. * @@ -274,6 +224,56 @@ public function assertNothingQueued() PHPUnit::assertEmpty($this->queuedMailables, 'The following mailables were queued unexpectedly: '.$mailableNames); } + /** + * Assert the total number of mailables that were sent. + * + * @param int $count + * @return void + */ + public function assertSentCount($count) + { + $total = collect($this->mailables)->count(); + + PHPUnit::assertSame( + $count, $total, + "The total number of mailables sent was {$total} instead of {$count}." + ); + } + + /** + * Assert the total number of mailables that were queued. + * + * @param int $count + * @return void + */ + public function assertQueuedCount($count) + { + $total = collect($this->queuedMailables)->count(); + + PHPUnit::assertSame( + $count, $total, + "The total number of mailables queued was {$total} instead of {$count}." + ); + } + + /** + * Assert the total number of mailables that were sent or queued. + * + * @param int $count + * @return void + */ + public function assertOutgoingCount($count) + { + $total = collect($this->mailables) + ->concat($this->queuedMailables) + ->count(); + + PHPUnit::assertSame( + $count, $total, + "The total number of outgoing mailables was {$total} instead of {$count}." + ); + } + /** * Get all of the mailables matching a truth-test callback. *