Skip to content

Commit 4413451

Browse files
committed
Add failing test where DataTransformer may fallback to returning the original object
1 parent 33d2da7 commit 4413451

File tree

5 files changed

+170
-16
lines changed

5 files changed

+170
-16
lines changed

features/bootstrap/DoctrineContext.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDtoCustom;
9191
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDtoNoInput;
9292
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDtoNoOutput;
93+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDtoOutputFallbackToSameClass;
9394
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDtoOutputSameClass;
9495
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyFriend;
9596
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyGroup;
@@ -591,7 +592,7 @@ public function thereAreDummyObjectsWithDummyDate(int $nb)
591592
$descriptions = ['Smart dummy.', 'Not so smart dummy.'];
592593

593594
for ($i = 1; $i <= $nb; ++$i) {
594-
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
595+
$date = new DateTime(sprintf('2015-04-%d', $i), new DateTimeZone('UTC'));
595596

596597
$dummy = $this->buildDummy();
597598
$dummy->setName('Dummy #'.$i);
@@ -622,11 +623,11 @@ public function thereAreDummyObjectsWithDummyDateAndDummyBoolean(int $nb, string
622623
$bool = false;
623624
} else {
624625
$expected = ['true', 'false', '1', '0'];
625-
throw new \InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $bool, implode('" | "', $expected)));
626+
throw new InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $bool, implode('" | "', $expected)));
626627
}
627628

628629
for ($i = 1; $i <= $nb; ++$i) {
629-
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
630+
$date = new DateTime(sprintf('2015-04-%d', $i), new DateTimeZone('UTC'));
630631

631632
$dummy = $this->buildDummy();
632633
$dummy->setName('Dummy #'.$i);
@@ -651,7 +652,7 @@ public function thereAreDummyObjectsWithDummyDateAndDummyBoolean(int $nb, string
651652
public function thereAreDummyObjectsWithDummyDateAndRelatedDummy(int $nb)
652653
{
653654
for ($i = 1; $i <= $nb; ++$i) {
654-
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
655+
$date = new DateTime(sprintf('2015-04-%d', $i), new DateTimeZone('UTC'));
655656

656657
$relatedDummy = $this->buildRelatedDummy();
657658
$relatedDummy->setName('RelatedDummy #'.$i);
@@ -679,7 +680,7 @@ public function thereAreDummyObjectsWithDummyDateAndRelatedDummy(int $nb)
679680
public function thereAreDummyObjectsWithDummyDateAndEmbeddedDummy(int $nb)
680681
{
681682
for ($i = 1; $i <= $nb; ++$i) {
682-
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
683+
$date = new DateTime(sprintf('2015-04-%d', $i), new DateTimeZone('UTC'));
683684

684685
$embeddableDummy = $this->buildEmbeddableDummy();
685686
$embeddableDummy->setDummyName('Embeddable #'.$i);
@@ -706,7 +707,7 @@ public function thereAreconvertedDateObjectsWith(int $nb)
706707
{
707708
for ($i = 1; $i <= $nb; ++$i) {
708709
$convertedDate = $this->buildConvertedDate();
709-
$convertedDate->nameConverted = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
710+
$convertedDate->nameConverted = new DateTime(sprintf('2015-04-%d', $i), new DateTimeZone('UTC'));
710711

711712
$this->manager->persist($convertedDate);
712713
}
@@ -792,7 +793,7 @@ public function thereAreDummyObjectsWithDummyBoolean(int $nb, string $bool)
792793
$bool = false;
793794
} else {
794795
$expected = ['true', 'false', '1', '0'];
795-
throw new \InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $bool, implode('" | "', $expected)));
796+
throw new InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $bool, implode('" | "', $expected)));
796797
}
797798
$descriptions = ['Smart dummy.', 'Not so smart dummy.'];
798799

@@ -820,7 +821,7 @@ public function thereAreDummyObjectsWithEmbeddedDummyBoolean(int $nb, string $bo
820821
$bool = false;
821822
} else {
822823
$expected = ['true', 'false', '1', '0'];
823-
throw new \InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $bool, implode('" | "', $expected)));
824+
throw new InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $bool, implode('" | "', $expected)));
824825
}
825826

826827
for ($i = 1; $i <= $nb; ++$i) {
@@ -847,7 +848,7 @@ public function thereAreDummyObjectsWithRelationEmbeddedDummyBoolean(int $nb, st
847848
$bool = false;
848849
} else {
849850
$expected = ['true', 'false', '1', '0'];
850-
throw new \InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $bool, implode('" | "', $expected)));
851+
throw new InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $bool, implode('" | "', $expected)));
851852
}
852853

853854
for ($i = 1; $i <= $nb; ++$i) {
@@ -977,7 +978,7 @@ public function thereIsAFooEntityWithRelatedBars()
977978
$foo = $this->buildDummyCar();
978979
$foo->setName('mustli');
979980
$foo->setCanSell(true);
980-
$foo->setAvailableAt(new \DateTime());
981+
$foo->setAvailableAt(new DateTime());
981982
$this->manager->persist($foo);
982983

983984
$bar1 = $this->buildDummyCarColor();
@@ -1078,7 +1079,7 @@ public function thePasswordForUserShouldBeHashed(string $password, string $user)
10781079
{
10791080
$user = $this->doctrine->getRepository($this->isOrm() ? User::class : UserDocument::class)->find($user);
10801081
if (!$this->passwordEncoder->isPasswordValid($user, $password)) {
1081-
throw new \Exception('User password mismatch');
1082+
throw new Exception('User password mismatch');
10821083
}
10831084
}
10841085

@@ -1143,7 +1144,7 @@ public function createPeopleWithPets()
11431144
public function thereAreDummyDateObjectsWithDummyDate(int $nb)
11441145
{
11451146
for ($i = 1; $i <= $nb; ++$i) {
1146-
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
1147+
$date = new DateTime(sprintf('2015-04-%d', $i), new DateTimeZone('UTC'));
11471148

11481149
$dummy = $this->buildDummyDate();
11491150
$dummy->dummyDate = $date;
@@ -1161,7 +1162,7 @@ public function thereAreDummyDateObjectsWithDummyDate(int $nb)
11611162
public function thereAreDummyDateObjectsWithNullableDateIncludeNullAfter(int $nb)
11621163
{
11631164
for ($i = 1; $i <= $nb; ++$i) {
1164-
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
1165+
$date = new DateTime(sprintf('2015-04-%d', $i), new DateTimeZone('UTC'));
11651166

11661167
$dummy = $this->buildDummyDate();
11671168
$dummy->dummyDate = $date;
@@ -1180,7 +1181,7 @@ public function thereAreDummyDateObjectsWithNullableDateIncludeNullAfter(int $nb
11801181
public function thereAreDummyDateObjectsWithNullableDateIncludeNullBefore(int $nb)
11811182
{
11821183
for ($i = 1; $i <= $nb; ++$i) {
1183-
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
1184+
$date = new DateTime(sprintf('2015-04-%d', $i), new DateTimeZone('UTC'));
11841185

11851186
$dummy = $this->buildDummyDate();
11861187
$dummy->dummyDate = $date;
@@ -1199,7 +1200,7 @@ public function thereAreDummyDateObjectsWithNullableDateIncludeNullBefore(int $n
11991200
public function thereAreDummyDateObjectsWithNullableDateIncludeNullBeforeAndAfter(int $nb)
12001201
{
12011202
for ($i = 1; $i <= $nb; ++$i) {
1202-
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
1203+
$date = new DateTime(sprintf('2015-04-%d', $i), new DateTimeZone('UTC'));
12031204

12041205
$dummy = $this->buildDummyDate();
12051206
$dummy->dummyDate = $date;
@@ -1217,7 +1218,7 @@ public function thereAreDummyDateObjectsWithNullableDateIncludeNullBeforeAndAfte
12171218
public function thereAreDummyImmutableDateObjectsWithDummyDate(int $nb)
12181219
{
12191220
for ($i = 1; $i <= $nb; ++$i) {
1220-
$date = new \DateTimeImmutable(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
1221+
$date = new DateTimeImmutable(sprintf('2015-04-%d', $i), new DateTimeZone('UTC'));
12211222
$dummy = new DummyImmutableDate();
12221223
$dummy->dummyDate = $date;
12231224

@@ -1393,6 +1394,19 @@ public function thereIsADummyDtoOutputSameClass()
13931394
$this->manager->clear();
13941395
}
13951396

1397+
/**
1398+
* @Given there is a DummyDtoOutputFallbackToSameClass
1399+
*/
1400+
public function thereIsADummyDtoOutputFallbackToSameClass()
1401+
{
1402+
$dto = new DummyDtoOutputFallbackToSameClass();
1403+
$dto->lorem = 'test';
1404+
$dto->ipsum = '1';
1405+
$this->manager->persist($dto);
1406+
$this->manager->flush();
1407+
$this->manager->clear();
1408+
}
1409+
13961410
/**
13971411
* @Given there is an order with same customer and recipient
13981412
*/

features/jsonld/input_output.feature

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,25 @@ Feature: JSON-LD DTO input and output
102102
}
103103
"""
104104

105+
@createSchema
106+
Scenario: Get an item with a data transformer that will return the original class as a fallback
107+
Given there is a DummyDtoOutputFallbackToSameClass
108+
When I send a "GET" request to "/dummy_dto_output_fallback_to_same_classes/1"
109+
Then the response status code should be 200
110+
And the response should be in JSON
111+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
112+
And the JSON should be equal to:
113+
"""
114+
{
115+
"@context": "/contexts/DummyDtoOutputFallbackToSameClass",
116+
"@id": "/dummy_dto_output_fallback_to_same_classes/1",
117+
"@type": "DummyDtoOutputFallbackToSameClass",
118+
"lorem": "test",
119+
"ipsum": "1",
120+
"id": 1
121+
}
122+
"""
123+
105124
@createSchema
106125
Scenario: Create a DummyDtoCustom object without output
107126
When I send a "POST" request to "/dummy_dto_custom_post_without_output" with body:
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\DataTransformer;
15+
16+
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
17+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\OutputDtoDummy;
18+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDtoOutputFallbackToSameClass;
19+
20+
/**
21+
* OutputDtoUnmodifiedDataTransformer
22+
*
23+
* @author Daniel West <[email protected]>
24+
*/
25+
final class OutputDtoUnmodifiedDataTransformer implements DataTransformerInterface
26+
{
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function transform($object, string $to, array $context = [])
31+
{
32+
return $object;
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function supportsTransformation($data, string $to, array $context = []): bool
39+
{
40+
return $data instanceof DummyDtoOutputFallbackToSameClass && OutputDtoDummy::class === $to;
41+
}
42+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto;
15+
16+
/**
17+
* OutputDtoDummy
18+
*
19+
* @author Daniel West <[email protected]>
20+
*/
21+
class OutputDtoDummy
22+
{}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\OutputDtoDummy;
18+
use Doctrine\ORM\Mapping as ORM;
19+
20+
/**
21+
* Dummy InputOutput.
22+
*
23+
* @author Kévin Dunglas <[email protected]>
24+
*
25+
* @ApiResource(attributes={"output"=OutputDtoDummy::class})
26+
* @ORM\Entity
27+
*/
28+
class DummyDtoOutputFallbackToSameClass
29+
{
30+
/**
31+
* @var int The id
32+
*
33+
* @ORM\Column(type="integer")
34+
* @ORM\Id
35+
* @ORM\GeneratedValue(strategy="AUTO")
36+
*/
37+
private $id;
38+
39+
/**
40+
* @var string
41+
*
42+
* @ORM\Column
43+
*/
44+
public $lorem;
45+
46+
/**
47+
* @var string
48+
*
49+
* @ORM\Column
50+
*/
51+
public $ipsum;
52+
53+
public function getId()
54+
{
55+
return $this->id;
56+
}
57+
}

0 commit comments

Comments
 (0)