From ad7047837c7317275e6b751a6df0cfec8bb72b0f Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sun, 8 Oct 2023 20:37:03 +0800 Subject: [PATCH] wip Signed-off-by: Mior Muhammad Zaki --- src/Concerns/TestGenerator.php | 4 +- tests/Commands/GeneratorCommandTest.php | 56 ++++++++++++++++--- .../app/Console/CodeGeneratorCommand.php | 3 + 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/Concerns/TestGenerator.php b/src/Concerns/TestGenerator.php index da7a3e0..0acbb72 100644 --- a/src/Concerns/TestGenerator.php +++ b/src/Concerns/TestGenerator.php @@ -17,9 +17,7 @@ protected function handleTestCreationUsingCanvas(string $path): bool return false; } - $sourcePath = \in_array(CreatesUsingGeneratorPreset::class, class_uses_recursive($this)) - ? $this->generatorPreset()->sourcePath() - : $this->laravel['path']; + $sourcePath = $this->generatorPreset()->sourcePath(); return $this->call('make:test', array_merge([ 'name' => Str::of($path)->after($sourcePath)->beforeLast('.php')->append('Test')->replace('\\', '/'), diff --git a/tests/Commands/GeneratorCommandTest.php b/tests/Commands/GeneratorCommandTest.php index 4c71938..0fcaa64 100644 --- a/tests/Commands/GeneratorCommandTest.php +++ b/tests/Commands/GeneratorCommandTest.php @@ -13,6 +13,7 @@ class GeneratorCommandTest extends TestCase protected $files = [ 'app/Value/Foo.php', + 'tests/Feature/Value/FooTest.php', ]; /** @test */ @@ -28,21 +29,40 @@ public function it_can_generate_class_file() } /** @test */ - public function it_cannot_generate_class_file_given_reserved_name() + public function it_can_generate_class_file_with_phpunit_test() { - $this->artisan('make:code', ['name' => '__halt_compiler']) - ->expectsOutputToContain('The name "__halt_compiler" is reserved by PHP.') - ->assertFailed(); + $this->artisan('make:code', ['name' => 'Value/Foo', '--test' => true]) + ->assertSuccessful(); + + $this->assertFileContains([ + 'namespace App\Value;', + 'class Foo', + ], 'app/Value/Foo.php'); + + $this->assertFileContains([ + 'namespace Tests\Feature\Value;', + 'use Tests\TestCase;', + 'class FooTest extends TestCase', + ], 'tests/Feature/Value/FooTest.php'); } + /** @test */ - public function it_cannot_generate_class_file_when_file_already_exist() + public function it_can_generate_class_file_with_pest_test() { - file_put_contents(base_path('app/Value/Foo.php'), 'artisan('make:code', ['name' => 'Value/Foo', '--pest' => true]) + ->assertSuccessful(); - $this->artisan('make:code', ['name' => 'Value/Foo']) - ->expectsOutputToContain('class [app/Value/Foo.php] already exists!') - ->assertFailed(); + $this->assertFileContains([ + 'namespace App\Value;', + 'class Foo', + ], 'app/Value/Foo.php'); + + $this->assertFileContains([ + 'test(\'example\', function () {', + '$response = $this->get(\'/\');', + '$response->assertStatus(200);', + ], 'tests/Feature/Value/FooTest.php'); } /** @test */ @@ -58,4 +78,22 @@ public function it_can_generate_class_file_when_file_already_exist_using_force_o 'class Foo', ], 'app/Value/Foo.php'); } + + /** @test */ + public function it_cannot_generate_class_file_given_reserved_name() + { + $this->artisan('make:code', ['name' => '__halt_compiler']) + ->expectsOutputToContain('The name "__halt_compiler" is reserved by PHP.') + ->assertFailed(); + } + + /** @test */ + public function it_cannot_generate_class_file_when_file_already_exist() + { + file_put_contents(base_path('app/Value/Foo.php'), 'artisan('make:code', ['name' => 'Value/Foo']) + ->expectsOutputToContain('class [app/Value/Foo.php] already exists!') + ->assertFailed(); + } } diff --git a/workbench/app/Console/CodeGeneratorCommand.php b/workbench/app/Console/CodeGeneratorCommand.php index 762bdd6..ef51dde 100644 --- a/workbench/app/Console/CodeGeneratorCommand.php +++ b/workbench/app/Console/CodeGeneratorCommand.php @@ -2,11 +2,14 @@ namespace Workbench\App\Console; +use Illuminate\Console\Concerns\CreatesMatchingTest; use Orchestra\Canvas\Core\Commands\GeneratorCommand; use Symfony\Component\Console\Input\InputOption; class CodeGeneratorCommand extends GeneratorCommand { + use CreatesMatchingTest; + /** * The name of the console command. *