|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GacelaTest\Unit\Framework\Container; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\Container\Container; |
|
8
|
|
|
use Gacela\Framework\Container\Locator; |
|
9
|
|
|
use Gacela\Framework\Exception\ServiceNotFoundException; |
|
10
|
|
|
use GacelaTest\Fixtures\StringValue; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
|
|
13
|
|
|
final class LocatorTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
protected function setUp(): void |
|
16
|
|
|
{ |
|
17
|
|
|
Locator::resetInstance(); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function test_get_concrete_class(): void |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var StringValue $stringValue */ |
|
23
|
|
|
$stringValue = Locator::getInstance()->get(StringValue::class); |
|
24
|
|
|
self::assertInstanceOf(StringValue::class, $stringValue); |
|
25
|
|
|
self::assertSame('', $stringValue->value()); |
|
26
|
|
|
$stringValue->setValue('updated value'); |
|
27
|
|
|
|
|
28
|
|
|
/** @var StringValue $stringValue2 */ |
|
29
|
|
|
$stringValue2 = Locator::getInstance()->get(StringValue::class); |
|
30
|
|
|
self::assertSame('updated value', $stringValue2->value()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function test_get_non_existing_singleton(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$nullValue = Locator::getSingleton(NonExisting::class); |
|
36
|
|
|
|
|
37
|
|
|
self::assertNull($nullValue); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function test_get_existing_singleton(): void |
|
41
|
|
|
{ |
|
42
|
|
|
Locator::addSingleton(StringValue::class, new StringValue('str')); |
|
43
|
|
|
|
|
44
|
|
|
$singleton = Locator::getSingleton(StringValue::class); |
|
45
|
|
|
|
|
46
|
|
|
self::assertEquals(new StringValue('str'), $singleton); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function test_get_existing_singleton_from_container(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$container = new Container(bindings: [ |
|
52
|
|
|
StringValue::class => new StringValue('str'), |
|
53
|
|
|
]); |
|
54
|
|
|
|
|
55
|
|
|
$singleton = Locator::getSingleton(StringValue::class, $container); |
|
56
|
|
|
|
|
57
|
|
|
self::assertEquals(new StringValue('str'), $singleton); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function test_get_required_throws_when_not_found(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->expectException(ServiceNotFoundException::class); |
|
63
|
|
|
$this->expectExceptionMessage('Service "GacelaTest\Unit\Framework\Container\NonExisting" not found in the container.'); |
|
64
|
|
|
|
|
65
|
|
|
Locator::getInstance()->getRequired(NonExisting::class); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function test_get_required_singleton_throws_when_not_found(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$this->expectException(ServiceNotFoundException::class); |
|
71
|
|
|
$this->expectExceptionMessage('Service "GacelaTest\Unit\Framework\Container\NonExisting" not found in the container.'); |
|
72
|
|
|
|
|
73
|
|
|
Locator::getRequiredSingleton(NonExisting::class); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function test_get_required_returns_existing_service(): void |
|
77
|
|
|
{ |
|
78
|
|
|
Locator::addSingleton(StringValue::class, new StringValue('required')); |
|
79
|
|
|
|
|
80
|
|
|
$singleton = Locator::getInstance()->getRequired(StringValue::class); |
|
81
|
|
|
|
|
82
|
|
|
self::assertEquals(new StringValue('required'), $singleton); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function test_get_required_singleton_returns_existing_service(): void |
|
86
|
|
|
{ |
|
87
|
|
|
Locator::addSingleton(StringValue::class, new StringValue('required')); |
|
88
|
|
|
|
|
89
|
|
|
$singleton = Locator::getRequiredSingleton(StringValue::class); |
|
90
|
|
|
|
|
91
|
|
|
self::assertEquals(new StringValue('required'), $singleton); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function test_get_required_singleton_from_container(): void |
|
95
|
|
|
{ |
|
96
|
|
|
$container = new Container(bindings: [ |
|
97
|
|
|
StringValue::class => new StringValue('from-container'), |
|
98
|
|
|
]); |
|
99
|
|
|
|
|
100
|
|
|
$singleton = Locator::getRequiredSingleton(StringValue::class, $container); |
|
101
|
|
|
|
|
102
|
|
|
self::assertEquals(new StringValue('from-container'), $singleton); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|