generated from Soyhuce/package-skeleton-laravel
- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add somake:notification command (#49)
* Add somake:notification command * Update README * Multilingual is now enabled by default * Add a pre-filled unit test for notifications * Delete 'ConvertModels.php' file in test-laravel * use trans() function * use pest expectation api * Add a test for the via() method * Simplified verification for notification testing
- Loading branch information
Showing
13 changed files
with
302 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace {{ $namespace }}; | ||
|
||
use Illuminate\Notifications\Notification; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
@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>|string | ||
*/ | ||
public function via(): array|string | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
public function toMail(): MailMessage | ||
{ | ||
return (new MailMessage()) | ||
->subject(trans('Welcome to our platform')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* @@covers \{{ $covered }} */ | ||
|
||
use {{ $classFqcn }}; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
|
||
it('uses the mail channel', function (): void { | ||
$notification = new {{ $classBasename }}(); | ||
|
||
expect($notification->via())->toBe(['mail']); | ||
}); | ||
|
||
it('formats the mail', function (): void { | ||
$mail = (new {{ $classBasename }}())->toMail(); | ||
|
||
expect($mail) | ||
->toBeInstanceOf(MailMessage::class) | ||
->subject->toBe('') | ||
->greeting->toBe('') | ||
->introLines->toBe(['']) | ||
->actionText->toBe('') | ||
->actionUrl->toBeUrl()->toBe('') | ||
->outroLines->toBe(['']) | ||
->salutation->toBe(''); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Soyhuce\Somake\Commands; | ||
|
||
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; | ||
use function Laravel\Prompts\text; | ||
|
||
class NotificationCommand extends Command | ||
{ | ||
use AsksDomain; | ||
use CreatesAssociatedUnitTest; | ||
|
||
/** @var string */ | ||
public $signature = 'somake:notification'; | ||
|
||
/** @var string */ | ||
public $description = 'Generates a Notification in Domain'; | ||
|
||
public function handle(Finder $finder, Writer $writer): void | ||
{ | ||
$notification = text(label: 'What is the Notification name ?', required: true); | ||
|
||
$queued = $this->confirm(question: 'Should the notification be queued ?'); | ||
|
||
$domain = $this->askDomain($finder->domains()); | ||
|
||
$writer | ||
->write( | ||
'notification', | ||
[ | ||
'notification' => $notification, | ||
'queued' => $queued, | ||
] | ||
) | ||
->toPath($finder->domainPath("{$domain}/Notifications/{$notification}.php")); | ||
|
||
$notificationFqcn = "Domain\\{$domain}\\Notifications\\{$notification}"; | ||
|
||
outro("The {$notificationFqcn} class was successfully created !"); | ||
|
||
$this->createUnitTest($notificationFqcn); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Domains/Test/UnitTestGenerators/NotificationTestGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Soyhuce\Somake\Domains\Test\UnitTestGenerators; | ||
|
||
use Illuminate\Notifications\Notification; | ||
use Soyhuce\Somake\Contracts\UnitTestGenerator; | ||
|
||
/** | ||
* @implements \Soyhuce\Somake\Contracts\UnitTestGenerator<mixed> | ||
*/ | ||
class NotificationTestGenerator implements UnitTestGenerator | ||
{ | ||
public static function shouldHandle(string $class): bool | ||
{ | ||
return is_subclass_of($class, Notification::class); | ||
} | ||
|
||
public function view(): string | ||
{ | ||
return 'test-unit-notification'; | ||
} | ||
|
||
public function data(string $class): array | ||
{ | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
test-laravel/app/Domain/User/Notifications/AccountCreatedNotification.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Domain\User\Notifications; | ||
|
||
use Domain\User\Models\User; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
use Illuminate\Notifications\Notification; | ||
|
||
class AccountCreatedNotification extends Notification | ||
{ | ||
/** | ||
* @return array<string>|string | ||
*/ | ||
public function via(): array|string | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
public function toMail(User $user): MailMessage | ||
{ | ||
return (new MailMessage()) | ||
->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('/')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types=1); | ||
|
||
it('creates the notification correctly', function (): void { | ||
$this->artisan('somake:notification') | ||
->expectsQuestion('What is the Notification name ?', 'WelcomeNotification') | ||
->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(); | ||
|
||
expect($this->app->basePath('app/Domain/User/Notifications/WelcomeNotification.php')) | ||
->toBeFile() | ||
->toMatchFileSnapshot(); | ||
}); | ||
|
||
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('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(); | ||
|
||
expect($this->app->basePath('app/Domain/User/Notifications/WelcomeNotification.php')) | ||
->toBeFile() | ||
->toMatchFileSnapshot(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...__snapshots__/files/NotificationCommandTest__it_creates_the_notification_correctly__1.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Domain\User\Notifications; | ||
|
||
use Illuminate\Notifications\Notification; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
|
||
class WelcomeNotification extends Notification implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
public function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @return array<string>|string | ||
*/ | ||
public function via(): array|string | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
public function toMail(): MailMessage | ||
{ | ||
return (new MailMessage()) | ||
->subject(trans('Welcome to our platform')); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
..._snapshots__/files/NotificationCommandTest__it_creates_the_notification_not_queued__1.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Domain\User\Notifications; | ||
|
||
use Illuminate\Notifications\Notification; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
|
||
class WelcomeNotification extends Notification | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @return array<string>|string | ||
*/ | ||
public function via(): array|string | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
public function toMail(): MailMessage | ||
{ | ||
return (new MailMessage()) | ||
->subject(trans('Welcome to our platform')); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...shots__/files/TestCommandTest__it_creates_correctly_the_unit_test_for_notification__1.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* @covers \Domain\User\Notifications\AccountCreatedNotification */ | ||
|
||
use Domain\User\Notifications\AccountCreatedNotification; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
|
||
it('uses the mail channel', function (): void { | ||
$notification = new AccountCreatedNotification(); | ||
|
||
expect($notification->via())->toBe(['mail']); | ||
}); | ||
|
||
it('formats the mail', function (): void { | ||
$mail = (new AccountCreatedNotification())->toMail(); | ||
|
||
expect($mail) | ||
->toBeInstanceOf(MailMessage::class) | ||
->subject->toBe('') | ||
->greeting->toBe('') | ||
->introLines->toBe(['']) | ||
->actionText->toBe('') | ||
->actionUrl->toBeUrl()->toBe('') | ||
->outroLines->toBe(['']) | ||
->salutation->toBe(''); | ||
}); |