1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\Container; |
4
|
|
|
|
5
|
|
|
use DI\ContainerBuilder; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Smr\Container\ResettableContainer; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers \Smr\Container\ResettableContainerTrait |
11
|
|
|
*/ |
12
|
|
|
class ResettableContainerTraitTest extends TestCase { |
13
|
|
|
|
14
|
|
|
private ContainerBuilder $builder; |
15
|
|
|
|
16
|
|
|
protected function setUp(): void { |
17
|
|
|
$this->builder = new ContainerBuilder(ResettableContainer::class); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function test_not_initialized_by_definition(): void { |
21
|
|
|
$this->builder->addDefinitions([ |
22
|
|
|
'foo' => 'bar', |
23
|
|
|
]); |
24
|
|
|
|
25
|
|
|
// Unlike has(), entries are not initialized by definitions |
26
|
|
|
self::assertFalse($this->builder->build()->initialized('foo')); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function test_initialized_by_definition_after_get(): void { |
30
|
|
|
$this->builder->addDefinitions([ |
31
|
|
|
'foo' => 'bar', |
32
|
|
|
]); |
33
|
|
|
$container = $this->builder->build(); |
34
|
|
|
$container->get('foo'); |
35
|
|
|
|
36
|
|
|
// Only once we get the entry for the first time is it initialized |
37
|
|
|
self::assertTrue($container->initialized('foo')); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function test_initialized_when_set_directly(): void { |
41
|
|
|
$container = $this->builder->build(); |
42
|
|
|
$container->set('foo', 'bar'); |
43
|
|
|
|
44
|
|
|
// The entry is also initialized if set directly |
45
|
|
|
self::assertTrue($container->initialized('foo')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function test_not_initialized_when_unknown(): void { |
49
|
|
|
// Entry is not initialized in a default container |
50
|
|
|
self::assertFalse($this->builder->build()->initialized('foo')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function test_reset_with_definition(): void { |
54
|
|
|
$this->builder->addDefinitions([ |
55
|
|
|
'foo' => 'bar', |
56
|
|
|
]); |
57
|
|
|
$container = $this->builder->build(); |
58
|
|
|
|
59
|
|
|
// Sanity check the state of the entry prior to reset |
60
|
|
|
self::assertTrue($container->has('foo')); |
61
|
|
|
self::assertFalse($container->initialized('foo')); |
62
|
|
|
|
63
|
|
|
// Now initialize the entry, and sanity check the state again |
64
|
|
|
self::assertSame('bar', $container->get('foo')); |
65
|
|
|
self::assertTrue($container->has('foo')); |
66
|
|
|
self::assertTrue($container->initialized('foo')); |
67
|
|
|
|
68
|
|
|
// Then reset the entry |
69
|
|
|
$container->reset('foo'); |
|
|
|
|
70
|
|
|
|
71
|
|
|
// It should still be gettable, but it is no longer initialized |
72
|
|
|
self::assertTrue($container->has('foo')); |
73
|
|
|
self::assertFalse($container->initialized('foo')); |
74
|
|
|
self::assertSame('bar', $container->get('foo')); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function test_reset_when_set_directly(): void { |
78
|
|
|
$container = $this->builder->build(); |
79
|
|
|
$container->set('foo', 'bar'); |
80
|
|
|
|
81
|
|
|
// After resetting an entry that does not have a definition (i.e. it is |
82
|
|
|
// only set directly), it is no longer gettable. |
83
|
|
|
$container->reset('foo'); |
84
|
|
|
self::assertFalse($container->has('foo')); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|