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