From 80c16165d18d3ab6462926d59ae62c8bdd7e6765 Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 28 Apr 2023 11:09:43 +0200 Subject: [PATCH] Add middleware test generation --- config/somake.php | 1 + .../views/test-unit-middleware.blade.php | 12 +++++ .../MiddlewareTestGenerator.php | 53 +++++++++++++++++++ .../PreventRequestsDuringMaintenanceTest.php | 6 +++ tests/Feature/TestCommandTest.php | 13 +++++ ...rectly_the_unit_test_for_middleware__1.php | 14 +++++ 6 files changed, 99 insertions(+) create mode 100644 resources/views/test-unit-middleware.blade.php create mode 100644 src/Domains/Test/UnitTestGenerators/MiddlewareTestGenerator.php create mode 100644 test-laravel/tests/Unit/Support/Http/Middleware/PreventRequestsDuringMaintenanceTest.php create mode 100644 tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_unit_test_for_middleware__1.php diff --git a/config/somake.php b/config/somake.php index 9c1f294..f8674d3 100644 --- a/config/somake.php +++ b/config/somake.php @@ -21,6 +21,7 @@ 'test_generators' => [ Soyhuce\Somake\Domains\Test\UnitTestGenerators\FormRequestTestGenerator::class, Soyhuce\Somake\Domains\Test\UnitTestGenerators\JsonResourceTestGenerator::class, + Soyhuce\Somake\Domains\Test\UnitTestGenerators\MiddlewareTestGenerator::class, Soyhuce\Somake\Domains\Test\UnitTestGenerators\DefaultTestGenerator::class, ], ]; diff --git a/resources/views/test-unit-middleware.blade.php b/resources/views/test-unit-middleware.blade.php new file mode 100644 index 0000000..99d782a --- /dev/null +++ b/resources/views/test-unit-middleware.blade.php @@ -0,0 +1,12 @@ +/* @@covers \{{ $covered }} */ + +use {{ $classFqcn }}; +use Illuminate\Support\Facades\Route; + +it('handles the request', function(): void { + Route::get('test', fn() => 'ok') + ->middleware({{ $classBasename }}::class); + + $this->get('test') + ->assertOk(); +}); diff --git a/src/Domains/Test/UnitTestGenerators/MiddlewareTestGenerator.php b/src/Domains/Test/UnitTestGenerators/MiddlewareTestGenerator.php new file mode 100644 index 0000000..f6a2d92 --- /dev/null +++ b/src/Domains/Test/UnitTestGenerators/MiddlewareTestGenerator.php @@ -0,0 +1,53 @@ + + */ +class MiddlewareTestGenerator implements UnitTestGenerator +{ + public static function shouldHandle(string $class): bool + { + if (!class_exists($class)) { + return false; + } + + $reflectionClass = new ReflectionClass($class); + if (!$reflectionClass->hasMethod('handle')) { + return false; + } + + $parameters = $reflectionClass->getMethod('handle')->getParameters(); + if (count($parameters) < 2) { + return false; + } + + if (!$parameters[0]->getType() instanceof ReflectionNamedType || $parameters[0]->getType()->getName() !== Request::class) { + return false; + } + + if (!$parameters[1]->getType() instanceof ReflectionNamedType || $parameters[1]->getType()->getName() !== Closure::class) { + return false; + } + + return true; + } + + public function view(): string + { + return 'test-unit-middleware'; + } + + public function data(string $class): array + { + return []; + } +} diff --git a/test-laravel/tests/Unit/Support/Http/Middleware/PreventRequestsDuringMaintenanceTest.php b/test-laravel/tests/Unit/Support/Http/Middleware/PreventRequestsDuringMaintenanceTest.php new file mode 100644 index 0000000..1334ac8 --- /dev/null +++ b/test-laravel/tests/Unit/Support/Http/Middleware/PreventRequestsDuringMaintenanceTest.php @@ -0,0 +1,6 @@ +toBeFile() ->toMatchFileSnapshot(); }); + +it('creates correctly the unit test for middleware', 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 ?', 'RedirectIfAuthenticated') + ->expectsOutput('The Tests\\Unit\\Support\\Http\\Middleware\\RedirectIfAuthenticatedTest class was successfully created !') + ->assertExitCode(0) + ->execute(); + + expect($this->app->basePath('tests/Unit/Support/Http/Middleware/RedirectIfAuthenticatedTest.php')) + ->toBeFile() + ->toMatchFileSnapshot(); +}); diff --git a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_unit_test_for_middleware__1.php b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_unit_test_for_middleware__1.php new file mode 100644 index 0000000..d1f1e4d --- /dev/null +++ b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_unit_test_for_middleware__1.php @@ -0,0 +1,14 @@ + 'ok') + ->middleware(RedirectIfAuthenticated::class); + + $this->get('test') + ->assertOk(); +});