1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Camino. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace SebastianFeldmann\Camino\Path; |
11
|
|
|
|
12
|
|
|
use Exception; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class DirectoryTest |
17
|
|
|
* |
18
|
|
|
* @package SebastianFeldmann\Camino |
19
|
|
|
*/ |
20
|
|
|
class DirectoryTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Tests Directory::getPath |
24
|
|
|
*/ |
25
|
|
|
public function testPathUnix(): void |
26
|
|
|
{ |
27
|
|
|
$directory = new Directory('/foo/bar/baz'); |
28
|
|
|
$this->assertEquals('/foo/bar/baz', $directory->getPath()); |
29
|
|
|
$this->assertEquals(3, $directory->getDepth()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Tests Directory::getPath |
34
|
|
|
*/ |
35
|
|
|
public function testPathWindows(): void |
36
|
|
|
{ |
37
|
|
|
$directory = new Directory('c:\\foo\\bar\\baz'); |
38
|
|
|
$this->assertEquals('c:\\foo\\bar\\baz', $directory->getPath()); |
39
|
|
|
$this->assertEquals(3, $directory->getDepth()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Tests Directory::getPath |
44
|
|
|
*/ |
45
|
|
|
public function testPathStream(): void |
46
|
|
|
{ |
47
|
|
|
$directory = new Directory('stream://foo/bar/baz'); |
48
|
|
|
$this->assertEquals('stream://foo/bar/baz', $directory->getPath()); |
49
|
|
|
$this->assertEquals(3, $directory->getDepth()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Tests Directory::getSegments |
54
|
|
|
*/ |
55
|
|
|
public function testGetSegments(): void |
56
|
|
|
{ |
57
|
|
|
$this->assertCount(3, (new Directory('/foo/bar/baz'))->getSegments()); |
58
|
|
|
$this->assertCount(2, (new Directory('/foo/bar'))->getSegments()); |
59
|
|
|
$this->assertCount(1, (new Directory('/foo'))->getSegments()); |
60
|
|
|
$this->assertCount(0, (new Directory('/'))->getSegments()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Tests Directory::isSubDirectory |
65
|
|
|
*/ |
66
|
|
|
public function testIsSubdirectoryPositive(): void |
67
|
|
|
{ |
68
|
|
|
$dir = new Directory('/foo/bar/baz'); |
69
|
|
|
$this->assertTrue($dir->isSubDirectoryOf(new Directory('/foo/bar'))); |
70
|
|
|
$this->assertTrue($dir->isSubDirectoryOf(new Directory('/foo'))); |
71
|
|
|
$this->assertTrue($dir->isSubDirectoryOf(new Directory('/'))); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Tests Directory::isSubDirectory |
76
|
|
|
*/ |
77
|
|
|
public function testIsSubdirectoryNegative(): void |
78
|
|
|
{ |
79
|
|
|
$dir = new Directory('/foo/bar'); |
80
|
|
|
$this->assertFalse($dir->isSubDirectoryOf(new Directory('/foo/bar/baz'))); |
81
|
|
|
$this->assertFalse($dir->isSubDirectoryOf(new Directory('/fiz'))); |
82
|
|
|
$this->assertFalse($dir->isSubDirectoryOf(new Directory('/fiz/baz'))); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Tests Directory::getPathRelativeFrom |
87
|
|
|
*/ |
88
|
|
|
public function testGetPathRelativeFrom(): void |
89
|
|
|
{ |
90
|
|
|
$dir = new Directory('/foo/bar/baz'); |
91
|
|
|
$parent = new Directory('/foo'); |
92
|
|
|
$this->assertEquals('bar/baz', $dir->getRelativePathFrom($parent)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Tests Directory::getPathRelativeFrom |
97
|
|
|
*/ |
98
|
|
|
public function testGetPathRelativeFromNoChild(): void |
99
|
|
|
{ |
100
|
|
|
$this->expectException(Exception::class); |
101
|
|
|
$dir = new Directory('/foo/bar/baz'); |
102
|
|
|
$dir->getRelativePathFrom(new Directory('/fiz')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Tests Directory::__toString |
107
|
|
|
*/ |
108
|
|
|
public function testToString(): void |
109
|
|
|
{ |
110
|
|
|
$dir = new Directory('/foo/bar/baz'); |
111
|
|
|
$this->assertEquals('/foo/bar/baz', (string) $dir); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Tests Directory::create |
116
|
|
|
*/ |
117
|
|
|
public function testFactoryExists(): void |
118
|
|
|
{ |
119
|
|
|
$dir = Directory::create(__DIR__); |
120
|
|
|
$this->assertEquals(__DIR__, $dir->getPath()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Tests Directory::create |
125
|
|
|
*/ |
126
|
|
|
public function testFactoryDoesNotExists(): void |
127
|
|
|
{ |
128
|
|
|
$this->expectException(\Exception::class); |
129
|
|
|
|
130
|
|
|
$dir = Directory::create('./foo'); |
131
|
|
|
$this->assertEquals('should-not-be-tested', $dir->getPath()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Tests Directory::create |
137
|
|
|
*/ |
138
|
|
|
public function testFactoryIsFile(): void |
139
|
|
|
{ |
140
|
|
|
$this->expectException(\Exception::class); |
141
|
|
|
|
142
|
|
|
$dir = Directory::create(__FILE__); |
143
|
|
|
$this->assertEquals('should-not-be-tested', $dir->getPath()); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|