Browse Source

Add api resource route generator on routes/web.php

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
51298db28f
  1. 27
      src/CrudMake.php
  2. 2
      src/stubs/route-web.stub
  3. 1
      tests/CrudMakeCommandTest.php
  4. 22
      tests/Generators/RouteWebGeneratorTest.php
  5. 1
      tests/TestCase.php

27
src/CrudMake.php

@ -43,6 +43,7 @@ class CrudMake extends Command
public function handle()
{
$this->getModelName();
$this->generateResourceRoute();
$this->generateModel();
$this->generateMigration();
@ -130,6 +131,13 @@ class CrudMake extends Command
$this->info($this->modelName.'Test (model) generated.');
}
public function generateResourceRoute()
{
$webRoutePath = $this->makeRouteFile(base_path('routes'), 'web.php');
$this->files->append($webRoutePath, $this->getWebRouteContent());
$this->info($this->modelName.' resource route generated on routes/web.php.');
}
public function getControllerContent()
{
$stub = $this->files->get(__DIR__.'/stubs/controller.model.stub');
@ -178,6 +186,12 @@ class CrudMake extends Command
return $this->replaceUnitTestDummyStrings($stub)->replaceClass($stub);
}
public function getWebRouteContent()
{
$stub = $this->files->get(__DIR__.'/stubs/route-web.stub');
return $this->replaceViewDummyStrings($stub)->replaceClass($stub);
}
protected function makeDirectory($path)
{
if (! $this->files->isDirectory($path)) {
@ -187,6 +201,19 @@ class CrudMake extends Command
return $path;
}
protected function makeRouteFile($routeDirPath, $filename)
{
if (! $this->files->isDirectory($routeDirPath)) {
$this->files->makeDirectory($routeDirPath, 0777, true, true);
}
if (! $this->files->exists($routeDirPath.'/'.$filename)) {
$this->files->put($routeDirPath.'/'.$filename, "<?php\n");
}
return $routeDirPath.'/'.$filename;
}
protected function replaceControllerDummyStrings(&$stub)
{
$stub = str_replace(

2
src/stubs/route-web.stub

@ -0,0 +1,2 @@
Route::apiResource('masters', 'MastersController';

1
tests/CrudMakeCommandTest.php

@ -19,6 +19,7 @@ class CrudMakeCommandTest extends TestCase
$this->assertFileExists(resource_path("views/{$this->tableName}/forms.blade.php"));
$this->assertFileExists(resource_path("lang/en/{$this->singleModelName}.php"));
$this->assertFileExists(database_path("factories/{$this->modelName}Factory.php"));
$this->assertFileExists(base_path("routes/web.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$this->pluralModelName}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$this->modelName}Test.php"));
}

22
tests/Generators/RouteWebGeneratorTest.php

@ -0,0 +1,22 @@
<?php
namespace Tests\Generators;
use Tests\TestCase;
class RouteWebGeneratorTest extends TestCase
{
/** @test */
public function it_creates_correct_web_route_content()
{
$this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]);
$routeWebPath = base_path('routes/web.php');
$this->assertFileExists($routeWebPath);
$routeWebFileContent = "<?php
Route::apiResource('items', 'ItemsController';
";
$this->assertEquals($routeWebFileContent, file_get_contents($routeWebPath));
}
}

1
tests/TestCase.php

@ -27,6 +27,7 @@ abstract class TestCase extends BaseTestCase
exec('rm -r '.app_path('Http'));
exec('rm '.database_path('migrations/*'));
exec('rm -r '.resource_path('views/'.$this->tableName));
exec('rm -r '.base_path('routes'));
exec('rm -r '.base_path('tests/Feature'));
exec('rm -r '.base_path('tests/Unit'));

Loading…
Cancel
Save