1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Small; |
6
|
|
|
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\ConfigInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\ConfigException; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ConfigTest |
14
|
|
|
* |
15
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Small |
16
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\Config |
17
|
|
|
*/ |
18
|
|
|
class ConfigTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
public const SERVER = [ |
21
|
|
|
ConfigInterface::PARAM_DB_USER => 'Value-' . ConfigInterface::PARAM_DB_USER, |
22
|
|
|
ConfigInterface::PARAM_DB_PASS => 'Value-' . ConfigInterface::PARAM_DB_PASS, |
23
|
|
|
ConfigInterface::PARAM_DB_HOST => 'Value-' . ConfigInterface::PARAM_DB_HOST, |
24
|
|
|
ConfigInterface::PARAM_DB_NAME => 'Value-' . ConfigInterface::PARAM_DB_NAME, |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @test |
29
|
|
|
* @small |
30
|
|
|
* */ |
31
|
|
|
public function itThrowsAnExceptionRequiredParamNotSet(): void |
32
|
|
|
{ |
33
|
|
|
$this->expectException(ConfigException::class); |
34
|
|
|
new Config([]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @test |
39
|
|
|
* @small |
40
|
|
|
* */ |
41
|
|
|
public function itThrowsAnExceptionIfParamIsIncorrectType(): void |
42
|
|
|
{ |
43
|
|
|
$this->expectException(ConfigException::class); |
44
|
|
|
$server[ConfigInterface::PARAM_DB_USER] = true; |
|
|
|
|
45
|
|
|
new Config([$server]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @test |
50
|
|
|
* @small |
51
|
|
|
* */ |
52
|
|
|
public function itCanHandleIntoToBoolConversion(): void |
53
|
|
|
{ |
54
|
|
|
$server = self::SERVER; |
55
|
|
|
$server[ConfigInterface::PARAM_DEVMODE] = 0; |
56
|
|
|
$server[ConfigInterface::PARAM_DB_DEBUG] = 1; |
57
|
|
|
$config = new Config($server); |
58
|
|
|
self::assertFalse($config->get(ConfigInterface::PARAM_DEVMODE)); |
59
|
|
|
self::assertTrue($config->get(ConfigInterface::PARAM_DB_DEBUG)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @test |
64
|
|
|
* @small |
65
|
|
|
* */ |
66
|
|
|
public function getParam(): void |
67
|
|
|
{ |
68
|
|
|
$config = new Config(self::SERVER); |
69
|
|
|
$expected = self::SERVER[ConfigInterface::PARAM_DB_NAME]; |
70
|
|
|
$actual = $config->get(ConfigInterface::PARAM_DB_NAME); |
71
|
|
|
self::assertSame($expected, $actual); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
* @small |
77
|
|
|
* */ |
78
|
|
|
public function getDefaultParam(): void |
79
|
|
|
{ |
80
|
|
|
$config = new Config(self::SERVER); |
81
|
|
|
$expected = ConfigInterface::OPTIONAL_PARAMS_WITH_DEFAULTS[ConfigInterface::PARAM_DB_DEBUG]; |
82
|
|
|
$actual = $config->get(ConfigInterface::PARAM_DB_DEBUG); |
83
|
|
|
self::assertSame($expected, $actual); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @test |
88
|
|
|
* @small |
89
|
|
|
* */ |
90
|
|
|
public function getProjectRootDirectory(): void |
91
|
|
|
{ |
92
|
|
|
$config = new Config(self::SERVER); |
93
|
|
|
$expected = realpath(__DIR__ . '/../../'); |
94
|
|
|
$actual = $config::getProjectRootDirectory(); |
95
|
|
|
self::assertSame($expected, $actual); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @test |
100
|
|
|
* @small |
101
|
|
|
* * */ |
102
|
|
|
public function getCalculatedDefaultParam(): void |
103
|
|
|
{ |
104
|
|
|
$config = new Config(self::SERVER); |
105
|
|
|
$expected = realpath(__DIR__ . '/../../') . '/src/Entities'; |
106
|
|
|
$actual = $config->get(ConfigInterface::PARAM_ENTITIES_PATH); |
107
|
|
|
self::assertSame($expected, $actual); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @test |
112
|
|
|
* @small |
113
|
|
|
* */ |
114
|
|
|
public function getConfiguredNotDefaultParam(): void |
115
|
|
|
{ |
116
|
|
|
$server = self::SERVER; |
117
|
|
|
$server[ConfigInterface::PARAM_ENTITIES_PATH] = realpath(__DIR__ . '/../../') . '/var/src/Entities'; |
118
|
|
|
$config = new Config($server); |
119
|
|
|
$expected = $server[ConfigInterface::PARAM_ENTITIES_PATH]; |
120
|
|
|
$actual = $config->get(ConfigInterface::PARAM_ENTITIES_PATH); |
121
|
|
|
self::assertSame($expected, $actual); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @test |
126
|
|
|
* @small |
127
|
|
|
* @coversNothing |
128
|
|
|
*/ |
129
|
|
|
public function paramsContainsAll(): void |
130
|
|
|
{ |
131
|
|
|
$countParams = count(ConfigInterface::PARAMS); |
132
|
|
|
$aggregated = array_merge( |
133
|
|
|
ConfigInterface::REQUIRED_PARAMS, |
134
|
|
|
ConfigInterface::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS, |
135
|
|
|
ConfigInterface::OPTIONAL_PARAMS_WITH_DEFAULTS |
136
|
|
|
); |
137
|
|
|
$countAggregated = count($aggregated); |
138
|
|
|
self::assertSame($countAggregated, $countParams); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|