Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Oct 8, 2023
1 parent d2ea00c commit ad70478
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
4 changes: 1 addition & 3 deletions src/Concerns/TestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('\\', '/'),
Expand Down
56 changes: 47 additions & 9 deletions tests/Commands/GeneratorCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class GeneratorCommandTest extends TestCase

protected $files = [
'app/Value/Foo.php',
'tests/Feature/Value/FooTest.php',
];

/** @test */
Expand All @@ -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'), '<?php '.PHP_EOL);
$this->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 */
Expand All @@ -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'), '<?php '.PHP_EOL);

$this->artisan('make:code', ['name' => 'Value/Foo'])
->expectsOutputToContain('class [app/Value/Foo.php] already exists!')
->assertFailed();
}
}
3 changes: 3 additions & 0 deletions workbench/app/Console/CodeGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit ad70478

Please sign in to comment.