Skip to content

Commit

Permalink
Add ControllerTestGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
axyr committed Aug 15, 2024
1 parent 3b046e3 commit 645b98f
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 0 deletions.
109 changes: 109 additions & 0 deletions resources/stubs/ControllerTest.stub
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);
}
}
6 changes: 6 additions & 0 deletions src/Generators/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public function name(): string
return $this->name;
}

public function namePlural(): string
{
return Str::plural($this->name());
}

public function basePath(): string
{
return config('crudgenerator.base_path');
Expand Down Expand Up @@ -149,6 +154,7 @@ public function replacements(): array
'{{testDirectory}}' => $this->testDirectory(),
'{{moduleName}}' => $this->module(),
'{{modelName}}' => $this->name(),
'{{modelNamePlural}}' => $this->namePlural(),
'{{variableName}}' => $this->variableName(),
'{{variableNamePlural}}' => $this->variableNamePlural(),
'{{userModelClassName}}' => $this->userModelClassName(),
Expand Down
2 changes: 2 additions & 0 deletions src/Generators/CombinedGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ public function generators(): array
RequestGenerator::class,
ResourceGenerator::class,
PermissionSeederGenerator::class,

ModuleServiceProviderGenerator::class,
ComposerGenerator::class,
RoutesGenerator::class,

ControllerAuthorizationTestGenerator::class,
ControllerTestGenerator::class,
];
}

Expand Down
30 changes: 30 additions & 0 deletions src/Generators/ControllerTestGenerator.php
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(),
]);
}
}
1 change: 1 addition & 0 deletions tests/Generators/CombinedGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function testGenerateAllFiles(): void
'app-modules/Posts/composer.json',
'app-modules/Posts/routes.php',
'app-modules/Posts/tests/Http/Controllers/CommentControllerAuthorizationTest.php',
'app-modules/Posts/tests/Http/Controllers/CommentControllerTest.php',
];

$this->assertEquals($expectedGeneratedFiles, $generator->generatedFiles());
Expand Down
40 changes: 40 additions & 0 deletions tests/Generators/ControllerTestGeneratorTest.php
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}"',
],
],
];
}
}

0 comments on commit 645b98f

Please sign in to comment.