Skip to content

Commit 4891c4a

Browse files
authored
Merge pull request #2917 from teohhanhui/fix/simplify-interface-as-resource-test-case
Simplify "interface as resource" test case
2 parents 9ad81f8 + f13024e commit 4891c4a

File tree

12 files changed

+50
-128
lines changed

12 files changed

+50
-128
lines changed

features/bootstrap/DoctrineContext.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,8 +1330,10 @@ public function thereIsTheFollowingProduct(PyStringNode $dataNode): void
13301330
$product = $this->isOrm() ? new Product() : new ProductDocument();
13311331
$product->setCode($data['code']);
13321332
if (isset($data['mainTaxon'])) {
1333-
$mainTaxonId = (int) str_replace('/taxons/', '', $data['mainTaxon']);
1334-
$mainTaxon = $this->manager->getRepository($this->isOrm() ? Taxon::class : TaxonDocument::class)->find($mainTaxonId);
1333+
$mainTaxonCode = str_replace('/taxons/', '', $data['mainTaxon']);
1334+
$mainTaxon = $this->manager->getRepository($this->isOrm() ? Taxon::class : TaxonDocument::class)->findOneBy([
1335+
'code' => $mainTaxonCode,
1336+
]);
13351337
$product->setMainTaxon($mainTaxon);
13361338
}
13371339
$this->manager->persist($product);

features/jsonld/interface_as_resource.feature

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ Feature: JSON-LD using interface as resource
1515
"code": "WONDERFUL_TAXON"
1616
}
1717
"""
18-
When I send a "GET" request to "/taxons/1"
18+
When I send a "GET" request to "/taxons/WONDERFUL_TAXON"
1919
Then the response status code should be 200
2020
And the response should be in JSON
2121
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
2222
And the JSON should be equal to:
2323
"""
2424
{
2525
"@context": "/contexts/Taxon",
26-
"@id": "/taxons/1",
26+
"@id": "/taxons/WONDERFUL_TAXON",
2727
"@type": "Taxon",
2828
"code": "WONDERFUL_TAXON"
2929
}
@@ -34,22 +34,22 @@ Feature: JSON-LD using interface as resource
3434
"""
3535
{
3636
"code": "GREAT_PRODUCT",
37-
"mainTaxon": "/taxons/1"
37+
"mainTaxon": "/taxons/WONDERFUL_TAXON"
3838
}
3939
"""
40-
When I send a "GET" request to "/products/1"
40+
When I send a "GET" request to "/products/GREAT_PRODUCT"
4141
Then the response status code should be 200
4242
And the response should be in JSON
4343
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
4444
And the JSON should be equal to:
4545
"""
4646
{
4747
"@context": "/contexts/Product",
48-
"@id": "/products/1",
48+
"@id": "/products/GREAT_PRODUCT",
4949
"@type": "Product",
5050
"code": "GREAT_PRODUCT",
5151
"mainTaxon": {
52-
"@id": "/taxons/1",
52+
"@id": "/taxons/WONDERFUL_TAXON",
5353
"@type": "Taxon",
5454
"code": "WONDERFUL_TAXON"
5555
}

tests/Fixtures/TestBundle/DataProvider/ProductDocumentItemDataProvider.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/Fixtures/TestBundle/DataProvider/ProductItemDataProvider.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@
1515

1616
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
1717
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
18+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Product as ProductDocument;
1819
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Product;
1920
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Model\ProductInterface;
2021
use Doctrine\Common\Persistence\ManagerRegistry;
2122

2223
class ProductItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
2324
{
2425
private $managerRegistry;
26+
private $orm;
2527

26-
public function __construct(ManagerRegistry $managerRegistry)
28+
public function __construct(ManagerRegistry $managerRegistry, bool $orm = true)
2729
{
2830
$this->managerRegistry = $managerRegistry;
31+
$this->orm = $orm;
2932
}
3033

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

tests/Fixtures/TestBundle/DataProvider/TaxonDocumentItemDataProvider.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/Fixtures/TestBundle/DataProvider/TaxonItemDataProvider.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@
1515

1616
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
1717
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
18+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Taxon as TaxonDocument;
1819
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Taxon;
1920
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Model\TaxonInterface;
2021
use Doctrine\Common\Persistence\ManagerRegistry;
2122

2223
class TaxonItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
2324
{
2425
private $managerRegistry;
26+
private $orm;
2527

26-
public function __construct(ManagerRegistry $managerRegistry)
28+
public function __construct(ManagerRegistry $managerRegistry, bool $orm = true)
2729
{
2830
$this->managerRegistry = $managerRegistry;
31+
$this->orm = $orm;
2932
}
3033

3134
/**
@@ -41,6 +44,8 @@ public function supports(string $resourceClass, string $operationName = null, ar
4144
*/
4245
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
4346
{
44-
return $this->managerRegistry->getRepository(Taxon::class)->find($id);
47+
return $this->managerRegistry->getRepository($this->orm ? Taxon::class : TaxonDocument::class)->findOneBy([
48+
'code' => $id,
49+
]);
4550
}
4651
}

tests/Fixtures/TestBundle/Entity/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Product implements ProductInterface
3434
/**
3535
* @var string|null
3636
*
37-
* @ORM\Column(type="string")
37+
* @ORM\Column(type="string", unique=true)
3838
*/
3939
private $code;
4040

tests/Fixtures/TestBundle/Entity/Taxon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Taxon implements TaxonInterface
3333
/**
3434
* @var string|null
3535
*
36-
* @ORM\Column(type="string")
36+
* @ORM\Column(type="string", unique=true)
3737
*/
3838
private $code;
3939

tests/Fixtures/TestBundle/Model/ProductInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
*/
3232
interface ProductInterface
3333
{
34-
/**
35-
* @ApiProperty(identifier=true)
36-
*/
3734
public function getId();
3835

3936
/**
37+
* @ApiProperty(identifier=true)
38+
*
4039
* @Groups({"product_read"})
40+
*
4141
* @Assert\NotBlank
4242
*/
4343
public function getCode(): ?string;

tests/Fixtures/TestBundle/Model/TaxonInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
*/
3232
interface TaxonInterface
3333
{
34-
/**
35-
* @ApiProperty(identifier=true)
36-
*/
3734
public function getId();
3835

3936
/**
37+
* @ApiProperty(identifier=true)
38+
*
4039
* @Groups({"product_read", "taxon_read"})
40+
*
4141
* @Assert\NotBlank
4242
*/
4343
public function getCode(): ?string;

0 commit comments

Comments
 (0)