diff --git a/config/somake.php b/config/somake.php index 07ac4e1..4d873e3 100644 --- a/config/somake.php +++ b/config/somake.php @@ -17,8 +17,5 @@ 'policy' => null, 'request' => Illuminate\Foundation\Http\FormRequest::class, 'resource' => Illuminate\Http\Resources\Json\JsonResource::class, - 'test_contract' => Tests\TestCase::class, - 'test_feature' => Tests\TestCase::class, - 'test_unit' => PHPUnit\Framework\TestCase::class, ], ]; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 7485fb2..c024045 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,10 +1,5 @@ parameters: ignoreErrors: - - - message: "#^Class Tests\\\\TestCase not found\\.$#" - count: 2 - path: config/somake.php - - message: "#^Method Soyhuce\\\\Somake\\\\Commands\\\\BuilderCommand\\:\\:askModel\\(\\) should return class\\-string\\ but returns array\\|bool\\|string\\.$#" count: 1 diff --git a/resources/views/test-contract.blade.php b/resources/views/test-contract.blade.php deleted file mode 100644 index 08aa4a6..0000000 --- a/resources/views/test-contract.blade.php +++ /dev/null @@ -1,19 +0,0 @@ -namespace {{ $namespace }}; - -use {{ $baseClass }}; - -/** - * @@coversDefaultClass \{{ $coveredClass }} - */ -class {{ $className }} extends {{ $baseClassName }} -{ - /** - * @@test - * @@covers ::{{ $coveredMethod }} - */ - public function success(): void - { - $this->{{ $verb }}Json("{{ $url }}") - ->assertValidContract(200); - } -} diff --git a/resources/views/test-feature.blade.php b/resources/views/test-feature.blade.php deleted file mode 100644 index 017ad77..0000000 --- a/resources/views/test-feature.blade.php +++ /dev/null @@ -1,19 +0,0 @@ -namespace {{ $namespace }}; - -use {{ $baseClass }}; - -/** - * @@coversDefaultClass \{{ $coveredClass }} - */ -class {{ $className }} extends {{ $baseClassName }} -{ - /** - * @@test - * @@covers ::{{ $coveredMethod }} - */ - public function simple(): void - { - $this->{{ $verb }}Json("{{ $url }}") - ->assertOk(); - } -} diff --git a/resources/views/test-unit.blade.php b/resources/views/test-unit.blade.php deleted file mode 100644 index 6b2fbd4..0000000 --- a/resources/views/test-unit.blade.php +++ /dev/null @@ -1,17 +0,0 @@ -namespace {{ $namespace }}; - -use {{ $baseClass }}; - -/** - * @@coversDefaultClass \{{ $coveredClass }} - */ -class {{ $className }} extends {{ $baseClassName }} -{ - /** - * @@test - * @@covers ::defineMe - */ - public function simple(): void - { - } -} diff --git a/src/Commands/TestCommand.php b/src/Commands/TestCommand.php index 9e1edce..f54530a 100644 --- a/src/Commands/TestCommand.php +++ b/src/Commands/TestCommand.php @@ -114,10 +114,6 @@ protected function askType(): string private function stubFile(string $type): string { - if (is_file(base_path('tests/Pest.php'))) { - return "pest-{$type}"; - } - - return "test-{$type}"; + return "pest-{$type}"; } } diff --git a/src/Domains/Request/RouteGuesser.php b/src/Domains/Request/RouteGuesser.php index dfc9bfd..50efc34 100644 --- a/src/Domains/Request/RouteGuesser.php +++ b/src/Domains/Request/RouteGuesser.php @@ -13,7 +13,9 @@ class RouteGuesser public function __construct(string $controller, string $method) { - $this->route = RouteFacade::getRoutes()->getByAction("{$controller}@{$method}"); + $action = $method === '__invoke' ? $controller : $controller . '@' . $method; + + $this->route = RouteFacade::getRoutes()->getByAction($action); } public function verb(): string diff --git a/test-laravel/app/App/Website/Videos/Controllers/InvokableVideoController.php b/test-laravel/app/App/Website/Videos/Controllers/InvokableVideoController.php new file mode 100644 index 0000000..fbf5dfc --- /dev/null +++ b/test-laravel/app/App/Website/Videos/Controllers/InvokableVideoController.php @@ -0,0 +1,13 @@ +noContent(); + } +} diff --git a/test-laravel/routes/api.php b/test-laravel/routes/api.php index 15c047c..bd366cf 100644 --- a/test-laravel/routes/api.php +++ b/test-laravel/routes/api.php @@ -1,5 +1,6 @@ user(); }); -Route::put('videos/{video}', [VideoController::class, 'update']); +Route::get('videos/{video}', [VideoController::class, 'show']); +Route::put('videos-invokable/{video}', InvokableVideoController::class); diff --git a/tests/Feature/TestCommandTest.php b/tests/Feature/TestCommandTest.php index 3aca22c..800d507 100644 --- a/tests/Feature/TestCommandTest.php +++ b/tests/Feature/TestCommandTest.php @@ -1,27 +1,12 @@ artisan('somake:test') - ->expectsQuestion('Which kind of test do you want to create ?', 'Contract') - ->expectsQuestion('Which controller do you want to cover ?', 'VideoController') - ->expectsQuestion('Which method do you want to cover ?', 'update') - ->expectsQuestion('What is the Test name ?', 'ContractUpdateVideoTest') - ->expectsOutput('The Tests\\Contract\\Website\\Videos\\Video\\ContractUpdateVideoTest class was successfully created !') - ->assertExitCode(0) - ->execute(); - - expect($this->app->basePath('tests/Contract/Website/Videos/Video/ContractUpdateVideoTest.php')) - ->toBeFile() - ->toMatchFileSnapshot(); -}); - -it('creates correctly the contract test with custom base class', function (): void { - config()->set('somake.base_classes.test_contract', 'Tests\\ContractTestCase'); + file_put_contents(base_path('tests/Pest.php'), ''); $this->artisan('somake:test') ->expectsQuestion('Which kind of test do you want to create ?', 'Contract') ->expectsQuestion('Which controller do you want to cover ?', 'VideoController') - ->expectsQuestion('Which method do you want to cover ?', null) + ->expectsQuestion('Which method do you want to cover ?', 'show') ->expectsQuestion('What is the Test name ?', 'ContractShowVideoTest') ->expectsOutput('The Tests\\Contract\\Website\\Videos\\Video\\ContractShowVideoTest class was successfully created !') ->assertExitCode(0) @@ -32,45 +17,13 @@ ->toMatchFileSnapshot(); }); -it('creates correctly the contract pest', function (): void { - file_put_contents(base_path('tests/Pest.php'), ''); - - $this->artisan('somake:test') - ->expectsQuestion('Which kind of test do you want to create ?', 'Contract') - ->expectsQuestion('Which controller do you want to cover ?', 'VideoController') - ->expectsQuestion('Which method do you want to cover ?', 'update') - ->expectsQuestion('What is the Test name ?', 'ContractUpdateVideoTest') - ->expectsOutput('The Tests\\Contract\\Website\\Videos\\Video\\ContractUpdateVideoTest class was successfully created !') - ->assertExitCode(0) - ->execute(); - - expect($this->app->basePath('tests/Contract/Website/Videos/Video/ContractUpdateVideoTest.php')) - ->toBeFile() - ->toMatchFileSnapshot(); -}); - it('creates correctly the feature test', function (): void { - $this->artisan('somake:test') - ->expectsQuestion('Which kind of test do you want to create ?', 'Feature') - ->expectsQuestion('Which controller do you want to cover ?', 'VideoController') - ->expectsQuestion('Which method do you want to cover ?', null) - ->expectsQuestion('What is the Test name ?', 'ShowVideoTest') - ->expectsOutput('The Tests\\Feature\\Website\\Videos\\VideoController\\ShowVideoTest class was successfully created !') - ->assertExitCode(0) - ->execute(); - - expect($this->app->basePath('tests/Feature/Website/Videos/VideoController/ShowVideoTest.php')) - ->toBeFile() - ->toMatchFileSnapshot(); -}); - -it('creates correctly the feature test with custom base class', function (): void { - config()->set('somake.base_classes.test_feature', 'Tests\\FeatureTestCase'); + file_put_contents(base_path('tests/Pest.php'), ''); $this->artisan('somake:test') ->expectsQuestion('Which kind of test do you want to create ?', 'Feature') ->expectsQuestion('Which controller do you want to cover ?', 'VideoController') - ->expectsQuestion('Which method do you want to cover ?', null) + ->expectsQuestion('Which method do you want to cover ?', 'show') ->expectsQuestion('What is the Test name ?', 'ShowVideoTest') ->expectsOutput('The Tests\\Feature\\Website\\Videos\\VideoController\\ShowVideoTest class was successfully created !') ->assertExitCode(0) @@ -96,52 +49,22 @@ ->toMatchFileSnapshot(); }); -it('creates correctly the feature pest', function (): void { - file_put_contents(base_path('tests/Pest.php'), ''); - +it('creates correctly the feature test with invokable controller', function (): void { $this->artisan('somake:test') ->expectsQuestion('Which kind of test do you want to create ?', 'Feature') - ->expectsQuestion('Which controller do you want to cover ?', 'VideoController') - ->expectsQuestion('Which method do you want to cover ?', null) - ->expectsQuestion('What is the Test name ?', 'ShowVideoTest') - ->expectsOutput('The Tests\\Feature\\Website\\Videos\\VideoController\\ShowVideoTest class was successfully created !') + ->expectsQuestion('Which controller do you want to cover ?', 'InvokableVideoController') + ->expectsQuestion('Which method do you want to cover ?', '__invoke') + ->expectsQuestion('What is the Test name ?', 'IndexVideosTest') + ->expectsOutput('The Tests\\Feature\\Website\\Videos\\InvokableVideoController\\IndexVideosTest class was successfully created !') ->assertExitCode(0) ->execute(); - expect($this->app->basePath('tests/Feature/Website/Videos/VideoController/ShowVideoTest.php')) + expect($this->app->basePath('tests/Feature/Website/Videos/InvokableVideoController/IndexVideosTest.php')) ->toBeFile() ->toMatchFileSnapshot(); }); it('creates correctly the unit test', 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 ?', 'User') - ->expectsOutput('The Tests\\Unit\\Domain\\User\\Models\\UserTest class was successfully created !') - ->assertExitCode(0) - ->execute(); - - expect($this->app->basePath('tests/Unit/Domain/User/Models/UserTest.php')) - ->toBeFile() - ->toMatchFileSnapshot(); -}); - -it('creates correctly the unit testwith custom base class', function (): void { - config()->set('somake.base_classes.test_unit', 'Tests\\UnitTestCase'); - - $this->artisan('somake:test') - ->expectsQuestion('Which kind of test do you want to create ?', 'Unit') - ->expectsQuestion('Which class do you want to cover ?', 'User') - ->expectsOutput('The Tests\\Unit\\Domain\\User\\Models\\UserTest class was successfully created !') - ->assertExitCode(0) - ->execute(); - - expect($this->app->basePath('tests/Unit/Domain/User/Models/UserTest.php')) - ->toBeFile() - ->toMatchFileSnapshot(); -}); - -it('creates correctly the unit pest', function (): void { file_put_contents(base_path('tests/Pest.php'), ''); $this->artisan('somake:test') diff --git a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_contract_pest__1.php b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_contract_pest__1.php deleted file mode 100644 index 607d449..0000000 --- a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_contract_pest__1.php +++ /dev/null @@ -1,8 +0,0 @@ -putJson("api/videos/{$video}") - ->assertValidContract(200); -}); diff --git a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_contract_test__1.php b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_contract_test__1.php index 190121c..c84d658 100644 --- a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_contract_test__1.php +++ b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_contract_test__1.php @@ -1,21 +1,8 @@ putJson("api/videos/{$video}") - ->assertValidContract(200); - } -} +it('respects success contract', function (): void { + $this->getJson("api/videos/{$video}") + ->assertValidContract(200); +}); diff --git a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_contract_test_with_custom_base_class__1.php b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_contract_test_with_custom_base_class__1.php deleted file mode 100644 index 635df4b..0000000 --- a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_contract_test_with_custom_base_class__1.php +++ /dev/null @@ -1,21 +0,0 @@ -getJson("/") - ->assertValidContract(200); - } -} diff --git a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_pest__1.php b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_pest__1.php deleted file mode 100644 index 02024c4..0000000 --- a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_pest__1.php +++ /dev/null @@ -1,8 +0,0 @@ -getJson("/") - ->assertOk(); -}); diff --git a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_test__1.php b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_test__1.php index 074e686..4f46604 100644 --- a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_test__1.php +++ b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_test__1.php @@ -1,21 +1,8 @@ getJson("/") - ->assertOk(); - } -} +it('is successful', function (): void { + $this->getJson("api/videos/{$video}") + ->assertOk(); +}); diff --git a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_test_with_custom_base_class__1.php b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_test_with_custom_base_class__1.php deleted file mode 100644 index 122cba4..0000000 --- a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_test_with_custom_base_class__1.php +++ /dev/null @@ -1,21 +0,0 @@ -getJson("/") - ->assertOk(); - } -} diff --git a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_test_with_invokable_controller__1.php b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_test_with_invokable_controller__1.php new file mode 100644 index 0000000..0c4dbd6 --- /dev/null +++ b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_test_with_invokable_controller__1.php @@ -0,0 +1,8 @@ +putJson("api/videos-invokable/{$video}") + ->assertOk(); +}); diff --git a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_test_with_unknown_method__1.php b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_test_with_unknown_method__1.php index 52e0af7..94be279 100644 --- a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_test_with_unknown_method__1.php +++ b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_feature_test_with_unknown_method__1.php @@ -1,21 +1,8 @@ getJson("/") - ->assertOk(); - } -} +it('is successful', function (): void { + $this->getJson("/") + ->assertOk(); +}); diff --git a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_unit_pest__1.php b/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_unit_pest__1.php deleted file mode 100644 index b2cba3d..0000000 --- a/tests/__snapshots__/files/TestCommandTest__it_creates_correctly_the_unit_pest__1.php +++ /dev/null @@ -1,6 +0,0 @@ -