Skip to content

Commit

Permalink
Use the ORM's attribute driver in tests when able
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Oct 12, 2023
1 parent a51fc79 commit ef9da17
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 54 deletions.
15 changes: 15 additions & 0 deletions tests/Gedmo/Mapping/Fixture/Sluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@

namespace Gedmo\Tests\Mapping\Fixture;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Sluggable\Handler\RelativeSlugHandler;
use Gedmo\Sluggable\Handler\TreeSlugHandler;

/**
* @ORM\Entity
*/
#[ORM\Entity]
class Sluggable
{
/**
Expand All @@ -26,20 +30,25 @@ class Sluggable
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
private $id;

/**
* @var string|null
*
* @ORM\Column(name="title", type="string", length=64)
*/
#[ORM\Column(name: 'title', type: Types::STRING, length: 64)]
private $title;

/**
* @var string|null
*
* @ORM\Column(name="code", type="string", length=16)
*/
#[ORM\Column(name: 'code', type: Types::STRING, length: 64, nullable: true)]
private $code;

/**
Expand All @@ -58,20 +67,26 @@ class Sluggable
* }, separator="-", updatable=false, fields={"title", "code"})
* @ORM\Column(name="slug", type="string", length=64, unique=true)
*/
#[Gedmo\Slug(separator: '-', updatable: false, fields: ['title', 'code'])]
#[Gedmo\SlugHandler(class: TreeSlugHandler::class, options: ['parentRelationField' => 'parent', 'separator' => '/'])]
#[Gedmo\SlugHandler(class: RelativeSlugHandler::class, options: ['relationField' => 'user', 'relationSlugField' => 'slug', 'separator' => '/'])]
#[ORM\Column(name: 'slug', type: Types::STRING, length: 64, unique: true)]
private $slug;

/**
* @var Sluggable|null
*
* @ORM\ManyToOne(targetEntity="Sluggable")
*/
#[ORM\ManyToOne(targetEntity: Sluggable::class)]
private $parent;

/**
* @var User|null
*
* @ORM\ManyToOne(targetEntity="User")
*/
#[ORM\ManyToOne(targetEntity: User::class)]
private $user;

public function getId(): ?int
Expand Down
15 changes: 9 additions & 6 deletions tests/Gedmo/Mapping/LoggableORMMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Gedmo\Loggable\Entity\LogEntry;
Expand Down Expand Up @@ -47,14 +48,16 @@ protected function setUp(): void

$config = $this->getBasicConfiguration();

$reader = new AnnotationReader();
$annotationDriver = new AnnotationDriver($reader);
$chain = new MappingDriverChain();

$yamlDriver = new YamlDriver(__DIR__.'/Driver/Yaml');
// TODO - The ORM's YAML mapping is deprecated and removed in 3.0
$chain->addDriver(new YamlDriver(__DIR__.'/Driver/Yaml'), 'Gedmo\Tests\Mapping\Fixture\Yaml');

$chain = new MappingDriverChain();
$chain->addDriver($yamlDriver, 'Gedmo\Tests\Mapping\Fixture\Yaml');
$chain->addDriver($annotationDriver, 'Gedmo\Tests\Mapping\Fixture');
if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
$chain->addDriver(new AttributeDriver([]), 'Gedmo\Tests\Mapping\Fixture');
} else {
$chain->addDriver(new AnnotationDriver(new AnnotationReader()), 'Gedmo\Tests\Mapping\Fixture');
}

$config->setMetadataDriverImpl($chain);

Expand Down
25 changes: 19 additions & 6 deletions tests/Gedmo/Mapping/MappingEventSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
use Gedmo\Mapping\ExtensionMetadataFactory;
use Gedmo\Sluggable\SluggableListener;
Expand All @@ -38,15 +39,18 @@ protected function setUp(): void

$config = $this->getBasicConfiguration();

$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));
if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
$config->setMetadataDriverImpl(new AttributeDriver([]));
} else {
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));
}

$conn = [
'driver' => 'pdo_sqlite',
'memory' => true,
];

$connection = DriverManager::getConnection($conn, $config);
$this->em = new EntityManager($connection, $config, new EventManager());
$this->em = new EntityManager(DriverManager::getConnection($conn, $config), $config, new EventManager());
}

public function testGetMetadataFactoryCacheFromDoctrineForSluggable(): void
Expand Down Expand Up @@ -97,12 +101,21 @@ public function testGetMetadataFactoryCacheFromDoctrineForSuperClassExtension():

// Create new configuration to use new array cache
$config = $this->getBasicConfiguration();
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));

if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
$config->setMetadataDriverImpl(new AttributeDriver([]));
} else {
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));
}

$config->setMetadataCache(new ArrayAdapter());
$this->em = EntityManager::create([

$conn = [
'driver' => 'pdo_sqlite',
'memory' => true,
], $config, new EventManager());
];

$this->em = new EntityManager(DriverManager::getConnection($conn, $config), $config, new EventManager());

$config = $subscriber->getExtensionMetadataFactory($this->em)->getExtensionMetadata($classMetadata);

Expand Down
11 changes: 9 additions & 2 deletions tests/Gedmo/Mapping/MappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
namespace Gedmo\Tests\Mapping;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Tools\SchemaTool;
use Gedmo\Sluggable\SluggableListener;
use Gedmo\Tests\Tree\Fixture\BehavioralCategory;
Expand Down Expand Up @@ -50,7 +52,12 @@ protected function setUp(): void
$config->setProxyDir(TESTS_TEMP_DIR);
$config->setProxyNamespace('Gedmo\Mapping\Proxy');
// $this->markTestSkipped('Skipping according to a bug in annotation reader creation.');
$config->setMetadataDriverImpl(new AnnotationDriver($_ENV['annotation_reader']));

if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
$config->setMetadataDriverImpl(new AttributeDriver([]));
} else {
$config->setMetadataDriverImpl(new AnnotationDriver($_ENV['annotation_reader']));
}

$conn = [
'driver' => 'pdo_sqlite',
Expand All @@ -63,7 +70,7 @@ protected function setUp(): void
$evm->addEventSubscriber($this->timestampable);
$evm->addEventSubscriber(new SluggableListener());
$evm->addEventSubscriber(new TreeListener());
$this->em = EntityManager::create($conn, $config, $evm);
$this->em = new EntityManager(DriverManager::getConnection($conn, $config), $config, $evm);

$schemaTool = new SchemaTool($this->em);
$schemaTool->dropSchema([]);
Expand Down
10 changes: 7 additions & 3 deletions tests/Gedmo/Mapping/MetadataFactory/ForcedMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Doctrine\ORM\Id\IdentityGenerator;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Tools\SchemaTool;
use Gedmo\Tests\Mapping\Fixture\Unmapped\Timestampable;
use Gedmo\Timestampable\TimestampableListener;
Expand Down Expand Up @@ -48,9 +49,12 @@ protected function setUp(): void
$config = new Configuration();
$config->setProxyDir(TESTS_TEMP_DIR);
$config->setProxyNamespace('Gedmo\Mapping\Proxy');
$config->setMetadataDriverImpl(
new AnnotationDriver($_ENV['annotation_reader'])
);

if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
$config->setMetadataDriverImpl(new AttributeDriver([]));
} else {
$config->setMetadataDriverImpl(new AnnotationDriver($_ENV['annotation_reader']));
}

$conn = [
'driver' => 'pdo_sqlite',
Expand Down
25 changes: 14 additions & 11 deletions tests/Gedmo/Mapping/SluggableMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Gedmo\Mapping\ExtensionMetadataFactory;
Expand Down Expand Up @@ -45,17 +46,19 @@ protected function setUp(): void
parent::setUp();

$config = $this->getBasicConfiguration();
$chainDriverImpl = new MappingDriverChain();
$chainDriverImpl->addDriver(
new YamlDriver([__DIR__.'/Driver/Yaml']),
'Gedmo\Tests\Mapping\Fixture\Yaml'
);
$reader = new AnnotationReader();
$chainDriverImpl->addDriver(
new AnnotationDriver($reader),
'Gedmo\Tests\Mapping\Fixture'
);
$config->setMetadataDriverImpl($chainDriverImpl);

$chain = new MappingDriverChain();

// TODO - The ORM's YAML mapping is deprecated and removed in 3.0
$chain->addDriver(new YamlDriver(__DIR__.'/Driver/Yaml'), 'Gedmo\Tests\Mapping\Fixture\Yaml');

if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
$chain->addDriver(new AttributeDriver([]), 'Gedmo\Tests\Mapping\Fixture');
} else {
$chain->addDriver(new AnnotationDriver(new AnnotationReader()), 'Gedmo\Tests\Mapping\Fixture');
}

$config->setMetadataDriverImpl($chain);

$conn = [
'driver' => 'pdo_sqlite',
Expand Down
13 changes: 7 additions & 6 deletions tests/Gedmo/Mapping/TimestampableMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ protected function setUp(): void
parent::setUp();

$config = $this->getBasicConfiguration();
$chainDriverImpl = new MappingDriverChain();
$chainDriverImpl->addDriver(
new YamlDriver([__DIR__.'/Driver/Yaml']),
'Gedmo\Tests\Mapping\Fixture\Yaml'
);
$config->setMetadataDriverImpl($chainDriverImpl);

$chain = new MappingDriverChain();

// TODO - The ORM's YAML mapping is deprecated and removed in 3.0
$chain->addDriver(new YamlDriver(__DIR__.'/Driver/Yaml'), 'Gedmo\Tests\Mapping\Fixture\Yaml');

$config->setMetadataDriverImpl($chain);

$conn = [
'driver' => 'pdo_sqlite',
Expand Down
13 changes: 7 additions & 6 deletions tests/Gedmo/Mapping/TranslatableMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ protected function setUp(): void
parent::setUp();

$config = $this->getBasicConfiguration();
$chainDriverImpl = new MappingDriverChain();
$chainDriverImpl->addDriver(
new YamlDriver([__DIR__.'/Driver/Yaml']),
'Gedmo\Tests\Mapping\Fixture\Yaml'
);
$config->setMetadataDriverImpl($chainDriverImpl);

$chain = new MappingDriverChain();

// TODO - The ORM's YAML mapping is deprecated and removed in 3.0
$chain->addDriver(new YamlDriver(__DIR__.'/Driver/Yaml'), 'Gedmo\Tests\Mapping\Fixture\Yaml');

$config->setMetadataDriverImpl($chain);

$conn = [
'driver' => 'pdo_sqlite',
Expand Down
33 changes: 19 additions & 14 deletions tests/Gedmo/Mapping/TreeMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

namespace Gedmo\Tests\Mapping;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Gedmo\Mapping\ExtensionMetadataFactory;
Expand Down Expand Up @@ -49,20 +52,22 @@ protected function setUp(): void
parent::setUp();

$config = $this->getBasicConfiguration();
$chainDriverImpl = new MappingDriverChain();
$chainDriverImpl->addDriver(
new YamlDriver([__DIR__.'/Driver/Yaml']),
'Gedmo\Tests\Mapping\Fixture\Yaml'
);
$chainDriverImpl->addDriver(
$config->newDefaultAnnotationDriver([], false),
'Gedmo\Tests\Tree\Fixture'
);
$chainDriverImpl->addDriver(
$config->newDefaultAnnotationDriver([], false),
'Gedmo\Tree'
);
$config->setMetadataDriverImpl($chainDriverImpl);

$chain = new MappingDriverChain();

// TODO - The ORM's YAML mapping is deprecated and removed in 3.0
$chain->addDriver(new YamlDriver(__DIR__.'/Driver/Yaml'), 'Gedmo\Tests\Mapping\Fixture\Yaml');

if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
$annotationOrAttributeDriver = new AttributeDriver([]);
} else {
$annotationOrAttributeDriver = new AnnotationDriver(new AnnotationReader());
}

$chain->addDriver($annotationOrAttributeDriver, 'Gedmo\Tests\Tree\Fixture');
$chain->addDriver($annotationOrAttributeDriver, 'Gedmo\Tree');

$config->setMetadataDriverImpl($chain);

$conn = [
'driver' => 'pdo_sqlite',
Expand Down

0 comments on commit ef9da17

Please sign in to comment.