From 51f6a49fd601e7e56976c93383acb3cb63c940f6 Mon Sep 17 00:00:00 2001 From: Marcin Czarnecki Date: Wed, 28 Aug 2024 19:38:04 +0200 Subject: [PATCH] Doctrine ODM: fix tests for 2.0 --- .github/workflows/ci.yaml | 3 --- phpstan/no-attributes.neon | 1 + tests/Fixtures/DoctrinePHPCR/Author.php | 6 ++++++ tests/Fixtures/DoctrinePHPCR/BlogPost.php | 13 ++++++++++++ tests/Fixtures/DoctrinePHPCR/Comment.php | 8 +++++++ .../Driver/DoctrinePHPCRDriverTest.php | 21 ++++++++++++++----- 6 files changed, 44 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 4cc603b62..5b12777d2 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -43,9 +43,6 @@ jobs: if: ${{ matrix.composer-stability }} run: composer config minimum-stability ${{ matrix.composer-stability }} - - name: Uninstall Doctrine ODM - run: composer remove doctrine/phpcr-odm jackalope/jackalope-doctrine-dbal --no-update --dev - - name: "Install dependencies with Composer" uses: "ramsey/composer-install@v1" with: diff --git a/phpstan/no-attributes.neon b/phpstan/no-attributes.neon index f541879d2..659043e82 100644 --- a/phpstan/no-attributes.neon +++ b/phpstan/no-attributes.neon @@ -1,3 +1,4 @@ parameters: ignoreErrors: - '~Call to an undefined method Reflection.*::getAttributes\(\)~' + - '~Instantiated class Doctrine\\ODM\\PHPCR\\Mapping\\Driver\\AttributeDriver not found~' diff --git a/tests/Fixtures/DoctrinePHPCR/Author.php b/tests/Fixtures/DoctrinePHPCR/Author.php index fa409af8c..56f1189f2 100644 --- a/tests/Fixtures/DoctrinePHPCR/Author.php +++ b/tests/Fixtures/DoctrinePHPCR/Author.php @@ -5,14 +5,19 @@ namespace JMS\Serializer\Tests\Fixtures\DoctrinePHPCR; use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM; +use Doctrine\ODM\PHPCR\Mapping\Attributes\Document; +use Doctrine\ODM\PHPCR\Mapping\Attributes\Field; +use Doctrine\ODM\PHPCR\Mapping\Attributes\Id; use JMS\Serializer\Annotation\SerializedName; /** @PHPCRODM\Document */ +#[Document] class Author { /** * @PHPCRODM\Id() */ + #[Id] protected $id; /** @@ -20,6 +25,7 @@ class Author * @SerializedName("full_name") */ #[SerializedName(name: 'full_name')] + #[Field(type: 'string')] private $name; public function __construct($name) diff --git a/tests/Fixtures/DoctrinePHPCR/BlogPost.php b/tests/Fixtures/DoctrinePHPCR/BlogPost.php index 796cb70de..fbe8e072a 100644 --- a/tests/Fixtures/DoctrinePHPCR/BlogPost.php +++ b/tests/Fixtures/DoctrinePHPCR/BlogPost.php @@ -6,6 +6,11 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM; +use Doctrine\ODM\PHPCR\Mapping\Attributes\Document; +use Doctrine\ODM\PHPCR\Mapping\Attributes\Field; +use Doctrine\ODM\PHPCR\Mapping\Attributes\Id; +use Doctrine\ODM\PHPCR\Mapping\Attributes\ReferenceMany; +use Doctrine\ODM\PHPCR\Mapping\Attributes\ReferenceOne; use JMS\Serializer\Annotation\Groups; use JMS\Serializer\Annotation\SerializedName; use JMS\Serializer\Annotation\Type; @@ -18,11 +23,13 @@ * @XmlRoot("blog-post") */ #[XmlRoot(name: 'blog-post')] +#[Document] class BlogPost { /** * @PHPCRODM\Id() */ + #[Id] protected $id; /** @@ -30,11 +37,13 @@ class BlogPost * @Groups({"comments","post"}) */ #[Groups(groups: ['comments', 'post'])] + #[Field(type: 'string')] private $title; /** * @PHPCRODM\Field(type="string") */ + #[Field(type: 'string')] protected $slug; /** @@ -42,6 +51,7 @@ class BlogPost * @XmlAttribute */ #[XmlAttribute] + #[Field(type: 'date')] private $createdAt; /** @@ -54,6 +64,7 @@ class BlogPost * @Groups({"post"}) * @XmlAttribute */ + #[Field(type: 'boolean')] #[Type(name: 'integer')] #[SerializedName(name: 'is_published')] #[Groups(groups: ['post'])] @@ -67,6 +78,7 @@ class BlogPost */ #[XmlList(entry: 'comment', inline: true)] #[Groups(groups: ['comments'])] + #[ReferenceMany(targetDocument:'Comment', property:'blogPost')] private $comments; /** @@ -74,6 +86,7 @@ class BlogPost * @Groups({"post"}) */ #[Groups(groups: ['post'])] + #[ReferenceOne(targetDocument:'Author')] private $author; public function __construct($title, Author $author, \DateTime $createdAt) diff --git a/tests/Fixtures/DoctrinePHPCR/Comment.php b/tests/Fixtures/DoctrinePHPCR/Comment.php index 86daa66bf..00ab4c835 100644 --- a/tests/Fixtures/DoctrinePHPCR/Comment.php +++ b/tests/Fixtures/DoctrinePHPCR/Comment.php @@ -6,18 +6,25 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM; +use Doctrine\ODM\PHPCR\Mapping\Attributes\Document; +use Doctrine\ODM\PHPCR\Mapping\Attributes\Field; +use Doctrine\ODM\PHPCR\Mapping\Attributes\Id; +use Doctrine\ODM\PHPCR\Mapping\Attributes\ReferenceOne; /** @PHPCRODM\Document */ +#[Document] class Comment { /** * @PHPCRODM\Id() */ + #[Id] protected $id; /** * @PHPCRODM\ReferenceOne(targetDocument="Author") */ + #[ReferenceOne(targetDocument:'Author')] private $author; /** @PHPCRODM\ReferenceOne(targetDocument="BlogPost") */ @@ -26,6 +33,7 @@ class Comment /** * @PHPCRODM\Field(type="string") */ + #[Field(type: 'string')] private $text; public function __construct(Author $author, $text) diff --git a/tests/Metadata/Driver/DoctrinePHPCRDriverTest.php b/tests/Metadata/Driver/DoctrinePHPCRDriverTest.php index 224dcb0d7..349e49a82 100644 --- a/tests/Metadata/Driver/DoctrinePHPCRDriverTest.php +++ b/tests/Metadata/Driver/DoctrinePHPCRDriverTest.php @@ -8,8 +8,10 @@ use Doctrine\ODM\PHPCR\Configuration; use Doctrine\ODM\PHPCR\DocumentManager; use Doctrine\ODM\PHPCR\Mapping\Driver\AnnotationDriver as DoctrinePHPCRDriver; +use Doctrine\ODM\PHPCR\Mapping\Driver\AttributeDriver as AttributeDoctrinePHPCRDriver; use Doctrine\Persistence\ManagerRegistry; use JMS\Serializer\Metadata\Driver\AnnotationDriver; +use JMS\Serializer\Metadata\Driver\AnnotationOrAttributeDriver; use JMS\Serializer\Metadata\Driver\DoctrinePHPCRTypeDriver; use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; use JMS\Serializer\Tests\Fixtures\BlogPost; @@ -101,10 +103,15 @@ protected function getDocumentManager() $config = new Configuration(); $config->setProxyDir(sys_get_temp_dir() . '/JMSDoctrineTestProxies'); $config->setProxyNamespace('JMS\Tests\Proxies'); - assert(class_exists(DoctrinePHPCRDriver::class)); - $config->setMetadataDriverImpl( - new DoctrinePHPCRDriver(new AnnotationReader(), __DIR__ . '/../../Fixtures/DoctrinePHPCR'), - ); + if (class_exists(DoctrinePHPCRDriver::class)) { + $config->setMetadataDriverImpl( + new DoctrinePHPCRDriver(new AnnotationReader(), __DIR__ . '/../../Fixtures/DoctrinePHPCR'), + ); + } else { + $config->setMetadataDriverImpl( + new AttributeDoctrinePHPCRDriver([__DIR__ . '/../../Fixtures/DoctrinePHPCR']), + ); + } $session = $this->getMockBuilder(SessionInterface::class)->getMock(); @@ -113,7 +120,11 @@ protected function getDocumentManager() public function getAnnotationDriver() { - return new AnnotationDriver(new AnnotationReader(), new IdenticalPropertyNamingStrategy()); + if (class_exists(DoctrinePHPCRDriver::class)) { + return new AnnotationDriver(new AnnotationReader(), new IdenticalPropertyNamingStrategy()); + } else { + return new AnnotationOrAttributeDriver(new IdenticalPropertyNamingStrategy()); + } } protected function getDoctrinePHPCRDriver()