diff --git a/features/main/patch.feature b/features/main/patch.feature index eeddbc5a1e0..2d3f1e45a0e 100644 --- a/features/main/patch.feature +++ b/features/main/patch.feature @@ -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: @@ -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" + } + ] + } + """ diff --git a/tests/Behat/DoctrineContext.php b/tests/Behat/DoctrineContext.php index ba3c614b24a..0e276958f2d 100644 --- a/tests/Behat/DoctrineContext.php +++ b/tests/Behat/DoctrineContext.php @@ -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(); diff --git a/tests/Fixtures/TestBundle/Entity/PatchDummyRelation.php b/tests/Fixtures/TestBundle/Entity/PatchDummyRelation.php index aa830e506cc..4fe9c3c7bb9 100644 --- a/tests/Fixtures/TestBundle/Entity/PatchDummyRelation.php +++ b/tests/Fixtures/TestBundle/Entity/PatchDummyRelation.php @@ -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; @@ -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; @@ -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; + } } diff --git a/tests/Fixtures/TestBundle/Entity/RelatedDummy.php b/tests/Fixtures/TestBundle/Entity/RelatedDummy.php index 73844a82116..f8830833bf0 100644 --- a/tests/Fixtures/TestBundle/Entity/RelatedDummy.php +++ b/tests/Fixtures/TestBundle/Entity/RelatedDummy.php @@ -78,6 +78,11 @@ class RelatedDummy extends ParentDummy */ public $relatedToDummyFriend; + /** + * @ORM\ManyToOne(targetEntity="PatchDummyRelation", inversedBy="relatedDummies") + */ + public $relatedPatchDummy; + /** * @var bool A dummy bool *