1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RemotelyLiving\PHPEnv\Tests\Integration; |
6
|
|
|
|
7
|
|
|
use RemotelyLiving\PHPEnv\Environment; |
8
|
|
|
use RemotelyLiving\PHPEnv\EnvironmentType; |
9
|
|
|
use RemotelyLiving\PHPEnv\Exceptions; |
10
|
|
|
use RemotelyLiving\PHPEnv\Interfaces; |
11
|
|
|
use RemotelyLiving\PHPEnv\Tests\Stubs; |
12
|
|
|
|
13
|
|
|
class EnvironmentFileTest extends AbstractTestCase |
14
|
|
|
{ |
15
|
|
|
private Interfaces\Environment $env; |
16
|
|
|
|
17
|
|
|
protected function setUp(): void |
18
|
|
|
{ |
19
|
|
|
clearstatcache(); |
20
|
|
|
$this->env = Environment::createWithEnvFile(EnvironmentType::DEVELOPMENT(), $this->getEnvFileStubPath()); |
21
|
|
|
|
22
|
|
|
parent::setUp(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function tearDown(): void |
26
|
|
|
{ |
27
|
|
|
$this->unregisterEnvVars(array_keys(Stubs\EnvVars::getFilled())); |
28
|
|
|
parent::tearDown(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testMarshallsVarsFromEnvFile(): void |
32
|
|
|
{ |
33
|
|
|
$expecteStdClass = new \stdClass(); |
34
|
|
|
$expecteStdClass->foo = 'bar'; |
35
|
|
|
|
36
|
|
|
$this->assertTrue($this->env->get(Stubs\EnvVars::KEY_BOOLISH_TRUE)->asBoolean()); |
37
|
|
|
$this->assertFalse($this->env->get(Stubs\EnvVars::KEY_BOOLISH_FALSE)->asBoolean()); |
38
|
|
|
$this->assertSame(100, $this->env->get(Stubs\EnvVars::KEY_INT)->asInteger()); |
39
|
|
|
$this->assertSame(100.01, $this->env->get(Stubs\EnvVars::KEY_FLOAT)->asFloat()); |
40
|
|
|
$this->assertEquals($expecteStdClass, $this->env->get(Stubs\EnvVars::KEY_SERIALIZED)->asUnserializedObject()); |
41
|
|
|
$this->assertEquals($expecteStdClass, $this->env->get(Stubs\EnvVars::KEY_JSON)->asJSONDecodedObject()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testThrowsOnBadEnvFileLocation(): void |
45
|
|
|
{ |
46
|
|
|
$this->expectException(Exceptions\RuntimeError::class); |
47
|
|
|
$this->expectExceptionMessage('foo.file does not exist as a readable env file'); |
48
|
|
|
|
49
|
|
|
Environment::createWithEnvFile(EnvironmentType::DEVELOPMENT(), 'foo.file'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testHasOrDoesNotHaveEnvVar(): void |
53
|
|
|
{ |
54
|
|
|
$this->assertFalse($this->env->has('foo')); |
55
|
|
|
$this->assertTrue($this->env->has(Stubs\EnvVars::KEY_BOOLISH_FALSE)); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|