-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace {{baseNamespace}}\{{moduleName}}\Tests; | ||
|
||
use {{userModelClassName}}; | ||
use {{baseNamespace}}\{{moduleName}}\Models\{{modelName}}; | ||
use {{baseNamespace}}\{{moduleName}}\Factories\{{modelName}}Factory; | ||
use {{baseNamespace}}\{{moduleName}}\Seeders\{{modelName}}PermissionSeeder; | ||
use Database\Factories\UserFactory; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Testing\Fluent\AssertableJson; | ||
use {{roleModelClassName}}; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Tests\TestCase; | ||
|
||
class {{testClassName}} extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function admin(): User | ||
{ | ||
(new {{modelName}}PermissionSeeder)->run(); | ||
|
||
$defaultGuardName = config('crudgenerator.default_guard_name'); | ||
$roleName = config('crudgenerator.default_role_name'); | ||
$role = Role::query()->firstOrCreate(['name' => $roleName], ['guard_name' => $defaultGuardName]); | ||
|
||
return UserFactory::new()->create()->assignRole($role); | ||
} | ||
|
||
public function testItLists{{modelNamePlural}}(): void | ||
{ | ||
$user = $this->admin(); | ||
{{modelName}}Factory::new()->count(20)->create(); | ||
|
||
$this | ||
->actingAs($user) | ||
->getJson('{{variableNamePlural}}?per_page=10') | ||
->assertStatus(Response::HTTP_OK) | ||
->assertJsonPath('data', function ($data) { | ||
$this->assertCount(10, $data); | ||
|
||
return true; | ||
}); | ||
} | ||
|
||
public function testItCreatesA{{modelName}}(): void | ||
{ | ||
$user = $this->admin(); | ||
$payload = []; | ||
|
||
$this | ||
->actingAs($user) | ||
->postJson('{{variableNamePlural}}', $payload) | ||
->assertStatus(Response::HTTP_CREATED) | ||
->assertJson(function (AssertableJson $json) use ($payload) { | ||
$json->where('data.id', 1); | ||
// $json->where('data.field', $payload['field']); | ||
}); | ||
|
||
$this->assertDatabaseHas({{modelName}}::class, $payload); | ||
} | ||
|
||
public function testItShowsA{{modelName}}(): void | ||
{ | ||
$user = $this->admin(); | ||
${{variableName}} = {{modelName}}Factory::new()->create(); | ||
|
||
$this | ||
->actingAs($user) | ||
->getJson("{{variableNamePlural}}/{${{variableName}}->id}") | ||
->assertStatus(Response::HTTP_OK) | ||
->assertJson(function (AssertableJson $json) use (${{variableName}}) { | ||
$json->where('data.id', ${{variableName}}->id); | ||
}); | ||
} | ||
|
||
public function testItUpdatesA{{modelName}}(): void | ||
{ | ||
$user = $this->admin(); | ||
${{variableName}} = {{modelName}}Factory::new()->create(); | ||
|
||
$payload = []; | ||
|
||
$this | ||
->actingAs($user) | ||
->patchJson("{{variableNamePlural}}/{${{variableName}}->id}", $payload) | ||
->assertStatus(Response::HTTP_OK) | ||
->assertJson(function (AssertableJson $json) use ($payload) { | ||
$json->where('data.id', 1); | ||
// $json->where('data.field', $payload['field']); | ||
}); | ||
|
||
$this->assertDatabaseHas({{modelName}}::class, $payload); | ||
} | ||
|
||
public function testItDeletesA{{modelName}}(): void | ||
{ | ||
$user = $this->admin(); | ||
${{variableName}} = {{modelName}}Factory::new()->create(); | ||
|
||
$this | ||
->actingAs($user) | ||
->deleteJson("{{variableNamePlural}}/{${{variableName}}->id}") | ||
->assertStatus(Response::HTTP_NO_CONTENT); | ||
|
||
$this->assertDatabaseEmpty({{modelName}}::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Axyr\CrudGenerator\Generators; | ||
|
||
class ControllerTestGenerator extends AbstractGenerator | ||
{ | ||
public function path(): string | ||
{ | ||
$parts = array_filter([$this->basePath(), $this->module(), $this->testDirectory(), $this->directory(), $this->className()]); | ||
|
||
return sprintf('%s%s', implode('/', $parts), $this->fileSuffix()); | ||
} | ||
|
||
public function directory(): string | ||
{ | ||
return 'Http/Controllers'; | ||
} | ||
|
||
public function className(): string | ||
{ | ||
return sprintf('%sControllerTest', $this->name()); | ||
} | ||
|
||
public function replacements(): array | ||
{ | ||
return array_merge(parent::replacements(), [ | ||
'{{testClassName}}' => $this->className(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Axyr\CrudGenerator\Tests\Generators; | ||
|
||
use Axyr\CrudGenerator\Generators\ControllerTestGenerator; | ||
|
||
class ControllerTestGeneratorTest extends GeneratorTestAbstract | ||
{ | ||
public function generatorClassName(): string | ||
{ | ||
return ControllerTestGenerator::class; | ||
} | ||
|
||
public static function dataGenerator(): array | ||
{ | ||
return [ | ||
[ | ||
'name' => 'Post', | ||
'module' => 'Posts', | ||
'expectedPath' => 'app-modules/Posts/tests/Http/Controllers/PostControllerTest.php', | ||
'expectedStrings' => [ | ||
'class PostControllerTest extends TestCase', | ||
'use App\Modules\Posts\Factories\PostFactory;', | ||
'use App\Modules\Posts\Models\Post;', | ||
'use App\Modules\Posts\Seeders\PostPermissionSeeder;', | ||
'use Spatie\Permission\Models\Role;', | ||
'(new PostPermissionSeeder)->run();', | ||
'function testItListsPosts', | ||
'function testItCreatesAPost', | ||
'function testItShowsAPost', | ||
'function testItUpdatesAPost', | ||
'function testItDeletesAPost', | ||
'$post = PostFactory::new()->create();', | ||
'->getJson(\'posts?per_page=10\')', | ||
'"posts/{$post->id}"', | ||
], | ||
], | ||
]; | ||
} | ||
} |