Browse Source

Apply base_test_path and base_test_class config on test class generators

tags/0.1.1
Nafies Luthfi 8 years ago
parent
commit
52bccfde61
  1. 27
      src/Generators/FeatureTestGenerator.php
  2. 10
      src/Generators/ModelPolicyTestGenerator.php
  3. 10
      src/Generators/ModelTestGenerator.php
  4. 144
      tests/Generators/FeatureTestGeneratorTest.php
  5. 60
      tests/Generators/ModelPolicyTestGeneratorTest.php
  6. 38
      tests/Generators/ModelTestGeneratorTest.php
  7. 14
      tests/TestCase.php

27
src/Generators/FeatureTestGenerator.php

@ -3,8 +3,8 @@
namespace Luthfi\CrudGenerator\Generators;
/**
* Feature Test Generator Class
*/
* Feature Test Generator Class
*/
class FeatureTestGenerator extends BaseGenerator
{
/**
@ -16,7 +16,7 @@ class FeatureTestGenerator extends BaseGenerator
$featureTestPath = $this->makeDirectory(base_path('tests/Feature'));
$this->generateFile("{$featureTestPath}/Manage{$this->modelNames['plural_model_name']}Test.php", $this->getContent());
$this->command->info('Manage'.$this->modelNames['plural_model_name'].'Test generated.');
$this->command->info('Manage' . $this->modelNames['plural_model_name'] . 'Test generated.');
}
/**
@ -24,7 +24,9 @@ class FeatureTestGenerator extends BaseGenerator
*/
protected function getContent()
{
$stub = $this->files->get(__DIR__.'/../stubs/test-feature.stub');
$stub = $this->files->get(__DIR__ . '/../stubs/test-feature.stub');
$baseTestClass = config('simple-crud.base_test_class');
$stub = str_replace('use Tests\BrowserKitTest', 'use ' . $baseTestClass, $stub);
return $this->replaceStubString($stub);
}
@ -36,12 +38,21 @@ class FeatureTestGenerator extends BaseGenerator
private function createBrowserKitBaseTestClass()
{
$testsPath = base_path('tests');
if (! $this->files->isDirectory($testsPath)) {
if (!$this->files->isDirectory($testsPath)) {
$this->files->makeDirectory($testsPath, 0777, true, true);
}
if (! $this->files->exists($testsPath.'/BrowserKitTest.php')) {
$this->generateFile($testsPath.'/BrowserKitTest.php', $this->getBrowserKitBaseTestContent());
$baseTestPath = base_path(config('simple-crud.base_test_path'));
$baseTestClass = class_basename(config('simple-crud.base_test_class'));
if (!$this->files->exists($baseTestPath)) {
$browserKitTestClassContent = str_replace(
'class BrowserKitTest extends',
"class {$baseTestClass} extends",
$this->getBrowserKitBaseTestContent()
);
$this->generateFile($baseTestPath, $browserKitTestClassContent);
$this->command->info('BrowserKitTest generated.');
}
@ -54,6 +65,6 @@ class FeatureTestGenerator extends BaseGenerator
*/
public function getBrowserKitBaseTestContent()
{
return $this->files->get(__DIR__.'/../stubs/test-browserkit-base-class.stub');
return $this->files->get(__DIR__ . '/../stubs/test-browserkit-base-class.stub');
}
}

10
src/Generators/ModelPolicyTestGenerator.php

@ -3,8 +3,8 @@
namespace Luthfi\CrudGenerator\Generators;
/**
* Model Test Generator Class
*/
* Model Test Generator Class
*/
class ModelPolicyTestGenerator extends BaseGenerator
{
/**
@ -14,7 +14,7 @@ class ModelPolicyTestGenerator extends BaseGenerator
{
$modelPolicyTestPath = $this->makeDirectory(base_path('tests/Unit/Policies'));
$this->generateFile("{$modelPolicyTestPath}/{$this->modelNames['model_name']}PolicyTest.php", $this->getContent());
$this->command->info($this->modelNames['model_name'].'PolicyTest (model policy) generated.');
$this->command->info($this->modelNames['model_name'] . 'PolicyTest (model policy) generated.');
}
/**
@ -22,7 +22,9 @@ class ModelPolicyTestGenerator extends BaseGenerator
*/
protected function getContent()
{
$stub = $this->files->get(__DIR__.'/../stubs/test-policy.stub');
$stub = $this->files->get(__DIR__ . '/../stubs/test-policy.stub');
$baseTestClass = config('simple-crud.base_test_class');
$stub = str_replace('use Tests\BrowserKitTest', 'use ' . $baseTestClass, $stub);
return $this->replaceStubString($stub);
}
}

10
src/Generators/ModelTestGenerator.php

@ -3,8 +3,8 @@
namespace Luthfi\CrudGenerator\Generators;
/**
* Model Test Generator Class
*/
* Model Test Generator Class
*/
class ModelTestGenerator extends BaseGenerator
{
/**
@ -14,7 +14,7 @@ class ModelTestGenerator extends BaseGenerator
{
$unitTestPath = $this->makeDirectory(base_path('tests/Unit/Models'));
$this->generateFile("{$unitTestPath}/{$this->modelNames['model_name']}Test.php", $this->getContent());
$this->command->info($this->modelNames['model_name'].'Test (model) generated.');
$this->command->info($this->modelNames['model_name'] . 'Test (model) generated.');
}
/**
@ -22,7 +22,9 @@ class ModelTestGenerator extends BaseGenerator
*/
protected function getContent()
{
$stub = $this->files->get(__DIR__.'/../stubs/test-unit.stub');
$stub = $this->files->get(__DIR__ . '/../stubs/test-unit.stub');
$baseTestClass = config('simple-crud.base_test_class');
$stub = str_replace('use Tests\BrowserKitTest', 'use ' . $baseTestClass, $stub);
return $this->replaceStubString($stub);
}
}

144
tests/Generators/FeatureTestGeneratorTest.php

@ -140,4 +140,148 @@ class Manage{$this->plural_model_name}Test extends TestCase
";
$this->assertEquals($modelClassContent, file_get_contents(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php")));
}
/** @test */
public function it_generates_base_test_class_based_on_config_file()
{
config(['simple-crud.base_test_path' => 'tests/TestCase.php']);
config(['simple-crud.base_test_class' => 'Tests\TestCase']);
$baseTestPath = base_path('tests/TestCase.php');
$baseTestClass = 'TestCase';
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
$this->assertFileExists($baseTestPath);
$browserKitTestClassContent = "<?php
namespace Tests;
use App\User;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
abstract class {$baseTestClass} extends BaseTestCase
{
use CreatesApplication;
protected \$baseUrl = 'http://localhost';
protected function setUp()
{
parent::setUp();
\Hash::setRounds(5);
}
protected function loginAsUser()
{
\$user = factory(User::class)->create();
\$this->actingAs(\$user);
return \$user;
}
}
";
$this->assertEquals($browserKitTestClassContent, file_get_contents($baseTestPath));
}
/** @test */
public function it_creates_correct_feature_test_class_with_base_test_class_based_on_config_file()
{
config(['simple-crud.base_test_path' => 'tests/TestCase.php']);
config(['simple-crud.base_test_class' => 'Tests\TestCase']);
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
$this->assertFileExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php"));
$modelClassContent = "<?php
namespace Tests\Feature;
use {$this->full_model_name};
use Tests\TestCase as TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class Manage{$this->plural_model_name}Test extends TestCase
{
use DatabaseMigrations;
/** @test */
public function user_can_see_{$this->lang_name}_list_in_{$this->lang_name}_index_page()
{
\${$this->single_model_var_name}1 = factory({$this->model_name}::class)->create(['name' => 'Testing name', 'description' => 'Testing 123']);
\${$this->single_model_var_name}2 = factory({$this->model_name}::class)->create(['name' => 'Testing name', 'description' => 'Testing 456']);
\$this->loginAsUser();
\$this->visit(route('{$this->table_name}.index'));
\$this->see(\${$this->single_model_var_name}1->name);
\$this->see(\${$this->single_model_var_name}2->name);
}
/** @test */
public function user_can_create_a_{$this->lang_name}()
{
\$this->loginAsUser();
\$this->visit(route('{$this->table_name}.index'));
\$this->click(trans('{$this->lang_name}.create'));
\$this->seePageIs(route('{$this->table_name}.index', ['action' => 'create']));
\$this->type('{$this->model_name} 1 name', 'name');
\$this->type('{$this->model_name} 1 description', 'description');
\$this->press(trans('{$this->lang_name}.create'));
\$this->seePageIs(route('{$this->table_name}.index'));
\$this->seeInDatabase('{$this->table_name}', [
'name' => '{$this->model_name} 1 name',
'description' => '{$this->model_name} 1 description',
]);
}
/** @test */
public function user_can_edit_a_{$this->lang_name}_within_search_query()
{
\$this->loginAsUser();
\${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']);
\$this->visit(route('{$this->table_name}.index', ['q' => '123']));
\$this->click('edit-{$this->single_model_var_name}-'.\${$this->single_model_var_name}->id);
\$this->seePageIs(route('{$this->table_name}.index', ['action' => 'edit', 'id' => \${$this->single_model_var_name}->id, 'q' => '123']));
\$this->type('{$this->model_name} 1 name', 'name');
\$this->type('{$this->model_name} 1 description', 'description');
\$this->press(trans('{$this->lang_name}.update'));
\$this->seePageIs(route('{$this->table_name}.index', ['q' => '123']));
\$this->seeInDatabase('{$this->table_name}', [
'name' => '{$this->model_name} 1 name',
'description' => '{$this->model_name} 1 description',
]);
}
/** @test */
public function user_can_delete_a_{$this->lang_name}()
{
\$this->loginAsUser();
\${$this->single_model_var_name} = factory({$this->model_name}::class)->create();
\$this->visit(route('{$this->table_name}.index', [\${$this->single_model_var_name}->id]));
\$this->click('del-{$this->single_model_var_name}-'.\${$this->single_model_var_name}->id);
\$this->seePageIs(route('{$this->table_name}.index', ['action' => 'delete', 'id' => \${$this->single_model_var_name}->id]));
\$this->seeInDatabase('{$this->table_name}', [
'id' => \${$this->single_model_var_name}->id,
]);
\$this->press(trans('app.delete_confirm_button'));
\$this->dontSeeInDatabase('{$this->table_name}', [
'id' => \${$this->single_model_var_name}->id,
]);
}
}
";
$this->assertEquals($modelClassContent, file_get_contents(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php")));
}
}

60
tests/Generators/ModelPolicyTestGeneratorTest.php

@ -62,4 +62,64 @@ class {$this->model_name}Test extends TestCase
";
$this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath));
}
/** @test */
public function it_creates_correct_model_policy_test_class_with_base_test_class_based_on_config_file()
{
config(['simple-crud.base_test_path' => 'tests/TestCase.php']);
config(['simple-crud.base_test_class' => 'Tests\TestCase']);
$userModel = config('auth.providers.users.model');
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
$modelPolicyPath = base_path("tests/Unit/Policies/{$this->model_name}PolicyTest.php");
$this->assertFileExists($modelPolicyPath);
$modelPolicyContent = "<?php
namespace Tests\Unit\Policies;
use {$this->full_model_name};
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase as TestCase;
class {$this->model_name}Test extends TestCase
{
use DatabaseMigrations;
/** @test */
public function user_can_create_{$this->lang_name}()
{
\$user = \$this->loginAsUser();
\$this->assertTrue(\$user->can('create', new {$this->model_name}));
}
/** @test */
public function user_can_view_{$this->lang_name}()
{
\$user = \$this->loginAsUser();
\${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => '{$this->model_name} 1 name']);
\$this->assertTrue(\$user->can('view', \${$this->single_model_var_name}));
}
/** @test */
public function user_can_update_{$this->lang_name}()
{
\$user = \$this->loginAsUser();
\${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => '{$this->model_name} 1 name']);
\$this->assertTrue(\$user->can('update', \${$this->single_model_var_name}));
}
/** @test */
public function user_can_delete_{$this->lang_name}()
{
\$user = \$this->loginAsUser();
\${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => '{$this->model_name} 1 name']);
\$this->assertTrue(\$user->can('delete', \${$this->single_model_var_name}));
}
}
";
$this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath));
}
}

38
tests/Generators/ModelTestGeneratorTest.php

@ -11,7 +11,8 @@ class ModelTestGeneratorTest extends TestCase
{
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
$this->assertFileExists(base_path("tests/Unit/Models/{$this->model_name}Test.php"));
$uniTestPath = base_path("tests/Unit/Models/{$this->model_name}Test.php");
$this->assertFileExists($uniTestPath);
$modelClassContent = "<?php
namespace Tests\Unit\Models;
@ -32,6 +33,39 @@ class {$this->model_name}Test extends TestCase
}
}
";
$this->assertEquals($modelClassContent, file_get_contents(base_path("tests/Unit/Models/{$this->model_name}Test.php")));
$this->assertEquals($modelClassContent, file_get_contents($uniTestPath));
}
/** @test */
public function it_creates_correct_unit_test_class_with_base_test_class_based_on_config_file()
{
config(['simple-crud.base_test_path' => 'tests/TestCase.php']);
config(['simple-crud.base_test_class' => 'Tests\TestCase']);
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
$uniTestPath = base_path("tests/Unit/Models/{$this->model_name}Test.php");
$this->assertFileExists($uniTestPath);
$modelClassContent = "<?php
namespace Tests\Unit\Models;
use {$this->full_model_name};
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase as TestCase;
class {$this->model_name}Test extends TestCase
{
use DatabaseMigrations;
/** @test */
public function it_has_name_attribute()
{
\${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => '{$this->model_name} 1 name']);
\$this->assertEquals('{$this->model_name} 1 name', \${$this->single_model_var_name}->name);
}
}
";
$this->assertEquals($modelClassContent, file_get_contents($uniTestPath));
}
}

14
tests/TestCase.php

@ -19,7 +19,7 @@ abstract class TestCase extends BaseTestCase
parent::setUp();
$this->model_name = class_basename('References/Category');
$this->full_model_name = 'App\\'.$this->model_name;
$this->full_model_name = 'App\\' . $this->model_name;
$this->plural_model_name = str_plural($this->model_name);
$this->table_name = snake_case($this->plural_model_name);
$this->lang_name = snake_case($this->model_name);
@ -36,30 +36,28 @@ abstract class TestCase extends BaseTestCase
protected function cleanUpGeneratedFiles()
{
$this->removeFileOrDir(app_path($this->model_name.'.php'));
$this->removeFileOrDir(app_path($this->model_name . '.php'));
$this->removeFileOrDir(app_path('Entities'));
$this->removeFileOrDir(app_path('Http'));
$this->removeFileOrDir(database_path('migrations'));
$this->removeFileOrDir(database_path('factories'));
$this->removeFileOrDir(resource_path('views/'.$this->table_name));
$this->removeFileOrDir(resource_path('views/' . $this->table_name));
$this->removeFileOrDir(resource_path("lang/en/app.php"));
$this->removeFileOrDir(resource_path("lang/en/{$this->lang_name}.php"));
$this->removeFileOrDir(base_path('routes'));
$this->removeFileOrDir(app_path('Policies'));
$this->removeFileOrDir(app_path('Providers'));
$this->removeFileOrDir(base_path('tests/BrowserKitTest.php'));
$this->removeFileOrDir(base_path('tests/Feature'));
$this->removeFileOrDir(base_path('tests/Unit'));
$this->removeFileOrDir(base_path('tests'));
}
protected function removeFileOrDir($path)
{
if (file_exists($path) && is_file($path)) {
exec('rm '.$path);
exec('rm ' . $path);
}
if (file_exists($path) && is_dir($path)) {
exec('rm -r '.$path);
exec('rm -r ' . $path);
}
}

Loading…
Cancel
Save