Skip to content

Commit

Permalink
Merge pull request #1520 from mbabker/test-doctrine-driver-with-attri…
Browse files Browse the repository at this point in the history
…bute-driver

Inject an attribute metadata driver into the DoctrineDriver when testing
  • Loading branch information
scyzoryck authored Oct 23, 2023
2 parents 9c2ac39 + 660a986 commit dc484a3
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions tests/Metadata/Driver/DoctrineDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
use Doctrine\ORM\Version as ORMVersion;
use Doctrine\Persistence\ManagerRegistry;
use JMS\Serializer\Metadata\Driver\AnnotationDriver;
use JMS\Serializer\Metadata\Driver\AnnotationOrAttributeDriver;
use JMS\Serializer\Metadata\Driver\DoctrineTypeDriver;
use JMS\Serializer\Metadata\Driver\NullDriver;
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
use JMS\Serializer\Tests\Fixtures\Doctrine\Embeddable\BlogPostWithEmbedded;
use Metadata\Driver\DriverChain;
use PHPUnit\Framework\TestCase;

class DoctrineDriverTest extends TestCase
Expand Down Expand Up @@ -94,7 +97,7 @@ public function testNonDoctrineEntityClassIsNotModified()
// because it has no Doctrine metadata.
$refClass = new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost');

$plainMetadata = $this->getAnnotationDriver()->loadMetadataForClass($refClass);
$plainMetadata = $this->getMetadataDriver()->loadMetadataForClass($refClass);
$doctrineMetadata = $this->getDoctrineDriver()->loadMetadataForClass($refClass);

// Do not compare timestamps
Expand All @@ -107,7 +110,7 @@ public function testNonDoctrineEntityClassIsNotModified()

public function testExcludePropertyNoPublicAccessorException()
{
$first = $this->getAnnotationDriver()
$first = $this->getMetadataDriver()
->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ExcludePublicAccessor'));

self::assertArrayHasKey('id', $first->propertyMetadata);
Expand Down Expand Up @@ -154,9 +157,20 @@ protected function getEntityManager()
return EntityManager::create($conn, $config);
}

public function getAnnotationDriver()
public function getMetadataDriver()
{
return new AnnotationDriver(new AnnotationReader(), new IdenticalPropertyNamingStrategy());
$driver = new DriverChain();
$namingStrategy = new IdenticalPropertyNamingStrategy();

if (PHP_VERSION_ID >= 80000) {
$driver->addDriver(new AnnotationOrAttributeDriver($namingStrategy));
} else {
$driver->addDriver(new AnnotationDriver(new AnnotationReader(), $namingStrategy));
}

$driver->addDriver(new NullDriver($namingStrategy));

return $driver;
}

protected function getDoctrineDriver()
Expand All @@ -167,7 +181,7 @@ protected function getDoctrineDriver()
->will($this->returnValue($this->getEntityManager()));

return new DoctrineTypeDriver(
$this->getAnnotationDriver(),
$this->getMetadataDriver(),
$registry
);
}
Expand Down

0 comments on commit dc484a3

Please sign in to comment.