1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DummyGenerator\Test\Container; |
6
|
|
|
|
7
|
|
|
use DummyGenerator\Container\ContainerException; |
8
|
|
|
use DummyGenerator\Container\DefinitionContainer; |
9
|
|
|
use DummyGenerator\Container\NotInContainerException; |
10
|
|
|
use DummyGenerator\Definitions\Extension\ExtensionInterface; |
11
|
|
|
use DummyGenerator\Test\Fixtures\InvalidExtensionInterfaceClass; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class DefinitionContainerTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
public function testCanCheckIfContainerHasDefinition(): void |
17
|
|
|
{ |
18
|
|
|
$definitions = [ |
19
|
|
|
'some_name' => fn () => 'some_value' |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
$container = new DefinitionContainer($definitions); |
23
|
|
|
|
24
|
|
|
self::assertTrue($container->has('some_name')); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testCanGetDefinitionFromContainer(): void |
28
|
|
|
{ |
29
|
|
|
$definitions = [ |
30
|
|
|
'some_name' => fn () => new class implements ExtensionInterface { |
31
|
|
|
} |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
$container = new DefinitionContainer($definitions); |
35
|
|
|
|
36
|
|
|
self::assertInstanceOf(ExtensionInterface::class, $container->get('some_name')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testThrowsExceptionIfDefinitionNotInContainer(): void |
40
|
|
|
{ |
41
|
|
|
$container = new DefinitionContainer([]); |
42
|
|
|
|
43
|
|
|
$this->expectException(NotInContainerException::class); |
44
|
|
|
$container->get('some_name'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testCanAddDefinitionToContainer(): void |
48
|
|
|
{ |
49
|
|
|
$container = new DefinitionContainer([]); |
50
|
|
|
|
51
|
|
|
$container->add('some_name', new class implements ExtensionInterface { |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
self::assertTrue($container->has('some_name')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testCanFindProcessorForMethod(): void |
58
|
|
|
{ |
59
|
|
|
$definitions = [ |
60
|
|
|
'some_name' => fn () => new class implements ExtensionInterface { |
61
|
|
|
public function hello(string $name): string |
62
|
|
|
{ |
63
|
|
|
return 'hello ' . $name; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
$container = new DefinitionContainer($definitions); |
69
|
|
|
|
70
|
|
|
$processor = $container->findProcessor('hello'); |
71
|
|
|
|
72
|
|
|
self::assertInstanceOf(ExtensionInterface::class, $processor); |
73
|
|
|
self::assertEquals('hello Johny', $processor->hello('Johny')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testThrowsExceptionIfCallbackDefinitionDoesNotReturnClass(): void |
77
|
|
|
{ |
78
|
|
|
$definitions = [ |
79
|
|
|
'some_name' => fn () => true |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
$container = new DefinitionContainer($definitions); |
83
|
|
|
|
84
|
|
|
$this->expectException(ContainerException::class); |
85
|
|
|
$this->expectExceptionMessage('Error while invoking callable for "some_name"'); |
86
|
|
|
$container->get('some_name'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testThrowsExceptionIfDefinitionPointsToNonExistingClass(): void |
90
|
|
|
{ |
91
|
|
|
$definitions = [ |
92
|
|
|
'some_name' => 'string_that_should_be_class_name' |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$container = new DefinitionContainer($definitions); |
96
|
|
|
|
97
|
|
|
$this->expectException(ContainerException::class); |
98
|
|
|
$this->expectExceptionMessage('Could not instantiate class for "some_name". Class was not found.'); |
99
|
|
|
$container->get('some_name'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testThrowsExceptionIfDefinitionPointsToClassThatCannotBeCreated(): void |
103
|
|
|
{ |
104
|
|
|
$definitions = [ |
105
|
|
|
'some_name' => InvalidExtensionInterfaceClass::class |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
$container = new DefinitionContainer($definitions); |
109
|
|
|
|
110
|
|
|
$this->expectException(ContainerException::class); |
111
|
|
|
$this->expectExceptionMessage('Could not instantiate class for "some_name"'); |
112
|
|
|
$container->get('some_name'); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|