Passed
Push — 6.0 ( 0e07aa...575f2b )
by yun
02:55
created

EnvTest::testSetEnv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 29
rs 9.7666
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace think\tests;
4
5
use org\bovigo\vfs\vfsStream;
6
use PHPUnit\Framework\TestCase;
7
use think\Env;
8
use think\Exception;
9
10
class EnvTest extends TestCase
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class EnvTest
Loading history...
11
{
12
    public function testEnvFile()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testEnvFile()
Loading history...
13
    {
14
        $root    = vfsStream::setup();
15
        $envFile = vfsStream::newFile('.env')->setContent("key1=value1\nkey2=value2");
16
        $root->addChild($envFile);
17
18
        $env = new Env();
19
20
        $env->load($envFile->url());
21
22
        $this->assertEquals('value1', $env->get('key1'));
23
        $this->assertEquals('value2', $env->get('key2'));
24
25
        $this->assertSame(['KEY1' => 'value1', 'KEY2' => 'value2'], $env->get());
26
    }
27
28
    public function testServerEnv()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testServerEnv()
Loading history...
29
    {
30
        $env = new Env();
31
32
        $this->assertEquals('value2', $env->get('key2', 'value2'));
33
34
        putenv('PHP_KEY7=value7');
35
        putenv('PHP_KEY8=false');
36
        putenv('PHP_KEY9=true');
37
38
        $this->assertEquals('value7', $env->get('key7'));
39
        $this->assertFalse($env->get('KEY8'));
40
        $this->assertTrue($env->get('key9'));
41
    }
42
43
    public function testSetEnv()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testSetEnv()
Loading history...
44
    {
45
        $env = new Env();
46
47
        $env->set([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
48
            'key1' => 'value1',
49
            'key2' => [
50
                'key1' => 'value1-2',
51
            ],
52
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
53
54
        $env->set('key3', 'value3');
55
56
        $env->key4 = 'value4';
0 ignored issues
show
Bug Best Practice introduced by
The property key4 does not exist on think\Env. Since you implemented __set, consider adding a @property annotation.
Loading history...
57
58
        $env['key5'] = 'value5';
59
60
        $this->assertEquals('value1', $env->get('key1'));
61
        $this->assertEquals('value1-2', $env->get('key2.key1'));
62
63
        $this->assertEquals('value3', $env->get('key3'));
64
65
        $this->assertEquals('value4', $env->key4);
0 ignored issues
show
Bug Best Practice introduced by
The property key4 does not exist on think\Env. Since you implemented __get, consider adding a @property annotation.
Loading history...
66
67
        $this->assertEquals('value5', $env['key5']);
68
69
        $this->expectException(Exception::class);
70
71
        unset($env['key5']);
72
    }
73
}
74