DummyTest   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 325
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 153
dl 0
loc 325
rs 10
c 1
b 0
f 1
wmc 21

20 Methods

Rating   Name   Duplication   Size   Complexity  
A testFileExistsReturnsFalseWhenThereAreNoFilesystemsRegistered() 0 9 1
A createFilesystemMock() 0 3 1
A setUp() 0 5 1
A testReadThrowsExceptionWhenNoMatchingFilesystemIsFound() 0 14 1
A testFileExistsReturnsFalseWhenNoMatchingFilesystemIsFound() 0 16 1
A testGetWebPath() 0 15 1
A testFlushDeletesAllFlushableFilesInAllRegisteredFilesystems() 0 20 1
A testFlushIgnoresPhpFiles() 0 13 1
A testFlushIgnoresDotGitignoreFiles() 0 13 1
A testWriteThrowsExceptionWhenThereAreNoFilesystemsRegistered() 0 8 1
A testReadReturnsNullIfFileCanNotBeRead() 0 12 1
A testReadReturnsContentIfFileIsReadable() 0 14 1
A testReadThrowsExceptionWhenThereAreNoFilesystemsRegistered() 0 7 1
A testReadReturnsNullIfPathDoesNotExist() 0 11 1
A testWriteReturnsTrueOnSuccess() 0 14 1
A testWriteUsesTheFirstCheckedFilesystem() 0 23 1
A testWriteReturnsFalseIfWritingFails() 0 12 1
A testFlushUsesSetIsFlushableCallback() 0 30 2
A testReadUsesTheFirstCheckedFilesystem() 0 25 1
A testWriteThrowsExceptionWhenNoMatchingFilesystemIsFound() 0 15 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Assets\CacheManager;
6
7
use League\Flysystem\DirectoryListing;
8
use League\Flysystem\Filesystem;
9
use League\Flysystem\FilesystemOperator;
10
use League\Flysystem\UnableToReadFile;
11
use League\Flysystem\UnableToWriteFile;
12
use PHPUnit\Framework\MockObject\MockObject;
13
use PHPUnit\Framework\TestCase;
14
15
class DummyTest extends TestCase
16
{
17
    /** @var Dummy - System Under Test */
18
    protected Dummy $sut;
19
20
    public function setUp(): void
21
    {
22
        $this->sut = new Dummy();
23
24
        parent::setUp();
25
    }
26
27
    /**
28
     * @return FilesystemOperator|MockObject
29
     */
30
    protected function createFilesystemMock()
31
    {
32
        return $this->createMock(Filesystem::class);
33
    }
34
35
    public function testFileExistsReturnsFalseWhenThereAreNoFilesystemsRegistered(): void
36
    {
37
        $expectedResult = false;
38
39
        $path = 'foo.ext';
40
41
        $actualResult = $this->sut->fileExists($path);
42
43
        $this->assertEquals($expectedResult, $actualResult);
44
    }
45
46
    public function testFileExistsReturnsFalseWhenNoMatchingFilesystemIsFound(): void
47
    {
48
        $expectedResult = false;
49
50
        $fs = $this->createFilesystemMock();
51
52
        $this->sut->registerFilesystem(
53
            $fs,
54
            fn () => false
55
        );
56
57
        $path = 'foo.ext';
58
59
        $actualResult = $this->sut->fileExists($path);
60
61
        $this->assertEquals($expectedResult, $actualResult);
62
    }
63
64
    public function testReadThrowsExceptionWhenThereAreNoFilesystemsRegistered(): void
65
    {
66
        $this->expectException(\InvalidArgumentException::class);
67
68
        $path = 'foo.ext';
69
70
        $this->sut->read($path);
71
    }
72
73
    public function testReadThrowsExceptionWhenNoMatchingFilesystemIsFound(): void
74
    {
75
        $this->expectException(\InvalidArgumentException::class);
76
77
        $fs = $this->createFilesystemMock();
78
79
        $this->sut->registerFilesystem(
80
            $fs,
81
            fn () => false
82
        );
83
84
        $path = 'foo.ext';
85
86
        $this->sut->read($path);
87
    }
88
89
    public function testReadReturnsNullIfPathDoesNotExist(): void
90
    {
91
        $fs = $this->createFilesystemMock();
92
        $fs->expects($this->once())->method('fileExists')->willReturn(false);
93
        $this->sut->registerFilesystem($fs);
94
95
        $path = 'foo.ext';
96
97
        $actualResult = $this->sut->read($path);
98
99
        $this->assertNull($actualResult);
100
    }
101
102
    public function testReadReturnsNullIfFileCanNotBeRead(): void
103
    {
104
        $path = 'foo.ext';
105
106
        $fs = $this->createFilesystemMock();
107
        $fs->expects($this->any())->method('fileExists')->willReturn(true);
108
        $fs->expects($this->once())->method('read')->willThrowException(new UnableToReadFile($path));
109
        $this->sut->registerFilesystem($fs);
110
111
        $actualResult = $this->sut->read($path);
112
113
        $this->assertNull($actualResult);
114
    }
115
116
    public function testReadReturnsContentIfFileIsReadable(): void
117
    {
118
        $expectedResult = 'bar';
119
120
        $fs = $this->createFilesystemMock();
121
        $fs->expects($this->any())->method('fileExists')->willReturn(true);
122
        $fs->expects($this->once())->method('read')->willReturn($expectedResult);
123
        $this->sut->registerFilesystem($fs);
124
125
        $path = 'foo.ext';
126
127
        $actualResult = $this->sut->read($path);
128
129
        $this->assertEquals($expectedResult, $actualResult);
130
    }
131
132
    public function testReadUsesTheFirstCheckedFilesystem(): void
133
    {
134
        $expectedResult = 'bar';
135
136
        $fs1 = $this->createFilesystemMock();
137
        $fs2 = $this->createFilesystemMock();
138
139
        $this->sut->registerFilesystem(
140
            $fs1,
141
            fn () => false
142
        );
143
        $this->sut->registerFilesystem(
144
            $fs2,
145
            fn () => true
146
        );
147
148
        $path = 'foo.ext';
149
150
        $fs1->expects($this->never())->method('read');
151
        $fs2->expects($this->once())->method('fileExists')->willReturn(true);
152
        $fs2->expects($this->once())->method('read')->willReturn($expectedResult);
153
154
        $actualResult = $this->sut->read($path);
155
156
        $this->assertEquals($expectedResult, $actualResult);
157
    }
158
159
    public function testWriteThrowsExceptionWhenThereAreNoFilesystemsRegistered(): void
160
    {
161
        $this->expectException(\InvalidArgumentException::class);
162
163
        $path    = 'foo.ext';
164
        $content = 'bar';
165
166
        $this->sut->write($path, $content);
167
    }
168
169
    public function testWriteThrowsExceptionWhenNoMatchingFilesystemIsFound(): void
170
    {
171
        $this->expectException(\InvalidArgumentException::class);
172
173
        $fs = $this->createFilesystemMock();
174
175
        $this->sut->registerFilesystem(
176
            $fs,
177
            fn () => false
178
        );
179
180
        $path    = 'foo.ext';
181
        $content = 'bar';
182
183
        $this->sut->write($path, $content);
184
    }
185
186
    public function testWriteReturnsFalseIfWritingFails(): void
187
    {
188
        $fs = $this->createFilesystemMock();
189
190
        $path    = 'foo.ext';
191
        $content = 'bar';
192
193
        $this->sut->registerFilesystem($fs);
194
195
        $fs->expects($this->once())->method('write')->willThrowException(new UnableToWriteFile($path));
196
197
        $this->sut->write($path, $content);
198
    }
199
200
    public function testWriteReturnsTrueOnSuccess(): void
201
    {
202
        $fs = $this->createFilesystemMock();
203
204
        $path    = 'foo.ext';
205
        $content = 'bar';
206
207
        $this->sut->registerFilesystem($fs);
208
209
        $fs->expects($this->once())->method('write');
210
211
        $actualResult = $this->sut->write($path, $content);
212
213
        $this->assertTrue($actualResult);
214
    }
215
216
    public function testWriteUsesTheFirstCheckedFilesystem(): void
217
    {
218
        $fs1 = $this->createFilesystemMock();
219
        $fs2 = $this->createFilesystemMock();
220
221
        $this->sut->registerFilesystem(
222
            $fs1,
223
            fn () => false
224
        );
225
        $this->sut->registerFilesystem(
226
            $fs2,
227
            fn () => true
228
        );
229
230
        $path    = 'foo.ext';
231
        $content = 'bar';
232
233
        $fs1->expects($this->never())->method('write')->willThrowException(new UnableToWriteFile($path));
234
        $fs2->expects($this->once())->method('write');
235
236
        $actualResult = $this->sut->write($path, $content);
237
238
        $this->assertTrue($actualResult);
239
    }
240
241
    public function testGetWebPath(): void
242
    {
243
        $path      = 'foo.ext';
244
        $timestamp = time();
245
246
        $fs = $this->createFilesystemMock();
247
248
        $this->sut->registerFilesystem($fs);
249
250
        $fs->expects($this->once())->method('lastModified')->with($path)->willReturn($timestamp);
251
252
        $actualResult = $this->sut->getWebPath($path);
253
254
        $this->assertStringContainsString($path, $actualResult);
255
        $this->assertNotSame($path, $actualResult);
256
    }
257
258
    public function testFlushDeletesAllFlushableFilesInAllRegisteredFilesystems(): void
259
    {
260
        $fs1 = $this->createFilesystemMock();
261
        $fs2 = $this->createFilesystemMock();
262
263
        $this->sut->registerFilesystem($fs1);
264
        $this->sut->registerFilesystem($fs2);
265
266
        $obj1 = ['path' => 'foo', 'basename' => ''];
267
        $obj2 = ['path' => 'bar', 'basename' => ''];
268
        $obj3 = ['path' => 'baz', 'basename' => ''];
269
        $obj4 = ['path' => 'quix', 'basename' => ''];
270
271
        $fs1->expects($this->once())->method('listContents')->willReturn(new DirectoryListing([$obj1, $obj2]));
272
        $fs2->expects($this->once())->method('listContents')->willReturn(new DirectoryListing([$obj3, $obj4]));
273
274
        $fs1->expects($this->exactly(2))->method('delete');
275
        $fs2->expects($this->exactly(2))->method('delete');
276
277
        $this->sut->flush();
278
    }
279
280
    public function testFlushIgnoresDotGitignoreFiles(): void
281
    {
282
        $fs = $this->createFilesystemMock();
283
284
        $this->sut->registerFilesystem($fs);
285
286
        $obj1 = ['path' => 'foo', 'basename' => '.gitignore'];
287
288
        $fs->expects($this->once())->method('listContents')->willReturn(new DirectoryListing([$obj1]));
289
290
        $fs->expects($this->never())->method('delete');
291
292
        $this->sut->flush();
293
    }
294
295
    public function testFlushIgnoresPhpFiles(): void
296
    {
297
        $fs = $this->createFilesystemMock();
298
299
        $this->sut->registerFilesystem($fs);
300
301
        $obj1 = ['path' => 'foo', 'basename' => 'index', 'extension' => 'php'];
302
303
        $fs->expects($this->once())->method('listContents')->willReturn(new DirectoryListing([$obj1]));
304
305
        $fs->expects($this->never())->method('delete');
306
307
        $this->sut->flush();
308
    }
309
310
    public function testFlushUsesSetIsFlushableCallback(): void
311
    {
312
        $this->sut->setIsFlushable(
313
            function ($obj) {
314
                if ($obj['path'] === 'protected') {
315
                    return false;
316
                }
317
318
                return true;
319
            }
320
        );
321
322
        $fs1 = $this->createFilesystemMock();
323
        $fs2 = $this->createFilesystemMock();
324
325
        $this->sut->registerFilesystem($fs1);
326
        $this->sut->registerFilesystem($fs2);
327
328
        $obj1 = ['path' => 'foo', 'basename' => 'index', 'extension' => 'php'];
329
        $obj2 = ['path' => 'foo', 'basename' => '.gitignore'];
330
        $obj3 = ['path' => 'protected', 'basename' => 'abc'];
331
        $obj4 = ['path' => 'protected', 'basename' => 'cba'];
332
333
        $fs1->expects($this->once())->method('listContents')->willReturn(new DirectoryListing([$obj1, $obj2]));
334
        $fs2->expects($this->once())->method('listContents')->willReturn(new DirectoryListing([$obj3, $obj4]));
335
336
        $fs1->expects($this->exactly(2))->method('delete');
337
        $fs2->expects($this->never())->method('delete');
338
339
        $this->sut->flush();
340
    }
341
}
342