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. 15
      src/Generators/FeatureTestGenerator.php
  2. 2
      src/Generators/ModelPolicyTestGenerator.php
  3. 2
      src/Generators/ModelTestGenerator.php
  4. 144
      tests/Generators/FeatureTestGeneratorTest.php
  5. 60
      tests/Generators/ModelPolicyTestGeneratorTest.php
  6. 38
      tests/Generators/ModelTestGeneratorTest.php
  7. 4
      tests/TestCase.php

15
src/Generators/FeatureTestGenerator.php

@ -25,6 +25,8 @@ class FeatureTestGenerator extends BaseGenerator
protected function getContent()
{
$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);
}
@ -40,8 +42,17 @@ class FeatureTestGenerator extends BaseGenerator
$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.');
}

2
src/Generators/ModelPolicyTestGenerator.php

@ -23,6 +23,8 @@ class ModelPolicyTestGenerator extends BaseGenerator
protected function getContent()
{
$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);
}
}

2
src/Generators/ModelTestGenerator.php

@ -23,6 +23,8 @@ class ModelTestGenerator extends BaseGenerator
protected function getContent()
{
$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));
}
}

4
tests/TestCase.php

@ -47,9 +47,7 @@ abstract class TestCase extends BaseTestCase
$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)

Loading…
Cancel
Save