From 14939c963cc5e46e8688114b160fb52ad60bf583 Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Wed, 23 Oct 2024 11:13:14 +0500 Subject: [PATCH 1/7] feat: modify nova test trait --- src/Traits/NovaTestTrait.php | 96 +++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 2 deletions(-) diff --git a/src/Traits/NovaTestTrait.php b/src/Traits/NovaTestTrait.php index 19d72ae..671bf63 100644 --- a/src/Traits/NovaTestTrait.php +++ b/src/Traits/NovaTestTrait.php @@ -1,10 +1,15 @@ $search, @@ -12,4 +17,91 @@ public function novaSearchParams(array $filters, string $search = '', int $perPa 'perPage' => $perPage, ]; } + + protected function novaCreateResource(string $resourceClass, ?array $data = []): TestResponse + { + $resourceUri = $this->getNovaResourceUri($resourceClass); + + return $this->postJson($resourceUri, $data); + } + + protected function novaUpdateResource(string $resourceClass, int $resourceId, ?array $data = []): TestResponse + { + $resourceUri = $this->getNovaResourceUri($resourceClass); + + return $this->putJson("{$resourceUri}/{$resourceId}", $data); + } + + protected function novaGetResource(string $resourceClass, int $resourceId, ?array $data = []): TestResponse + { + $resourceUri = $this->getNovaResourceUri($resourceClass); + + return $this->getJson("{$resourceUri}/{$resourceId}", $data); + } + + protected function novaSearchResource(string $resourceClass, ?array $request = []): TestResponse + { + $resourceUri = $this->getNovaResourceUri($resourceClass); + + return $this->getJson($resourceUri, $request); + } + + protected function novaGetCreationFields(string $resourceClass): TestResponse + { + $resourceUri = $this->getNovaResourceUri($resourceClass); + + return $this->getJson("{$resourceUri}/creation-fields"); + } + + protected function novaRunAction(string $resourceClass, string $action, ?array $request = []): TestResponse + { + $resourceUri = $this->getNovaResourceUri($resourceClass); + + $actionUri = app($action)->uriKey(); + + return $this->postJson("{$resourceUri}/action?action={$actionUri}", $request); + } + + protected function novaGetActions(string $resourceClass, array $resourceIds): TestResponse + { + $resourceUri = $this->getNovaResourceUri($resourceClass); + + $request = [ + 'resources' => implode(',', $resourceIds), + ]; + + return $this->json('get', "{$resourceUri}/actions", $request); + } + + protected function novaDeleteResource(string $resourceClass, array $resourceIds): TestResponse + { + $resourceUri = $this->getNovaResourceUri($resourceClass); + + $request = [ + 'resources' => $resourceIds, + ]; + + return $this->deleteJson($resourceUri, $request); + } + + protected function novaGetUpdatableFields(string $resourceClass, int $resourceId): TestResponse + { + $resourceUri = $this->getNovaResourceUri($resourceClass); + + return $this->getJson("{$resourceUri}/{$resourceId}/update-fields"); + } + + protected function novaActingAs(?Authenticatable $user = null): TestCase|self + { + return (empty($user)) ? $this : $this->actingAs($user, 'web'); + } + + protected function getNovaResourceUri(string $modelClass): string + { + $modelName = Str::afterLast($modelClass, '\\'); + + $modelName = Str::kebab($modelName); + + return "/nova-api/{$modelName}-resources"; + } } From 7b219b283ccd3f51c6f888cd71f00f9536fbc87b Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Wed, 23 Oct 2024 11:28:05 +0500 Subject: [PATCH 2/7] fix: nova test trait namespace --- src/Traits/NovaTestTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/NovaTestTrait.php b/src/Traits/NovaTestTrait.php index 671bf63..f700602 100644 --- a/src/Traits/NovaTestTrait.php +++ b/src/Traits/NovaTestTrait.php @@ -1,6 +1,6 @@ Date: Wed, 23 Oct 2024 12:14:46 +0500 Subject: [PATCH 3/7] chore: move nova test trait to Tests --- src/{Traits => Tests}/NovaTestTrait.php | 2 +- tests/NovaTestTraitTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) rename src/{Traits => Tests}/NovaTestTrait.php (98%) diff --git a/src/Traits/NovaTestTrait.php b/src/Tests/NovaTestTrait.php similarity index 98% rename from src/Traits/NovaTestTrait.php rename to src/Tests/NovaTestTrait.php index f700602..d20720c 100644 --- a/src/Traits/NovaTestTrait.php +++ b/src/Tests/NovaTestTrait.php @@ -1,6 +1,6 @@ Date: Wed, 23 Oct 2024 15:43:30 +0500 Subject: [PATCH 4/7] feat: cover by tests NovaTestTrait --- src/Tests/NovaTestTrait.php | 8 +- tests/NovaTestTraitTest.php | 155 ++++++++++++++++++++++++++ tests/support/Mock/TestNovaAction.php | 11 ++ 3 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 tests/support/Mock/TestNovaAction.php diff --git a/src/Tests/NovaTestTrait.php b/src/Tests/NovaTestTrait.php index d20720c..f4217ca 100644 --- a/src/Tests/NovaTestTrait.php +++ b/src/Tests/NovaTestTrait.php @@ -43,7 +43,7 @@ protected function novaSearchResource(string $resourceClass, ?array $request = [ { $resourceUri = $this->getNovaResourceUri($resourceClass); - return $this->getJson($resourceUri, $request); + return $this->json('get', $resourceUri, $request); } protected function novaGetCreationFields(string $resourceClass): TestResponse @@ -59,7 +59,7 @@ protected function novaRunAction(string $resourceClass, string $action, ?array $ $actionUri = app($action)->uriKey(); - return $this->postJson("{$resourceUri}/action?action={$actionUri}", $request); + return $this->json('POST', "{$resourceUri}/action?action={$actionUri}", $request); } protected function novaGetActions(string $resourceClass, array $resourceIds): TestResponse @@ -93,7 +93,9 @@ protected function novaGetUpdatableFields(string $resourceClass, int $resourceId protected function novaActingAs(?Authenticatable $user = null): TestCase|self { - return (empty($user)) ? $this : $this->actingAs($user, 'web'); + return (empty($user)) + ? $this + : $this->actingAs($user, 'web'); } protected function getNovaResourceUri(string $modelClass): string diff --git a/tests/NovaTestTraitTest.php b/tests/NovaTestTraitTest.php index 0eade8d..2ba3dbe 100644 --- a/tests/NovaTestTraitTest.php +++ b/tests/NovaTestTraitTest.php @@ -2,7 +2,14 @@ namespace RonasIT\Support\Tests; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Route; +use RonasIT\Support\Tests\Support\Mock\MockAuthUser; +use RonasIT\Support\Tests\Support\Mock\TestModel; +use RonasIT\Support\Tests\Support\Mock\TestNovaAction; use RonasIT\Support\Traits\MockTrait; +use Symfony\Component\HttpFoundation\Response; class NovaTestTraitTest extends HelpersTestCase { @@ -21,4 +28,152 @@ public function testMockSingleCall() 'perPage' => 25, ]); } + + public function testGetNovaResourceUri() + { + $result = $this->getNovaResourceUri(TestModel::class); + + $this->assertEquals($result, '/nova-api/test-model-resources'); + } + + public function testNovaCreateResource() + { + Route::post('/nova-api/test-model-resources', function (Request $request) { + $request->validate([ + 'key' => 'required|string', + ]); + + return response('', Response::HTTP_CREATED); + }); + + $result = $this->novaCreateResource(TestModel::class, ['key' => 'value']); + + $result->assertCreated(); + } + + public function testNovaUpdateResource() + { + Route::put('/nova-api/test-model-resources/1', function (Request $request) { + $request->validate([ + 'key' => 'required|string', + ]); + + return response($request->all(), Response::HTTP_OK); + }); + + $result = $this->novaUpdateResource(TestModel::class, 1, ['key' => 'value']); + + $result->assertOk(); + } + + public function testNovaGetResource() + { + Route::get('/nova-api/test-model-resources/1', function () { + return response('', Response::HTTP_OK); + }); + + $result = $this->novaGetResource(TestModel::class, 1); + + $result->assertOk(); + } + + public function testNovaSearchResource() + { + Route::get('/nova-api/test-model-resources', function (Request $request) { + $request->validate([ + 'key' => 'required|string', + ]); + + return response($request->all(), Response::HTTP_OK); + }); + + $result = $this->novaSearchResource(TestModel::class, ['key' => 'value']); + + $result->assertOk(); + } + + public function testNovaGetCreationFields() + { + Route::get('/nova-api/test-model-resources/creation-fields', function (Request $request) { + return response('', Response::HTTP_OK); + }); + + $result = $this->novaGetCreationFields(TestModel::class); + + $result->assertOk(); + } + + public function testNovaGetActions() + { + Route::get('/nova-api/test-model-resources/actions', function (Request $request) { + $request->validate([ + 'resources' => 'required|string', + ]); + + return response($request->all(), Response::HTTP_OK); + }); + + $result = $this->novaGetActions(TestModel::class, [1, 2]); + + $result->assertOk(); + + $result->assertContent('{"resources":"1,2"}'); + } + + public function testNovaDeleteResource() + { + Route::delete('/nova-api/test-model-resources', function (Request $request) { + $request->validate([ + 'resources' => 'required|array', + ]); + + return response($request->all(), Response::HTTP_OK); + }); + + $result = $this->novaDeleteResource(TestModel::class, [1, 2]); + + $result->assertOk(); + + $result->assertContent('{"resources":[1,2]}'); + } + + public function testNovaGetUpdatableFields() + { + Route::get('/nova-api/test-model-resources/1/update-fields', function (Request $request) { + return response('', Response::HTTP_OK); + }); + + $result = $this->novaGetUpdatableFields(TestModel::class, 1); + + $result->assertOk(); + } + + public function testNovaRunAction() + { + Route::post('/nova-api/test-model-resources/action', function (Request $request) { + return response($request->all(), Response::HTTP_OK); + }); + + $result = $this->novaRunAction(TestModel::class, TestNovaAction::class, ['key' => 'value']); + + $result->assertOk(); + + $result->assertContent('{"key":"value","action":"test-nova-action"}'); + } + + public function testNovaActingAs() + { + $mockedUser = new MockAuthUser(); + + $this->novaActingAs($mockedUser); + + $this->assertEquals($mockedUser, Auth::user()); + } + + public function testNovaActingAsUserNotSet() + { + $result = $this->novaActingAs(); + + $this->assertEquals(true, ($result instanceof self)); + } } diff --git a/tests/support/Mock/TestNovaAction.php b/tests/support/Mock/TestNovaAction.php new file mode 100644 index 0000000..6083262 --- /dev/null +++ b/tests/support/Mock/TestNovaAction.php @@ -0,0 +1,11 @@ + Date: Wed, 23 Oct 2024 15:44:49 +0500 Subject: [PATCH 5/7] feat: cover by tests NovaTestTrait --- tests/support/Mock/TestNovaAction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/support/Mock/TestNovaAction.php b/tests/support/Mock/TestNovaAction.php index 6083262..c3d4fe6 100644 --- a/tests/support/Mock/TestNovaAction.php +++ b/tests/support/Mock/TestNovaAction.php @@ -8,4 +8,4 @@ public function uriKey(): string { return 'test-nova-action'; } -} \ No newline at end of file +} From 0d1a30ed5d86b4cda2d1ce29004eafbbe03991bc Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Wed, 30 Oct 2024 12:37:59 +0500 Subject: [PATCH 6/7] chore: rename nova api calls, fix namespace for NovaTestTrait --- src/{Tests => Traits}/NovaTestTrait.php | 22 +++++++++++----------- tests/NovaTestTraitTest.php | 19 ++++++++++--------- 2 files changed, 21 insertions(+), 20 deletions(-) rename src/{Tests => Traits}/NovaTestTrait.php (69%) diff --git a/src/Tests/NovaTestTrait.php b/src/Traits/NovaTestTrait.php similarity index 69% rename from src/Tests/NovaTestTrait.php rename to src/Traits/NovaTestTrait.php index f4217ca..3950a43 100644 --- a/src/Tests/NovaTestTrait.php +++ b/src/Traits/NovaTestTrait.php @@ -1,6 +1,6 @@ getNovaResourceUri($resourceClass); return $this->postJson($resourceUri, $data); } - protected function novaUpdateResource(string $resourceClass, int $resourceId, ?array $data = []): TestResponse + protected function novaUpdateResourceAPICall(string $resourceClass, int $resourceId, ?array $data = []): TestResponse { $resourceUri = $this->getNovaResourceUri($resourceClass); return $this->putJson("{$resourceUri}/{$resourceId}", $data); } - protected function novaGetResource(string $resourceClass, int $resourceId, ?array $data = []): TestResponse + protected function novaGetResourceAPICall(string $resourceClass, int $resourceId, ?array $data = []): TestResponse { $resourceUri = $this->getNovaResourceUri($resourceClass); return $this->getJson("{$resourceUri}/{$resourceId}", $data); } - protected function novaSearchResource(string $resourceClass, ?array $request = []): TestResponse + protected function novaSearchResourceAPICall(string $resourceClass, ?array $request = []): TestResponse { $resourceUri = $this->getNovaResourceUri($resourceClass); return $this->json('get', $resourceUri, $request); } - protected function novaGetCreationFields(string $resourceClass): TestResponse + protected function novaGetCreationFieldsAPICall(string $resourceClass): TestResponse { $resourceUri = $this->getNovaResourceUri($resourceClass); return $this->getJson("{$resourceUri}/creation-fields"); } - protected function novaRunAction(string $resourceClass, string $action, ?array $request = []): TestResponse + protected function novaRunActionAPICall(string $resourceClass, string $actionClass, ?array $request = []): TestResponse { $resourceUri = $this->getNovaResourceUri($resourceClass); - $actionUri = app($action)->uriKey(); + $actionUri = app($actionClass)->uriKey(); return $this->json('POST', "{$resourceUri}/action?action={$actionUri}", $request); } - protected function novaGetActions(string $resourceClass, array $resourceIds): TestResponse + protected function novaGetActionsAPICall(string $resourceClass, array $resourceIds): TestResponse { $resourceUri = $this->getNovaResourceUri($resourceClass); @@ -73,7 +73,7 @@ protected function novaGetActions(string $resourceClass, array $resourceIds): Te return $this->json('get', "{$resourceUri}/actions", $request); } - protected function novaDeleteResource(string $resourceClass, array $resourceIds): TestResponse + protected function novaDeleteResourceAPICall(string $resourceClass, array $resourceIds): TestResponse { $resourceUri = $this->getNovaResourceUri($resourceClass); @@ -84,7 +84,7 @@ protected function novaDeleteResource(string $resourceClass, array $resourceIds) return $this->deleteJson($resourceUri, $request); } - protected function novaGetUpdatableFields(string $resourceClass, int $resourceId): TestResponse + protected function novaGetUpdatableFieldsAPICall(string $resourceClass, int $resourceId): TestResponse { $resourceUri = $this->getNovaResourceUri($resourceClass); diff --git a/tests/NovaTestTraitTest.php b/tests/NovaTestTraitTest.php index 2ba3dbe..fd6e4a0 100644 --- a/tests/NovaTestTraitTest.php +++ b/tests/NovaTestTraitTest.php @@ -9,6 +9,7 @@ use RonasIT\Support\Tests\Support\Mock\TestModel; use RonasIT\Support\Tests\Support\Mock\TestNovaAction; use RonasIT\Support\Traits\MockTrait; +use RonasIT\Support\Traits\NovaTestTrait; use Symfony\Component\HttpFoundation\Response; class NovaTestTraitTest extends HelpersTestCase @@ -46,7 +47,7 @@ public function testNovaCreateResource() return response('', Response::HTTP_CREATED); }); - $result = $this->novaCreateResource(TestModel::class, ['key' => 'value']); + $result = $this->novaCreateResourceAPICall(TestModel::class, ['key' => 'value']); $result->assertCreated(); } @@ -61,7 +62,7 @@ public function testNovaUpdateResource() return response($request->all(), Response::HTTP_OK); }); - $result = $this->novaUpdateResource(TestModel::class, 1, ['key' => 'value']); + $result = $this->novaUpdateResourceAPICall(TestModel::class, 1, ['key' => 'value']); $result->assertOk(); } @@ -72,7 +73,7 @@ public function testNovaGetResource() return response('', Response::HTTP_OK); }); - $result = $this->novaGetResource(TestModel::class, 1); + $result = $this->novaGetResourceAPICall(TestModel::class, 1); $result->assertOk(); } @@ -87,7 +88,7 @@ public function testNovaSearchResource() return response($request->all(), Response::HTTP_OK); }); - $result = $this->novaSearchResource(TestModel::class, ['key' => 'value']); + $result = $this->novaSearchResourceAPICall(TestModel::class, ['key' => 'value']); $result->assertOk(); } @@ -98,7 +99,7 @@ public function testNovaGetCreationFields() return response('', Response::HTTP_OK); }); - $result = $this->novaGetCreationFields(TestModel::class); + $result = $this->novaGetCreationFieldsAPICall(TestModel::class); $result->assertOk(); } @@ -113,7 +114,7 @@ public function testNovaGetActions() return response($request->all(), Response::HTTP_OK); }); - $result = $this->novaGetActions(TestModel::class, [1, 2]); + $result = $this->novaGetActionsAPICall(TestModel::class, [1, 2]); $result->assertOk(); @@ -130,7 +131,7 @@ public function testNovaDeleteResource() return response($request->all(), Response::HTTP_OK); }); - $result = $this->novaDeleteResource(TestModel::class, [1, 2]); + $result = $this->novaDeleteResourceAPICall(TestModel::class, [1, 2]); $result->assertOk(); @@ -143,7 +144,7 @@ public function testNovaGetUpdatableFields() return response('', Response::HTTP_OK); }); - $result = $this->novaGetUpdatableFields(TestModel::class, 1); + $result = $this->novaGetUpdatableFieldsAPICall(TestModel::class, 1); $result->assertOk(); } @@ -154,7 +155,7 @@ public function testNovaRunAction() return response($request->all(), Response::HTTP_OK); }); - $result = $this->novaRunAction(TestModel::class, TestNovaAction::class, ['key' => 'value']); + $result = $this->novaRunActionAPICall(TestModel::class, TestNovaAction::class, ['key' => 'value']); $result->assertOk(); From f0624222685cb517c890e7d430366cb15a35e767 Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Fri, 1 Nov 2024 16:24:19 +0500 Subject: [PATCH 7/7] chore: rename nova api calls, fix namespace for NovaTestTrait --- src/Traits/NovaTestTrait.php | 42 ++++++++++++++++++------------------ tests/NovaTestTraitTest.php | 2 +- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Traits/NovaTestTrait.php b/src/Traits/NovaTestTrait.php index 3950a43..53c6a68 100644 --- a/src/Traits/NovaTestTrait.php +++ b/src/Traits/NovaTestTrait.php @@ -20,75 +20,75 @@ protected function novaSearchParams(array $filters, string $search = '', int $pe protected function novaCreateResourceAPICall(string $resourceClass, ?array $data = []): TestResponse { - $resourceUri = $this->getNovaResourceUri($resourceClass); + $uri = $this->generateNovaUri($resourceClass); - return $this->postJson($resourceUri, $data); + return $this->json('post', $uri, $data); } protected function novaUpdateResourceAPICall(string $resourceClass, int $resourceId, ?array $data = []): TestResponse { - $resourceUri = $this->getNovaResourceUri($resourceClass); + $uri = $this->generateNovaUri($resourceClass); - return $this->putJson("{$resourceUri}/{$resourceId}", $data); + return $this->json('put', "{$uri}/{$resourceId}", $data); } protected function novaGetResourceAPICall(string $resourceClass, int $resourceId, ?array $data = []): TestResponse { - $resourceUri = $this->getNovaResourceUri($resourceClass); + $uri = $this->generateNovaUri($resourceClass); - return $this->getJson("{$resourceUri}/{$resourceId}", $data); + return $this->json('get', "{$uri}/{$resourceId}", $data); } protected function novaSearchResourceAPICall(string $resourceClass, ?array $request = []): TestResponse { - $resourceUri = $this->getNovaResourceUri($resourceClass); + $uri = $this->generateNovaUri($resourceClass); - return $this->json('get', $resourceUri, $request); + return $this->json('get', $uri, $request); } protected function novaGetCreationFieldsAPICall(string $resourceClass): TestResponse { - $resourceUri = $this->getNovaResourceUri($resourceClass); + $uri = $this->generateNovaUri($resourceClass, '/creation-fields'); - return $this->getJson("{$resourceUri}/creation-fields"); + return $this->json('get', $uri); } protected function novaRunActionAPICall(string $resourceClass, string $actionClass, ?array $request = []): TestResponse { - $resourceUri = $this->getNovaResourceUri($resourceClass); - $actionUri = app($actionClass)->uriKey(); - return $this->json('POST', "{$resourceUri}/action?action={$actionUri}", $request); + $uri = $this->generateNovaUri($resourceClass, "/action?action={$actionUri}"); + + return $this->json('post', $uri, $request); } protected function novaGetActionsAPICall(string $resourceClass, array $resourceIds): TestResponse { - $resourceUri = $this->getNovaResourceUri($resourceClass); + $uri = $this->generateNovaUri($resourceClass, '/actions'); $request = [ 'resources' => implode(',', $resourceIds), ]; - return $this->json('get', "{$resourceUri}/actions", $request); + return $this->json('get', $uri, $request); } protected function novaDeleteResourceAPICall(string $resourceClass, array $resourceIds): TestResponse { - $resourceUri = $this->getNovaResourceUri($resourceClass); + $uri = $this->generateNovaUri($resourceClass); $request = [ 'resources' => $resourceIds, ]; - return $this->deleteJson($resourceUri, $request); + return $this->json('delete', $uri, $request); } protected function novaGetUpdatableFieldsAPICall(string $resourceClass, int $resourceId): TestResponse { - $resourceUri = $this->getNovaResourceUri($resourceClass); + $uri = $this->generateNovaUri($resourceClass, "/{$resourceId}/update-fields"); - return $this->getJson("{$resourceUri}/{$resourceId}/update-fields"); + return $this->json('get', $uri); } protected function novaActingAs(?Authenticatable $user = null): TestCase|self @@ -98,12 +98,12 @@ protected function novaActingAs(?Authenticatable $user = null): TestCase|self : $this->actingAs($user, 'web'); } - protected function getNovaResourceUri(string $modelClass): string + protected function generateNovaUri(string $modelClass, string $path = ''): string { $modelName = Str::afterLast($modelClass, '\\'); $modelName = Str::kebab($modelName); - return "/nova-api/{$modelName}-resources"; + return "/nova-api/{$modelName}-resources{$path}"; } } diff --git a/tests/NovaTestTraitTest.php b/tests/NovaTestTraitTest.php index fd6e4a0..1f95219 100644 --- a/tests/NovaTestTraitTest.php +++ b/tests/NovaTestTraitTest.php @@ -32,7 +32,7 @@ public function testMockSingleCall() public function testGetNovaResourceUri() { - $result = $this->getNovaResourceUri(TestModel::class); + $result = $this->generateNovaUri(TestModel::class); $this->assertEquals($result, '/nova-api/test-model-resources'); }