Skip to content

Commit

Permalink
Merge pull request #2917 from teohhanhui/fix/simplify-interface-as-re…
Browse files Browse the repository at this point in the history
…source-test-case

Simplify "interface as resource" test case
  • Loading branch information
teohhanhui committed Jul 8, 2019
2 parents 9ad81f8 + f13024e commit 4891c4a
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 128 deletions.
6 changes: 4 additions & 2 deletions features/bootstrap/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -1330,8 +1330,10 @@ public function thereIsTheFollowingProduct(PyStringNode $dataNode): void
$product = $this->isOrm() ? new Product() : new ProductDocument();
$product->setCode($data['code']);
if (isset($data['mainTaxon'])) {
$mainTaxonId = (int) str_replace('/taxons/', '', $data['mainTaxon']);
$mainTaxon = $this->manager->getRepository($this->isOrm() ? Taxon::class : TaxonDocument::class)->find($mainTaxonId);
$mainTaxonCode = str_replace('/taxons/', '', $data['mainTaxon']);
$mainTaxon = $this->manager->getRepository($this->isOrm() ? Taxon::class : TaxonDocument::class)->findOneBy([
'code' => $mainTaxonCode,
]);
$product->setMainTaxon($mainTaxon);
}
$this->manager->persist($product);
Expand Down
12 changes: 6 additions & 6 deletions features/jsonld/interface_as_resource.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ Feature: JSON-LD using interface as resource
"code": "WONDERFUL_TAXON"
}
"""
When I send a "GET" request to "/taxons/1"
When I send a "GET" request to "/taxons/WONDERFUL_TAXON"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be equal to:
"""
{
"@context": "/contexts/Taxon",
"@id": "/taxons/1",
"@id": "/taxons/WONDERFUL_TAXON",
"@type": "Taxon",
"code": "WONDERFUL_TAXON"
}
Expand All @@ -34,22 +34,22 @@ Feature: JSON-LD using interface as resource
"""
{
"code": "GREAT_PRODUCT",
"mainTaxon": "/taxons/1"
"mainTaxon": "/taxons/WONDERFUL_TAXON"
}
"""
When I send a "GET" request to "/products/1"
When I send a "GET" request to "/products/GREAT_PRODUCT"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be equal to:
"""
{
"@context": "/contexts/Product",
"@id": "/products/1",
"@id": "/products/GREAT_PRODUCT",
"@type": "Product",
"code": "GREAT_PRODUCT",
"mainTaxon": {
"@id": "/taxons/1",
"@id": "/taxons/WONDERFUL_TAXON",
"@type": "Taxon",
"code": "WONDERFUL_TAXON"
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@

use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Product as ProductDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Product;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Model\ProductInterface;
use Doctrine\Common\Persistence\ManagerRegistry;

class ProductItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
{
private $managerRegistry;
private $orm;

public function __construct(ManagerRegistry $managerRegistry)
public function __construct(ManagerRegistry $managerRegistry, bool $orm = true)
{
$this->managerRegistry = $managerRegistry;
$this->orm = $orm;
}

/**
Expand All @@ -41,6 +44,8 @@ public function supports(string $resourceClass, string $operationName = null, ar
*/
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
{
return $this->managerRegistry->getRepository(Product::class)->find($id);
return $this->managerRegistry->getRepository($this->orm ? Product::class : ProductDocument::class)->findOneBy([
'code' => $id,
]);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@

use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Taxon as TaxonDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Taxon;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Model\TaxonInterface;
use Doctrine\Common\Persistence\ManagerRegistry;

class TaxonItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
{
private $managerRegistry;
private $orm;

public function __construct(ManagerRegistry $managerRegistry)
public function __construct(ManagerRegistry $managerRegistry, bool $orm = true)
{
$this->managerRegistry = $managerRegistry;
$this->orm = $orm;
}

/**
Expand All @@ -41,6 +44,8 @@ public function supports(string $resourceClass, string $operationName = null, ar
*/
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
{
return $this->managerRegistry->getRepository(Taxon::class)->find($id);
return $this->managerRegistry->getRepository($this->orm ? Taxon::class : TaxonDocument::class)->findOneBy([
'code' => $id,
]);
}
}
2 changes: 1 addition & 1 deletion tests/Fixtures/TestBundle/Entity/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Product implements ProductInterface
/**
* @var string|null
*
* @ORM\Column(type="string")
* @ORM\Column(type="string", unique=true)
*/
private $code;

Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/TestBundle/Entity/Taxon.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Taxon implements TaxonInterface
/**
* @var string|null
*
* @ORM\Column(type="string")
* @ORM\Column(type="string", unique=true)
*/
private $code;

Expand Down
6 changes: 3 additions & 3 deletions tests/Fixtures/TestBundle/Model/ProductInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
*/
interface ProductInterface
{
/**
* @ApiProperty(identifier=true)
*/
public function getId();

/**
* @ApiProperty(identifier=true)
*
* @Groups({"product_read"})
*
* @Assert\NotBlank
*/
public function getCode(): ?string;
Expand Down
6 changes: 3 additions & 3 deletions tests/Fixtures/TestBundle/Model/TaxonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
*/
interface TaxonInterface
{
/**
* @ApiProperty(identifier=true)
*/
public function getId();

/**
* @ApiProperty(identifier=true)
*
* @Groups({"product_read", "taxon_read"})
*
* @Assert\NotBlank
*/
public function getCode(): ?string;
Expand Down
18 changes: 10 additions & 8 deletions tests/Fixtures/app/config/config_mongodb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,21 @@ services:
arguments: [ { 'name': 'ipartial', 'description': 'ipartial' } ]
tags: [ { name: 'api_platform.filter', id: 'related_to_dummy_friend.mongodb.name' } ]

app.data_provider.product.item:
class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider\ProductDocumentItemDataProvider'
ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider\ProductItemDataProvider:
public: false
arguments: ['@doctrine_mongodb']
arguments:
$managerRegistry: '@doctrine_mongodb'
$orm: false
tags:
- { name: 'api_platform.item_data_provider' }
- name: 'api_platform.item_data_provider'

app.data_provider.taxon.item:
class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider\TaxonDocumentItemDataProvider'
ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider\TaxonItemDataProvider:
public: false
arguments: ['@doctrine_mongodb']
arguments:
$managerRegistry: '@doctrine_mongodb'
$orm: false
tags:
- { name: 'api_platform.item_data_provider' }
- name: 'api_platform.item_data_provider'

app.dummy_dto_no_input.data_provider:
class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider\DummyDtoNoInputCollectionDataProvider'
Expand Down
16 changes: 8 additions & 8 deletions tests/Fixtures/app/config/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ services:
arguments: [ { 'name': 'ipartial', 'description': 'ipartial' } ]
tags: [ { name: 'api_platform.filter', id: 'related_to_dummy_friend.name' } ]

app.data_provider.product.item:
class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider\ProductItemDataProvider'
ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider\ProductItemDataProvider:
public: false
arguments: ['@doctrine']
arguments:
$managerRegistry: '@doctrine'
tags:
- { name: 'api_platform.item_data_provider' }
- name: 'api_platform.item_data_provider'

app.data_provider.taxon.item:
class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider\TaxonItemDataProvider'
ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider\TaxonItemDataProvider:
public: false
arguments: ['@doctrine']
arguments:
$managerRegistry: '@doctrine'
tags:
- { name: 'api_platform.item_data_provider' }
- name: 'api_platform.item_data_provider'

app.dummy_dto_no_input.data_provider:
class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider\DummyDtoNoInputCollectionDataProvider'
Expand Down

0 comments on commit 4891c4a

Please sign in to comment.