From ead7c1b96d6d0343459c6abb4d410aa8dee9914a Mon Sep 17 00:00:00 2001 From: eden Date: Mon, 4 Dec 2023 09:22:37 +0100 Subject: [PATCH 1/9] Add somake:notification command --- resources/views/notification.blade.php | 37 +++++++++++++++ src/Commands/NotificationCommand.php | 47 +++++++++++++++++++ src/SomakeServiceProvider.php | 2 + tests/Feature/NotificationCommandTest.php | 31 ++++++++++++ ..._creates_the_notification_correctly__1.php | 32 +++++++++++++ ...not_queued_and_without_multilingual__1.php | 27 +++++++++++ 6 files changed, 176 insertions(+) create mode 100644 resources/views/notification.blade.php create mode 100644 src/Commands/NotificationCommand.php create mode 100644 tests/Feature/NotificationCommandTest.php create mode 100644 tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_correctly__1.php create mode 100644 tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued_and_without_multilingual__1.php diff --git a/resources/views/notification.blade.php b/resources/views/notification.blade.php new file mode 100644 index 0000000..83476e6 --- /dev/null +++ b/resources/views/notification.blade.php @@ -0,0 +1,37 @@ +namespace {{ $namespace }}; + +use Illuminate\Notifications\Notification; +use Illuminate\Notifications\Messages\MailMessage; +@if($multilingual) +use Illuminate\Support\Facades\Lang; +@endif +@if($queued) +use Illuminate\Bus\Queueable; +use Illuminate\Contracts\Queue\ShouldQueue; +@endif + +class {{ $notification }} extends Notification @if($queued)implements ShouldQueue @endif + +{ +@if($queued) + use Queueable; + +@endif + public function __construct() + { + } + + /** + * @return array|string + */ + public function via(): array|string + { + return ['mail']; + } + + public function toMail(): MailMessage + { + return (new MailMessage()) + ->subject(@if($multilingual)Lang::get('Welcome to our platform')@else'Welcome to our platform'@endif); + } +} diff --git a/src/Commands/NotificationCommand.php b/src/Commands/NotificationCommand.php new file mode 100644 index 0000000..7c93bb3 --- /dev/null +++ b/src/Commands/NotificationCommand.php @@ -0,0 +1,47 @@ +confirm(question: 'Should the notification be queued ?'); + + $multilingual = $this->confirm(question: 'Should the notification be multilingual ?', default: true); + + $domain = $this->askDomain($finder->domains()); + + $writer + ->write( + 'notification', + [ + 'notification' => $notification, + 'queued' => $queued, + 'multilingual' => $multilingual, + ] + ) + ->toPath($finder->domainPath("{$domain}/Notifications/{$notification}.php")); + + $notificationFqcn = "Domain\\{$domain}\\Notifications\\{$notification}"; + + outro("The {$notificationFqcn} class was successfully created !"); + } +} diff --git a/src/SomakeServiceProvider.php b/src/SomakeServiceProvider.php index ea8fcd0..e565e9a 100644 --- a/src/SomakeServiceProvider.php +++ b/src/SomakeServiceProvider.php @@ -16,6 +16,7 @@ use Soyhuce\Somake\Commands\MiddlewareCommand; use Soyhuce\Somake\Commands\MigrationCommand; use Soyhuce\Somake\Commands\ModelCommand; +use Soyhuce\Somake\Commands\NotificationCommand; use Soyhuce\Somake\Commands\PolicyCommand; use Soyhuce\Somake\Commands\ProviderCommand; use Soyhuce\Somake\Commands\RequestCommand; @@ -48,6 +49,7 @@ public function configurePackage(Package $package): void MiddlewareCommand::class, MigrationCommand::class, ModelCommand::class, + NotificationCommand::class, PolicyCommand::class, ProviderCommand::class, RequestCommand::class, diff --git a/tests/Feature/NotificationCommandTest.php b/tests/Feature/NotificationCommandTest.php new file mode 100644 index 0000000..32ff70e --- /dev/null +++ b/tests/Feature/NotificationCommandTest.php @@ -0,0 +1,31 @@ +artisan('somake:notification') + ->expectsQuestion('What is the Notification name ?', 'WelcomeNotification') + ->expectsQuestion('Should the notification be queued ?', true) + ->expectsQuestion('Should the notification be multilingual ?', true) + ->expectsQuestion('What is the Domain ?', 'User') + ->expectsOutputToContain('The Domain\\User\\Notifications\\WelcomeNotification class was successfully created !') + ->assertExitCode(0) + ->execute(); + + expect($this->app->basePath('app/Domain/User/Notifications/WelcomeNotification.php')) + ->toBeFile() + ->toMatchFileSnapshot(); +}); + +it('creates the notification not queued and without multilingual', function (): void { + $this->artisan('somake:notification') + ->expectsQuestion('What is the Notification name ?', 'WelcomeNotification') + ->expectsQuestion('Should the notification be queued ?', false) + ->expectsQuestion('Should the notification be multilingual ?', false) + ->expectsQuestion('What is the Domain ?', 'User') + ->expectsOutputToContain('The Domain\\User\\Notifications\\WelcomeNotification class was successfully created !') + ->assertExitCode(0) + ->execute(); + + expect($this->app->basePath('app/Domain/User/Notifications/WelcomeNotification.php')) + ->toBeFile() + ->toMatchFileSnapshot(); +}); diff --git a/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_correctly__1.php b/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_correctly__1.php new file mode 100644 index 0000000..d8325e4 --- /dev/null +++ b/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_correctly__1.php @@ -0,0 +1,32 @@ +|string + */ + public function via(): array|string + { + return ['mail']; + } + + public function toMail(): MailMessage + { + return (new MailMessage()) + ->subject(Lang::get('Welcome to our platform')); + } +} diff --git a/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued_and_without_multilingual__1.php b/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued_and_without_multilingual__1.php new file mode 100644 index 0000000..8d821f9 --- /dev/null +++ b/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued_and_without_multilingual__1.php @@ -0,0 +1,27 @@ +|string + */ + public function via(): array|string + { + return ['mail']; + } + + public function toMail(): MailMessage + { + return (new MailMessage()) + ->subject('Welcome to our platform'); + } +} From d94353943146b902f1dcad4f354bd022386aabb2 Mon Sep 17 00:00:00 2001 From: eden Date: Mon, 4 Dec 2023 09:40:38 +0100 Subject: [PATCH 2/9] Update README --- README.md | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 815ed8c..1f80896 100644 --- a/README.md +++ b/README.md @@ -46,16 +46,17 @@ The configuration of generated classes is done via interactive questions. ### Domain -| Commande | Description | Generation path | -|-------------------|------------------|---------------------------------| -| `somake:action` | Action | `app/Domain/[domain]/Actions` | -| `somake:builder` | Eloquent Builder | `app/Domain/[domain]/Builders` | -| `somake:data` | Data | `app/Domain/[domain]/Data` | -| `somake:enum` | Enum | `app/Domain/[domain]/Enums` | -| `somake:event` | Event | `app/Domain/[domain]/Events` | -| `somake:listener` | Listener | `app/Domain/[domain]/Listeners` | -| `somake:model` | Model | `app/Domain/[domain]/Models` | -| `somake:policy` | Policy | `app/Domain/[domain]/Policies` | +| Commande | Description | Generation path | +|-----------------------|------------------|-------------------------------------| +| `somake:action` | Action | `app/Domain/[domain]/Actions` | +| `somake:builder` | Eloquent Builder | `app/Domain/[domain]/Builders` | +| `somake:data` | Data | `app/Domain/[domain]/Data` | +| `somake:enum` | Enum | `app/Domain/[domain]/Enums` | +| `somake:event` | Event | `app/Domain/[domain]/Events` | +| `somake:listener` | Listener | `app/Domain/[domain]/Listeners` | +| `somake:model` | Model | `app/Domain/[domain]/Models` | +| `somake:notification` | Notification | `app/Domain/[domain]/Notifications` | +| `somake:policy` | Policy | `app/Domain/[domain]/Policies` | ### Support From b833bbbd091cd261c022bef4beb88cd06e9b9483 Mon Sep 17 00:00:00 2001 From: eden Date: Mon, 4 Dec 2023 14:45:13 +0100 Subject: [PATCH 3/9] Multilingual is now enabled by default --- resources/views/notification.blade.php | 4 +--- src/Commands/NotificationCommand.php | 3 --- .../app/App/Commands/ConvertModels.php | 18 ++++++++++++++++++ tests/Feature/NotificationCommandTest.php | 4 +--- ...creates_the_notification_not_queued__1.php} | 3 ++- 5 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 test-laravel/app/App/Commands/ConvertModels.php rename tests/__snapshots__/files/{NotificationCommandTest__it_creates_the_notification_not_queued_and_without_multilingual__1.php => NotificationCommandTest__it_creates_the_notification_not_queued__1.php} (82%) diff --git a/resources/views/notification.blade.php b/resources/views/notification.blade.php index 83476e6..a8bedd3 100644 --- a/resources/views/notification.blade.php +++ b/resources/views/notification.blade.php @@ -2,9 +2,7 @@ use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\MailMessage; -@if($multilingual) use Illuminate\Support\Facades\Lang; -@endif @if($queued) use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -32,6 +30,6 @@ public function via(): array|string public function toMail(): MailMessage { return (new MailMessage()) - ->subject(@if($multilingual)Lang::get('Welcome to our platform')@else'Welcome to our platform'@endif); + ->subject(Lang::get('Welcome to our platform')); } } diff --git a/src/Commands/NotificationCommand.php b/src/Commands/NotificationCommand.php index 7c93bb3..9f8fbba 100644 --- a/src/Commands/NotificationCommand.php +++ b/src/Commands/NotificationCommand.php @@ -25,8 +25,6 @@ public function handle(Finder $finder, Writer $writer): void $queued = $this->confirm(question: 'Should the notification be queued ?'); - $multilingual = $this->confirm(question: 'Should the notification be multilingual ?', default: true); - $domain = $this->askDomain($finder->domains()); $writer @@ -35,7 +33,6 @@ public function handle(Finder $finder, Writer $writer): void [ 'notification' => $notification, 'queued' => $queued, - 'multilingual' => $multilingual, ] ) ->toPath($finder->domainPath("{$domain}/Notifications/{$notification}.php")); diff --git a/test-laravel/app/App/Commands/ConvertModels.php b/test-laravel/app/App/Commands/ConvertModels.php new file mode 100644 index 0000000..193f0c0 --- /dev/null +++ b/test-laravel/app/App/Commands/ConvertModels.php @@ -0,0 +1,18 @@ +artisan('somake:notification') ->expectsQuestion('What is the Notification name ?', 'WelcomeNotification') ->expectsQuestion('Should the notification be queued ?', true) - ->expectsQuestion('Should the notification be multilingual ?', true) ->expectsQuestion('What is the Domain ?', 'User') ->expectsOutputToContain('The Domain\\User\\Notifications\\WelcomeNotification class was successfully created !') ->assertExitCode(0) @@ -15,11 +14,10 @@ ->toMatchFileSnapshot(); }); -it('creates the notification not queued and without multilingual', function (): void { +it('creates the notification not queued', function (): void { $this->artisan('somake:notification') ->expectsQuestion('What is the Notification name ?', 'WelcomeNotification') ->expectsQuestion('Should the notification be queued ?', false) - ->expectsQuestion('Should the notification be multilingual ?', false) ->expectsQuestion('What is the Domain ?', 'User') ->expectsOutputToContain('The Domain\\User\\Notifications\\WelcomeNotification class was successfully created !') ->assertExitCode(0) diff --git a/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued_and_without_multilingual__1.php b/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued__1.php similarity index 82% rename from tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued_and_without_multilingual__1.php rename to tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued__1.php index 8d821f9..881c911 100644 --- a/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued_and_without_multilingual__1.php +++ b/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued__1.php @@ -4,6 +4,7 @@ use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\MailMessage; +use Illuminate\Support\Facades\Lang; class WelcomeNotification extends Notification { @@ -22,6 +23,6 @@ public function via(): array|string public function toMail(): MailMessage { return (new MailMessage()) - ->subject('Welcome to our platform'); + ->subject(Lang::get('Welcome to our platform')); } } From 33ae756703a86b7e53e299633896b7d8168f972f Mon Sep 17 00:00:00 2001 From: eden Date: Mon, 4 Dec 2023 15:59:58 +0100 Subject: [PATCH 4/9] 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 From 7117d83d99faf087c966dd8f7923cdfe057f281c Mon Sep 17 00:00:00 2001 From: eden Date: Mon, 4 Dec 2023 16:13:39 +0100 Subject: [PATCH 5/9] Delete 'ConvertModels.php' file in test-laravel --- .../app/App/Commands/ConvertModels.php | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 test-laravel/app/App/Commands/ConvertModels.php diff --git a/test-laravel/app/App/Commands/ConvertModels.php b/test-laravel/app/App/Commands/ConvertModels.php deleted file mode 100644 index 193f0c0..0000000 --- a/test-laravel/app/App/Commands/ConvertModels.php +++ /dev/null @@ -1,18 +0,0 @@ - Date: Tue, 5 Dec 2023 11:21:16 +0100 Subject: [PATCH 6/9] use trans() function --- resources/views/notification.blade.php | 3 +-- .../User/Notifications/AccountCreatedNotification.php | 9 ++++----- ...andTest__it_creates_the_notification_correctly__1.php | 3 +-- ...ndTest__it_creates_the_notification_not_queued__1.php | 3 +-- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/resources/views/notification.blade.php b/resources/views/notification.blade.php index a8bedd3..9919f51 100644 --- a/resources/views/notification.blade.php +++ b/resources/views/notification.blade.php @@ -2,7 +2,6 @@ use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\MailMessage; -use Illuminate\Support\Facades\Lang; @if($queued) use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -30,6 +29,6 @@ public function via(): array|string public function toMail(): MailMessage { return (new MailMessage()) - ->subject(Lang::get('Welcome to our platform')); + ->subject(trans('Welcome to our platform')); } } diff --git a/test-laravel/app/Domain/User/Notifications/AccountCreatedNotification.php b/test-laravel/app/Domain/User/Notifications/AccountCreatedNotification.php index 4293bf7..21ff071 100644 --- a/test-laravel/app/Domain/User/Notifications/AccountCreatedNotification.php +++ b/test-laravel/app/Domain/User/Notifications/AccountCreatedNotification.php @@ -5,7 +5,6 @@ use Domain\User\Models\User; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; -use Illuminate\Support\Facades\Lang; class AccountCreatedNotification extends Notification { @@ -20,9 +19,9 @@ public function via(): array|string 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('/')); + ->greeting(trans('Hello :name,', ['name' => $user->name])) + ->subject(trans('Your account has been created')) + ->line(trans('Your account has been created, you can now connect to the platform using your usual credentials.')) + ->action(trans('Connect'), url('/')); } } diff --git a/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_correctly__1.php b/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_correctly__1.php index d8325e4..e0c8b70 100644 --- a/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_correctly__1.php +++ b/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_correctly__1.php @@ -4,7 +4,6 @@ use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\MailMessage; -use Illuminate\Support\Facades\Lang; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -27,6 +26,6 @@ public function via(): array|string public function toMail(): MailMessage { return (new MailMessage()) - ->subject(Lang::get('Welcome to our platform')); + ->subject(trans('Welcome to our platform')); } } diff --git a/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued__1.php b/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued__1.php index 881c911..2799c4a 100644 --- a/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued__1.php +++ b/tests/__snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued__1.php @@ -4,7 +4,6 @@ use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\MailMessage; -use Illuminate\Support\Facades\Lang; class WelcomeNotification extends Notification { @@ -23,6 +22,6 @@ public function via(): array|string public function toMail(): MailMessage { return (new MailMessage()) - ->subject(Lang::get('Welcome to our platform')); + ->subject(trans('Welcome to our platform')); } } From 8f1b5553e0d6d7d319d8f7c12310eb939f4dce30 Mon Sep 17 00:00:00 2001 From: eden Date: Tue, 5 Dec 2023 12:16:23 +0100 Subject: [PATCH 7/9] use pest expectation api --- .../views/test-unit-notification.blade.php | 19 +++++++++++-------- ...ctly_the_unit_test_for_notification__1.php | 19 +++++++++++-------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/resources/views/test-unit-notification.blade.php b/resources/views/test-unit-notification.blade.php index 758c820..ccfc109 100644 --- a/resources/views/test-unit-notification.blade.php +++ b/resources/views/test-unit-notification.blade.php @@ -1,19 +1,22 @@ /* @@covers \{{ $covered }} */ use {{ $classFqcn }}; +use Illuminate\Notifications\Messages\MailMessage; it('formats the mail', function (): void { $notification = new {{ $classBasename }}(); - $this->assertEquals(['mail'], $notification->via()); + expect($notification->via())->toBe(['mail']); $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); + expect($mail) + ->toBeInstanceOf(MailMessage::class) + ->subject->toBe('') + ->greeting->toBe('') + ->introLines->toBe(['']) + ->actionText->toBe('') + ->actionUrl->toBeUrl()->toBe('') + ->outroLines->toBe(['']) + ->salutation->toBe(''); }); \ No newline at end of file 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 index c268e34..202475b 100644 --- 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 @@ -3,19 +3,22 @@ /* @covers \Domain\User\Notifications\AccountCreatedNotification */ use Domain\User\Notifications\AccountCreatedNotification; +use Illuminate\Notifications\Messages\MailMessage; it('formats the mail', function (): void { $notification = new AccountCreatedNotification(); - $this->assertEquals(['mail'], $notification->via()); + expect($notification->via())->toBe(['mail']); $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); + expect($mail) + ->toBeInstanceOf(MailMessage::class) + ->subject->toBe('') + ->greeting->toBe('') + ->introLines->toBe(['']) + ->actionText->toBe('') + ->actionUrl->toBeUrl()->toBe('') + ->outroLines->toBe(['']) + ->salutation->toBe(''); }); \ No newline at end of file From d52c406e896212317bac1cd776ad3a3bdd627312 Mon Sep 17 00:00:00 2001 From: eden Date: Tue, 5 Dec 2023 12:27:04 +0100 Subject: [PATCH 8/9] Add a test for the via() method --- resources/views/test-unit-notification.blade.php | 8 +++++--- ...reates_correctly_the_unit_test_for_notification__1.php | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/resources/views/test-unit-notification.blade.php b/resources/views/test-unit-notification.blade.php index ccfc109..629bc23 100644 --- a/resources/views/test-unit-notification.blade.php +++ b/resources/views/test-unit-notification.blade.php @@ -3,12 +3,14 @@ use {{ $classFqcn }}; use Illuminate\Notifications\Messages\MailMessage; -it('formats the mail', function (): void { +it('uses the mail channel', function (): void { $notification = new {{ $classBasename }}(); - expect($notification->via())->toBe(['mail']); + expect($notification->via())->toContain('mail'); +}); - $mail = $notification->toMail(); +it('formats the mail', function (): void { + $mail = (new {{ $classBasename }}())->toMail(); expect($mail) ->toBeInstanceOf(MailMessage::class) 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 index 202475b..7e7b771 100644 --- 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 @@ -5,12 +5,14 @@ use Domain\User\Notifications\AccountCreatedNotification; use Illuminate\Notifications\Messages\MailMessage; -it('formats the mail', function (): void { +it('uses the mail channel', function (): void { $notification = new AccountCreatedNotification(); - expect($notification->via())->toBe(['mail']); + expect($notification->via())->toContain('mail'); +}); - $mail = $notification->toMail(); +it('formats the mail', function (): void { + $mail = (new AccountCreatedNotification())->toMail(); expect($mail) ->toBeInstanceOf(MailMessage::class) From f765190a76e70008ddcf5b00724935b2c5825fd2 Mon Sep 17 00:00:00 2001 From: eden Date: Tue, 5 Dec 2023 13:38:10 +0100 Subject: [PATCH 9/9] Simplified verification for notification testing --- resources/views/test-unit-notification.blade.php | 2 +- .../NotificationTestGenerator.php | 15 ++------------- ...orrectly_the_unit_test_for_notification__1.php | 2 +- 3 files changed, 4 insertions(+), 15 deletions(-) diff --git a/resources/views/test-unit-notification.blade.php b/resources/views/test-unit-notification.blade.php index 629bc23..8b92a79 100644 --- a/resources/views/test-unit-notification.blade.php +++ b/resources/views/test-unit-notification.blade.php @@ -6,7 +6,7 @@ it('uses the mail channel', function (): void { $notification = new {{ $classBasename }}(); - expect($notification->via())->toContain('mail'); + expect($notification->via())->toBe(['mail']); }); it('formats the mail', function (): void { diff --git a/src/Domains/Test/UnitTestGenerators/NotificationTestGenerator.php b/src/Domains/Test/UnitTestGenerators/NotificationTestGenerator.php index d07da0d..7e0d7b2 100644 --- a/src/Domains/Test/UnitTestGenerators/NotificationTestGenerator.php +++ b/src/Domains/Test/UnitTestGenerators/NotificationTestGenerator.php @@ -2,8 +2,7 @@ namespace Soyhuce\Somake\Domains\Test\UnitTestGenerators; -use Illuminate\Support\Str; -use ReflectionClass; +use Illuminate\Notifications\Notification; use Soyhuce\Somake\Contracts\UnitTestGenerator; /** @@ -13,17 +12,7 @@ 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'); + return is_subclass_of($class, Notification::class); } public function view(): string 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 index 7e7b771..343355b 100644 --- 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 @@ -8,7 +8,7 @@ it('uses the mail channel', function (): void { $notification = new AccountCreatedNotification(); - expect($notification->via())->toContain('mail'); + expect($notification->via())->toBe(['mail']); }); it('formats the mail', function (): void {