Skip to content

Commit ecf069c

Browse files
committed
Merge branch 'fix/#815-#816-revert-bc-break-preventing-non-boolean-proxy-generator-modes-2.8' into 2.8
Backport #815 Backport #816
2 parents 3dd2f6c + 59efe45 commit ecf069c

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

lib/Doctrine/Common/Proxy/AbstractProxyFactory.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ abstract class AbstractProxyFactory
6969
*/
7070
const AUTOGENERATE_EVAL = 3;
7171

72+
private const AUTOGENERATE_MODES = [
73+
self::AUTOGENERATE_NEVER,
74+
self::AUTOGENERATE_ALWAYS,
75+
self::AUTOGENERATE_FILE_NOT_EXISTS,
76+
self::AUTOGENERATE_EVAL,
77+
];
78+
7279
/**
7380
* @var \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory
7481
*/
@@ -80,7 +87,7 @@ abstract class AbstractProxyFactory
8087
private $proxyGenerator;
8188

8289
/**
83-
* @var bool Whether to automatically (re)generate proxy classes.
90+
* @var int Whether to automatically (re)generate proxy classes.
8491
*/
8592
private $autoGenerate;
8693

@@ -93,12 +100,19 @@ abstract class AbstractProxyFactory
93100
* @param \Doctrine\Common\Proxy\ProxyGenerator $proxyGenerator
94101
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory $metadataFactory
95102
* @param bool|int $autoGenerate
103+
*
104+
* @throws \Doctrine\Common\Proxy\Exception\InvalidArgumentException When auto generate mode is not valid.
96105
*/
97106
public function __construct(ProxyGenerator $proxyGenerator, ClassMetadataFactory $metadataFactory, $autoGenerate)
98107
{
99108
$this->proxyGenerator = $proxyGenerator;
100109
$this->metadataFactory = $metadataFactory;
101-
$this->autoGenerate = (bool)$autoGenerate;
110+
111+
$this->autoGenerate = (int)$autoGenerate;
112+
113+
if ( ! in_array($this->autoGenerate, self::AUTOGENERATE_MODES, true)) {
114+
throw InvalidArgumentException::invalidAutoGenerateMode($autoGenerate);
115+
}
102116
}
103117

104118
/**

lib/Doctrine/Common/Proxy/Exception/InvalidArgumentException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,14 @@ public static function classMustNotBeFinal($className)
109109
{
110110
return new self(sprintf('Unable to create a proxy for a final class "%s".', $className));
111111
}
112+
113+
/**
114+
* @param mixed $value
115+
*
116+
* @return self
117+
*/
118+
public static function invalidAutoGenerateMode($value): self
119+
{
120+
return new self(sprintf('Invalid auto generate mode "%s" given.', $value));
121+
}
112122
}

tests/Doctrine/Tests/Common/Proxy/AbstractProxyFactoryTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,52 @@
1414

1515
class AbstractProxyFactoryTest extends DoctrineTestCase
1616
{
17+
public function dataAutoGenerateValues(): array
18+
{
19+
return [
20+
[0, 0],
21+
[1, 1],
22+
[2, 2],
23+
[3, 3],
24+
['2', 2],
25+
[true, 1],
26+
[false, 0],
27+
['', 0]
28+
];
29+
}
30+
31+
/**
32+
* @dataProvider dataAutoGenerateValues
33+
*
34+
* @param mixed $autoGenerate
35+
* @param int $expected
36+
*/
37+
public function testNoExceptionIsThrownForValidIntegerAutoGenerateValues($autoGenerate, int $expected): void
38+
{
39+
$proxyGenerator = $this->createMock(ProxyGenerator::class);
40+
$metadataFactory = $this->createMock(ClassMetadataFactory::class);
41+
42+
$proxyFactory = $this->getMockForAbstractClass(
43+
AbstractProxyFactory::class,
44+
[$proxyGenerator, $metadataFactory, $autoGenerate]
45+
);
46+
47+
self::assertAttributeSame($expected, 'autoGenerate', $proxyFactory);
48+
}
49+
50+
public function testInvalidAutoGenerateValueThrowsException(): void
51+
{
52+
$proxyGenerator = $this->createMock(ProxyGenerator::class);
53+
$metadataFactory = $this->createMock(ClassMetadataFactory::class);
54+
55+
$this->expectException(InvalidArgumentException::class);
56+
57+
$this->getMockForAbstractClass(
58+
AbstractProxyFactory::class,
59+
[$proxyGenerator, $metadataFactory, 5]
60+
);
61+
}
62+
1763
public function testGenerateProxyClasses()
1864
{
1965
$metadata = $this->createMock(ClassMetadata::class);

0 commit comments

Comments
 (0)