Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert proxy factory auto generate mode to integer #815

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/Doctrine/Common/Proxy/AbstractProxyFactory.php
Expand Up @@ -69,6 +69,13 @@ abstract class AbstractProxyFactory
*/
const AUTOGENERATE_EVAL = 3;

private const AUTOGENERATE_MODES = [
self::AUTOGENERATE_NEVER,
self::AUTOGENERATE_ALWAYS,
self::AUTOGENERATE_FILE_NOT_EXISTS,
self::AUTOGENERATE_EVAL
];

/**
* @var \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory
*/
Expand All @@ -80,7 +87,7 @@ abstract class AbstractProxyFactory
private $proxyGenerator;

/**
* @var bool Whether to automatically (re)generate proxy classes.
* @var int Whether to automatically (re)generate proxy classes.
*/
private $autoGenerate;

Expand All @@ -93,12 +100,19 @@ abstract class AbstractProxyFactory
* @param \Doctrine\Common\Proxy\ProxyGenerator $proxyGenerator
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory $metadataFactory
* @param bool|int $autoGenerate
*
* @throws \Doctrine\Common\Proxy\Exception\InvalidArgumentException When auto generate mode is not valid.
*/
public function __construct(ProxyGenerator $proxyGenerator, ClassMetadataFactory $metadataFactory, $autoGenerate)
{
$this->proxyGenerator = $proxyGenerator;
$this->metadataFactory = $metadataFactory;
$this->autoGenerate = (bool)$autoGenerate;

if ( ! in_array($autoGenerate, self::AUTOGENERATE_MODES, false)) {
throw InvalidArgumentException::invalidAutoGenerateMode($autoGenerate);
}

$this->autoGenerate = (int)$autoGenerate;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions lib/Doctrine/Common/Proxy/Exception/InvalidArgumentException.php
Expand Up @@ -109,4 +109,14 @@ public static function classMustNotBeFinal($className)
{
return new self(sprintf('Unable to create a proxy for a final class "%s".', $className));
}

/**
* @param mixed $value
*
* @return self
*/
public static function invalidAutoGenerateMode($value): self
{
return new self(sprintf('Invalid auto generate mode "%s" given.', $value));
}
}
46 changes: 46 additions & 0 deletions tests/Doctrine/Tests/Common/Proxy/AbstractProxyFactoryTest.php
Expand Up @@ -14,6 +14,52 @@

class AbstractProxyFactoryTest extends DoctrineTestCase
{
public function dataAutoGenerateValues(): array
{
return [
[0, 0],
[1, 1],
[2, 2],
[3, 3],
['2', 2],
[true, 1],
[false, 0],
['', 0]
];
}

/**
* @dataProvider dataAutoGenerateValues
*
* @param mixed $autoGenerate
* @param int $expected
*/
public function testNoExceptionIsThrownForValidIntegerAutoGenerateValues($autoGenerate, int $expected): void
{
$proxyGenerator = $this->createMock(ProxyGenerator::class);
$metadataFactory = $this->createMock(ClassMetadataFactory::class);

$proxyFactory = $this->getMockForAbstractClass(
AbstractProxyFactory::class,
[$proxyGenerator, $metadataFactory, $autoGenerate]
);

self::assertAttributeSame($expected, 'autoGenerate', $proxyFactory);
}

public function testInvalidAutoGenerateValueThrowsException(): void
{
$proxyGenerator = $this->createMock(ProxyGenerator::class);
$metadataFactory = $this->createMock(ClassMetadataFactory::class);

$this->expectException(InvalidArgumentException::class);

$this->getMockForAbstractClass(
AbstractProxyFactory::class,
[$proxyGenerator, $metadataFactory, 5]
);
}

public function testGenerateProxyClasses()
{
$metadata = $this->createMock(ClassMetadata::class);
Expand Down