|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ProxyManagerTest\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator; |
|
6
|
|
|
|
|
7
|
|
|
use Laminas\Code\Generator\PropertyGenerator; |
|
8
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicGet; |
|
11
|
|
|
use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap; |
|
12
|
|
|
use ProxyManagerTestAsset\ClassWithMagicMethods; |
|
13
|
|
|
use ProxyManagerTestAsset\EmptyClass; |
|
14
|
|
|
use ReflectionClass; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Tests for {@see \ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicGet} |
|
18
|
|
|
* |
|
19
|
|
|
* @group Coverage |
|
20
|
|
|
*/ |
|
21
|
|
|
final class MagicGetTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @covers \ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicGet::__construct |
|
25
|
|
|
*/ |
|
26
|
|
|
public function testBodyStructure() : void |
|
27
|
|
|
{ |
|
28
|
|
|
$reflection = new ReflectionClass(EmptyClass::class); |
|
29
|
|
|
$initializer = $this->createMock(PropertyGenerator::class); |
|
|
|
|
|
|
30
|
|
|
$valueHolder = $this->createMock(PropertyGenerator::class); |
|
|
|
|
|
|
31
|
|
|
$publicProperties = $this->createMock(PublicPropertiesMap::class); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
$initializer->method('getName')->willReturn('foo'); |
|
|
|
|
|
|
34
|
|
|
$valueHolder->method('getName')->willReturn('bar'); |
|
|
|
|
|
|
35
|
|
|
$publicProperties->method('isEmpty')->willReturn(false); |
|
|
|
|
|
|
36
|
|
|
$publicProperties->method('getName')->willReturn('bar'); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
$magicGet = new MagicGet($reflection, $initializer, $valueHolder, $publicProperties); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
self::assertSame('__get', $magicGet->getName()); |
|
|
|
|
|
|
41
|
|
|
self::assertCount(1, $magicGet->getParameters()); |
|
|
|
|
|
|
42
|
|
|
self::assertStringMatchesFormat( |
|
|
|
|
|
|
43
|
|
|
"\$this->foo && \$this->foo->__invoke(\$this->bar, \$this, '__get', ['name' => \$name]" |
|
44
|
|
|
. ", \$this->foo);\n\n" |
|
45
|
|
|
. "if (isset(self::\$bar[\$name])) {\n return \$this->bar->\$name;\n}" |
|
46
|
|
|
. '%areturn %s;', |
|
47
|
|
|
$magicGet->getBody() |
|
48
|
|
|
); |
|
49
|
|
|
self::assertStringNotMatchesFormat('%Areturn $this->bar->__get($name);%A', $magicGet->getBody()); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @group 344 |
|
54
|
|
|
*/ |
|
55
|
|
|
public function testBodyStructureWithPreExistingGetMethod() : void |
|
56
|
|
|
{ |
|
57
|
|
|
$reflection = new ReflectionClass(ClassWithMagicMethods::class); |
|
58
|
|
|
$initializer = $this->createMock(PropertyGenerator::class); |
|
|
|
|
|
|
59
|
|
|
$valueHolder = $this->createMock(PropertyGenerator::class); |
|
|
|
|
|
|
60
|
|
|
$publicProperties = $this->createMock(PublicPropertiesMap::class); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
$initializer->method('getName')->willReturn('foo'); |
|
|
|
|
|
|
63
|
|
|
$valueHolder->method('getName')->willReturn('bar'); |
|
|
|
|
|
|
64
|
|
|
$publicProperties->method('isEmpty')->willReturn(false); |
|
|
|
|
|
|
65
|
|
|
$publicProperties->method('getName')->willReturn('bar'); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
$magicGet = new MagicGet($reflection, $initializer, $valueHolder, $publicProperties); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
self::assertSame('__get', $magicGet->getName()); |
|
|
|
|
|
|
70
|
|
|
self::assertCount(1, $magicGet->getParameters()); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
self::assertStringMatchesFormat( |
|
|
|
|
|
|
73
|
|
|
"\$this->foo && \$this->foo->__invoke(\$this->bar, \$this, '__get', ['name' => \$name]" |
|
74
|
|
|
. ", \$this->foo);\n\n" |
|
75
|
|
|
. "if (isset(self::\$bar[\$name])) {\n return \$this->bar->\$name;\n}\n\n" |
|
76
|
|
|
. 'return $this->bar->__get($name);', |
|
77
|
|
|
$magicGet->getBody(), |
|
78
|
|
|
'The pre-existing magic `__get` is called, if the property is not accessible' |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.