|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
|
4
|
|
|
|
|
5
|
|
|
use Dotenv\Dotenv; |
|
6
|
|
|
use Dotenv\Exception\ValidationException; |
|
7
|
|
|
use MySqlProperties; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class MySqlPropertiesTest |
|
12
|
|
|
* @package SmrTest\lib\DefaultGame |
|
13
|
|
|
* @covers MySqlProperties |
|
14
|
|
|
*/ |
|
15
|
|
|
class MySqlPropertiesTest extends TestCase { |
|
16
|
|
|
private const RESOURCES = ROOT . "test/resources/mysql-config/validation"; |
|
17
|
|
|
|
|
18
|
|
|
public function test_determine_config_environment_test_path() { |
|
19
|
|
|
//# Given the MYSQL_CONFIG_ENVIRONMENT env variable has already been set by PHPUnit |
|
20
|
|
|
// When determining the config to load |
|
21
|
|
|
$environment = MySqlProperties::determineConfigEnvironmentFile(); |
|
22
|
|
|
self::assertEquals("test.env", $environment); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function test_determine_config_environment_unset_path() { |
|
26
|
|
|
//# Given the MYSQL_CONFIG_ENVIRONMENT is not set in the environment |
|
27
|
|
|
unset($_ENV["MYSQL_CONFIG_ENVIRONMENT"]); |
|
28
|
|
|
// When determining the config to load |
|
29
|
|
|
$environment = MySqlProperties::determineConfigEnvironmentFile(); |
|
30
|
|
|
self::assertEquals(".env", $environment); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function test_validate_config_happy_path() { |
|
34
|
|
|
// Given required environment file is present |
|
35
|
|
|
$config = Dotenv::createArrayBacked(self::RESOURCES, "good.env"); |
|
36
|
|
|
$config->load(); |
|
37
|
|
|
// When performing validation of the config |
|
38
|
|
|
MySqlProperties::validateConfig($config); |
|
39
|
|
|
// Then no errors are present. |
|
40
|
|
|
$this->assertTrue(true); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function test_validate_config_bad_port_throws_exception() { |
|
44
|
|
|
try { |
|
45
|
|
|
// Given required environment file is present that contains an invalid port number definition |
|
46
|
|
|
$config = Dotenv::createArrayBacked(self::RESOURCES, "bad-port.env"); |
|
47
|
|
|
$config->load(); |
|
48
|
|
|
// When performing validation on the config |
|
49
|
|
|
MySqlProperties::validateConfig($config); |
|
50
|
|
|
} finally { |
|
51
|
|
|
// Then a validation exception is expected |
|
52
|
|
|
$this->expectException(ValidationException::class); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function test_validate_config_missing_config_values_throws_exception() { |
|
57
|
|
|
try { |
|
58
|
|
|
// Given required environment file is present that is missing required fields |
|
59
|
|
|
$config = Dotenv::createArrayBacked(self::RESOURCES, "bad.env"); |
|
60
|
|
|
$config->load(); |
|
61
|
|
|
// When performing validation on the config |
|
62
|
|
|
MySqlProperties::validateConfig($config); |
|
63
|
|
|
} finally { |
|
64
|
|
|
// Then a validation exception is expected |
|
65
|
|
|
$this->expectException(ValidationException::class); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|