1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
<?php
declare(strict_types=1);
namespace ProxyManagerTest\ProxyGenerator\LazyLoadingGhost\MethodGenerator;
use Laminas\Code\Generator\MethodGenerator;
use Laminas\Code\Generator\PropertyGenerator;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicIsset;
use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\PrivatePropertiesMap;
use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\ProtectedPropertiesMap;
use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
use ProxyManagerTestAsset\ClassWithMagicMethods;
use ProxyManagerTestAsset\ProxyGenerator\LazyLoading\MethodGenerator\ClassWithTwoPublicProperties;
use ReflectionClass;
/**
* Tests for {@see \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicIsset}
*
* @group Coverage
*/
final class MagicIssetTest extends TestCase
{
/** @var PropertyGenerator&MockObject */
private $initializer;
/** @var MethodGenerator&MockObject */
private $initMethod;
/** @var PublicPropertiesMap&MockObject */
private $publicProperties;
/** @var ProtectedPropertiesMap&MockObject */
private $protectedProperties;
/** @var PrivatePropertiesMap&MockObject */
private $privateProperties;
private $expectedCode = <<<'PHP'
$this->foo && $this->baz('__isset', array('name' => $name));
if (isset(self::$bar[$name])) {
return isset($this->$name);
}
if (isset(self::$baz[$name])) {
// check protected property access via compatible class
$callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
$caller = isset($callers[1]) ? $callers[1] : [];
$object = isset($caller['object']) ? $caller['object'] : '';
$expectedType = self::$baz[$name];
if ($object instanceof $expectedType) {
return isset($this->$name);
}
$class = isset($caller['class']) ? $caller['class'] : '';
if ($class === $expectedType || is_subclass_of($class, $expectedType)) {
return isset($this->$name);
}
} else {
// check private property access via same class
$callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
$caller = isset($callers[1]) ? $callers[1] : [];
$class = isset($caller['class']) ? $caller['class'] : '';
static $accessorCache = [];
if (isset(self::$tab[$name][$class])) {
$cacheKey = $class . '#' . $name;
$accessor = isset($accessorCache[$cacheKey])
? $accessorCache[$cacheKey]
: $accessorCache[$cacheKey] = \Closure::bind(static function ($instance) use ($name) {
return isset($instance->$name);
}, null, $class);
return $accessor($this);
}
if ('ReflectionProperty' === $class) {
$tmpClass = key(self::$tab[$name]);
$cacheKey = $tmpClass . '#' . $name;
$accessor = isset($accessorCache[$cacheKey])
? $accessorCache[$cacheKey]
: $accessorCache[$cacheKey] = \Closure::bind(static function ($instance) use ($name) {
return isset($instance->$name);
}, null, $tmpClass);
return $accessor($this);
}
}
%A
PHP;
protected function setUp(): void
{
$this->initializer = $this->createMock(PropertyGenerator::class);
$this->initMethod = $this->createMock(MethodGenerator::class);
$this->publicProperties = $this->createMock(PublicPropertiesMap::class);
$this->protectedProperties = $this->createMock(ProtectedPropertiesMap::class);
$this->privateProperties = $this->createMock(PrivatePropertiesMap::class);
$this->initializer->method('getName')->willReturn('foo');
$this->initMethod->method('getName')->willReturn('baz');
$this->publicProperties->method('isEmpty')->willReturn(false);
$this->publicProperties->method('getName')->willReturn('bar');
$this->protectedProperties->method('getName')->willReturn('baz');
$this->privateProperties->method('getName')->willReturn('tab');
}
/**
* @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicIsset
*/
public function testBodyStructureWithPublicProperties(): void
{
$magicIsset = new MagicIsset(
new ReflectionClass(ClassWithTwoPublicProperties::class),
$this->initializer,
$this->initMethod,
$this->publicProperties,
$this->protectedProperties,
$this->privateProperties
);
self::assertSame('__isset', $magicIsset->getName());
self::assertCount(1, $magicIsset->getParameters());
self::assertStringMatchesFormat($this->expectedCode, $magicIsset->getBody());
}
/**
* @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicIsset
*/
public function testBodyStructureWithOverriddenMagicGet(): void
{
$magicIsset = new MagicIsset(
new ReflectionClass(ClassWithMagicMethods::class),
$this->initializer,
$this->initMethod,
$this->publicProperties,
$this->protectedProperties,
$this->privateProperties
);
self::assertSame('__isset', $magicIsset->getName());
self::assertCount(1, $magicIsset->getParameters());
$body = $magicIsset->getBody();
self::assertStringMatchesFormat($this->expectedCode, $body);
self::assertStringMatchesFormat('%Areturn parent::__isset($name);', $body);
}
}
|