1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Environments; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Constant\Env; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class EnvironmentTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
private const KEY = "FOOBAR001"; |
13
|
|
|
|
14
|
|
|
public function setUp(): void |
15
|
|
|
{ |
16
|
|
|
Environment::unsetVar(static::KEY); |
17
|
|
|
Environment::unsetVar(Env::ENV_NAME); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function tearDown(): void |
21
|
|
|
{ |
22
|
|
|
Environment::unsetVar(static::KEY); |
23
|
|
|
Environment::unsetVar(Env::ENV_NAME); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testUnset(): void |
27
|
|
|
{ |
28
|
|
|
$value = static::KEY; |
29
|
|
|
|
30
|
|
|
Environment::setVar(static::KEY, $value); |
31
|
|
|
$this->assertEquals($value, Environment::getVar(static::KEY)); |
32
|
|
|
|
33
|
|
|
Environment::unsetVar(static::KEY); |
34
|
|
|
|
35
|
|
|
$this->assertNull(Environment::getVar(static::KEY)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testMustGetVarGetsDefaultIfEnvironmentVariableIsMissing(): void |
39
|
|
|
{ |
40
|
|
|
$value = static::KEY; |
41
|
|
|
|
42
|
|
|
$this->assertEquals($value, Environment::mustGetVar(static::KEY, $value)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testMustGetVarGetsEnvironmentVariableValueByDefault(): void |
46
|
|
|
{ |
47
|
|
|
$value = static::KEY; |
48
|
|
|
$wrong = "BARBAZ002"; |
49
|
|
|
|
50
|
|
|
Environment::setVar(static::KEY, $value); |
51
|
|
|
|
52
|
|
|
$this->assertEquals($value, Environment::mustGetVar(static::KEY, $wrong)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testMustGetVarThrowsExceptionIfEnvironmentVariableAndDefaultAreMissing(): void |
56
|
|
|
{ |
57
|
|
|
$this->expectException(\RuntimeException::class); |
58
|
|
|
|
59
|
|
|
Environment::mustGetVar(static::KEY); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testOnlyIsStagingIsTrueIfEnvNameIsStaging(): void |
63
|
|
|
{ |
64
|
|
|
Environment::setVar(Env::ENV_NAME, Environment::STAGING); |
65
|
|
|
$this->assertFalse(Environment::isTesting()); |
66
|
|
|
$this->assertFalse(Environment::isDevelopment()); |
67
|
|
|
$this->assertFalse(Environment::isProduction()); |
68
|
|
|
$this->assertTrue(Environment::isStaging()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testOnlyIsTestingIsTrueIfEnvNameIsTesting(): void |
72
|
|
|
{ |
73
|
|
|
Environment::setVar(Env::ENV_NAME, Environment::TESTING); |
74
|
|
|
$this->assertFalse(Environment::isStaging()); |
75
|
|
|
$this->assertFalse(Environment::isDevelopment()); |
76
|
|
|
$this->assertFalse(Environment::isProduction()); |
77
|
|
|
$this->assertTrue(Environment::isTesting()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testOnlyIsDevelopmentIsTrueIfEnvNameIsDevelopment(): void |
81
|
|
|
{ |
82
|
|
|
Environment::setVar(Env::ENV_NAME, Environment::DEVELOPMENT); |
83
|
|
|
$this->assertFalse(Environment::isStaging()); |
84
|
|
|
$this->assertFalse(Environment::isTesting()); |
85
|
|
|
$this->assertFalse(Environment::isProduction()); |
86
|
|
|
$this->assertTrue(Environment::isDevelopment()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testOnlyIsProductionIsTrueIfEnvNameIsProduction(): void |
90
|
|
|
{ |
91
|
|
|
Environment::setVar(Env::ENV_NAME, Environment::PRODUCTION); |
92
|
|
|
$this->assertFalse(Environment::isStaging()); |
93
|
|
|
$this->assertFalse(Environment::isTesting()); |
94
|
|
|
$this->assertFalse(Environment::isDevelopment()); |
95
|
|
|
$this->assertTrue(Environment::isProduction()); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|