Passed
Branch 2.0 (27b8db)
by Anton
05:03
created

PublishTest::testInvalidFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
9
10
namespace Spiral\Framework\Module;
11
12
use Spiral\Boot\DirectoriesInterface;
13
use Spiral\Framework\ConsoleTest;
14
15
class PublishTest extends ConsoleTest
16
{
17
    protected const TEST_FILE   = __DIR__ . '/test.txt';
18
    protected const TEST_FILE_2 = __DIR__ . '/PublishTest.php';
19
20
    public function tearDown()
21
    {
22
        if (file_exists(self::TEST_FILE)) {
23
            unlink(self::TEST_FILE);
24
        }
25
26
        $this->runCommand('cache:clean');
27
    }
28
29
    public function testPublish()
30
    {
31
        $file = $this->file('runtime', 'test.txt');
32
        file_put_contents(self::TEST_FILE, 'test');
33
34
        $this->assertFileNotExists($file);
35
36
        $this->runCommandDebug('publish', [
37
            'type'   => 'replace',
38
            'target' => '@runtime/test.txt',
39
            'source' => self::TEST_FILE,
40
            'mode'   => 'runtime'
41
        ]);
42
43
        $this->assertFileExists($file);
44
        $this->assertSame('test', file_get_contents($file));
45
    }
46
47
    public function testReplace()
48
    {
49
        $this->runCommandDebug('conf');
50
51
        $file = $this->file('runtime', 'test.txt');
52
        file_put_contents($file, 'original');
53
        file_put_contents(self::TEST_FILE, 'test');
54
55
        $this->runCommandDebug('publish', [
56
            'type'   => 'replace',
57
            'target' => '@runtime/test.txt',
58
            'source' => self::TEST_FILE,
59
            'mode'   => 'runtime'
60
        ]);
61
62
        $this->assertSame('test', file_get_contents($file));
63
    }
64
65
    public function testFollow()
66
    {
67
        $this->runCommandDebug('conf');
68
        $file = $this->file('runtime', 'test.txt');
69
        file_put_contents($file, 'original');
70
        file_put_contents(self::TEST_FILE, 'test');
71
72
        $this->runCommandDebug('publish', [
73
            'type'   => 'follow',
74
            'target' => '@runtime/test.txt',
75
            'source' => self::TEST_FILE,
76
            'mode'   => 'runtime'
77
        ]);
78
79
        $this->assertSame('original', file_get_contents($file));
80
    }
81
82
    /**
83
     * @expectedException \Spiral\Module\Exception\PublishException
84
     */
85
    public function testInvalid()
86
    {
87
        $this->runCommandDebug('conf');
88
        $file = $this->file('runtime', 'test.txt');
89
        file_put_contents($file, 'original');
90
        file_put_contents(self::TEST_FILE, 'test');
91
92
        $this->runCommandDebug('publish', [
93
            'type'   => 'invalid',
94
            'target' => '@runtime/test.txt',
95
            'source' => self::TEST_FILE,
96
            'mode'   => 'runtime'
97
        ]);
98
    }
99
100
    public function testReadonly()
101
    {
102
        $this->runCommandDebug('conf');
103
        $file = $this->file('runtime', 'test.txt');
104
        file_put_contents($file, 'original');
105
        file_put_contents(self::TEST_FILE, 'test');
106
107
        $this->runCommandDebug('publish', [
108
            'type'   => 'replace',
109
            'target' => '@runtime/test.txt',
110
            'source' => self::TEST_FILE,
111
            'mode'   => 'readonly'
112
        ]);
113
114
        $this->assertSame('test', file_get_contents($file));
115
    }
116
117
    public function testEnsure()
118
    {
119
        $dir = $this->file('runtime', 'dir', false);
0 ignored issues
show
Unused Code introduced by
The call to Spiral\Framework\Module\PublishTest::file() has too many arguments starting with false. ( Ignorable by Annotation )

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

119
        /** @scrutinizer ignore-call */ 
120
        $dir = $this->file('runtime', 'dir', false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
120
        $this->assertFalse(is_dir($dir));
121
122
        $this->runCommandDebug('publish', [
123
            'type'   => 'ensure',
124
            'target' => '@runtime/dir',
125
        ]);
126
127
        $this->assertTrue(is_dir($dir));
128
129
        rmdir($dir);
130
    }
131
132
    public function testPublishDirectoryReplace()
133
    {
134
        $this->runCommandDebug('conf');
135
        $file = $this->file('runtime', 'test.txt');
136
        file_put_contents($file, 'original');
137
        file_put_contents(self::TEST_FILE, 'test');
138
139
        $this->runCommandDebug('publish', [
140
            'type'   => 'replace',
141
            'target' => '@runtime',
142
            'source' => __DIR__,
143
            'mode'   => 'runtime'
144
        ]);
145
146
        $this->assertSame('test', file_get_contents($file));
147
        $this->assertSame(file_get_contents(__FILE__), file_get_contents(self::TEST_FILE_2));
148
    }
149
150
    public function testPublishDirectoryFollow()
151
    {
152
        $this->runCommandDebug('conf');
153
        $file = $this->file('runtime', 'test.txt');
154
        file_put_contents($file, 'original');
155
        file_put_contents(self::TEST_FILE, 'test');
156
157
        $this->runCommandDebug('publish', [
158
            'type'   => 'follow',
159
            'target' => '@runtime',
160
            'source' => __DIR__,
161
            'mode'   => 'runtime'
162
        ]);
163
164
        $this->assertSame('original', file_get_contents($file));
165
        $this->assertSame(file_get_contents(__FILE__), file_get_contents(self::TEST_FILE_2));
166
    }
167
168
    public function testPublishDirectoryReplaceStar()
169
    {
170
        $this->runCommandDebug('conf');
171
        $file = $this->file('runtime', 'test.txt');
172
        file_put_contents($file, 'original');
173
        file_put_contents(self::TEST_FILE, 'test');
174
175
        $this->runCommandDebug('publish', [
176
            'type'   => 'replace',
177
            'target' => '@runtime',
178
            'source' => __DIR__ . '/*',
179
            'mode'   => 'runtime'
180
        ]);
181
182
        $this->assertSame('test', file_get_contents($file));
183
        $this->assertSame(file_get_contents(__FILE__), file_get_contents(self::TEST_FILE_2));
184
    }
185
186
    public function testPublishDirectoryFollowStar()
187
    {
188
        $this->runCommandDebug('conf');
189
        $file = $this->file('runtime', 'test.txt');
190
        file_put_contents($file, 'original');
191
        file_put_contents(self::TEST_FILE, 'test');
192
193
        $this->runCommandDebug('publish', [
194
            'type'   => 'follow',
195
            'target' => '@runtime',
196
            'source' => __DIR__ . '/*',
197
            'mode'   => 'runtime'
198
        ]);
199
200
        $this->assertSame('original', file_get_contents($file));
201
        $this->assertSame(file_get_contents(__FILE__), file_get_contents(self::TEST_FILE_2));
202
    }
203
204
    /**
205
     * @expectedException \Spiral\Module\Exception\PublishException
206
     */
207
    public function testInvalidFile()
208
    {
209
        $this->runCommandDebug('conf');
210
211
        $this->runCommandDebug('publish', [
212
            'type'   => 'follow',
213
            'target' => '@runtime/test.txt',
214
            'source' => self::TEST_FILE . 'invalid',
215
            'mode'   => 'runtime'
216
        ]);
217
    }
218
219
    /**
220
     * @expectedException \Spiral\Module\Exception\PublishException
221
     */
222
    public function testInvalidDir()
223
    {
224
        $this->runCommandDebug('conf');
225
226
        $this->runCommandDebug('publish', [
227
            'type'   => 'follow',
228
            'target' => '@runtime/test.txt',
229
            'source' => self::TEST_FILE . 'invalid/*',
230
            'mode'   => 'runtime'
231
        ]);
232
    }
233
234
    protected function file(string $dir, string $name)
235
    {
236
        return $this->app->get(DirectoriesInterface::class)->get($dir) . $name;
237
    }
238
}