Skip to content

Commit

Permalink
Add middleware test generation
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi committed Apr 28, 2023
1 parent 35a734f commit 80c1616
Show file tree
Hide file tree
Showing 6 changed files with 99 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 @@ -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,
],
];
12 changes: 12 additions & 0 deletions resources/views/test-unit-middleware.blade.php
Original file line number Diff line number Diff line change
@@ -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();
});
53 changes: 53 additions & 0 deletions src/Domains/Test/UnitTestGenerators/MiddlewareTestGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types=1);

namespace Soyhuce\Somake\Domains\Test\UnitTestGenerators;

use Closure;
use Illuminate\Http\Request;
use ReflectionClass;
use ReflectionNamedType;
use Soyhuce\Somake\Contracts\UnitTestGenerator;
use function count;

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

/* @covers \Support\Http\Middleware\PreventRequestsDuringMaintenance */

it('is successful', function (): void {
});
13 changes: 13 additions & 0 deletions tests/Feature/TestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,16 @@
->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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/* @covers \Support\Http\Middleware\RedirectIfAuthenticated */

use Support\Http\Middleware\RedirectIfAuthenticated;
use Illuminate\Support\Facades\Route;

it('handles the request', function(): void {
Route::get('test', fn() => 'ok')
->middleware(RedirectIfAuthenticated::class);

$this->get('test')
->assertOk();
});

0 comments on commit 80c1616

Please sign in to comment.