Passed
Push — master ( 309dcb...6acad1 )
by Petr
02:25
created

VolumeDeepTest::testLookup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 39
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 53
rs 9.296

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace StorageTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_storage\Storage\Target;
8
use kalanis\kw_storage\StorageException;
9
10
11
class VolumeDeepTest extends CommonTestClass
12
{
13
    public function tearDown(): void
14
    {
15
        $this->rmFile($this->mockTestFile('', false));
16
        $this->rmFile($this->mockTestFile('2', false));
17
        $this->rmFile($this->mockTestFile('3', false));
18
        $this->rmFile($this->mockTestFile('4', false));
19
        $this->rmFile($this->mockTestFile('5', false));
20
        $this->rmDir($this->mockTestFile('', false));
21
        $this->rmDir('some');
22
        parent::tearDown();
23
    }
24
25
    /**
26
     * @throws StorageException
27
     */
28
    public function testLookup(): void
29
    {
30
        $volume = new Target\VolumeTargetFlat();
31
        $this->assertTrue($volume->check($this->getTestDir()));
32
        $testFiles = [
33
            'dummyFile.tst' => $this->getTestDir() . 'dummyFile.tst',
34
            'dummyFile.0.tst' => $this->getTestDir() . 'dummyFile.0.tst',
35
            'dummyFile.1.tst' => $this->getTestDir() . 'dummyFile.1.tst',
36
            'dummyFile.2.tst' => $this->getTestDir() . 'dummyFile.2.tst',
37
        ];
38
        $removal = $volume->removeMulti($testFiles);
39
        $this->assertEquals([
40
            'dummyFile.tst' => false,
41
            'dummyFile.0.tst' => false,
42
            'dummyFile.1.tst' => false,
43
            'dummyFile.2.tst' => false,
44
        ], $removal);
45
46
        file_put_contents($this->getTestDir() . 'dummyFile.tst', 'asdfghjklqwertzuiopyxcvbnm');
47
        file_put_contents($this->getTestDir() . 'dummyFile.0.tst', 'asdfghjklqwertzuiopyxcvbnm');
48
        file_put_contents($this->getTestDir() . 'dummyFile.1.tst', 'asdfghjklqwertzuiopyxcvbnm');
49
        file_put_contents($this->getTestDir() . 'dummyFile.2.tst', 'asdfghjklqwertzuiopyxcvbnm');
50
51
        // non-existent path
52
        $this->assertEquals(0, count(array_filter(array_filter(iterator_to_array($volume->lookup('this path does not exists'))), [$this, 'dotDirs'])));
53
54
        // empty path - must show everything
55
        // 5 -> + gitkeep; but here goes into exec path, so the results are environment-dependent
56
//        $this->assertEquals(5, count(array_filter(array_filter(iterator_to_array($volume->lookup(''))), [$this, 'dotDirs'])));
57
58
        $files = iterator_to_array($volume->lookup($this->getTestDir()));
59
        sort($files);
60
        $files = array_filter(array_filter($files), [$this, 'dotDirs']);
61
62
        $this->assertEquals(count($testFiles) + 1, count($files)); // because gitkeep
63
        $this->assertEquals(DIRECTORY_SEPARATOR . '.gitkeep', reset($files));
64
        $this->assertEquals(DIRECTORY_SEPARATOR . 'dummyFile.0.tst', next($files));
65
        $this->assertEquals(DIRECTORY_SEPARATOR . 'dummyFile.1.tst', next($files));
66
        $this->assertEquals(DIRECTORY_SEPARATOR . 'dummyFile.2.tst', next($files));
67
        $this->assertEquals(DIRECTORY_SEPARATOR . 'dummyFile.tst', next($files));
68
69
        $removal = $volume->removeMulti($testFiles);
70
        $this->assertFalse($volume->exists($this->getTestDir() . 'dummyFile.tst'));
71
        $this->assertFalse($volume->exists($this->getTestDir() . 'dummyFile.0.tst'));
72
        $this->assertFalse($volume->exists($this->getTestDir() . 'dummyFile.1.tst'));
73
        $this->assertFalse($volume->exists($this->getTestDir() . 'dummyFile.2.tst'));
74
75
        $this->assertEquals([
76
            'dummyFile.tst' => true,
77
            'dummyFile.0.tst' => true,
78
            'dummyFile.1.tst' => true,
79
            'dummyFile.2.tst' => true,
80
        ], $removal);
81
    }
82
83
    public function dotDirs(string $path): bool
84
    {
85
        return !in_array($path, ['.', '..', DIRECTORY_SEPARATOR . '.', DIRECTORY_SEPARATOR . '..']);
86
    }
87
}
88