Skip to content

Commit d17eb1f

Browse files
author
Amrouche Hamza
committed
add failing behat test case and revert ItemNormalizer
1 parent 1caa29c commit d17eb1f

File tree

6 files changed

+174
-7
lines changed

6 files changed

+174
-7
lines changed

features/main/custom_normalized.feature

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,67 @@ Feature: Using custom normalized entity
2222
"@context": "/contexts/CustomNormalizedDummy",
2323
"@id": "/custom_normalized_dummies/1",
2424
"@type": "CustomNormalizedDummy",
25+
"id": 1,
2526
"name": "My Dummy",
2627
"alias": "My alias"
2728
}
2829
"""
2930

31+
Scenario: Create a resource with relation
32+
When I add "Content-Type" header equal to "application/json"
33+
When I add "Accept" header equal to "application/json"
34+
And I send a "POST" request to "/related_normalized_dummies" with body:
35+
"""
36+
{
37+
"name": "My Dummy"
38+
}
39+
"""
40+
Then the response status code should be 201
41+
And the response should be in JSON
42+
And the header "Content-Type" should be equal to "application/json; charset=utf-8"
43+
And the JSON should be equal to:
44+
"""
45+
{
46+
"id": 1,
47+
"name": "My Dummy",
48+
"customNormalizedDummy": []
49+
}
50+
"""
51+
52+
Scenario: Create a resource with relation
53+
When I add "Content-Type" header equal to "application/json"
54+
When I add "Accept" header equal to "application/json"
55+
And I send a "PUT" request to "/related_normalized_dummies/1" with body:
56+
"""
57+
{
58+
"name": "My Dummy",
59+
"alias": "My alias",
60+
"customNormalizedDummy":[{
61+
"@context": "/contexts/CustomNormalizedDummy",
62+
"@id": "/custom_normalized_dummies/2",
63+
"@type": "CustomNormalizedDummy",
64+
"id": 2,
65+
"name": "My Dummy",
66+
"alias": "My alias"
67+
}]
68+
}
69+
"""
70+
Then the response status code should be 201
71+
And the response should be in JSON
72+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
73+
And the JSON should be equal to:
74+
"""
75+
{
76+
"@context": "/contexts/CustomNormalizedDummy",
77+
"@id": "/custom_normalized_dummies/1",
78+
"@type": "CustomNormalizedDummy",
79+
"id": 1,
80+
"name": "My Dummy",
81+
"alias": "My alias"
82+
}
83+
"""
84+
85+
3086
Scenario: Get a resource
3187
When I send a "GET" request to "/custom_normalized_dummies/1"
3288
Then the response status code should be 200
@@ -38,6 +94,7 @@ Feature: Using custom normalized entity
3894
"@context": "/contexts/CustomNormalizedDummy",
3995
"@id": "/custom_normalized_dummies/1",
4096
"@type": "CustomNormalizedDummy",
97+
"id": 1,
4198
"name": "My Dummy",
4299
"alias": "My alias"
43100
}
@@ -58,6 +115,7 @@ Feature: Using custom normalized entity
58115
{
59116
"@id": "/custom_normalized_dummies/1",
60117
"@type": "CustomNormalizedDummy",
118+
"id": 1,
61119
"name": "My Dummy",
62120
"alias": "My alias"
63121
}
@@ -83,6 +141,7 @@ Feature: Using custom normalized entity
83141
"@context": "/contexts/CustomNormalizedDummy",
84142
"@id": "/custom_normalized_dummies/1",
85143
"@type": "CustomNormalizedDummy",
144+
"id": 1,
86145
"name": "My Dummy modified",
87146
"alias": "My alias"
88147
}

src/Serializer/ItemNormalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ final class ItemNormalizer extends AbstractItemNormalizer
2828
public function denormalize($data, $class, $format = null, array $context = [])
2929
{
3030
// Avoid issues with proxies if we populated the object
31-
if (isset($data['@id']) && !isset($context['object_to_populate'])) {
31+
if (isset($data['id']) && !isset($context['object_to_populate'])) {
3232
if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
3333
throw new InvalidArgumentException('Update is not allowed for this operation.');
3434
}
3535

36-
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['@id'], $context + ['fetch_data' => false]);
36+
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['id'], $context + ['fetch_data' => false]);
3737
}
3838

3939
return parent::denormalize($data, $class, $format, $context);

tests/Fixtures/TestBundle/Entity/CustomNormalizedDummy.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CustomNormalizedDummy
3636
* @ORM\Column(type="integer")
3737
* @ORM\Id
3838
* @ORM\GeneratedValue(strategy="AUTO")
39+
* @Groups({"input", "output"})
3940
*/
4041
private $id;
4142

tests/Fixtures/TestBundle/Entity/DummyFriend.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class DummyFriend
4949
/**
5050
* Get id.
5151
*
52-
* @return id
52+
* @return int
5353
*/
5454
public function getId()
5555
{
@@ -69,7 +69,7 @@ public function setId($id)
6969
/**
7070
* Get name.
7171
*
72-
* @return name
72+
* @return string
7373
*/
7474
public function getName()
7575
{
@@ -79,7 +79,7 @@ public function getName()
7979
/**
8080
* Set name.
8181
*
82-
* @param name the value to set
82+
* @param string the value to set
8383
*/
8484
public function setName($name)
8585
{
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
13+
14+
use ApiPlatform\Core\Annotation\ApiProperty;
15+
use ApiPlatform\Core\Annotation\ApiResource;
16+
use Doctrine\Common\Collections\ArrayCollection;
17+
use Doctrine\ORM\Mapping as ORM;
18+
use Symfony\Component\Serializer\Annotation\Groups;
19+
use Symfony\Component\Validator\Constraints as Assert;
20+
21+
/**
22+
* Related to Normalized Dummy.
23+
*
24+
* @author Amrouche Hamza <[email protected]>
25+
*
26+
* @ApiResource(attributes={
27+
* "normalization_context"={"groups"={"related_output", "output"}},
28+
* "denormalization_context"={"groups"={"related_input", "input"}}
29+
*
30+
* })
31+
* @ORM\Entity
32+
*/
33+
class RelatedNormalizedDummy
34+
{
35+
/**
36+
* @var int The id
37+
*
38+
* @ORM\Column(type="integer")
39+
* @ORM\Id
40+
* @ORM\GeneratedValue(strategy="AUTO")
41+
* @Groups({"related_output", "related_input"})
42+
*/
43+
private $id;
44+
45+
/**
46+
* @var string The dummy name
47+
*
48+
* @ORM\Column
49+
* @Assert\NotBlank
50+
* @ApiProperty(iri="http://schema.org/name")
51+
* @Groups({"related_output", "related_input"})
52+
*/
53+
private $name;
54+
55+
/**
56+
* @var ArrayCollection Several Normalized dummies
57+
*
58+
* @ORM\ManyToMany(targetEntity="CustomNormalizedDummy")
59+
* @Groups({"related_output", "related_input"})
60+
*/
61+
public $customNormalizedDummy;
62+
63+
public function __construct()
64+
{
65+
$this->customNormalizedDummy = new ArrayCollection();
66+
}
67+
68+
/**
69+
* @return int
70+
*/
71+
public function getId()
72+
{
73+
return $this->id;
74+
}
75+
76+
/**
77+
* @param string $name
78+
*/
79+
public function setName($name)
80+
{
81+
$this->name = $name;
82+
}
83+
84+
/**
85+
* @return string
86+
*/
87+
public function getName()
88+
{
89+
return $this->name;
90+
}
91+
92+
/**
93+
* @return ArrayCollection
94+
*/
95+
public function getCustomNormalizedDummy()
96+
{
97+
return $this->customNormalizedDummy;
98+
}
99+
100+
/**
101+
* @param ArrayCollection $customNormalizedDummy
102+
*/
103+
public function setCustomNormalizedDummy(ArrayCollection $customNormalizedDummy)
104+
{
105+
$this->customNormalizedDummy = $customNormalizedDummy;
106+
}
107+
}

tests/Serializer/ItemNormalizerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function testDenormalizeWithIri()
153153
);
154154
$normalizer->setSerializer($serializerProphecy->reveal());
155155

156-
$this->assertInstanceOf(Dummy::class, $normalizer->denormalize(['@id' => '/dummies/12', 'id' => '12', 'name' => 'hello'], Dummy::class, null, $context));
156+
$this->assertInstanceOf(Dummy::class, $normalizer->denormalize(['id' => '/dummies/12', 'name' => 'hello'], Dummy::class, null, $context));
157157
}
158158

159159
/**
@@ -182,6 +182,6 @@ public function testDenormalizeWithIdAndUpdateNotAllowed()
182182
$resourceClassResolverProphecy->reveal()
183183
);
184184
$normalizer->setSerializer($serializerProphecy->reveal());
185-
$normalizer->denormalize(['@id' => '/dummies/12', 'id' => '12', 'name' => 'hello'], Dummy::class, null, $context);
185+
$normalizer->denormalize(['id' => '12', 'name' => 'hello'], Dummy::class, null, $context);
186186
}
187187
}

0 commit comments

Comments
 (0)