Skip to content

Commit

Permalink
Merge pull request #1557 from scyzoryck/fix-doctrine-odm-tests
Browse files Browse the repository at this point in the history
Doctrine ODM: fix tests for 2.0
  • Loading branch information
goetas authored Aug 29, 2024
2 parents 3c18cb8 + 51f6a49 commit 745aeb7
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions phpstan/no-attributes.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
parameters:
ignoreErrors:
- '~Call to an undefined method Reflection.*::getAttributes\(\)~'
- '~Instantiated class Doctrine\\ODM\\PHPCR\\Mapping\\Driver\\AttributeDriver not found~'
6 changes: 6 additions & 0 deletions tests/Fixtures/DoctrinePHPCR/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@
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;

/**
* @PHPCRODM\Field(type="string")
* @SerializedName("full_name")
*/
#[SerializedName(name: 'full_name')]
#[Field(type: 'string')]
private $name;

public function __construct($name)
Expand Down
13 changes: 13 additions & 0 deletions tests/Fixtures/DoctrinePHPCR/BlogPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,30 +23,35 @@
* @XmlRoot("blog-post")
*/
#[XmlRoot(name: 'blog-post')]
#[Document]
class BlogPost
{
/**
* @PHPCRODM\Id()
*/
#[Id]
protected $id;

/**
* @PHPCRODM\Field(type="string")
* @Groups({"comments","post"})
*/
#[Groups(groups: ['comments', 'post'])]
#[Field(type: 'string')]
private $title;

/**
* @PHPCRODM\Field(type="string")
*/
#[Field(type: 'string')]
protected $slug;

/**
* @PHPCRODM\Field(type="date")
* @XmlAttribute
*/
#[XmlAttribute]
#[Field(type: 'date')]
private $createdAt;

/**
Expand All @@ -54,6 +64,7 @@ class BlogPost
* @Groups({"post"})
* @XmlAttribute
*/
#[Field(type: 'boolean')]
#[Type(name: 'integer')]
#[SerializedName(name: 'is_published')]
#[Groups(groups: ['post'])]
Expand All @@ -67,13 +78,15 @@ class BlogPost
*/
#[XmlList(entry: 'comment', inline: true)]
#[Groups(groups: ['comments'])]
#[ReferenceMany(targetDocument:'Comment', property:'blogPost')]
private $comments;

/**
* @PHPCRODM\ReferenceOne(targetDocument="Author")
* @Groups({"post"})
*/
#[Groups(groups: ['post'])]
#[ReferenceOne(targetDocument:'Author')]
private $author;

public function __construct($title, Author $author, \DateTime $createdAt)
Expand Down
8 changes: 8 additions & 0 deletions tests/Fixtures/DoctrinePHPCR/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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") */
Expand All @@ -26,6 +33,7 @@ class Comment
/**
* @PHPCRODM\Field(type="string")
*/
#[Field(type: 'string')]
private $text;

public function __construct(Author $author, $text)
Expand Down
21 changes: 16 additions & 5 deletions tests/Metadata/Driver/DoctrinePHPCRDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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()
Expand Down

0 comments on commit 745aeb7

Please sign in to comment.