1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\Container; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Smr\Container\DiContainer; |
7
|
|
|
use Smr\DatabaseProperties; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers \Smr\Container\DiContainer |
11
|
|
|
*/ |
12
|
|
|
class DiContainerTest extends TestCase { |
13
|
|
|
|
14
|
|
|
private const PHPDI_COMPILED_CONTAINER_FILE = '/tmp/CompiledContainer.php'; |
15
|
|
|
|
16
|
|
|
protected function tearDown(): void { |
17
|
|
|
if (file_exists(self::PHPDI_COMPILED_CONTAINER_FILE)) { |
18
|
|
|
unlink(self::PHPDI_COMPILED_CONTAINER_FILE); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function test_compilation_enabled_true(): void { |
23
|
|
|
// Given the container is built with compilation enabled |
24
|
|
|
DiContainer::initialize(true); |
25
|
|
|
// Then |
26
|
|
|
self::assertFileExists(self::PHPDI_COMPILED_CONTAINER_FILE); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function test_compilation_enabled_false(): void { |
30
|
|
|
// Given the container is built with compilation disabled |
31
|
|
|
DiContainer::initialize(false); |
32
|
|
|
// Then |
33
|
|
|
self::assertFileDoesNotExist(self::PHPDI_COMPILED_CONTAINER_FILE); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function test_container_get_and_make(): void { |
37
|
|
|
// Start with a fresh container |
38
|
|
|
DiContainer::initialize(false); |
39
|
|
|
|
40
|
|
|
// The first get should construct a new object |
41
|
|
|
$class = DatabaseProperties::class; |
42
|
|
|
$instance1 = DiContainer::get($class); |
43
|
|
|
self::assertInstanceOf($class, $instance1); |
44
|
|
|
|
45
|
|
|
// Getting the same class should now give the exact same object |
46
|
|
|
$instance2 = DiContainer::get($class); |
47
|
|
|
self::assertSame($instance1, $instance2); |
48
|
|
|
|
49
|
|
|
// Using make should construct a new object |
50
|
|
|
$instance3 = DiContainer::make($class); |
51
|
|
|
self::assertNotSame($instance1, $instance3); |
52
|
|
|
self::assertEquals($instance1, $instance3); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function test_factory_DatabaseName(): void { |
56
|
|
|
// Start with a fresh container |
57
|
|
|
DiContainer::initialize(false); |
58
|
|
|
// Then make sure the 'DatabaseName' is as expected |
59
|
|
|
$dbName = DiContainer::get('DatabaseName'); |
60
|
|
|
self::assertSame($dbName, 'smr_live_test'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @runInSeparateProcess |
65
|
|
|
*/ |
66
|
|
|
public function test_initialized(): void { |
67
|
|
|
// Note that we need to run in a separate process since this is the |
68
|
|
|
// only way to ensure that the DiContainer is not yet initialized |
69
|
|
|
// (and static properties cannot be unset). |
70
|
|
|
|
71
|
|
|
// Before the DiContainer is initialized, all entries should report |
72
|
|
|
// that they are not initialized as well. |
73
|
|
|
$entry = 'DatabaseName'; |
74
|
|
|
self::assertFalse(DiContainer::initialized($entry)); |
75
|
|
|
|
76
|
|
|
// After the DiContainer is initialized, all entries should still |
77
|
|
|
// report that they are not initialized. |
78
|
|
|
DiContainer::initialize(false); |
79
|
|
|
self::assertFalse(DiContainer::initialized($entry)); |
80
|
|
|
|
81
|
|
|
// Only once the entry is requested should it be initialized. |
82
|
|
|
DiContainer::get($entry); |
83
|
|
|
self::assertTrue(DiContainer::initialized($entry)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|