Browse Source

Make sure same base test case do not use alias

tags/1.2.0
Nafies Luthfi 7 years ago
parent
commit
1683201aef
  1. 2
      src/Generators/FeatureTestGenerator.php
  2. 2
      src/Generators/ModelPolicyTestGenerator.php
  3. 1
      src/Generators/ModelTestGenerator.php
  4. 2
      tests/Generators/Api/ApiFeatureTestGeneratorTest.php
  5. 151
      tests/Generators/FeatureTestGeneratorTest.php
  6. 62
      tests/Generators/ModelPolicyTestGeneratorTest.php
  7. 53
      tests/Generators/ModelTestGeneratorTest.php
  8. 2
      tests/Generators/Simple/FeatureTestGeneratorTest.php

2
src/Generators/FeatureTestGenerator.php

@ -38,6 +38,8 @@ class FeatureTestGenerator extends BaseGenerator
$stub = $this->getStubFileContent($stubName);
$baseTestClass = config('simple-crud.base_test_class');
$stub = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $stub);
$stub = str_replace('use Tests\TestCase as TestCase', 'use Tests\TestCase', $stub);
return $this->replaceStubString($stub);
}

2
src/Generators/ModelPolicyTestGenerator.php

@ -30,6 +30,8 @@ class ModelPolicyTestGenerator extends BaseGenerator
$stub = $this->getStubFileContent($stubName);
$baseTestClass = config('simple-crud.base_test_class');
$stub = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $stub);
$stub = str_replace('use Tests\TestCase as TestCase', 'use Tests\TestCase', $stub);
return $this->replaceStubString($stub);
}
}

1
src/Generators/ModelTestGenerator.php

@ -31,6 +31,7 @@ class ModelTestGenerator extends BaseGenerator
$baseTestClass = config('simple-crud.base_test_class');
$modelFileContent = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $modelFileContent);
$modelFileContent = str_replace('use Tests\TestCase as TestCase', 'use Tests\TestCase', $modelFileContent);
$userModel = config('auth.providers.users.model');

2
tests/Generators/Api/ApiFeatureTestGeneratorTest.php

@ -141,7 +141,7 @@ class Manage{$this->model_name}Test extends TestCase
namespace Tests\Feature\Api;
use {$this->full_model_name};
use Tests\TestCase as TestCase;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class Manage{$this->model_name}Test extends TestCase

151
tests/Generators/FeatureTestGeneratorTest.php

@ -245,6 +245,155 @@ abstract class {$baseTestClass} extends BaseTestCase
/** @test */
public function it_creates_correct_feature_test_class_with_base_test_class_based_on_config_file()
{
config(['simple-crud.base_test_path' => 'tests/MyTestCase.php']);
config(['simple-crud.base_test_class' => 'Tests\MyTestCase']);
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
$this->assertFileExists(base_path("tests/Feature/Manage{$this->model_name}Test.php"));
$modelClassContent = "<?php
namespace Tests\Feature;
use {$this->full_model_name};
use Tests\MyTestCase as TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class Manage{$this->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} = factory({$this->model_name}::class)->create();
\$this->loginAsUser();
\$this->visitRoute('{$this->table_name}.index');
\$this->see(\${$this->single_model_var_name}->name);
}
private function getCreateFields(array \$overrides = [])
{
return array_merge([
'name' => '{$this->model_name} 1 name',
'description' => '{$this->model_name} 1 description',
], \$overrides);
}
/** @test */
public function user_can_create_a_{$this->lang_name}()
{
\$this->loginAsUser();
\$this->visitRoute('{$this->table_name}.index');
\$this->click(__('{$this->lang_name}.create'));
\$this->seeRouteIs('{$this->table_name}.create');
\$this->submitForm(__('{$this->lang_name}.create'), \$this->getCreateFields());
\$this->seeRouteIs('{$this->table_name}.show', {$this->model_name}::first());
\$this->seeInDatabase('{$this->table_name}', \$this->getCreateFields());
}
/** @test */
public function create_{$this->lang_name}_action_must_pass_validations()
{
\$this->loginAsUser();
// Name empty
\$this->post(route('{$this->table_name}.store'), \$this->getCreateFields(['name' => '']));
\$this->assertSessionHasErrors('name');
// Name 70 characters
\$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([
'name' => str_repeat('Test Title', 7),
]));
\$this->assertSessionHasErrors('name');
// Description 256 characters
\$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([
'description' => str_repeat('Long description', 16),
]));
\$this->assertSessionHasErrors('description');
}
private function getEditFields(array \$overrides = [])
{
return array_merge([
'name' => '{$this->model_name} 1 name',
'description' => '{$this->model_name} 1 description',
], \$overrides);
}
/** @test */
public function user_can_edit_a_{$this->lang_name}()
{
\$this->loginAsUser();
\${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']);
\$this->visitRoute('{$this->table_name}.show', \${$this->single_model_var_name});
\$this->click('edit-{$this->lang_name}-'.\${$this->single_model_var_name}->id);
\$this->seeRouteIs('{$this->table_name}.edit', \${$this->single_model_var_name});
\$this->submitForm(__('{$this->lang_name}.update'), \$this->getEditFields());
\$this->seeRouteIs('{$this->table_name}.show', \${$this->single_model_var_name});
\$this->seeInDatabase('{$this->table_name}', \$this->getEditFields([
'id' => \${$this->single_model_var_name}->id,
]));
}
/** @test */
public function edit_{$this->lang_name}_action_must_pass_validations()
{
\$this->loginAsUser();
\${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']);
// Name empty
\$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields(['name' => '']));
\$this->assertSessionHasErrors('name');
// Name 70 characters
\$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields([
'name' => str_repeat('Test Title', 7),
]));
\$this->assertSessionHasErrors('name');
// Description 256 characters
\$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields([
'description' => str_repeat('Long description', 16),
]));
\$this->assertSessionHasErrors('description');
}
/** @test */
public function user_can_delete_a_{$this->lang_name}()
{
\$this->loginAsUser();
\${$this->single_model_var_name} = factory({$this->model_name}::class)->create();
factory({$this->model_name}::class)->create();
\$this->visitRoute('{$this->table_name}.edit', \${$this->single_model_var_name});
\$this->click('del-{$this->lang_name}-'.\${$this->single_model_var_name}->id);
\$this->seeRouteIs('{$this->table_name}.edit', [\${$this->single_model_var_name}, 'action' => 'delete']);
\$this->press(__('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->model_name}Test.php")));
}
/** @test */
public function same_base_test_case_class_name_dont_use_alias()
{
config(['simple-crud.base_test_path' => 'tests/TestCase.php']);
config(['simple-crud.base_test_class' => 'Tests\TestCase']);
@ -256,7 +405,7 @@ abstract class {$baseTestClass} extends BaseTestCase
namespace Tests\Feature;
use {$this->full_model_name};
use Tests\TestCase as TestCase;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class Manage{$this->model_name}Test extends TestCase

62
tests/Generators/ModelPolicyTestGeneratorTest.php

@ -66,6 +66,66 @@ class {$this->model_name}PolicyTest extends TestCase
/** @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/MyTestCase.php']);
config(['simple-crud.base_test_class' => 'Tests\MyTestCase']);
$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\MyTestCase as TestCase;
class {$this->model_name}PolicyTest extends TestCase
{
use DatabaseMigrations;
/** @test */
public function user_can_create_{$this->lang_name}()
{
\$user = \$this->createUser();
\$this->assertTrue(\$user->can('create', new {$this->model_name}));
}
/** @test */
public function user_can_view_{$this->lang_name}()
{
\$user = \$this->createUser();
\${$this->single_model_var_name} = factory({$this->model_name}::class)->create();
\$this->assertTrue(\$user->can('view', \${$this->single_model_var_name}));
}
/** @test */
public function user_can_update_{$this->lang_name}()
{
\$user = \$this->createUser();
\${$this->single_model_var_name} = factory({$this->model_name}::class)->create();
\$this->assertTrue(\$user->can('update', \${$this->single_model_var_name}));
}
/** @test */
public function user_can_delete_{$this->lang_name}()
{
\$user = \$this->createUser();
\${$this->single_model_var_name} = factory({$this->model_name}::class)->create();
\$this->assertTrue(\$user->can('delete', \${$this->single_model_var_name}));
}
}
";
$this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath));
}
/** @test */
public function same_base_test_case_class_name_dont_use_alias()
{
config(['simple-crud.base_test_path' => 'tests/TestCase.php']);
config(['simple-crud.base_test_class' => 'Tests\TestCase']);
@ -82,7 +142,7 @@ namespace Tests\Unit\Policies;
use {$this->full_model_name};
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase as TestCase;
use Tests\TestCase;
class {$this->model_name}PolicyTest extends TestCase
{

53
tests/Generators/ModelTestGeneratorTest.php

@ -57,6 +57,57 @@ class {$this->model_name}Test extends TestCase
/** @test */
public function it_creates_correct_unit_test_class_with_base_test_class_based_on_config_file()
{
config(['simple-crud.base_test_path' => 'tests/MyTestCase.php']);
config(['simple-crud.base_test_class' => 'Tests\MyTestCase']);
$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 App\User;
use {$this->full_model_name};
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\MyTestCase as TestCase;
class {$this->model_name}Test extends TestCase
{
use DatabaseMigrations;
/** @test */
public function a_{$this->lang_name}_has_name_link_attribute()
{
\${$this->single_model_var_name} = factory({$this->model_name}::class)->create();
\$this->assertEquals(
link_to_route('{$this->table_name}.show', \${$this->single_model_var_name}->name, [\${$this->single_model_var_name}], [
'title' => __(
'app.show_detail_title',
['name' => \${$this->single_model_var_name}->name, 'type' => __('{$this->lang_name}.{$this->lang_name}')]
),
]), \${$this->single_model_var_name}->name_link
);
}
/** @test */
public function a_{$this->lang_name}_has_belongs_to_creator_relation()
{
\${$this->single_model_var_name} = factory({$this->model_name}::class)->make();
\$this->assertInstanceOf(User::class, \${$this->single_model_var_name}->creator);
\$this->assertEquals(\${$this->single_model_var_name}->creator_id, \${$this->single_model_var_name}->creator->id);
}
}
";
$this->assertEquals($modelClassContent, file_get_contents($uniTestPath));
}
/** @test */
public function same_base_test_case_class_name_dont_use_alias()
{
config(['simple-crud.base_test_path' => 'tests/TestCase.php']);
config(['simple-crud.base_test_class' => 'Tests\TestCase']);
@ -71,7 +122,7 @@ namespace Tests\Unit\Models;
use App\User;
use {$this->full_model_name};
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase as TestCase;
use Tests\TestCase;
class {$this->model_name}Test extends TestCase
{

2
tests/Generators/Simple/FeatureTestGeneratorTest.php

@ -209,7 +209,7 @@ abstract class {$baseTestClass} extends BaseTestCase
namespace Tests\Feature;
use {$this->full_model_name};
use Tests\TestCase as TestCase;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class Manage{$this->model_name}Test extends TestCase

Loading…
Cancel
Save