Skip to content

Commit b090fbe

Browse files
committed
Implement forward compatibility with Persistence 3
1 parent 5b8263e commit b090fbe

File tree

7 files changed

+133
-68
lines changed

7 files changed

+133
-68
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"doctrine/inflector": "^1.4 || ^2.0",
3333
"doctrine/instantiator": "^1.3",
3434
"doctrine/lexer": "^1.0",
35-
"doctrine/persistence": "^2.2",
35+
"doctrine/persistence": "3.0.x-dev as 2.2.0",
3636
"psr/cache": "^1 || ^2 || ^3",
3737
"symfony/console": "^3.0 || ^4.0 || ^5.0 || ^6.0",
3838
"symfony/polyfill-php72": "^1.23",

lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ public function getExpressionBuilder()
4343
return $this->wrapped->getExpressionBuilder();
4444
}
4545

46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function getRepository($className)
50+
{
51+
return parent::getRepository($className);
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
public function getClassMetadata($className)
58+
{
59+
return parent::getClassMetadata($className);
60+
}
61+
4662
/**
4763
* {@inheritdoc}
4864
*/

tests/Doctrine/Tests/Models/Company/CompanyFlexContract.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Doctrine\ORM\Mapping\Entity;
1212
use Doctrine\ORM\Mapping\EntityResult;
1313
use Doctrine\ORM\Mapping\FieldResult;
14+
use Doctrine\ORM\Mapping\GeneratedValue;
15+
use Doctrine\ORM\Mapping\Id;
1416
use Doctrine\ORM\Mapping\JoinColumn;
1517
use Doctrine\ORM\Mapping\JoinTable;
1618
use Doctrine\ORM\Mapping\ManyToMany;
@@ -65,6 +67,14 @@
6567
#[ORM\Entity]
6668
class CompanyFlexContract extends CompanyContract
6769
{
70+
/**
71+
* @Id
72+
* @GeneratedValue
73+
* @Column(type="integer")
74+
* @var int
75+
*/
76+
public $id;
77+
6878
/**
6979
* @Column(type="integer")
7080
* @var int

tests/Doctrine/Tests/ORM/Decorator/EntityManagerDecoratorTest.php

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77
use Doctrine\ORM\Decorator\EntityManagerDecorator;
88
use Doctrine\ORM\EntityManagerInterface;
99
use Doctrine\ORM\Query\ResultSetMapping;
10+
use Generator;
11+
use LogicException;
1012
use PHPUnit\Framework\MockObject\MockObject;
1113
use PHPUnit\Framework\TestCase;
1214
use ReflectionClass;
1315
use ReflectionMethod;
16+
use ReflectionNamedType;
17+
use stdClass;
1418

15-
use function array_fill;
19+
use function assert;
1620
use function in_array;
21+
use function sprintf;
1722

1823
class EntityManagerDecoratorTest extends TestCase
1924
{
@@ -40,21 +45,18 @@ protected function setUp(): void
4045
$this->wrapped = $this->createMock(EntityManagerInterface::class);
4146
}
4247

43-
/** @psalm-return array<string, mixed[]> */
44-
public function getMethodParameters(): array
48+
/** @psalm-return Generator<string, mixed[]> */
49+
public function getMethodParameters(): Generator
4550
{
46-
$class = new ReflectionClass(EntityManagerInterface::class);
47-
$methods = [];
51+
$class = new ReflectionClass(EntityManagerInterface::class);
4852

4953
foreach ($class->getMethods() as $method) {
5054
if ($method->isConstructor() || $method->isStatic() || ! $method->isPublic()) {
5155
continue;
5256
}
5357

54-
$methods[$method->getName()] = $this->getParameters($method);
58+
yield $method->getName() => $this->getParameters($method);
5559
}
56-
57-
return $methods;
5860
}
5961

6062
/**
@@ -77,19 +79,34 @@ static function (): void {
7779
];
7880
}
7981

80-
if ($method->getNumberOfRequiredParameters() === 0) {
81-
return [$method->getName(), []];
82-
}
82+
$parameters = [];
8383

84-
if ($method->getNumberOfRequiredParameters() > 0) {
85-
return [$method->getName(), array_fill(0, $method->getNumberOfRequiredParameters(), 'req') ?: []];
86-
}
84+
foreach ($method->getParameters() as $parameter) {
85+
if ($parameter->getType() === null) {
86+
$parameters[] = 'mixed';
87+
continue;
88+
}
8789

88-
if ($method->getNumberOfParameters() !== $method->getNumberOfRequiredParameters()) {
89-
return [$method->getName(), array_fill(0, $method->getNumberOfParameters(), 'all') ?: []];
90+
$type = $parameter->getType();
91+
assert($type instanceof ReflectionNamedType);
92+
switch ($type->getName()) {
93+
case 'string':
94+
$parameters[] = 'parameter';
95+
break;
96+
97+
case 'object':
98+
$parameters[] = new stdClass();
99+
break;
100+
101+
default:
102+
throw new LogicException(sprintf(
103+
'Type %s is not handled yet',
104+
(string) $parameter->getType()
105+
));
106+
}
90107
}
91108

92-
return [];
109+
return [$method->getName(), $parameters];
93110
}
94111

95112
/**

tests/Doctrine/Tests/ORM/Functional/PersistentCollectionTest.php

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Doctrine\Common\Collections\ArrayCollection;
88
use Doctrine\Common\Collections\Collection;
99
use Doctrine\Common\Collections\Criteria;
10-
use Doctrine\Common\Persistence\PersistentObject;
1110
use Doctrine\ORM\Mapping\Column;
1211
use Doctrine\ORM\Mapping\Entity;
1312
use Doctrine\ORM\Mapping\GeneratedValue;
@@ -24,24 +23,23 @@ protected function setUp(): void
2423
PersistentCollectionHolder::class,
2524
PersistentCollectionContent::class
2625
);
27-
28-
PersistentObject::setObjectManager($this->_em);
2926
}
3027

3128
public function testPersist(): void
3229
{
33-
$collectionHolder = new PersistentCollectionHolder();
34-
$content = new PersistentCollectionContent('first element');
30+
$collectionHolder = new PersistentCollectionHolder(1);
31+
$content = new PersistentCollectionContent(1);
3532
$collectionHolder->addElement($content);
3633

3734
$this->_em->persist($collectionHolder);
3835
$this->_em->flush();
3936
$this->_em->clear();
4037

4138
$collectionHolder = $this->_em->find(PersistentCollectionHolder::class, $collectionHolder->getId());
39+
self::assertNotNull($collectionHolder);
4240
$collectionHolder->getCollection();
4341

44-
$content = new PersistentCollectionContent('second element');
42+
$content = new PersistentCollectionContent(2);
4543
$collectionHolder->addElement($content);
4644

4745
self::assertEquals(2, $collectionHolder->getCollection()->count());
@@ -52,19 +50,20 @@ public function testPersist(): void
5250
*/
5351
public function testExtraLazyIsEmptyDoesNotInitializeCollection(): void
5452
{
55-
$collectionHolder = new PersistentCollectionHolder();
53+
$collectionHolder = new PersistentCollectionHolder(1);
5654

5755
$this->_em->persist($collectionHolder);
5856
$this->_em->flush();
5957
$this->_em->clear();
6058

6159
$collectionHolder = $this->_em->find(PersistentCollectionHolder::class, $collectionHolder->getId());
62-
$collection = $collectionHolder->getRawCollection();
60+
self::assertNotNull($collectionHolder);
61+
$collection = $collectionHolder->getRawCollection();
6362

6463
self::assertTrue($collection->isEmpty());
6564
self::assertFalse($collection->isInitialized());
6665

67-
$collectionHolder->addElement(new PersistentCollectionContent());
66+
$collectionHolder->addElement(new PersistentCollectionContent(1));
6867

6968
$this->_em->flush();
7069
$this->_em->clear();
@@ -82,7 +81,7 @@ public function testExtraLazyIsEmptyDoesNotInitializeCollection(): void
8281
*/
8382
public function testMatchingDoesNotModifyTheGivenCriteria(): void
8483
{
85-
$collectionHolder = new PersistentCollectionHolder();
84+
$collectionHolder = new PersistentCollectionHolder(1);
8685

8786
$this->_em->persist($collectionHolder);
8887
$this->_em->flush();
@@ -91,6 +90,7 @@ public function testMatchingDoesNotModifyTheGivenCriteria(): void
9190
$criteria = new Criteria();
9291

9392
$collectionHolder = $this->_em->find(PersistentCollectionHolder::class, $collectionHolder->getId());
93+
self::assertNotNull($collectionHolder);
9494
$collectionHolder->getCollection()->matching($criteria);
9595

9696
self::assertEmpty($criteria->getWhereExpression());
@@ -103,42 +103,48 @@ public function testMatchingDoesNotModifyTheGivenCriteria(): void
103103
/**
104104
* @Entity
105105
*/
106-
class PersistentCollectionHolder extends PersistentObject
106+
class PersistentCollectionHolder
107107
{
108108
/**
109109
* @Id
110110
* @Column(type="integer")
111111
* @GeneratedValue
112112
* @var int
113113
*/
114-
protected $id;
114+
private $id;
115115

116116
/**
117-
* @var Collection
117+
* @var Collection<int, PersistentCollectionContent>
118118
* @ManyToMany(targetEntity="PersistentCollectionContent", cascade={"all"}, fetch="EXTRA_LAZY")
119119
*/
120-
protected $collection;
120+
private $collection;
121121

122-
public function __construct()
122+
public function __construct(int $id)
123123
{
124+
$this->id = $id;
124125
$this->collection = new ArrayCollection();
125126
}
126127

128+
public function getId(): int
129+
{
130+
return $this->id;
131+
}
132+
127133
public function addElement(PersistentCollectionContent $element): void
128134
{
129135
$this->collection->add($element);
130136
}
131137

132138
/**
133-
* @return Collection
139+
* @return Collection<int, PersistentCollectionContent>
134140
*/
135141
public function getCollection(): Collection
136142
{
137143
return clone $this->collection;
138144
}
139145

140146
/**
141-
* @return Collection
147+
* @return Collection<int, PersistentCollectionContent>
142148
*/
143149
public function getRawCollection(): Collection
144150
{
@@ -149,13 +155,23 @@ public function getRawCollection(): Collection
149155
/**
150156
* @Entity
151157
*/
152-
class PersistentCollectionContent extends PersistentObject
158+
class PersistentCollectionContent
153159
{
154160
/**
155161
* @Id
156162
* @Column(type="integer")
157163
* @GeneratedValue
158164
* @var int
159165
*/
160-
protected $id;
166+
private $id;
167+
168+
public function __construct(int $id)
169+
{
170+
$this->id = $id;
171+
}
172+
173+
public function getId(): int
174+
{
175+
return $this->id;
176+
}
161177
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\ORM\Functional;
6+
7+
/**
8+
* @Entity
9+
*/
10+
class PersistentEntity extends PersistentObject
11+
{
12+
/**
13+
* @Id
14+
* @Column(type="integer")
15+
* @GeneratedValue
16+
* @var int
17+
*/
18+
protected $id;
19+
20+
/**
21+
* @Column(type="string")
22+
* @var string
23+
*/
24+
protected $name;
25+
26+
/**
27+
* @ManyToOne(targetEntity="PersistentEntity")
28+
* @var PersistentEntity
29+
*/
30+
protected $parent;
31+
}

tests/Doctrine/Tests/ORM/Functional/PersistentObjectTest.php

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
namespace Doctrine\Tests\ORM\Functional;
66

77
use Doctrine\Common\Persistence\PersistentObject;
8-
use Doctrine\ORM\Mapping\Column;
9-
use Doctrine\ORM\Mapping\Entity;
10-
use Doctrine\ORM\Mapping\GeneratedValue;
11-
use Doctrine\ORM\Mapping\Id;
12-
use Doctrine\ORM\Mapping\ManyToOne;
138
use Doctrine\Tests\OrmFunctionalTestCase;
149

10+
use function class_exists;
11+
1512
/**
1613
* Test that Doctrine ORM correctly works with the ObjectManagerAware and PersistentObject
1714
* classes from Common.
@@ -22,6 +19,10 @@ class PersistentObjectTest extends OrmFunctionalTestCase
2219
{
2320
protected function setUp(): void
2421
{
22+
if (! class_exists(PersistentObject::class)) {
23+
$this->markTestSkipped('This tests requires doctrine/persistence 2.');
24+
}
25+
2526
parent::setUp();
2627

2728
$this->createSchemaForModels(PersistentEntity::class);
@@ -85,29 +86,3 @@ public function testSetAssociation(): void
8586
self::assertSame($entity, $entity->getParent());
8687
}
8788
}
88-
89-
/**
90-
* @Entity
91-
*/
92-
class PersistentEntity extends PersistentObject
93-
{
94-
/**
95-
* @Id
96-
* @Column(type="integer")
97-
* @GeneratedValue
98-
* @var int
99-
*/
100-
protected $id;
101-
102-
/**
103-
* @Column(type="string")
104-
* @var string
105-
*/
106-
protected $name;
107-
108-
/**
109-
* @ManyToOne(targetEntity="PersistentEntity")
110-
* @var PersistentEntity
111-
*/
112-
protected $parent;
113-
}

0 commit comments

Comments
 (0)