Passed
Push — master ( 9bfafb...fde232 )
by Petr
07:42
created

VolumeTest::justFilesCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace SourcesTests;
4
5
6
use kalanis\kw_files\Interfaces\ITypes;
7
use kalanis\kw_paths\PathsException;
8
use kalanis\kw_tree\Essentials\FileNode;
9
use kalanis\kw_tree\Interfaces\ITree;
10
use kalanis\kw_tree\DataSources\Volume;
11
12
13
/**
14
 * Class VolumeTest
15
 * @package SourcesTests
16
 */
17
class VolumeTest extends \CommonTestClass
18
{
19
    /**
20
     * @throws PathsException
21
     */
22
    public function testSimple(): void
23
    {
24
        $results = $this
25
            ->getLib()
26
            ->process()
27
            ->getRoot();
28
        $this->assertNotEmpty($results);
29
        $sub = $results->getSubNodes();
30
        $this->assertNotEmpty($sub);
31
    }
32
33
    /**
34
     * @throws PathsException
35
     */
36
    public function testFilesAllCallback(): void
37
    {
38
        $results = $this
39
            ->getLib()
40
            ->wantDeep(true)
41
            ->setFilterCallback([$this, 'justDirsCallback'])
42
            ->setOrdering(ITree::ORDER_DESC)
43
            ->process()
44
            ->getRoot();
45
        $this->assertNotEmpty($results);
46
        $sub = $results->getSubNodes();
47
        $this->assertNotEmpty($sub);
48
49
        $node = reset($sub);
50
        /** @var FileNode $node */
51
        $this->assertEquals(['sub'], $node->getPath());
52
        $this->assertEquals(ITypes::TYPE_DIR, $node->getType());
53
        $this->assertTrue($node->isReadable());
54
        $this->assertTrue($node->isWritable());
55
        $this->assertEmpty($node->getSubNodes());
56
57
        $node = next($sub);
58
        $this->assertEquals(['next_one'], $node->getPath());
59
        $this->assertNotEmpty($node->getSubNodes());
60
61
        $subs = $node->getSubNodes();
62
        $node = reset($subs);
63
        $this->assertEquals(['next_one', 'sub_one'], $node->getPath());
64
        $this->assertEmpty($node->getSubNodes());
65
66
        $this->assertFalse(next($subs));
67
68
        $node = next($sub);
69
        $this->assertEquals(['last_one'], $node->getPath());
70
        $this->assertEmpty($node->getSubNodes());
71
72
        $this->assertFalse(next($sub));
73
    }
74
75
    /**
76
     * @throws PathsException
77
     */
78
    public function testFilesLevel(): void
79
    {
80
        $results = $this
81
            ->getLib()
82
            ->wantDeep(false)
83
            ->setFilterCallback([$this, 'justFilesCallback'])
84
            ->setOrdering(ITree::ORDER_ASC)
85
            ->process()
86
            ->getRoot();
87
        $this->assertNotEmpty($results);
88
        $sub = $results->getSubNodes();
89
        $this->assertNotEmpty($sub);
90
    }
91
92
    public function justDirsCallback(\SplFileInfo $node): bool
93
    {
94
        return $node->isDir();
95
    }
96
97
    public function justFilesCallback(\SplFileInfo $node): bool
98
    {
99
        return $node->isFile();
100
    }
101
102
    /**
103
     * @throws PathsException
104
     */
105
    public function testReversedShallow(): void
106
    {
107
        $results = $this
108
            ->getLib()
109
            ->wantDeep(false)
110
            ->setStartPath(['sub'])
111
            ->setOrdering(ITree::ORDER_ASC)
112
            ->process()
113
            ->getRoot();
114
        $this->assertNotEmpty($results);
115
    }
116
117
    /**
118
     * @throws PathsException
119
     */
120
    public function testNothing(): void
121
    {
122
        $lib = $this
123
            ->getLib()
124
            ->setStartPath(['path', 'does not', 'exists'])
125
            ->setOrdering(ITree::ORDER_NONE)
126
            ->process();
127
        $this->assertEmpty($lib->getRoot());
128
    }
129
130
    /**
131
     * @throws PathsException
132
     * @return Volume
133
     */
134
    protected function getLib(): Volume
135
    {
136
        return new Volume(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR. 'data' . DIRECTORY_SEPARATOR . 'tree');
137
    }
138
}
139