From e883346e4ce3d9e375bc5f2a5ca551a4ccc1f736 Mon Sep 17 00:00:00 2001 From: Urban Suppiger Date: Sun, 14 Apr 2024 13:15:16 +0200 Subject: [PATCH] fix tests for MongoDB --- .../PropertyCollectionIriOnlyRelation.php | 43 +++++++++++++++ ...tyCollectionIriOnlyRelationSecondLevel.php | 54 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 tests/Fixtures/TestBundle/Document/PropertyCollectionIriOnlyRelationSecondLevel.php diff --git a/tests/Fixtures/TestBundle/Document/PropertyCollectionIriOnlyRelation.php b/tests/Fixtures/TestBundle/Document/PropertyCollectionIriOnlyRelation.php index a7fcbd46d8d..af90a375e5e 100644 --- a/tests/Fixtures/TestBundle/Document/PropertyCollectionIriOnlyRelation.php +++ b/tests/Fixtures/TestBundle/Document/PropertyCollectionIriOnlyRelation.php @@ -13,9 +13,12 @@ namespace ApiPlatform\Tests\Fixtures\TestBundle\Document; +use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\GetCollection; use ApiPlatform\Metadata\Link; use ApiPlatform\Metadata\Post; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; use Symfony\Component\Serializer\Annotation\Groups; @@ -42,6 +45,16 @@ class PropertyCollectionIriOnlyRelation #[ODM\ReferenceOne(targetDocument: PropertyCollectionIriOnly::class)] private ?PropertyCollectionIriOnly $propertyCollectionIriOnly = null; + #[ODM\ReferenceMany(targetDocument: PropertyCollectionIriOnlyRelationSecondLevel::class)] + #[ApiProperty(uriTemplate: '/property_collection_iri_only_relations/{parentId}/children')] + #[Groups('read')] + private Collection $children; + + public function __construct() + { + $this->children = new ArrayCollection(); + } + public function getId(): ?int { return $this->id ?? 9999; @@ -56,4 +69,34 @@ public function setPropertyCollectionIriOnly(?PropertyCollectionIriOnly $propert { $this->propertyCollectionIriOnly = $propertyCollectionIriOnly; } + + /** + * @return Collection + */ + public function getChildren(): Collection + { + return $this->children; + } + + public function addChild(PropertyCollectionIriOnlyRelationSecondLevel $child): self + { + if (!$this->children->contains($child)) { + $this->children->add($child); + $child->setParent($this); + } + + return $this; + } + + public function removeChild(PropertyCollectionIriOnlyRelationSecondLevel $child): self + { + if ($this->children->removeElement($child)) { + // set the owning side to null (unless already changed) + if ($child->getParent() === $this) { + $child->setParent(null); + } + } + + return $this; + } } diff --git a/tests/Fixtures/TestBundle/Document/PropertyCollectionIriOnlyRelationSecondLevel.php b/tests/Fixtures/TestBundle/Document/PropertyCollectionIriOnlyRelationSecondLevel.php new file mode 100644 index 00000000000..b190aa0a94e --- /dev/null +++ b/tests/Fixtures/TestBundle/Document/PropertyCollectionIriOnlyRelationSecondLevel.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\Tests\Fixtures\TestBundle\Document; + +use ApiPlatform\Metadata\GetCollection; +use ApiPlatform\Metadata\Link; +use ApiPlatform\Metadata\Post; +use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; + +#[ + Post, + GetCollection(uriTemplate: '/property-collection-relation-second-levels'), + GetCollection( + uriTemplate: '/property_collection_iri_only_relations/{parentId}/children', + uriVariables: [ + 'parentId' => new Link(toProperty: 'parent', fromClass: PropertyCollectionIriOnlyRelation::class), + ] + ) +] +#[ODM\Document] +class PropertyCollectionIriOnlyRelationSecondLevel +{ + #[ODM\Id(strategy: 'INCREMENT', type: 'int')] + private ?int $id = null; + + #[ODM\ReferenceOne(targetDocument: PropertyCollectionIriOnlyRelation::class)] + private ?PropertyCollectionIriOnlyRelation $parent = null; + + public function getId(): ?int + { + return $this->id ?? 9999; + } + + public function getParent(): ?PropertyCollectionIriOnlyRelation + { + return $this->parent; + } + + public function setParent(?PropertyCollectionIriOnlyRelation $parent): void + { + $this->parent = $parent; + } +}