Completed
Push — master ( 17598f...abee1f )
by Kirill
13s queued 11s
created

PublishTest::testInvalidFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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

121
        /** @scrutinizer ignore-call */ 
122
        $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...
122
        $this->assertFalse(is_dir($dir));
123
124
        $this->runCommandDebug('publish', [
125
            'type'   => 'ensure',
126
            'target' => '@runtime/dir',
127
        ]);
128
129
        $this->assertTrue(is_dir($dir));
130
131
        rmdir($dir);
132
    }
133
134
    public function testPublishDirectoryReplace(): void
135
    {
136
        $this->runCommandDebug('conf');
137
        $file = $this->file('runtime', 'test.txt');
138
        file_put_contents($file, 'original');
139
        file_put_contents(self::TEST_FILE, 'test');
140
141
        $this->runCommandDebug('publish', [
142
            'type'   => 'replace',
143
            'target' => '@runtime',
144
            'source' => __DIR__,
145
            'mode'   => 'runtime'
146
        ]);
147
148
        $this->assertSame('test', file_get_contents($file));
149
        $this->assertSame(file_get_contents(__FILE__), file_get_contents(self::TEST_FILE_2));
150
    }
151
152
    public function testPublishDirectoryFollow(): void
153
    {
154
        $this->runCommandDebug('conf');
155
        $file = $this->file('runtime', 'test.txt');
156
        file_put_contents($file, 'original');
157
        file_put_contents(self::TEST_FILE, 'test');
158
159
        $this->runCommandDebug('publish', [
160
            'type'   => 'follow',
161
            'target' => '@runtime',
162
            'source' => __DIR__,
163
            'mode'   => 'runtime'
164
        ]);
165
166
        $this->assertSame('original', file_get_contents($file));
167
        $this->assertSame(file_get_contents(__FILE__), file_get_contents(self::TEST_FILE_2));
168
    }
169
170
    public function testPublishDirectoryReplaceStar(): void
171
    {
172
        $this->runCommandDebug('conf');
173
        $file = $this->file('runtime', 'test.txt');
174
        file_put_contents($file, 'original');
175
        file_put_contents(self::TEST_FILE, 'test');
176
177
        $this->runCommandDebug('publish', [
178
            'type'   => 'replace',
179
            'target' => '@runtime',
180
            'source' => __DIR__ . '/*',
181
            'mode'   => 'runtime'
182
        ]);
183
184
        $this->assertSame('test', file_get_contents($file));
185
        $this->assertSame(file_get_contents(__FILE__), file_get_contents(self::TEST_FILE_2));
186
    }
187
188
    public function testPublishDirectoryFollowStar(): void
189
    {
190
        $this->runCommandDebug('conf');
191
        $file = $this->file('runtime', 'test.txt');
192
        file_put_contents($file, 'original');
193
        file_put_contents(self::TEST_FILE, 'test');
194
195
        $this->runCommandDebug('publish', [
196
            'type'   => 'follow',
197
            'target' => '@runtime',
198
            'source' => __DIR__ . '/*',
199
            'mode'   => 'runtime'
200
        ]);
201
202
        $this->assertSame('original', file_get_contents($file));
203
        $this->assertSame(file_get_contents(__FILE__), file_get_contents(self::TEST_FILE_2));
204
    }
205
206
    public function testInvalidFile(): void
207
    {
208
        $this->expectException(PublishException::class);
209
210
        $this->runCommandDebug('conf');
211
212
        $this->runCommandDebug('publish', [
213
            'type'   => 'follow',
214
            'target' => '@runtime/test.txt',
215
            'source' => self::TEST_FILE . 'invalid',
216
            'mode'   => 'runtime'
217
        ]);
218
    }
219
220
    public function testInvalidDir(): void
221
    {
222
        $this->expectException(PublishException::class);
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
}
239