Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generate nova test cases #83

Merged
merged 6 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Generators/AbstractTestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,17 @@ protected function createDump(): void
mkdir_recursively($fixturePath);
}

file_put_contents($this->getFixturesPath('dump.sql'), $content);
$dumpName = $this->getDumpName();

file_put_contents($this->getFixturesPath("{$dumpName}.sql"), $content);

event(new SuccessCreateMessage("Created a new Test dump on path: "
. "{$this->paths['tests']}/fixtures/{$this->getTestClassName()}/dump.sql"));
. "{$this->paths['tests']}/fixtures/{$this->getTestClassName()}/{$dumpName}.sql"));
}

protected function getDumpName(): string
{
return 'dump';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return 'dump';
return 'dump.sql';

}

protected function getInserts(): array
Expand Down
9 changes: 8 additions & 1 deletion src/Generators/NovaTestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function generateTests(): void
$filters = $this->collectFilters();

$fileContent = $this->getStub('nova_test', [
'url_path' => $this->getPluralName(Str::kebab($this->model)),
'url_path' => Str::kebab($this->model) . '-resources',
'entity' => $this->model,
'entities' => $this->getPluralName($this->model),
'lower_entity' => Str::snake($this->model),
Expand Down Expand Up @@ -171,4 +171,11 @@ protected function getFilters(): array

return $filters;
}

protected function getDumpName(): string
{
$modelName = Str::snake($this->model);

return "nova_{$modelName}_dump";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return "nova_{$modelName}_dump";
return "nova_{$modelName}_dump.sql";

}
}
38 changes: 17 additions & 21 deletions stubs/nova_test.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@

use App\Models\{{$entity}};
use RonasIT\Support\Tests\ModelTestState;
use RonasIT\Support\Traits\AuthTestTrait;
@if($shouldUseStatus)
use Symfony\Component\HttpFoundation\Response;
@endif

class Nova{{$entity}}Test extends TestCase
{
use AuthTestTrait;

protected static $user;
protected static ${{$lower_entity}}State;
protected static User $user;
protected static ModelTestState ${{$lower_entity}}State;

public function setUp(): void
{
parent::setUp();

self::$user = 1;
self::$user = User::find(1);
self::${{$lower_entity}}State ??= new ModelTestState({{$entity}}::class);

$this->skipDocumentationCollecting();
Expand All @@ -29,7 +26,7 @@ public function testCreate(): void
{
$data = $this->getJsonFixture('create_{{$lower_entity}}_request.json');

$response = $this->actingViaSession(self::$user)->json('post', '/nova-api/{{$url_path}}', $data);
$response = $this->actingAs(self::$user, 'web')->json('post', '/nova-api/{{$url_path}}', $data);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_CREATED);
Expand Down Expand Up @@ -60,7 +57,7 @@ public function testCreateNoAuth(): void

public function testCreateValidationError(): void
{
$response = $this->actingViaSession(self::$user)->json('post', '/nova-api/{{$url_path}}');
$response = $this->actingAs(self::$user, 'web')->json('post', '/nova-api/{{$url_path}}');

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
Expand All @@ -78,7 +75,7 @@ public function testUpdate(): void
{
$data = $this->getJsonFixture('update_{{$lower_entity}}_request.json');

$response = $this->actingViaSession(self::$user)->json('put', '/nova-api/{{$url_path}}/1', $data);
$response = $this->actingAs(self::$user, 'web')->json('put', '/nova-api/{{$url_path}}/1', $data);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_NO_CONTENT);
Expand All @@ -94,7 +91,7 @@ public function testUpdateNotExists(): void
{
$data = $this->getJsonFixture('update_{{$lower_entity}}_request.json');

$response = $this->actingViaSession(self::$user)->json('put', '/nova-api/{{$url_path}}/0', $data);
$response = $this->actingAs(self::$user, 'web')->json('put', '/nova-api/{{$url_path}}/0', $data);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_NOT_FOUND);
Expand All @@ -118,7 +115,7 @@ public function testUpdateNoAuth(): void

public function testUpdateValidationError(): void
{
$response = $this->actingViaSession(self::$user)->json('put', '/nova-api/{{$url_path}}/4');
$response = $this->actingAs(self::$user, 'web')->json('put', '/nova-api/{{$url_path}}/4');

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
Expand All @@ -132,7 +129,7 @@ public function testUpdateValidationError(): void

public function testGetUpdatableFields(): void
{
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/{{$url_path}}/1/update-fields');
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/{{$url_path}}/1/update-fields');

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_OK);
Expand All @@ -146,7 +143,7 @@ public function testGetUpdatableFields(): void

public function testDelete(): void
{
$response = $this->actingViaSession(self::$user)->json('delete', '/nova-api/{{$url_path}}', [
$response = $this->actingAs(self::$user, 'web')->json('delete', '/nova-api/{{$url_path}}', [
'resources' => [1, 2]
]);

Expand All @@ -162,7 +159,7 @@ public function testDelete(): void

public function testDeleteNotExists(): void
{
$response = $this->actingViaSession(self::$user)->json('delete', '/nova-api/{{$url_path}}', [
$response = $this->actingAs(self::$user, 'web')->json('delete', '/nova-api/{{$url_path}}', [
'resources' => [0]
]);

Expand All @@ -188,7 +185,7 @@ public function testDeleteNoAuth(): void

public function testGet(): void
{
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/{{$url_path}}/1');
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/{{$url_path}}/1');

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_OK);
Expand All @@ -202,7 +199,7 @@ public function testGet(): void

public function testGetNotExists(): void
{
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/{{$url_path}}/0');
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/{{$url_path}}/0');

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_NOT_FOUND);
Expand Down Expand Up @@ -238,7 +235,7 @@ public function testSearchUnauthorized(): void

public function testGetFieldsVisibleOnCreate(): void
{
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/{{$url_path}}/creation-fields');
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/{{$url_path}}/creation-fields');

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_OK);
Expand Down Expand Up @@ -270,8 +267,7 @@ public function getRun{{$entity}}ActionsData(): array
*/
public function testRun{{$entity}}Actions($action, $request, ${{$lower_entities}}StateFixture): void
{
$request['action'] = $action;
$response = $this->actingViaSession(self::$user)->json('post', "/nova-api/{{$url_path}}/action", $request);
$response = $this->actingAs(self::$user, 'web')->json('post', "/nova-api/{{$url_path}}/action?action={$action}", $request);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_OK);
Expand Down Expand Up @@ -304,7 +300,7 @@ public function get{{$entity}}ActionsData(): array
*/
public function testGet{{$entity}}Actions(array $request, string $responseFixture): void
{
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/{{$url_path}}/actions', $request);
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/{{$url_path}}/actions', $request);

@if($shouldUseStatus)
$response->assertStatus(Response::HTTP_OK);
Expand Down Expand Up @@ -335,7 +331,7 @@ public function get{{$entity}}FiltersData(): array
*/
public function testFilter{{$entity}}(array $filters, string $responseFixture): void
{
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/{{$url_path}}', [
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/{{$url_path}}', [
'filters' => base64_encode(json_encode($filters))
]);

Expand Down
2 changes: 1 addition & 1 deletion tests/NovaTestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function testCreateWithActions()
$this->rollbackToDefaultBasePath();

$this->assertGeneratedFileEquals('created_resource_test.php', 'tests/NovaPostTest.php');
$this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/NovaPostTest/dump.sql');
$this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/NovaPostTest/nova_post_dump.sql');
$this->assertGeneratedFileEquals('create_post_request.json', 'tests/fixtures/NovaPostTest/create_post_request.json');
$this->assertGeneratedFileEquals('create_post_response.json', 'tests/fixtures/NovaPostTest/create_post_response.json');
$this->assertGeneratedFileEquals('update_post_request.json', 'tests/fixtures/NovaPostTest/update_post_request.json');
Expand Down
48 changes: 22 additions & 26 deletions tests/fixtures/NovaTestGeneratorTest/created_resource_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@

use App\Models\Post;
use RonasIT\Support\Tests\ModelTestState;
use RonasIT\Support\Traits\AuthTestTrait;

class NovaPostTest extends TestCase
{
use AuthTestTrait;

protected static $user;
protected static $postState;
protected static User $user;
protected static ModelTestState $postState;

public function setUp(): void
{
parent::setUp();

self::$user = 1;
self::$user = User::find(1);
self::$postState ??= new ModelTestState(Post::class);

$this->skipDocumentationCollecting();
Expand All @@ -27,7 +24,7 @@ public function testCreate(): void
{
$data = $this->getJsonFixture('create_post_request.json');

$response = $this->actingViaSession(self::$user)->json('post', '/nova-api/posts', $data);
$response = $this->actingAs(self::$user, 'web')->json('post', '/nova-api/post-resources', $data);

$response->assertCreated();

Expand All @@ -41,7 +38,7 @@ public function testCreateNoAuth(): void
{
$data = $this->getJsonFixture('create_post_request.json');

$response = $this->json('post', '/nova-api/posts', $data);
$response = $this->json('post', '/nova-api/post-resources', $data);

$response->assertUnauthorized();

Expand All @@ -50,7 +47,7 @@ public function testCreateNoAuth(): void

public function testCreateValidationError(): void
{
$response = $this->actingViaSession(self::$user)->json('post', '/nova-api/posts');
$response = $this->actingAs(self::$user, 'web')->json('post', '/nova-api/post-resources');

$response->assertUnprocessable();

Expand All @@ -64,7 +61,7 @@ public function testUpdate(): void
{
$data = $this->getJsonFixture('update_post_request.json');

$response = $this->actingViaSession(self::$user)->json('put', '/nova-api/posts/1', $data);
$response = $this->actingAs(self::$user, 'web')->json('put', '/nova-api/post-resources/1', $data);

$response->assertNoContent();

Expand All @@ -76,7 +73,7 @@ public function testUpdateNotExists(): void
{
$data = $this->getJsonFixture('update_post_request.json');

$response = $this->actingViaSession(self::$user)->json('put', '/nova-api/posts/0', $data);
$response = $this->actingAs(self::$user, 'web')->json('put', '/nova-api/post-resources/0', $data);

$response->assertNotFound();
}
Expand All @@ -85,14 +82,14 @@ public function testUpdateNoAuth(): void
{
$data = $this->getJsonFixture('update_post_request.json');

$response = $this->json('put', '/nova-api/posts/1', $data);
$response = $this->json('put', '/nova-api/post-resources/1', $data);

$response->assertUnauthorized();
}

public function testUpdateValidationError(): void
{
$response = $this->actingViaSession(self::$user)->json('put', '/nova-api/posts/4');
$response = $this->actingAs(self::$user, 'web')->json('put', '/nova-api/post-resources/4');

$response->assertUnprocessable();

Expand All @@ -102,7 +99,7 @@ public function testUpdateValidationError(): void

public function testGetUpdatableFields(): void
{
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/posts/1/update-fields');
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/post-resources/1/update-fields');

$response->assertOk();

Expand All @@ -112,7 +109,7 @@ public function testGetUpdatableFields(): void

public function testDelete(): void
{
$response = $this->actingViaSession(self::$user)->json('delete', '/nova-api/posts', [
$response = $this->actingAs(self::$user, 'web')->json('delete', '/nova-api/post-resources', [
'resources' => [1, 2]
]);

Expand All @@ -124,7 +121,7 @@ public function testDelete(): void

public function testDeleteNotExists(): void
{
$response = $this->actingViaSession(self::$user)->json('delete', '/nova-api/posts', [
$response = $this->actingAs(self::$user, 'web')->json('delete', '/nova-api/post-resources', [
'resources' => [0]
]);

Expand All @@ -133,7 +130,7 @@ public function testDeleteNotExists(): void

public function testDeleteNoAuth(): void
{
$response = $this->json('delete', '/nova-api/posts', [
$response = $this->json('delete', '/nova-api/post-resources', [
'resources' => [1, 2]
]);

Expand All @@ -142,7 +139,7 @@ public function testDeleteNoAuth(): void

public function testGet(): void
{
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/posts/1');
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/post-resources/1');

$response->assertOk();

Expand All @@ -152,21 +149,21 @@ public function testGet(): void

public function testGetNotExists(): void
{
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/posts/0');
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/post-resources/0');

$response->assertNotFound();
}

public function testGetNoAuth(): void
{
$response = $this->json('get', '/nova-api/posts/1');
$response = $this->json('get', '/nova-api/post-resources/1');

$response->assertUnauthorized();
}

public function testSearchUnauthorized(): void
{
$response = $this->json('get', '/nova-api/posts', [
$response = $this->json('get', '/nova-api/post-resources', [
'orderBy' => 'id',
'orderByDirection' => 'asc'
]);
Expand All @@ -176,7 +173,7 @@ public function testSearchUnauthorized(): void

public function testGetFieldsVisibleOnCreate(): void
{
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/posts/creation-fields');
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/post-resources/creation-fields');

$response->assertOk();

Expand Down Expand Up @@ -209,8 +206,7 @@ public function getRunPostActionsData(): array
*/
public function testRunPostActions($action, $request, $postsStateFixture): void
{
$request['action'] = $action;
$response = $this->actingViaSession(self::$user)->json('post', "/nova-api/posts/action", $request);
$response = $this->actingAs(self::$user, 'web')->json('post', "/nova-api/post-resources/action?action={$action}", $request);

$response->assertOk();

Expand Down Expand Up @@ -243,7 +239,7 @@ public function getPostActionsData(): array
*/
public function testGetPostActions(array $request, string $responseFixture): void
{
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/posts/actions', $request);
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/post-resources/actions', $request);

$response->assertOk();

Expand Down Expand Up @@ -274,7 +270,7 @@ public function getPostFiltersData(): array
*/
public function testFilterPost(array $filters, string $responseFixture): void
{
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/posts', [
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/post-resources', [
'filters' => base64_encode(json_encode($filters))
]);

Expand Down
Loading