diff --git a/test/assets/module/ZFTestApigilityDb/config/xml/ZFTestApigilityDb.Entity.Product.dcm.xml b/test/assets/module/ZFTestApigilityDb/config/xml/ZFTestApigilityDb.Entity.Product.dcm.xml new file mode 100644 index 00000000..3047656b --- /dev/null +++ b/test/assets/module/ZFTestApigilityDb/config/xml/ZFTestApigilityDb.Entity.Product.dcm.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/test/assets/module/ZFTestApigilityDb/src/ZFTestApigilityDb/Entity/Product.php b/test/assets/module/ZFTestApigilityDb/src/ZFTestApigilityDb/Entity/Product.php new file mode 100644 index 00000000..f0018bf1 --- /dev/null +++ b/test/assets/module/ZFTestApigilityDb/src/ZFTestApigilityDb/Entity/Product.php @@ -0,0 +1,13 @@ +id; + } +} diff --git a/test/assets/module/ZFTestApigilityDb/src/ZFTestApigilityDb/Type/RevGenerator.php b/test/assets/module/ZFTestApigilityDb/src/ZFTestApigilityDb/Type/RevGenerator.php new file mode 100644 index 00000000..1ccf313b --- /dev/null +++ b/test/assets/module/ZFTestApigilityDb/src/ZFTestApigilityDb/Type/RevGenerator.php @@ -0,0 +1,18 @@ +getGuidTypeDeclarationSQL($fieldDeclaration); + } + + public function convertToPHPValue($value, AbstractPlatform $platform) + { + if (! $value) { + return null; + } + + return strrev($value); + } + + public function convertToDatabaseValue($value, AbstractPlatform $platform) + { + if (! $value) { + return null; + } + + return strrev($value); + } + + public function getName() + { + return static::NAME; + } + + public function requiresSQLCommentHint(AbstractPlatform $platform) + { + return true; + } +} diff --git a/test/config/ORM/local.php b/test/config/ORM/local.php index a61014f4..261e9dbf 100644 --- a/test/config/ORM/local.php +++ b/test/config/ORM/local.php @@ -1,9 +1,11 @@ [ 'connection' => [ @@ -16,5 +18,12 @@ ], ], ], + 'configuration' => [ + 'orm_default' => [ + 'types' => [ + RevType::NAME => RevType::class, + ], + ], + ], ], ]; diff --git a/test/src/Server/ORM/CRUD/CRUDTest.php b/test/src/Server/ORM/CRUD/CRUDTest.php index ca7563b9..56f35f35 100644 --- a/test/src/Server/ORM/CRUD/CRUDTest.php +++ b/test/src/Server/ORM/CRUD/CRUDTest.php @@ -1,7 +1,7 @@ 'doctrine.entitymanager.orm_default', + 'serviceName' => 'Product', + 'entityClass' => Product::class, + 'routeIdentifierName' => 'product_id', + 'entityIdentifierName' => 'id', + 'routeMatch' => '/test/rest/product', + 'collectionHttpMethods' => [ + 0 => 'GET', + 1 => 'POST', + 2 => 'PATCH', + 3 => 'DELETE', + ], + ]; + $this->setModuleName($restServiceResource, 'ZFTestApigilityDbApi'); $artistEntity = $restServiceResource->create($artistResourceDefinition); $artistByNameEntity = $restServiceResource->create($artistResourceDefinitionWithNonKeyIdentifier); $albumEntity = $restServiceResource->create($albumResourceDefinition); + $productEntity = $restServiceResource->create($productResourceDefinition); $this->assertInstanceOf(DoctrineRestServiceEntity::class, $artistEntity); $this->assertInstanceOf(DoctrineRestServiceEntity::class, $artistByNameEntity); $this->assertInstanceOf(DoctrineRestServiceEntity::class, $albumEntity); + $this->assertInstanceOf(DoctrineRestServiceEntity::class, $productEntity); // Build relation $filter = new FilterChain(); @@ -221,6 +239,37 @@ function (DoctrineResourceEvent $e) { $this->assertEquals('ZFTestCreateFailure', $body['detail']); } + public function testFetchByCustomIdField() + { + $product = $this->createProduct(); + + $this->getRequest()->getHeaders()->addHeaderLine('Accept', 'application/json'); + $this->getRequest()->setMethod(Request::METHOD_GET); + + $this->dispatch('/test/rest/product/' . $product->getId()); + $body = json_decode($this->getResponse()->getBody(), true); + + $this->assertResponseStatusCode(200); + $this->assertEquals($product->getId(), $body['id']); + $this->validateTriggeredEvents([ + DoctrineResourceEvent::EVENT_FETCH_PRE, + DoctrineResourceEvent::EVENT_FETCH_POST, + ]); + } + + public function testFetchByCustomIdFieldWithInvalidIdValue() + { + $product = $this->createProduct(); + + $this->getRequest()->getHeaders()->addHeaderLine('Accept', 'application/json'); + $this->getRequest()->setMethod(Request::METHOD_GET); + + $this->dispatch('/test/rest/product/' . strrev($product->getId())); + + $this->assertResponseStatusCode(404); + $this->validateTriggeredEvents([DoctrineResourceEvent::EVENT_FETCH_PRE]); + } + public function testFetch() { $artist = $this->createArtist('Artist Name'); @@ -723,4 +772,16 @@ protected function createAlbum($name = null, Artist $artist = null) return $album; } + + /** + * @return Product + */ + protected function createProduct() + { + $product = new Product(); + $this->em->persist($product); + $this->em->flush(); + + return $product; + } }