Skip to content

Commit 1a31449

Browse files
mbabkerfranmomu
authored andcommitted
Another round of mapping refactoring in tests for cross-version compatibility
1 parent 598fead commit 1a31449

23 files changed

+313
-124
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping">
3+
<entity name="Gedmo\Tests\Mapping\Fixture\Xml\User" table="users">
4+
<indexes>
5+
<index name="search_idx" columns="username"/>
6+
</indexes>
7+
<id name="id" type="integer" column="id">
8+
<generator strategy="IDENTITY"/>
9+
</id>
10+
<field name="password" type="string" column="password" length="32">
11+
<gedmo:translatable/>
12+
</field>
13+
<field name="username" type="string" column="username" length="128">
14+
<gedmo:translatable/>
15+
</field>
16+
<field name="company" type="string" column="company" length="128" nullable="true">
17+
<gedmo:translatable fallback="true"/>
18+
</field>
19+
<gedmo:translation entity="Gedmo\Tests\Translatable\Fixture\PersonTranslation" locale="localeField"/>
20+
</entity>
21+
</doctrine-mapping>

tests/Gedmo/Mapping/ExtensionORMTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function testGeneratedValues(): void
5858
$user = new User();
5959
$user->setName('encode me');
6060
$user->setPassword('secret');
61+
$user->setUsername('some_username');
6162
$this->em->persist($user);
6263
$this->em->flush();
6364

tests/Gedmo/Mapping/Fixture/User.php

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,24 @@
1313

1414
use Doctrine\DBAL\Types\Types;
1515
use Doctrine\ORM\Mapping as ORM;
16+
use Gedmo\Mapping\Annotation as Gedmo;
1617
use Gedmo\Tests\Mapping\Mock\Extension\Encoder\Mapping as Ext;
18+
use Gedmo\Tests\Translatable\Fixture\PersonTranslation;
1719

1820
/**
19-
* @ORM\Table(name="test_users")
21+
* @ORM\Table(name="users")
22+
* @ORM\Table(
23+
* name="users",
24+
* indexes={@ORM\Index(name="search_idx", columns={"username"})}
25+
* )
2026
* @ORM\Entity
27+
*
28+
* @Gedmo\TranslationEntity(class="Gedmo\Tests\Translatable\Fixture\PersonTranslation")
2129
*/
22-
#[ORM\Table(name: 'test_users')]
30+
#[ORM\Table(name: 'users')]
2331
#[ORM\Entity]
32+
#[ORM\Index(columns: ['username'], name: 'search_idx')]
33+
#[Gedmo\TranslationEntity(class: PersonTranslation::class)]
2434
class User
2535
{
2636
/**
@@ -48,11 +58,40 @@ class User
4858
* @Ext\Encode(type="md5")
4959
*
5060
* @ORM\Column(length=32)
61+
*
62+
* @Gedmo\Translatable
5163
*/
5264
#[Ext\Encode(type: 'md5')]
5365
#[ORM\Column(length: 32)]
66+
#[Gedmo\Translatable]
5467
private ?string $password = null;
5568

69+
/**
70+
* @ORM\Column(length=128)
71+
*
72+
* @Gedmo\Translatable
73+
*/
74+
#[ORM\Column(length: 128)]
75+
#[Gedmo\Translatable]
76+
private ?string $username = null;
77+
78+
/**
79+
* @ORM\Column(length=128, nullable=true)
80+
*
81+
* @Gedmo\Translatable(fallback=true)
82+
*/
83+
#[ORM\Column(length: 128, nullable: true)]
84+
#[Gedmo\Translatable(fallback: true)]
85+
private ?string $company = null;
86+
87+
/**
88+
* @var string
89+
*
90+
* @Gedmo\Locale
91+
*/
92+
#[Gedmo\Locale]
93+
private $localeField;
94+
5695
public function setName(?string $name): void
5796
{
5897
$this->name = $name;
@@ -72,4 +111,24 @@ public function getPassword(): ?string
72111
{
73112
return $this->password;
74113
}
114+
115+
public function setUsername(string $username): void
116+
{
117+
$this->username = $username;
118+
}
119+
120+
public function getUsername(): string
121+
{
122+
return $this->username;
123+
}
124+
125+
public function setCompany(string $company): void
126+
{
127+
$this->company = $company;
128+
}
129+
130+
public function getCompany(): string
131+
{
132+
return $this->company;
133+
}
75134
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gedmo\Tests\Mapping\Fixture\Xml;
13+
14+
class User
15+
{
16+
/**
17+
* @var int
18+
*/
19+
private $id;
20+
21+
private ?string $password = null;
22+
23+
private ?string $username = null;
24+
25+
private ?string $company = null;
26+
27+
/**
28+
* @var string
29+
*/
30+
private $localeField;
31+
32+
public function getId(): int
33+
{
34+
return $this->id;
35+
}
36+
37+
public function setPassword(string $password): void
38+
{
39+
$this->password = $password;
40+
}
41+
42+
public function getPassword(): string
43+
{
44+
return $this->password;
45+
}
46+
47+
public function setUsername(string $username): void
48+
{
49+
$this->username = $username;
50+
}
51+
52+
public function getUsername(): string
53+
{
54+
return $this->username;
55+
}
56+
57+
public function setCompany(string $company): void
58+
{
59+
$this->company = $company;
60+
}
61+
62+
public function getCompany(): string
63+
{
64+
return $this->company;
65+
}
66+
}

tests/Gedmo/Mapping/LoggableORMMappingTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Doctrine\ORM\EntityManager;
1515
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
16-
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
1716
use Doctrine\ORM\Mapping\Driver\YamlDriver;
1817
use Gedmo\Loggable\Entity\LogEntry;
1918
use Gedmo\Loggable\LoggableListener;
@@ -58,7 +57,7 @@ protected function setUp(): void
5857
*/
5958
public static function dataLoggableObject(): \Generator
6059
{
61-
if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
60+
if (PHP_VERSION_ID >= 80000) {
6261
yield 'Model with attributes' => [AnnotatedLoggable::class];
6362
}
6463

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

123-
if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
122+
if (PHP_VERSION_ID >= 80000) {
124123
yield 'Model with attributes' => [AnnotatedLoggableComposite::class];
125124
}
126125

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

168-
if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
167+
if (PHP_VERSION_ID >= 80000) {
169168
yield 'Model with attributes' => [AnnotatedLoggableCompositeRelation::class];
170169
}
171170

@@ -213,7 +212,7 @@ public function testLoggableCompositeRelationMapping(string $className): void
213212
*/
214213
public static function dataLoggableObjectWithEmbedded(): \Generator
215214
{
216-
if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
215+
if (PHP_VERSION_ID >= 80000) {
217216
yield 'Model with attributes' => [AnnotatedLoggableWithEmbedded::class];
218217
}
219218

tests/Gedmo/Mapping/MappingEventSubscriberTest.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace Gedmo\Tests\Mapping;
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
15-
use Doctrine\Common\EventManager;
16-
use Doctrine\DBAL\DriverManager;
1715
use Doctrine\ORM\EntityManager;
1816
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
1917
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
@@ -24,7 +22,6 @@
2422
use Gedmo\Tests\Mapping\Fixture\SuperClassExtension;
2523
use Gedmo\Tests\Mapping\Mock\Extension\Encoder\EncoderListener;
2624
use Psr\Cache\CacheItemPoolInterface;
27-
use Symfony\Component\Cache\Adapter\ArrayAdapter;
2825

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

3734
$config = $this->getBasicConfiguration();
3835

39-
if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
36+
if (PHP_VERSION_ID >= 80000) {
4037
$config->setMetadataDriverImpl(new AttributeDriver([]));
4138
} else {
4239
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));
4340
}
4441

45-
$conn = [
46-
'driver' => 'pdo_sqlite',
47-
'memory' => true,
48-
];
49-
50-
$this->em = new EntityManager(DriverManager::getConnection($conn, $config), $config, new EventManager());
42+
$this->em = $this->getBasicEntityManager($config);
5143
}
5244

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

98-
if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
90+
if (PHP_VERSION_ID >= 80000) {
9991
$config->setMetadataDriverImpl(new AttributeDriver([]));
10092
} else {
10193
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));
10294
}
10395

104-
$config->setMetadataCache(new ArrayAdapter());
105-
106-
$conn = [
107-
'driver' => 'pdo_sqlite',
108-
'memory' => true,
109-
];
110-
111-
$this->em = new EntityManager(DriverManager::getConnection($conn, $config), $config, new EventManager());
96+
$this->em = $this->getBasicEntityManager($config);
11297

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

tests/Gedmo/Mapping/MappingTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ protected function setUp(): void
4545
$config = new Configuration();
4646
$config->setProxyDir(TESTS_TEMP_DIR);
4747
$config->setProxyNamespace('Gedmo\Mapping\Proxy');
48-
// $this->markTestSkipped('Skipping according to a bug in annotation reader creation.');
4948

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

tests/Gedmo/Mapping/MetadataFactory/ForcedMetadataTest.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
use Doctrine\DBAL\DriverManager;
1616
use Doctrine\ORM\Configuration;
1717
use Doctrine\ORM\EntityManager;
18-
use Doctrine\ORM\EntityManagerInterface;
1918
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
2019
use Doctrine\ORM\Events;
2120
use Doctrine\ORM\Id\IdentityGenerator;
2221
use Doctrine\ORM\Mapping\ClassMetadata;
2322
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
2423
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
2524
use Doctrine\ORM\Tools\SchemaTool;
25+
use Gedmo\Mapping\Driver\AttributeReader;
2626
use Gedmo\Tests\Mapping\Fixture\Unmapped\Timestampable;
2727
use Gedmo\Timestampable\TimestampableListener;
2828
use PHPUnit\Framework\TestCase;
@@ -36,30 +36,36 @@ final class ForcedMetadataTest extends TestCase
3636
{
3737
private TimestampableListener $timestampable;
3838

39-
private EntityManagerInterface $em;
39+
private EntityManager $em;
4040

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

47-
if (PHP_VERSION_ID >= 80000 && class_exists(AttributeDriver::class)) {
47+
if (PHP_VERSION_ID >= 80000) {
4848
$config->setMetadataDriverImpl(new AttributeDriver([]));
4949
} else {
5050
$config->setMetadataDriverImpl(new AnnotationDriver($_ENV['annotation_reader']));
5151
}
5252

53-
$conn = [
54-
'driver' => 'pdo_sqlite',
55-
'memory' => true,
56-
];
53+
$this->timestampable = new TimestampableListener();
54+
55+
if (PHP_VERSION_ID >= 80000) {
56+
$this->timestampable->setAnnotationReader(new AttributeReader());
57+
} else {
58+
$this->timestampable->setAnnotationReader($_ENV['annotation_reader']);
59+
}
5760

5861
$evm = new EventManager();
59-
$this->timestampable = new TimestampableListener();
60-
$this->timestampable->setAnnotationReader($_ENV['annotation_reader']);
6162
$evm->addEventSubscriber($this->timestampable);
62-
$connection = DriverManager::getConnection($conn, $config);
63+
64+
$connection = DriverManager::getConnection([
65+
'driver' => 'pdo_sqlite',
66+
'memory' => true,
67+
], $config);
68+
6369
$this->em = new EntityManager($connection, $config, $evm);
6470
}
6571

@@ -72,6 +78,8 @@ public function testShouldWork(): void
7278
$this->em,
7379
Timestampable::class
7480
);
81+
82+
// @todo: This assertion fails when run in isolation
7583
static::assertTrue(isset($conf['create']));
7684

7785
$test = new Timestampable();

0 commit comments

Comments
 (0)