1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Tests\Innmind\Reflection\ExtractionStrategy; |
5
|
|
|
|
6
|
|
|
use Innmind\Reflection\{ |
7
|
|
|
ExtractionStrategy\ReflectionStrategy, |
8
|
|
|
ExtractionStrategy, |
9
|
|
|
Exception\LogicException, |
10
|
|
|
}; |
11
|
|
|
use Fixtures\Innmind\Reflection\Foo; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class ReflectionStrategyTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
public function testInterface() |
17
|
|
|
{ |
18
|
|
|
$this->assertInstanceOf( |
19
|
|
|
ExtractionStrategy::class, |
20
|
|
|
new ReflectionStrategy |
21
|
|
|
); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testSupports() |
25
|
|
|
{ |
26
|
|
|
$o = new class { |
27
|
|
|
public $a; |
28
|
|
|
private $b; |
|
|
|
|
29
|
|
|
protected $c; |
30
|
|
|
}; |
31
|
|
|
|
32
|
|
|
$s = new ReflectionStrategy; |
33
|
|
|
|
34
|
|
|
$this->assertTrue($s->supports($o, 'a')); |
35
|
|
|
$this->assertTrue($s->supports($o, 'b')); |
36
|
|
|
$this->assertTrue($s->supports($o, 'c')); |
37
|
|
|
$this->assertFalse($s->supports($o, 'd')); |
38
|
|
|
|
39
|
|
|
$std = new \stdClass; |
40
|
|
|
$std->foo = 'bar'; |
41
|
|
|
|
42
|
|
|
$this->assertTrue($s->supports($std, 'foo')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testSupportsInheritMethod() |
46
|
|
|
{ |
47
|
|
|
$strategy = new ReflectionStrategy; |
48
|
|
|
$object = new class extends Foo {}; |
49
|
|
|
|
50
|
|
|
$this->assertTrue($strategy->supports($object, 'someProperty')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testThrowWhenExtractingUnsuppportedProperty() |
54
|
|
|
{ |
55
|
|
|
$o = new \stdClass; |
56
|
|
|
$s = new ReflectionStrategy; |
57
|
|
|
|
58
|
|
|
$this->expectException(LogicException::class); |
59
|
|
|
|
60
|
|
|
$s->extract($o, 'a'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testExtract() |
64
|
|
|
{ |
65
|
|
|
$o = new class { |
66
|
|
|
public $a = 24; |
67
|
|
|
private $b = 42; |
|
|
|
|
68
|
|
|
protected $c = 66; |
69
|
|
|
}; |
70
|
|
|
$s = new ReflectionStrategy; |
71
|
|
|
|
72
|
|
|
$this->assertSame(24, $s->extract($o, 'a')); |
73
|
|
|
$this->assertSame(42, $s->extract($o, 'b')); |
74
|
|
|
$this->assertSame(66, $s->extract($o, 'c')); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testExtractInheritedProperty() |
78
|
|
|
{ |
79
|
|
|
$child = new class extends Foo {}; |
80
|
|
|
|
81
|
|
|
$strategy = new ReflectionStrategy; |
82
|
|
|
|
83
|
|
|
$this->assertSame(42, $strategy->extract($child, 'someProperty')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testExtractDynamicallySetProperty() |
87
|
|
|
{ |
88
|
|
|
$strategy = new ReflectionStrategy; |
89
|
|
|
$std = new \stdClass; |
90
|
|
|
$std->foo = 'bar'; |
91
|
|
|
|
92
|
|
|
$this->assertSame('bar', $strategy->extract($std, 'foo')); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|