Completed
Push — master ( 2b0ab6...774eed )
by Marin
02:15
created

EnveditorTest::testContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Codervio\Envmanager\Tests;
4
5
use Codervio\Envmanager\Enveditor;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * Envparser unit test
10
 *
11
 */
12
class EnveditorTest extends TestCase
13
{
14
    public $tmpPathEnv;
15
    public $instance;
16
17
    const DS = DIRECTORY_SEPARATOR;
18
19
    protected function setUp()
20
    {
21
        $this->tmpPathEnv = sys_get_temp_dir() . self::DS .'EnveditorTestEnv.env';
22
23
        if (!touch($this->tmpPathEnv)) {
24
            throw new \RuntimeException(sprintf('A test failed. Could not write a file to "%s" path', $this->tmpPathEnv));
25
        }
26
27
        $tmpData = <<<EOF
28
FOO=bar
29
EOF;
30
31
        file_put_contents($this->tmpPathEnv, $tmpData);
32
    }
33
34
    public function testInstance()
35
    {
36
        $this->instance = new Enveditor($this->tmpPathEnv);
37
        $instanceClass = $this->instance instanceof EnvEditor;
38
39
        $this->assertEquals($instanceClass, true);
40
    }
41
42
    public function testWrite()
43
    {
44
        $this->instance = new Enveditor($this->tmpPathEnv);
45
46
        $this->instance->persist('FOO2', 'valuetest', 'mycomment', true);
47
48
        $this->instance->save();
49
50
        $this->assertTrue($this->instance->save());
51
52
    }
53
54
    public function testContent()
55
    {
56
        $this->instance = new Enveditor($this->tmpPathEnv);
57
58
        $this->instance->persist('FOO2', 'valuetest', 'mycomment', true);
59
60
        $this->instance->save();
61
62
        $content = <<<EOF
63
export FOO2="valuetest" # mycomment
64
65
EOF;
66
67
        $this->assertSame($content, $this->instance->getContent());
68
    }
69
70
    public function testComment()
71
    {
72
        $this->instance = new Enveditor($this->tmpPathEnv);
73
74
        $this->instance->persist('FOO2', 'valuetest', 'mycomment', true);
75
76
        $this->instance->addComment('my comment');
77
78
        $this->instance->save();
79
80
        $content = <<<EOF
81
export FOO2="valuetest" # mycomment
82
# my comment
83
84
EOF;
85
86
        $this->assertSame($content, $this->instance->getContent());
87
    }
88
89
    public function testEmptyline()
90
    {
91
        $this->instance = new Enveditor($this->tmpPathEnv);
92
93
        $this->instance->persist('FOO2', 'valuetest', 'mycomment', true);
94
95
        $this->instance->addEmptyLine();
96
97
        $this->instance->addComment('my comment');
98
99
        $this->instance->save();
100
101
        $content = <<<EOF
102
export FOO2="valuetest" # mycomment
103
104
# my comment
105
106
EOF;
107
108
        $this->assertSame($content, $this->instance->getContent());
109
    }
110
111
    public function testRemoveComment()
112
    {
113
        $this->instance = new Enveditor($this->tmpPathEnv);
114
115
        $this->instance->addComment('my comment');
116
        $this->instance->addComment('new test');
117
        $this->instance->removeComment('my comment');
118
119
        $this->instance->save();
120
121
        $content = <<<EOF
122
# new test
123
124
EOF;
125
126
        $this->assertSame($content, $this->instance->getContent());
127
    }
128
129
    public function testRemoveVariable()
130
    {
131
        $this->instance = new Enveditor($this->tmpPathEnv);
132
133
        $this->instance->persist('FOO3', 'fsdf', 'sfdsdfsdf', false);
134
        $this->instance->persist('FOO4', 'sdfsfsf', 'fsfsfs', false);
135
        $this->instance->persist('FOO5', 'fsdfrwfssdds', null, true);
136
137
        $this->instance->remove('FOO4');
138
139
        $this->instance->save();
140
141
        $content = <<<EOF
142
FOO3="fsdf" # sfdsdfsdf
143
export FOO5="fsdfrwfssdds"
144
145
EOF;
146
147
        $this->assertSame($content, $this->instance->getContent());
148
    }
149
150
    public function testRemoveFile()
151
    {
152
        $this->instance = new Enveditor($this->tmpPathEnv);
153
154
        $this->instance->persist('FOO3', 'fsdf', 'sfdsdfsdf', false);
155
156
        $this->instance->save();
157
158
        $this->instance->removeFile();
159
160
        $this->assertFalse(is_file($this->tmpPathEnv));
161
    }
162
163
    public function testClearContent()
164
    {
165
        $this->instance = new Enveditor($this->tmpPathEnv);
166
167
        $this->instance->addComment('1');
168
        $this->instance->addComment('2');
169
        $this->instance->clearContent();
170
        $this->instance->addComment('3');
171
172
        $this->instance->save();
173
174
        $content = <<<EOF
175
# 3
176
177
EOF;
178
179
        $this->assertSame($content, $this->instance->getContent());
180
    }
181
182
    public function testForceClear()
183
    {
184
        $this->instance = new Enveditor($this->tmpPathEnv);
185
        $this->instance->forceClear();
186
        $this->instance->addComment('1');
187
        $this->instance->save();
188
189
        $content = '';
190
191
        $this->assertSame($content, $this->instance->getContent());
192
    }
193
}
194