From 95639be51802c83fa4f7c5385de1557953b100a9 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 21 Jun 2019 14:29:00 +0200 Subject: [PATCH 1/2] #469 ensure factory considers `proxyOptions` when generating unique class names --- .../Factory/AbstractBaseFactory.php | 1 + .../Factory/AbstractBaseFactoryTest.php | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/ProxyManager/Factory/AbstractBaseFactory.php b/src/ProxyManager/Factory/AbstractBaseFactory.php index 83f8aa6ca..0a16c786c 100644 --- a/src/ProxyManager/Factory/AbstractBaseFactory.php +++ b/src/ProxyManager/Factory/AbstractBaseFactory.php @@ -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 diff --git a/tests/ProxyManagerTest/Factory/AbstractBaseFactoryTest.php b/tests/ProxyManagerTest/Factory/AbstractBaseFactoryTest.php index 9ccd01a3d..0b9ed303b 100644 --- a/tests/ProxyManagerTest/Factory/AbstractBaseFactoryTest.php +++ b/tests/ProxyManagerTest/Factory/AbstractBaseFactoryTest.php @@ -17,6 +17,7 @@ use ReflectionMethod; use stdClass; use Zend\Code\Generator\ClassGenerator; +use function uniqid; /** * Tests for {@see \ProxyManager\Factory\AbstractBaseFactory} @@ -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(function (string $className) : bool { + eval('class ' . $className . ' {}'); + + return true; + })); + + self::assertSame( + $generatedClass, + $generateProxy->invoke($this->factory, stdClass::class, $proxyOptions) + ); + } } From 34ee79302d2bcd2ec663d7e4922f2382264c74df Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 10 Aug 2019 10:30:33 +0200 Subject: [PATCH 2/2] s/function/static function for anonymous functions not relying on `$this` --- tests/ProxyManagerTest/Factory/AbstractBaseFactoryTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/ProxyManagerTest/Factory/AbstractBaseFactoryTest.php b/tests/ProxyManagerTest/Factory/AbstractBaseFactoryTest.php index 0b9ed303b..f3473a7b4 100644 --- a/tests/ProxyManagerTest/Factory/AbstractBaseFactoryTest.php +++ b/tests/ProxyManagerTest/Factory/AbstractBaseFactoryTest.php @@ -140,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; @@ -153,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), @@ -196,7 +196,7 @@ public function testGeneratesClassBasedOnGivenProxyOptions() : void $this ->proxyAutoloader ->method('__invoke') - ->will(self::returnCallback(function (string $className) : bool { + ->will(self::returnCallback(static function (string $className) : bool { eval('class ' . $className . ' {}'); return true;