From 3d67ad870a61fc9851f7948e270b46aaedd4c329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 3 Mar 2024 21:52:26 +0100 Subject: [PATCH] Added tests for all API endpoints of Structural elements --- src/Entity/Attachments/Attachment.php | 1 + src/Entity/Attachments/AttachmentType.php | 2 +- src/Entity/Parts/MeasurementUnit.php | 2 +- tests/API/AuthenticatedApiTestCase.php | 2 +- .../Endpoints/AttachmentTypeEndpointTest.php | 75 +++++++++++++ tests/API/Endpoints/CategoryEndpointTest.php | 76 +++++++++++++ tests/API/Endpoints/CrudEndpointTestCase.php | 102 ++++++++++++++++++ tests/API/Endpoints/CurrencyEndpointTest.php | 64 +++++++++++ .../API/Endpoints/FootprintsEndpointTest.php | 76 +++++++++++++ tests/API/Endpoints/InfoEndpointTest.php | 40 +++++++ .../Endpoints/ManufacturersEndpointTest.php | 76 +++++++++++++ .../MeasurementUnitsEndpointTest.php | 74 +++++++++++++ tests/API/Endpoints/PartEndpointTest.php | 72 +++++++++++++ tests/API/Endpoints/ProjectsEndpointTest.php | 76 +++++++++++++ .../StorageLocationsEndpointTest.php | 76 +++++++++++++ tests/API/Endpoints/SuppliersEndpointTest.php | 76 +++++++++++++ 16 files changed, 887 insertions(+), 3 deletions(-) create mode 100644 tests/API/Endpoints/AttachmentTypeEndpointTest.php create mode 100644 tests/API/Endpoints/CategoryEndpointTest.php create mode 100644 tests/API/Endpoints/CrudEndpointTestCase.php create mode 100644 tests/API/Endpoints/CurrencyEndpointTest.php create mode 100644 tests/API/Endpoints/FootprintsEndpointTest.php create mode 100644 tests/API/Endpoints/InfoEndpointTest.php create mode 100644 tests/API/Endpoints/ManufacturersEndpointTest.php create mode 100644 tests/API/Endpoints/MeasurementUnitsEndpointTest.php create mode 100644 tests/API/Endpoints/PartEndpointTest.php create mode 100644 tests/API/Endpoints/ProjectsEndpointTest.php create mode 100644 tests/API/Endpoints/StorageLocationsEndpointTest.php create mode 100644 tests/API/Endpoints/SuppliersEndpointTest.php diff --git a/src/Entity/Attachments/Attachment.php b/src/Entity/Attachments/Attachment.php index d705d2d2..ae66da19 100644 --- a/src/Entity/Attachments/Attachment.php +++ b/src/Entity/Attachments/Attachment.php @@ -180,6 +180,7 @@ abstract class Attachment extends AbstractNamedDBElement #[ORM\JoinColumn(name: 'type_id', nullable: false)] #[Selectable] #[Groups(['attachment:read', 'attachment:write'])] + #[ApiProperty(readableLink: false)] protected ?AttachmentType $attachment_type = null; #[Groups(['attachment:read'])] diff --git a/src/Entity/Attachments/AttachmentType.php b/src/Entity/Attachments/AttachmentType.php index a514f53a..58546e6a 100644 --- a/src/Entity/Attachments/AttachmentType.php +++ b/src/Entity/Attachments/AttachmentType.php @@ -92,7 +92,7 @@ class AttachmentType extends AbstractStructuralDBElement #[ORM\ManyToOne(targetEntity: AttachmentType::class, inversedBy: 'children')] #[ORM\JoinColumn(name: 'parent_id')] #[Groups(['attachment_type:read', 'attachment_type:write'])] - #[ApiProperty(readableLink: false, writableLink: false)] + #[ApiProperty(readableLink: true, writableLink: false)] protected ?AbstractStructuralDBElement $parent = null; /** diff --git a/src/Entity/Parts/MeasurementUnit.php b/src/Entity/Parts/MeasurementUnit.php index f23734a8..38c59600 100644 --- a/src/Entity/Parts/MeasurementUnit.php +++ b/src/Entity/Parts/MeasurementUnit.php @@ -74,7 +74,7 @@ use Symfony\Component\Validator\Constraints as Assert; denormalizationContext: ['groups' => ['measurement_unit:write', 'api:basic:write', 'attachment:write', 'parameter:write'], 'openapi_definition_name' => 'Write'], )] #[ApiResource( - uriTemplate: '/footprints/{id}/children.{_format}', + uriTemplate: '/measurement_units/{id}/children.{_format}', operations: [ new GetCollection( openapi: new Operation(summary: 'Retrieves the children elements of a MeasurementUnit.'), diff --git a/tests/API/AuthenticatedApiTestCase.php b/tests/API/AuthenticatedApiTestCase.php index 601ba595..b1bde64b 100644 --- a/tests/API/AuthenticatedApiTestCase.php +++ b/tests/API/AuthenticatedApiTestCase.php @@ -27,7 +27,7 @@ use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; use ApiPlatform\Symfony\Bundle\Test\Client; use App\DataFixtures\APITokenFixtures; -class AuthenticatedApiTestCase extends ApiTestCase +abstract class AuthenticatedApiTestCase extends ApiTestCase { /** * Creates an API client with authentication. diff --git a/tests/API/Endpoints/AttachmentTypeEndpointTest.php b/tests/API/Endpoints/AttachmentTypeEndpointTest.php new file mode 100644 index 00000000..f90f3d94 --- /dev/null +++ b/tests/API/Endpoints/AttachmentTypeEndpointTest.php @@ -0,0 +1,75 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Tests\API\Endpoints; + +use App\Tests\API\Endpoints\CrudEndpointTestCase; + +class AttachmentTypeEndpointTest extends CrudEndpointTestCase +{ + + protected function getBasePath(): string + { + return '/api/attachment_types'; + } + + public function testGetCollection(): void + { + $this->_testGetCollection(); + self::assertJsonContains([ + 'hydra:totalItems' => 7, + ]); + } + + public function testGetChildrenCollection(): void + { + $this->_testGetChildrenCollection(1); + } + + public function testGetItem(): void + { + $this->_testGetItem(1); + $this->_testGetItem(2); + $this->_testGetItem(3); + } + + public function testCreateItem(): void + { + $this->_testPostItem([ + 'name' => 'Test Part', + 'parent' => '/api/attachment_types/1', + ]); + } + + public function testUpdateItem(): void + { + $this->_testPatchItem(1, [ + 'name' => 'Test Part Updated', + ]); + } + + public function testDeleteItem(): void + { + $this->_testDeleteItem(6); + } +} \ No newline at end of file diff --git a/tests/API/Endpoints/CategoryEndpointTest.php b/tests/API/Endpoints/CategoryEndpointTest.php new file mode 100644 index 00000000..d35f26d4 --- /dev/null +++ b/tests/API/Endpoints/CategoryEndpointTest.php @@ -0,0 +1,76 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Tests\Endpoints; + +use App\Tests\API\Endpoints\CrudEndpointTestCase; + +class CategoryEndpointTest extends CrudEndpointTestCase +{ + + protected function getBasePath(): string + { + return '/api/categories'; + } + + public function testGetCollection(): void + { + $this->_testGetCollection(); + self::assertJsonContains([ + 'hydra:totalItems' => 7, + ]); + } + + public function testGetChildrenCollection(): void + { + $this->_testGetChildrenCollection(1); + } + + public function testGetItem(): void + { + $this->_testGetItem(1); + $this->_testGetItem(2); + $this->_testGetItem(3); + } + + public function testCreateItem(): void + { + $this->_testPostItem([ + 'name' => 'Test API', + 'parent' => '/api/categories/1', + ]); + } + + public function testUpdateItem(): void + { + $this->_testPatchItem(1, [ + 'name' => 'Updated', + 'parent' => '/api/categories/2', + ]); + } + + public function testDeleteItem(): void + { + $this->_testDeleteItem(5); + } +} \ No newline at end of file diff --git a/tests/API/Endpoints/CrudEndpointTestCase.php b/tests/API/Endpoints/CrudEndpointTestCase.php new file mode 100644 index 00000000..f60c2a50 --- /dev/null +++ b/tests/API/Endpoints/CrudEndpointTestCase.php @@ -0,0 +1,102 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Tests\API\Endpoints; + +use App\Tests\API\AuthenticatedApiTestCase; +use Symfony\Contracts\HttpClient\ResponseInterface; + +abstract class CrudEndpointTestCase extends AuthenticatedApiTestCase +{ + /** + * Returns the base path of the endpoint. + * @return string + */ + abstract protected function getBasePath(): string; + + protected function getItemPath(int $id): string + { + $basePath = $this->getBasePath(); + if (!str_ends_with($basePath, '/')) { + $basePath .= '/'; + } + + return $basePath . $id; + } + + protected function _testGetCollection(): ResponseInterface + { + $response = self::createAuthenticatedClient()->request('GET', $this->getBasePath()); + self::assertResponseIsSuccessful(); + self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8'); + + return $response; + } + + protected function _testGetChildrenCollection(int $id): ResponseInterface + { + $response = self::createAuthenticatedClient()->request('GET', $this->getItemPath($id) . '/children'); + self::assertResponseIsSuccessful(); + self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8'); + + return $response; + } + + protected function _testGetItem(int $id): ResponseInterface + { + $response = self::createAuthenticatedClient()->request('GET', $this->getItemPath($id)); + self::assertResponseIsSuccessful(); + self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8'); + + return $response; + } + + protected function _testPostItem(array $data): ResponseInterface + { + $response = self::createAuthenticatedClient()->request('POST', $this->getBasePath(), ['json' => $data]); + self::assertResponseIsSuccessful(); + self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8'); + + return $response; + } + + protected function _testPatchItem(int $id, array $data): ResponseInterface + { + $response = self::createAuthenticatedClient()->request('PATCH', $this->getItemPath($id), [ + 'json' => $data, + 'headers' => ['Content-Type' => 'application/merge-patch+json'] + ]); + self::assertResponseIsSuccessful(); + self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8'); + + return $response; + } + + protected function _testDeleteItem(int $id): ResponseInterface + { + $response = self::createAuthenticatedClient()->request('DELETE', $this->getItemPath($id)); + self::assertResponseIsSuccessful(); + + return $response; + } +} \ No newline at end of file diff --git a/tests/API/Endpoints/CurrencyEndpointTest.php b/tests/API/Endpoints/CurrencyEndpointTest.php new file mode 100644 index 00000000..78434ea3 --- /dev/null +++ b/tests/API/Endpoints/CurrencyEndpointTest.php @@ -0,0 +1,64 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Tests\API\Endpoints; + + +class CurrencyEndpointTest extends CrudEndpointTestCase +{ + + protected function getBasePath(): string + { + return '/api/currencies'; + } + + public function testGetCollection(): void + { + $this->_testGetCollection(); + self::assertJsonContains([ + 'hydra:totalItems' => 0, + ]); + } + + + public function testCreateItem(): void + { + $this->_testPostItem([ + 'name' => 'Test API', + 'iso_code' => 'USD', + ]); + } + + /*public function testUpdateItem(): void + { + $this->_testPatchItem(1, [ + 'name' => 'Updated', + + ]); + } + + public function testDeleteItem(): void + { + $this->_testDeleteItem(5); + }*/ +} \ No newline at end of file diff --git a/tests/API/Endpoints/FootprintsEndpointTest.php b/tests/API/Endpoints/FootprintsEndpointTest.php new file mode 100644 index 00000000..f3f359a2 --- /dev/null +++ b/tests/API/Endpoints/FootprintsEndpointTest.php @@ -0,0 +1,76 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Tests\API\Endpoints; + +use App\Tests\API\Endpoints\CrudEndpointTestCase; + +class FootprintsEndpointTest extends CrudEndpointTestCase +{ + + protected function getBasePath(): string + { + return '/api/footprints'; + } + + public function testGetCollection(): void + { + $this->_testGetCollection(); + self::assertJsonContains([ + 'hydra:totalItems' => 7, + ]); + } + + public function testGetChildrenCollection(): void + { + $this->_testGetChildrenCollection(1); + } + + public function testGetItem(): void + { + $this->_testGetItem(1); + $this->_testGetItem(2); + $this->_testGetItem(3); + } + + public function testCreateItem(): void + { + $this->_testPostItem([ + 'name' => 'Test API', + 'parent' => '/api/footprints/1', + ]); + } + + public function testUpdateItem(): void + { + $this->_testPatchItem(1, [ + 'name' => 'Updated', + 'parent' => '/api/footprints/2', + ]); + } + + public function testDeleteItem(): void + { + $this->_testDeleteItem(5); + } +} \ No newline at end of file diff --git a/tests/API/Endpoints/InfoEndpointTest.php b/tests/API/Endpoints/InfoEndpointTest.php new file mode 100644 index 00000000..09f02e8a --- /dev/null +++ b/tests/API/Endpoints/InfoEndpointTest.php @@ -0,0 +1,40 @@ +. + */ + +declare(strict_types=1); + + +namespace API\Endpoints; + +use App\Tests\API\AuthenticatedApiTestCase; + +class InfoEndpointTest extends AuthenticatedApiTestCase +{ + public function testGetInfo(): void + { + $response = self::createAuthenticatedClient()->request('GET', '/api/info'); + + self::assertResponseIsSuccessful(); + self::assertJsonContains([ + '@id' => '/api/info', + 'title' => 'Part-DB', + ]); + } +} \ No newline at end of file diff --git a/tests/API/Endpoints/ManufacturersEndpointTest.php b/tests/API/Endpoints/ManufacturersEndpointTest.php new file mode 100644 index 00000000..482ec98d --- /dev/null +++ b/tests/API/Endpoints/ManufacturersEndpointTest.php @@ -0,0 +1,76 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Tests\API\Endpoints; + +use App\Tests\API\Endpoints\CrudEndpointTestCase; + +class ManufacturersEndpointTest extends CrudEndpointTestCase +{ + + protected function getBasePath(): string + { + return '/api/manufacturers'; + } + + public function testGetCollection(): void + { + $this->_testGetCollection(); + self::assertJsonContains([ + 'hydra:totalItems' => 7, + ]); + } + + public function testGetChildrenCollection(): void + { + $this->_testGetChildrenCollection(1); + } + + public function testGetItem(): void + { + $this->_testGetItem(1); + $this->_testGetItem(2); + $this->_testGetItem(3); + } + + public function testCreateItem(): void + { + $this->_testPostItem([ + 'name' => 'Test API', + 'parent' => '/api/manufacturers/1', + ]); + } + + public function testUpdateItem(): void + { + $this->_testPatchItem(5, [ + 'name' => 'Updated', + 'parent' => '/api/manufacturers/2', + ]); + } + + public function testDeleteItem(): void + { + $this->_testDeleteItem(7); + } +} \ No newline at end of file diff --git a/tests/API/Endpoints/MeasurementUnitsEndpointTest.php b/tests/API/Endpoints/MeasurementUnitsEndpointTest.php new file mode 100644 index 00000000..db7341db --- /dev/null +++ b/tests/API/Endpoints/MeasurementUnitsEndpointTest.php @@ -0,0 +1,74 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Tests\API\Endpoints; + +class MeasurementUnitsEndpointTest extends CrudEndpointTestCase +{ + + protected function getBasePath(): string + { + return '/api/measurement_units'; + } + + public function testGetCollection(): void + { + $this->_testGetCollection(); + self::assertJsonContains([ + 'hydra:totalItems' => 7, + ]); + } + + public function testGetChildrenCollection(): void + { + $this->_testGetChildrenCollection(1); + } + + public function testGetItem(): void + { + $this->_testGetItem(1); + $this->_testGetItem(2); + $this->_testGetItem(3); + } + + public function testCreateItem(): void + { + $this->_testPostItem([ + 'name' => 'Test API', + 'parent' => '/api/measurement_units/1', + ]); + } + + public function testUpdateItem(): void + { + $this->_testPatchItem(5, [ + 'name' => 'Updated', + 'parent' => '/api/measurement_units/2', + ]); + } + + public function testDeleteItem(): void + { + $this->_testDeleteItem(4); + } +} \ No newline at end of file diff --git a/tests/API/Endpoints/PartEndpointTest.php b/tests/API/Endpoints/PartEndpointTest.php new file mode 100644 index 00000000..9406fc78 --- /dev/null +++ b/tests/API/Endpoints/PartEndpointTest.php @@ -0,0 +1,72 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Tests\API\Endpoints; + +class PartEndpointTest extends CrudEndpointTestCase +{ + + protected function getBasePath(): string + { + return '/api/parts'; + } + + public function testGetCollection(): void + { + $this->_testGetCollection(); + self::assertJsonContains([ + 'hydra:totalItems' => 3, + ]); + } + + public function testGetItem(): void + { + $this->_testGetItem(1); + $this->_testGetItem(2); + $this->_testGetItem(3); + } + + public function testCreateItem(): void + { + $this->_testPostItem([ + 'name' => 'Test Part', + 'description' => 'This is a test part', + 'category' => '/api/categories/1', + 'manufacturer' => '/api/manufacturers/1', + ]); + } + + public function testUpdateItem(): void + { + $this->_testPatchItem(1, [ + 'name' => 'Test Part Updated', + 'category' => '/api/categories/2', + 'manufacturer' => '/api/manufacturers/2', + ]); + } + + public function testDeleteItem(): void + { + $this->_testDeleteItem(1); + } +} \ No newline at end of file diff --git a/tests/API/Endpoints/ProjectsEndpointTest.php b/tests/API/Endpoints/ProjectsEndpointTest.php new file mode 100644 index 00000000..9daf584a --- /dev/null +++ b/tests/API/Endpoints/ProjectsEndpointTest.php @@ -0,0 +1,76 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Tests\API\Endpoints; + +use App\Tests\API\Endpoints\CrudEndpointTestCase; + +class ProjectsEndpointTest extends CrudEndpointTestCase +{ + + protected function getBasePath(): string + { + return '/api/projects'; + } + + public function testGetCollection(): void + { + $this->_testGetCollection(); + self::assertJsonContains([ + 'hydra:totalItems' => 7, + ]); + } + + public function testGetChildrenCollection(): void + { + $this->_testGetChildrenCollection(1); + } + + public function testGetItem(): void + { + $this->_testGetItem(1); + $this->_testGetItem(2); + $this->_testGetItem(3); + } + + public function testCreateItem(): void + { + $this->_testPostItem([ + 'name' => 'Test API', + 'parent' => '/api/projects/1', + ]); + } + + public function testUpdateItem(): void + { + $this->_testPatchItem(5, [ + 'name' => 'Updated', + 'parent' => '/api/projects/2', + ]); + } + + public function testDeleteItem(): void + { + $this->_testDeleteItem(7); + } +} \ No newline at end of file diff --git a/tests/API/Endpoints/StorageLocationsEndpointTest.php b/tests/API/Endpoints/StorageLocationsEndpointTest.php new file mode 100644 index 00000000..8d9641c4 --- /dev/null +++ b/tests/API/Endpoints/StorageLocationsEndpointTest.php @@ -0,0 +1,76 @@ +. + */ + +declare(strict_types=1); + + +namespace API\Endpoints; + +use App\Tests\API\Endpoints\CrudEndpointTestCase; + +class StorageLocationsEndpointTest extends CrudEndpointTestCase +{ + + protected function getBasePath(): string + { + return '/api/storage_locations'; + } + + public function testGetCollection(): void + { + $this->_testGetCollection(); + self::assertJsonContains([ + 'hydra:totalItems' => 7, + ]); + } + + public function testGetChildrenCollection(): void + { + $this->_testGetChildrenCollection(1); + } + + public function testGetItem(): void + { + $this->_testGetItem(1); + $this->_testGetItem(2); + $this->_testGetItem(3); + } + + public function testCreateItem(): void + { + $this->_testPostItem([ + 'name' => 'Test API', + 'parent' => '/api/storage_locations/1', + ]); + } + + public function testUpdateItem(): void + { + $this->_testPatchItem(5, [ + 'name' => 'Updated', + 'parent' => '/api/storage_locations/2', + ]); + } + + public function testDeleteItem(): void + { + $this->_testDeleteItem(7); + } +} \ No newline at end of file diff --git a/tests/API/Endpoints/SuppliersEndpointTest.php b/tests/API/Endpoints/SuppliersEndpointTest.php new file mode 100644 index 00000000..1941f849 --- /dev/null +++ b/tests/API/Endpoints/SuppliersEndpointTest.php @@ -0,0 +1,76 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Tests\API\Endpoints; + +use App\Tests\API\Endpoints\CrudEndpointTestCase; + +class SuppliersEndpointTest extends CrudEndpointTestCase +{ + + protected function getBasePath(): string + { + return '/api/suppliers'; + } + + public function testGetCollection(): void + { + $this->_testGetCollection(); + self::assertJsonContains([ + 'hydra:totalItems' => 7, + ]); + } + + public function testGetChildrenCollection(): void + { + $this->_testGetChildrenCollection(1); + } + + public function testGetItem(): void + { + $this->_testGetItem(1); + $this->_testGetItem(2); + $this->_testGetItem(3); + } + + public function testCreateItem(): void + { + $this->_testPostItem([ + 'name' => 'Test API', + 'parent' => '/api/suppliers/1', + ]); + } + + public function testUpdateItem(): void + { + $this->_testPatchItem(5, [ + 'name' => 'Updated', + 'parent' => '/api/suppliers/2', + ]); + } + + public function testDeleteItem(): void + { + $this->_testDeleteItem(7); + } +} \ No newline at end of file