Skip to content

Test patching 1:n relations #4263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion features/main/patch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Feature: Sending PATCH requets
Then the JSON node "name" should not exist

@createSchema
Scenario: Patch the relation
Scenario: Patch 1:1 relation
Given there is a PatchDummyRelation
When I add "Content-Type" header equal to "application/merge-patch+json"
And I send a "PATCH" request to "/patch_dummy_relations/1" with body:
Expand Down Expand Up @@ -58,3 +58,44 @@ Feature: Sending PATCH requets
}
}
"""

@createSchema
Scenario: Patch 1:n relation
Given there is a PatchDummyRelation
When I add "Content-Type" header equal to "application/merge-patch+json"
And I send a "PATCH" request to "/patch_dummy_relations/1" with body:
"""
{
"relatedDummies": [
{
"@id": "/related_dummies/2",
"symfony": "A new name"
}
]
}
"""
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/PatchDummyRelation",
"@id": "/patch_dummy_relations/1",
"@type": "PatchDummyRelation",
"relatedDummies": [
{
"@id": "/related_dummies/1",
"@type": "https://schema.org/Product",
"id": 1,
"symfony": "symfony"
},
{
"@id": "/related_dummies/2",
"@type": "https://schema.org/Product",
"id": 2,
"symfony": "A new name"
}
]
}
"""
10 changes: 8 additions & 2 deletions tests/Behat/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -1833,8 +1833,14 @@ public function thereIsAnInitializeInput(int $id)
public function thereIsAPatchDummyRelation()
{
$dummy = $this->buildPatchDummyRelation();
$related = $this->buildRelatedDummy();
$dummy->setRelated($related);
$related1 = $this->buildRelatedDummy();
$dummy->setRelated($related1);

$related2 = $this->buildRelatedDummy();
$dummy->addRelatedDummy($related2);
$related3 = $this->buildRelatedDummy();
$dummy->addRelatedDummy($related3);

$this->manager->persist($related);
$this->manager->persist($dummy);
$this->manager->flush();
Expand Down
37 changes: 37 additions & 0 deletions tests/Fixtures/TestBundle/Entity/PatchDummyRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

Expand Down Expand Up @@ -47,6 +48,17 @@ class PatchDummyRelation
*/
protected $related;

/**
* @ORM\OneToMany(targetEntity="RelatedDummy", mappedBy="relatedPatchDummy")
* @Groups({"chicago"})
*/
protected $relatedDummies;

public function __construct()
{
$this->relatedDummies = new ArrayCollection();
}

public function getRelated()
{
return $this->related;
Expand All @@ -56,4 +68,29 @@ public function setRelated(RelatedDummy $relatedDummy)
{
$this->related = $relatedDummy;
}

/**
* @return Collection|RelatedDummy[]
*/
public function getRelatedDummies(): Collection
{
return $this->relatedDummies;
}

public function addRelatedDummy(RelatedDummy $relatedDummy): self
{
if (!$this->relatedDummies->contains($relatedDummy)) {
$this->relatedDummies[] = $relatedDummy;
$relatedDummy->relatedPatchDummy = $this;
}

return $this;
}

public function removeRelatedDummy(RelatedDummy $relatedDummy): self
{
$this->relatedDummies->removeElement($relatedDummy);

return $this;
}
}
5 changes: 5 additions & 0 deletions tests/Fixtures/TestBundle/Entity/RelatedDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ class RelatedDummy extends ParentDummy
*/
public $relatedToDummyFriend;

/**
* @ORM\ManyToOne(targetEntity="PatchDummyRelation", inversedBy="relatedDummies")
*/
public $relatedPatchDummy;

/**
* @var bool A dummy bool
*
Expand Down