From 33ae756703a86b7e53e299633896b7d8168f972f Mon Sep 17 00:00:00 2001 From: eden Date: Mon, 4 Dec 2023 15:59:58 +0100 Subject: [PATCH] Add a pre-filled unit test for notifications --- config/somake.php | 1 + .../views/test-unit-notification.blade.php | 19 ++++++++++ src/Commands/NotificationCommand.php | 4 ++ .../NotificationTestGenerator.php | 38 +++++++++++++++++++ .../AccountCreatedNotification.php | 28 ++++++++++++++ tests/Feature/NotificationCommandTest.php | 2 + tests/Feature/TestCommandTest.php | 13 +++++++ ...ctly_the_unit_test_for_notification__1.php | 21 ++++++++++ 8 files changed, 126 insertions(+) create mode 100644 resources/views/test-unit-notification.blade.php create mode 100644 src/Domains/Test/UnitTestGenerators/NotificationTestGenerator.php create mode 100644 test-laravel/app/Domain/User/Notifications/AccountCreatedNotification.php create mode 100644 tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_unit_test_for_notification__1.php diff --git a/config/somake.php b/config/somake.php index 96f02cb..95f7ab1 100644 --- a/config/somake.php +++ b/config/somake.php @@ -23,6 +23,7 @@ Soyhuce\Somake\Domains\Test\UnitTestGenerators\ListenerTestGenerator::class, Soyhuce\Somake\Domains\Test\UnitTestGenerators\JsonResourceTestGenerator::class, Soyhuce\Somake\Domains\Test\UnitTestGenerators\MiddlewareTestGenerator::class, + Soyhuce\Somake\Domains\Test\UnitTestGenerators\NotificationTestGenerator::class, Soyhuce\Somake\Domains\Test\UnitTestGenerators\DefaultTestGenerator::class, ], ]; diff --git a/resources/views/test-unit-notification.blade.php b/resources/views/test-unit-notification.blade.php new file mode 100644 index 0000000..758c820 --- /dev/null +++ b/resources/views/test-unit-notification.blade.php @@ -0,0 +1,19 @@ +/* @@covers \{{ $covered }} */ + +use {{ $classFqcn }}; + +it('formats the mail', function (): void { + $notification = new {{ $classBasename }}(); + + $this->assertEquals(['mail'], $notification->via()); + + $mail = $notification->toMail(); + + $this->assertEquals('', $mail->subject); + $this->assertEquals('', $mail->greeting); + $this->assertEquals([''], $mail->introLines); + $this->assertEquals('', $mail->actionText); + $this->assertEquals('', $mail->actionUrl); + $this->assertEquals([''], $mail->outroLines); + $this->assertEquals('', $mail->salutation); +}); \ No newline at end of file diff --git a/src/Commands/NotificationCommand.php b/src/Commands/NotificationCommand.php index 9f8fbba..2ffea20 100644 --- a/src/Commands/NotificationCommand.php +++ b/src/Commands/NotificationCommand.php @@ -4,6 +4,7 @@ use Illuminate\Console\Command; use Soyhuce\Somake\Commands\Concerns\AsksDomain; +use Soyhuce\Somake\Commands\Concerns\CreatesAssociatedUnitTest; use Soyhuce\Somake\Support\Finder; use Soyhuce\Somake\Support\Writer; use function Laravel\Prompts\outro; @@ -12,6 +13,7 @@ class NotificationCommand extends Command { use AsksDomain; + use CreatesAssociatedUnitTest; /** @var string */ public $signature = 'somake:notification'; @@ -40,5 +42,7 @@ public function handle(Finder $finder, Writer $writer): void $notificationFqcn = "Domain\\{$domain}\\Notifications\\{$notification}"; outro("The {$notificationFqcn} class was successfully created !"); + + $this->createUnitTest($notificationFqcn); } } diff --git a/src/Domains/Test/UnitTestGenerators/NotificationTestGenerator.php b/src/Domains/Test/UnitTestGenerators/NotificationTestGenerator.php new file mode 100644 index 0000000..d07da0d --- /dev/null +++ b/src/Domains/Test/UnitTestGenerators/NotificationTestGenerator.php @@ -0,0 +1,38 @@ + + */ +class NotificationTestGenerator implements UnitTestGenerator +{ + public static function shouldHandle(string $class): bool + { + if (!class_exists($class)) { + return false; + } + + if (!Str::is('Domain\\*\\Notifications\\*', $class)) { + return false; + } + + $reflectionClass = new ReflectionClass($class); + + return $reflectionClass->hasMethod('toMail'); + } + + public function view(): string + { + return 'test-unit-notification'; + } + + public function data(string $class): array + { + return []; + } +} diff --git a/test-laravel/app/Domain/User/Notifications/AccountCreatedNotification.php b/test-laravel/app/Domain/User/Notifications/AccountCreatedNotification.php new file mode 100644 index 0000000..4293bf7 --- /dev/null +++ b/test-laravel/app/Domain/User/Notifications/AccountCreatedNotification.php @@ -0,0 +1,28 @@ +|string + */ + public function via(): array|string + { + return ['mail']; + } + + public function toMail(User $user): MailMessage + { + return (new MailMessage()) + ->greeting(Lang::get('Hello :name,', ['name' => $user->name])) + ->subject(Lang::get('Your account has been created')) + ->line(Lang::get('Your account has been created, you can now connect to the platform using your usual credentials.')) + ->action(Lang::get('Connect'), url('/')); + } +} diff --git a/tests/Feature/NotificationCommandTest.php b/tests/Feature/NotificationCommandTest.php index ddaae60..9b3b337 100644 --- a/tests/Feature/NotificationCommandTest.php +++ b/tests/Feature/NotificationCommandTest.php @@ -6,6 +6,7 @@ ->expectsQuestion('Should the notification be queued ?', true) ->expectsQuestion('What is the Domain ?', 'User') ->expectsOutputToContain('The Domain\\User\\Notifications\\WelcomeNotification class was successfully created !') + ->expectsQuestion('Do you want to create a Unit Test for Domain\User\Notifications\WelcomeNotification ?', false) ->assertExitCode(0) ->execute(); @@ -20,6 +21,7 @@ ->expectsQuestion('Should the notification be queued ?', false) ->expectsQuestion('What is the Domain ?', 'User') ->expectsOutputToContain('The Domain\\User\\Notifications\\WelcomeNotification class was successfully created !') + ->expectsQuestion('Do you want to create a Unit Test for Domain\User\Notifications\WelcomeNotification ?', false) ->assertExitCode(0) ->execute(); diff --git a/tests/Feature/TestCommandTest.php b/tests/Feature/TestCommandTest.php index 77f7385..8e93581 100644 --- a/tests/Feature/TestCommandTest.php +++ b/tests/Feature/TestCommandTest.php @@ -132,3 +132,16 @@ ->toBeFile() ->toMatchFileSnapshot(); }); + +it('creates correctly the unit test for notification', function (): void { + $this->artisan('somake:test') + ->expectsQuestion('Which kind of test do you want to create ?', 'Unit') + ->expectsQuestion('Which class do you want to cover ?', 'AccountCreatedNotification') + ->expectsOutputToContain('The Tests\\Unit\\Domain\\User\\Notifications\\AccountCreatedNotificationTest class was successfully created !') + ->assertExitCode(0) + ->execute(); + + expect($this->app->basePath('tests/Unit/Domain/User/Notifications/AccountCreatedNotificationTest.php')) + ->toBeFile() + ->toMatchFileSnapshot(); +}); diff --git a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_unit_test_for_notification__1.php b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_unit_test_for_notification__1.php new file mode 100644 index 0000000..c268e34 --- /dev/null +++ b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_unit_test_for_notification__1.php @@ -0,0 +1,21 @@ +assertEquals(['mail'], $notification->via()); + + $mail = $notification->toMail(); + + $this->assertEquals('', $mail->subject); + $this->assertEquals('', $mail->greeting); + $this->assertEquals([''], $mail->introLines); + $this->assertEquals('', $mail->actionText); + $this->assertEquals('', $mail->actionUrl); + $this->assertEquals([''], $mail->outroLines); + $this->assertEquals('', $mail->salutation); +}); \ No newline at end of file