Skip to content

Commit

Permalink
Add a pre-filled unit test for notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
EdenMl committed Dec 4, 2023
1 parent b833bbb commit 33ae756
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/somake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
];
19 changes: 19 additions & 0 deletions resources/views/test-unit-notification.blade.php
Original file line number Diff line number Diff line change
@@ -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);
});
4 changes: 4 additions & 0 deletions src/Commands/NotificationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,6 +13,7 @@
class NotificationCommand extends Command
{
use AsksDomain;
use CreatesAssociatedUnitTest;

/** @var string */
public $signature = 'somake:notification';
Expand Down Expand Up @@ -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);
}
}
38 changes: 38 additions & 0 deletions src/Domains/Test/UnitTestGenerators/NotificationTestGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace Soyhuce\Somake\Domains\Test\UnitTestGenerators;

use Illuminate\Support\Str;
use ReflectionClass;
use Soyhuce\Somake\Contracts\UnitTestGenerator;

/**
* @implements \Soyhuce\Somake\Contracts\UnitTestGenerator<mixed>
*/
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 [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace Domain\User\Notifications;

use Domain\User\Models\User;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Lang;

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(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('/'));
}
}
2 changes: 2 additions & 0 deletions tests/Feature/NotificationCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/TestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/* @covers \Domain\User\Notifications\AccountCreatedNotification */

use Domain\User\Notifications\AccountCreatedNotification;

it('formats the mail', function (): void {
$notification = new AccountCreatedNotification();

$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);
});

0 comments on commit 33ae756

Please sign in to comment.