|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Boot; |
|
13
|
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Spiral\Boot\Environment; |
|
16
|
|
|
use Spiral\Boot\EnvironmentInterface; |
|
17
|
|
|
use Spiral\Tests\Boot\Fixtures\TestCore; |
|
18
|
|
|
|
|
19
|
|
|
class EnvironmentTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
public function testValue(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$env = $this->getEnv(['key' => 'value']); |
|
24
|
|
|
|
|
25
|
|
|
$this->assertSame('value', $env->get('key')); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testDefault(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$env = $this->getEnv(['key' => 'value']); |
|
31
|
|
|
|
|
32
|
|
|
$this->assertSame('default', $env->get('other', 'default')); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testID(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$env = $this->getEnv(['key' => 'value']); |
|
38
|
|
|
|
|
39
|
|
|
$id = $env->getID(); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertNotEmpty($id); |
|
42
|
|
|
|
|
43
|
|
|
$env->set('other', 'value'); |
|
44
|
|
|
$this->assertNotSame($id, $env->getID()); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertSame('value', $env->get('other', 'default')); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testNormalize(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$env = $this->getEnv(['key' => 'true', 'other' => false]); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertTrue($env->get('key')); |
|
54
|
|
|
$this->assertFalse($env->get('other')); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testSetVariableWithOverwriting(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$env = $this->getEnv(['key' => 'foo']); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertSame('foo', $env->get('key')); |
|
62
|
|
|
$env->set('key', 'bar'); |
|
63
|
|
|
$this->assertSame('bar', $env->get('key')); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testSetVariableWithoutOverwriting(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$env = $this->getEnv(['key' => 'foo'], false); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertSame('foo', $env->get('key')); |
|
71
|
|
|
$env->set('key', 'bar'); |
|
72
|
|
|
$this->assertSame('foo', $env->get('key')); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testSetNullValueWithOverwriting(): void |
|
76
|
|
|
{ |
|
77
|
|
|
$env = $this->getEnv(['key' => null]); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertNull($env->get('key')); |
|
80
|
|
|
$env->set('key', 'bar'); |
|
81
|
|
|
$this->assertSame('bar', $env->get('key')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testSetNullValueWithoutOverwriting(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$env = $this->getEnv(['key' => null], false); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertNull($env->get('key')); |
|
89
|
|
|
$env->set('key', 'bar'); |
|
90
|
|
|
$this->assertNull($env->get('key')); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param array $env |
|
95
|
|
|
* @return EnvironmentInterface |
|
96
|
|
|
* @throws \Throwable |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function getEnv(array $env, bool $overwite= true): EnvironmentInterface |
|
99
|
|
|
{ |
|
100
|
|
|
$core = TestCore::init(['root' => __DIR__], new Environment($env, $overwite)); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
return $core->getContainer()->get(EnvironmentInterface::class); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.