|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Tests\Innmind\Reflection\InjectionStrategy; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Reflection\{ |
|
7
|
|
|
InjectionStrategy\ReflectionStrategy, |
|
8
|
|
|
InjectionStrategy, |
|
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
|
|
|
InjectionStrategy::class, |
|
20
|
|
|
new ReflectionStrategy |
|
21
|
|
|
); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testSupports() |
|
25
|
|
|
{ |
|
26
|
|
|
$s = new ReflectionStrategy; |
|
27
|
|
|
$o = new class { |
|
28
|
|
|
private $a; |
|
29
|
|
|
}; |
|
30
|
|
|
|
|
31
|
|
|
$this->assertTrue($s->supports($o, 'a', null)); |
|
32
|
|
|
$this->assertFalse($s->supports($o, 'b', null)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testInject() |
|
36
|
|
|
{ |
|
37
|
|
|
$s = new ReflectionStrategy; |
|
38
|
|
|
$o = new class { |
|
39
|
|
|
private $a; |
|
40
|
|
|
protected $b; |
|
41
|
|
|
public $c; |
|
42
|
|
|
|
|
43
|
|
|
public function a() |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->a; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function b() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->b; |
|
51
|
|
|
} |
|
52
|
|
|
}; |
|
53
|
|
|
|
|
54
|
|
|
$this->assertSame($o, $s->inject($o, 'a', 'bar')); |
|
55
|
|
|
$this->assertSame('bar', $o->a()); |
|
56
|
|
|
$this->assertSame($o, $s->inject($o, 'b', 'bar')); |
|
57
|
|
|
$this->assertSame('bar', $o->b()); |
|
58
|
|
|
$this->assertSame($o, $s->inject($o, 'c', 'bar')); |
|
59
|
|
|
$this->assertSame('bar', $o->c); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testInjectInheritedProperty() |
|
63
|
|
|
{ |
|
64
|
|
|
$strategy = new ReflectionStrategy; |
|
65
|
|
|
$object = new class extends Foo {}; |
|
66
|
|
|
|
|
67
|
|
|
$this->assertSame(42, $object->someProperty()); |
|
68
|
|
|
$this->assertSame($object, $strategy->inject($object, 'someProperty', 24)); |
|
69
|
|
|
$this->assertSame(24, $object->someProperty()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testThrowWhenInjectingUnsupportedProperty() |
|
73
|
|
|
{ |
|
74
|
|
|
$s = new ReflectionStrategy; |
|
75
|
|
|
$o = new class { |
|
76
|
|
|
public $b; |
|
77
|
|
|
}; |
|
78
|
|
|
|
|
79
|
|
|
$this->expectException(LogicException::class); |
|
80
|
|
|
|
|
81
|
|
|
$s->inject($o, 'a', 'foo'); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|