Passed
Pull Request — master (#599)
by Aleksei
08:43 queued 02:55
created

EnvironmentTest::testSetVariableWithOverwriting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 1
b 0
f 0
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));
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Boot\AbstractKernel::init() has been deprecated: since 3.0. Use Kernel::create(...)->run() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

100
        $core = /** @scrutinizer ignore-deprecated */ TestCore::init(['root' => __DIR__], new Environment($env, $overwite));

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.

Loading history...
101
102
        return $core->getContainer()->get(EnvironmentInterface::class);
103
    }
104
}
105