Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Added test case for custom field; parameter should binded with type
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed Jan 18, 2018
1 parent 2b72469 commit 8cc7419
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="ZFTestApigilityDb\Entity\Product">
<id name="id" type="rev">
<generator strategy="CUSTOM"/>
<custom-id-generator class="ZFTestApigilityDb\Type\RevGenerator"/>
</id>
</entity>
</doctrine-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace ZFTestApigilityDb\Entity;

class Product
{
protected $id;

public function getId()
{
return $this->id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace ZFTestApigilityDb\Type;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Id\AbstractIdGenerator;

class RevGenerator extends AbstractIdGenerator
{
public function generate(EntityManager $em, $entity)
{
do {
$value = md5(time() . mt_rand());
} while ($value === strrev($value));

return $value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace ZFTestApigilityDb\Type;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

class RevType extends Type
{
const NAME = 'rev';

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->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;
}
}
11 changes: 10 additions & 1 deletion test/config/ORM/local.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2016-2018 Zend Technologies USA Inc. (http://www.zend.com)
*/

use ZFTestApigilityDb\Type\RevType;

return [
'doctrine' => [
'connection' => [
Expand All @@ -16,5 +18,12 @@
],
],
],
'configuration' => [
'orm_default' => [
'types' => [
RevType::NAME => RevType::class,
],
],
],
],
];
63 changes: 62 additions & 1 deletion test/src/Server/ORM/CRUD/CRUDTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2016-2018 Zend Technologies USA Inc. (http://www.zend.com)
*/

namespace ZFTest\Apigility\Doctrine\Server\ORM\CRUD;
Expand All @@ -22,6 +22,7 @@
use ZFTest\Apigility\Doctrine\TestCase;
use ZFTestApigilityDb\Entity\Album;
use ZFTestApigilityDb\Entity\Artist;
use ZFTestApigilityDb\Entity\Product;
use ZFTestApigilityGeneral\Listener\EventCatcher;

class CRUDTest extends TestCase
Expand Down Expand Up @@ -94,14 +95,31 @@ protected function buildORMApi()
],
];

$productResourceDefinition = [
'objectManager' => '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();
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 8cc7419

Please sign in to comment.