Skip to content

Commit

Permalink
Another round of mapping refactoring in tests for cross-version compa…
Browse files Browse the repository at this point in the history
…tibility
  • Loading branch information
mbabker authored and franmomu committed Dec 15, 2023
1 parent 598fead commit 1a31449
Show file tree
Hide file tree
Showing 23 changed files with 313 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping">
<entity name="Gedmo\Tests\Mapping\Fixture\Xml\User" table="users">
<indexes>
<index name="search_idx" columns="username"/>
</indexes>
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="password" type="string" column="password" length="32">
<gedmo:translatable/>
</field>
<field name="username" type="string" column="username" length="128">
<gedmo:translatable/>
</field>
<field name="company" type="string" column="company" length="128" nullable="true">
<gedmo:translatable fallback="true"/>
</field>
<gedmo:translation entity="Gedmo\Tests\Translatable\Fixture\PersonTranslation" locale="localeField"/>
</entity>
</doctrine-mapping>
1 change: 1 addition & 0 deletions tests/Gedmo/Mapping/ExtensionORMTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function testGeneratedValues(): void
$user = new User();
$user->setName('encode me');
$user->setPassword('secret');
$user->setUsername('some_username');
$this->em->persist($user);
$this->em->flush();

Expand Down
63 changes: 61 additions & 2 deletions tests/Gedmo/Mapping/Fixture/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Tests\Mapping\Mock\Extension\Encoder\Mapping as Ext;
use Gedmo\Tests\Translatable\Fixture\PersonTranslation;

/**
* @ORM\Table(name="test_users")
* @ORM\Table(name="users")
* @ORM\Table(
* name="users",
* indexes={@ORM\Index(name="search_idx", columns={"username"})}
* )
* @ORM\Entity
*
* @Gedmo\TranslationEntity(class="Gedmo\Tests\Translatable\Fixture\PersonTranslation")
*/
#[ORM\Table(name: 'test_users')]
#[ORM\Table(name: 'users')]
#[ORM\Entity]
#[ORM\Index(columns: ['username'], name: 'search_idx')]
#[Gedmo\TranslationEntity(class: PersonTranslation::class)]
class User
{
/**
Expand Down Expand Up @@ -48,11 +58,40 @@ class User
* @Ext\Encode(type="md5")
*
* @ORM\Column(length=32)
*
* @Gedmo\Translatable
*/
#[Ext\Encode(type: 'md5')]
#[ORM\Column(length: 32)]
#[Gedmo\Translatable]
private ?string $password = null;

/**
* @ORM\Column(length=128)
*
* @Gedmo\Translatable
*/
#[ORM\Column(length: 128)]
#[Gedmo\Translatable]
private ?string $username = null;

/**
* @ORM\Column(length=128, nullable=true)
*
* @Gedmo\Translatable(fallback=true)
*/
#[ORM\Column(length: 128, nullable: true)]
#[Gedmo\Translatable(fallback: true)]
private ?string $company = null;

/**
* @var string
*
* @Gedmo\Locale
*/
#[Gedmo\Locale]
private $localeField;

public function setName(?string $name): void
{
$this->name = $name;
Expand All @@ -72,4 +111,24 @@ public function getPassword(): ?string
{
return $this->password;
}

public function setUsername(string $username): void
{
$this->username = $username;
}

public function getUsername(): string
{
return $this->username;
}

public function setCompany(string $company): void
{
$this->company = $company;
}

public function getCompany(): string
{
return $this->company;
}
}
66 changes: 66 additions & 0 deletions tests/Gedmo/Mapping/Fixture/Xml/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gedmo\Tests\Mapping\Fixture\Xml;

class User
{
/**
* @var int
*/
private $id;

private ?string $password = null;

private ?string $username = null;

private ?string $company = null;

/**
* @var string
*/
private $localeField;

public function getId(): int
{
return $this->id;
}

public function setPassword(string $password): void
{
$this->password = $password;
}

public function getPassword(): string
{
return $this->password;
}

public function setUsername(string $username): void
{
$this->username = $username;
}

public function getUsername(): string
{
return $this->username;
}

public function setCompany(string $company): void
{
$this->company = $company;
}

public function getCompany(): string
{
return $this->company;
}
}
9 changes: 4 additions & 5 deletions tests/Gedmo/Mapping/LoggableORMMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
use Gedmo\Loggable\Entity\LogEntry;
use Gedmo\Loggable\LoggableListener;
Expand Down Expand Up @@ -58,7 +57,7 @@ protected function setUp(): void
*/
public static function dataLoggableObject(): \Generator
{
if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
if (PHP_VERSION_ID >= 80000) {
yield 'Model with attributes' => [AnnotatedLoggable::class];
}

Expand Down Expand Up @@ -120,7 +119,7 @@ public static function dataLoggableObjectWithCompositeKey(): \Generator
{
yield 'Model with XML mapping' => [XmlLoggableComposite::class];

if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
if (PHP_VERSION_ID >= 80000) {
yield 'Model with attributes' => [AnnotatedLoggableComposite::class];
}

Expand Down Expand Up @@ -165,7 +164,7 @@ public static function dataLoggableObjectWithCompositeKeyAndRelation(): \Generat
{
yield 'Model with XML mapping' => [XmlLoggableCompositeRelation::class];

if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
if (PHP_VERSION_ID >= 80000) {
yield 'Model with attributes' => [AnnotatedLoggableCompositeRelation::class];
}

Expand Down Expand Up @@ -213,7 +212,7 @@ public function testLoggableCompositeRelationMapping(string $className): void
*/
public static function dataLoggableObjectWithEmbedded(): \Generator
{
if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
if (PHP_VERSION_ID >= 80000) {
yield 'Model with attributes' => [AnnotatedLoggableWithEmbedded::class];
}

Expand Down
23 changes: 4 additions & 19 deletions tests/Gedmo/Mapping/MappingEventSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
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;
Expand All @@ -24,7 +22,6 @@
use Gedmo\Tests\Mapping\Fixture\SuperClassExtension;
use Gedmo\Tests\Mapping\Mock\Extension\Encoder\EncoderListener;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

final class MappingEventSubscriberTest extends ORMMappingTestCase
{
Expand All @@ -36,18 +33,13 @@ protected function setUp(): void

$config = $this->getBasicConfiguration();

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

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

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

public function testGetMetadataFactoryCacheFromDoctrineForSluggable(): void
Expand Down Expand Up @@ -95,20 +87,13 @@ public function testGetMetadataFactoryCacheFromDoctrineForSuperClassExtension():
// Create new configuration to use new array cache
$config = $this->getBasicConfiguration();

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

$config->setMetadataCache(new ArrayAdapter());

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

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

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

Expand Down
3 changes: 1 addition & 2 deletions tests/Gedmo/Mapping/MappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ protected function setUp(): void
$config = new Configuration();
$config->setProxyDir(TESTS_TEMP_DIR);
$config->setProxyNamespace('Gedmo\Mapping\Proxy');
// $this->markTestSkipped('Skipping according to a bug in annotation reader creation.');

if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
if (PHP_VERSION_ID >= 80000) {
$config->setMetadataDriverImpl(new AttributeDriver([]));
} else {
$config->setMetadataDriverImpl(new AnnotationDriver($_ENV['annotation_reader']));
Expand Down
28 changes: 18 additions & 10 deletions tests/Gedmo/Mapping/MetadataFactory/ForcedMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
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\Mapping\Driver\AttributeReader;
use Gedmo\Tests\Mapping\Fixture\Unmapped\Timestampable;
use Gedmo\Timestampable\TimestampableListener;
use PHPUnit\Framework\TestCase;
Expand All @@ -36,30 +36,36 @@ final class ForcedMetadataTest extends TestCase
{
private TimestampableListener $timestampable;

private EntityManagerInterface $em;
private EntityManager $em;

protected function setUp(): void
{
$config = new Configuration();
$config->setProxyDir(TESTS_TEMP_DIR);
$config->setProxyNamespace('Gedmo\Mapping\Proxy');

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

$conn = [
'driver' => 'pdo_sqlite',
'memory' => true,
];
$this->timestampable = new TimestampableListener();

if (PHP_VERSION_ID >= 80000) {
$this->timestampable->setAnnotationReader(new AttributeReader());
} else {
$this->timestampable->setAnnotationReader($_ENV['annotation_reader']);
}

$evm = new EventManager();
$this->timestampable = new TimestampableListener();
$this->timestampable->setAnnotationReader($_ENV['annotation_reader']);
$evm->addEventSubscriber($this->timestampable);
$connection = DriverManager::getConnection($conn, $config);

$connection = DriverManager::getConnection([
'driver' => 'pdo_sqlite',
'memory' => true,
], $config);

$this->em = new EntityManager($connection, $config, $evm);
}

Expand All @@ -72,6 +78,8 @@ public function testShouldWork(): void
$this->em,
Timestampable::class
);

// @todo: This assertion fails when run in isolation
static::assertTrue(isset($conf['create']));

$test = new Timestampable();
Expand Down
Loading

0 comments on commit 1a31449

Please sign in to comment.