Skip to content

Commit

Permalink
Merge pull request #470 from Ocramius/fix/#469-ensure-factory-generat…
Browse files Browse the repository at this point in the history
…es-separate-classes

#469 ensure factory considers `proxyOptions` when generating unique class names
  • Loading branch information
Ocramius authored Aug 10, 2019
2 parents 41cac6e + 34ee793 commit 4d15474
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/ProxyManager/Factory/AbstractBaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected function generateProxy(string $className, array $proxyOptions = []) :
'className' => $className,
'factory' => get_class($this),
'proxyManagerVersion' => Version::getVersion(),
'proxyOptions' => $proxyOptions,
];
$proxyClassName = $this
->configuration
Expand Down
42 changes: 40 additions & 2 deletions tests/ProxyManagerTest/Factory/AbstractBaseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ReflectionMethod;
use stdClass;
use Zend\Code\Generator\ClassGenerator;
use function uniqid;

/**
* Tests for {@see \ProxyManager\Factory\AbstractBaseFactory}
Expand Down Expand Up @@ -139,7 +140,7 @@ public function testGeneratesClass() : void
->expects(self::once())
->method('__invoke')
->with($generatedClass)
->will(self::returnCallback(function ($className) : bool {
->will(self::returnCallback(static function ($className) : bool {
eval('class ' . $className . ' {}');

return true;
Expand All @@ -152,7 +153,7 @@ public function testGeneratesClass() : void
->expects(self::once())
->method('generate')
->with(
self::callback(function (\ReflectionClass $reflectionClass) : bool {
self::callback(static function (\ReflectionClass $reflectionClass) : bool {
return $reflectionClass->getName() === 'stdClass';
}),
self::isInstanceOf(ClassGenerator::class),
Expand All @@ -169,4 +170,41 @@ public function testGeneratesClass() : void
$generateProxy->invoke($this->factory, stdClass::class, ['some' => 'proxy', 'options' => 'here'])
);
}

/**
* @group 469
*/
public function testGeneratesClassBasedOnGivenProxyOptions() : void
{
$generateProxy = new ReflectionMethod($this->factory, 'generateProxy');

$generateProxy->setAccessible(true);
$generatedClass = UniqueIdentifierGenerator::getIdentifier('fooBar');
$proxyOptions = [uniqid('key', true) => uniqid('value', true)];

$this
->classNameInflector
->expects(self::any())
->method('getProxyClassName')
->with('stdClass', self::callback(static function (array $parameters) use ($proxyOptions) : bool {
self::assertSame($proxyOptions, $parameters['proxyOptions']);

return true;
}))
->will(self::returnValue($generatedClass));

$this
->proxyAutoloader
->method('__invoke')
->will(self::returnCallback(static function (string $className) : bool {
eval('class ' . $className . ' {}');

return true;
}));

self::assertSame(
$generatedClass,
$generateProxy->invoke($this->factory, stdClass::class, $proxyOptions)
);
}
}

0 comments on commit 4d15474

Please sign in to comment.