|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Tests\Innmind\Reflection\Instanciator; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Reflection\{ |
|
7
|
|
|
Instanciator\ReflectionInstanciator, |
|
8
|
|
|
Instanciator, |
|
9
|
|
|
Exception\InstanciationFailed, |
|
10
|
|
|
}; |
|
11
|
|
|
use Innmind\Immutable\{ |
|
12
|
|
|
Map, |
|
13
|
|
|
Set, |
|
14
|
|
|
}; |
|
15
|
|
|
use function Innmind\Immutable\unwrap; |
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
|
|
18
|
|
|
class ReflectionInstanciatorTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
public function testInterface() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->assertInstanceOf( |
|
23
|
|
|
Instanciator::class, |
|
24
|
|
|
new ReflectionInstanciator |
|
25
|
|
|
); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testBuild() |
|
29
|
|
|
{ |
|
30
|
|
|
$i = new ReflectionInstanciator; |
|
31
|
|
|
|
|
32
|
|
|
$object = $i->build( |
|
33
|
|
|
Foo::class, |
|
34
|
|
|
Map::of('string', 'mixed') |
|
35
|
|
|
('o', $o = new \stdClass) |
|
36
|
|
|
('bar', 'foo') |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertInstanceOf(Foo::class, $object); |
|
40
|
|
|
$this->assertSame([$o, 'foo', null], $object->properties); |
|
41
|
|
|
|
|
42
|
|
|
$object = $i->build( |
|
43
|
|
|
Foo::class, |
|
44
|
|
|
Map::of('string', 'mixed') |
|
45
|
|
|
('o', $o = new \stdClass) |
|
46
|
|
|
('bar', 'foo') |
|
47
|
|
|
('baz', 42) |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertInstanceOf(Foo::class, $object); |
|
51
|
|
|
$this->assertSame([$o, 'foo', 42], $object->properties); |
|
52
|
|
|
|
|
53
|
|
|
$object = $i->build('stdClass', Map::of('string', 'mixed')); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertInstanceOf('stdClass', $object); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testThrowWhenClassCannotBeInstanciated() |
|
59
|
|
|
{ |
|
60
|
|
|
$i = new ReflectionInstanciator; |
|
61
|
|
|
|
|
62
|
|
|
$this->expectException(InstanciationFailed::class); |
|
63
|
|
|
$this->expectExceptionMessage('Class "Tests\Innmind\Reflection\Instanciator\Foo" cannot be instanciated'); |
|
64
|
|
|
|
|
65
|
|
|
$i->build(Foo::class, Map::of('string', 'mixed')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testGetParameters() |
|
69
|
|
|
{ |
|
70
|
|
|
$i = new ReflectionInstanciator; |
|
71
|
|
|
|
|
72
|
|
|
$parameters = $i->parameters(Foo::class); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertInstanceOf(Set::class, $parameters); |
|
75
|
|
|
$this->assertSame('string', (string) $parameters->type()); |
|
76
|
|
|
$this->assertSame(['o', 'bar', 'baz'], unwrap($parameters)); |
|
77
|
|
|
|
|
78
|
|
|
$parameters = $i->parameters('stdClass'); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertSame(0, $parameters->count()); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
class Foo |
|
85
|
|
|
{ |
|
86
|
|
|
public $properties; |
|
87
|
|
|
|
|
88
|
|
|
public function __construct(\stdClass $o, string $bar, $baz = null) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->properties = [$o, $bar, $baz]; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|