1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Filesystem; |
6
|
|
|
|
7
|
|
|
use League\Flysystem\Filesystem; |
8
|
|
|
use League\Flysystem\FilesystemOperator; |
9
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class FileFinderTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** @var FileFinder - System Under Test */ |
15
|
|
|
protected $sut; |
16
|
|
|
|
17
|
|
|
public function setUp(): void |
18
|
|
|
{ |
19
|
|
|
$this->sut = new FileFinder(); |
20
|
|
|
|
21
|
|
|
parent::setUp(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return FilesystemOperator|MockObject |
26
|
|
|
*/ |
27
|
|
|
protected function createFilesystemMock() |
28
|
|
|
{ |
29
|
|
|
return $this->createMock(Filesystem::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testReadWithoutFilesystems() |
33
|
|
|
{ |
34
|
|
|
$path = 'foo'; |
35
|
|
|
|
36
|
|
|
$actualResult = $this->sut->read($path); |
37
|
|
|
|
38
|
|
|
$this->assertNull($actualResult); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testReadWithOnlyDefaultFilesystem() |
42
|
|
|
{ |
43
|
|
|
$fs = $this->createFilesystemMock(); |
44
|
|
|
|
45
|
|
|
$path = 'foo'; |
46
|
|
|
$expectedResult = 'bar'; |
47
|
|
|
|
48
|
|
|
$this->sut->registerFilesystem($fs); |
49
|
|
|
|
50
|
|
|
$fs->expects($this->any())->method('fileExists')->willReturn(true); |
51
|
|
|
$fs->expects($this->once())->method('read')->with($path)->willReturn($expectedResult); |
52
|
|
|
|
53
|
|
|
$actualResult = $this->sut->read($path); |
54
|
|
|
|
55
|
|
|
$this->assertSame($expectedResult, $actualResult); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testReadWithOnlyVendorFilesystemImplicit() |
59
|
|
|
{ |
60
|
|
|
$fs = $this->createFilesystemMock(); |
61
|
|
|
|
62
|
|
|
$path = '/vendor-one/foo'; |
63
|
|
|
$realPath = '/foo'; |
64
|
|
|
$expectedResult = 'bar'; |
65
|
|
|
|
66
|
|
|
$this->sut->registerFilesystem($fs, 'vendor-one', 1); |
67
|
|
|
|
68
|
|
|
$fs->expects($this->any())->method('fileExists')->willReturn(true); |
69
|
|
|
$fs->expects($this->once())->method('read')->with($realPath)->willReturn($expectedResult); |
70
|
|
|
|
71
|
|
|
$actualResult = $this->sut->read($path); |
72
|
|
|
|
73
|
|
|
$this->assertSame($expectedResult, $actualResult); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testReadWithOnlyVendorFilesystemExplicit() |
77
|
|
|
{ |
78
|
|
|
$fs = $this->createFilesystemMock(); |
79
|
|
|
|
80
|
|
|
$path = 'foo'; |
81
|
|
|
$expectedResult = 'bar'; |
82
|
|
|
|
83
|
|
|
$this->sut->registerFilesystem($fs, 'vendor-one', 1); |
84
|
|
|
|
85
|
|
|
$fs->expects($this->any())->method('fileExists')->willReturn(true); |
86
|
|
|
$fs->expects($this->once())->method('read')->with($path)->willReturn($expectedResult); |
87
|
|
|
|
88
|
|
|
$actualResult = $this->sut->read($path, 'vendor-one'); |
89
|
|
|
|
90
|
|
|
$this->assertSame($expectedResult, $actualResult); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testReadRespectsPriorities() |
94
|
|
|
{ |
95
|
|
|
$fs1 = $this->createFilesystemMock(); |
96
|
|
|
$fs2 = $this->createFilesystemMock(); |
97
|
|
|
|
98
|
|
|
$path = 'foo'; |
99
|
|
|
$expectedResult = 'bar'; |
100
|
|
|
|
101
|
|
|
$this->sut->registerFilesystem($fs1); |
102
|
|
|
$this->sut->registerFilesystem($fs2, 'vendor-one', 1); |
103
|
|
|
|
104
|
|
|
$fs1->expects($this->any())->method('fileExists')->willReturn(true); |
105
|
|
|
$fs2->expects($this->any())->method('fileExists')->willReturn(true); |
106
|
|
|
$fs1->expects($this->never())->method('read'); |
107
|
|
|
$fs2->expects($this->once())->method('read')->with($path)->willReturn($expectedResult); |
108
|
|
|
|
109
|
|
|
$actualResult = $this->sut->read($path, 'vendor-one'); |
110
|
|
|
|
111
|
|
|
$this->assertSame($expectedResult, $actualResult); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testReadSuppressesExceptionsInReading() |
115
|
|
|
{ |
116
|
|
|
$fs1 = $this->createFilesystemMock(); |
117
|
|
|
|
118
|
|
|
$path = 'foo'; |
119
|
|
|
|
120
|
|
|
$this->sut->registerFilesystem($fs1, 'vendor-one', 1); |
121
|
|
|
|
122
|
|
|
$fs1->expects($this->any())->method('fileExists')->willReturn(true); |
123
|
|
|
$fs1->expects($this->once())->method('read')->willThrowException(new \Exception('baz')); |
124
|
|
|
|
125
|
|
|
$actualResult = $this->sut->read($path, 'vendor-one'); |
126
|
|
|
|
127
|
|
|
$this->assertNull($actualResult); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testFileExistsWithoutFilesystems() |
131
|
|
|
{ |
132
|
|
|
$path = 'foo'; |
133
|
|
|
|
134
|
|
|
$actualResult = $this->sut->fileExists($path); |
135
|
|
|
|
136
|
|
|
$this->assertFalse($actualResult); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testFileExistsWithOnlyDefaultFilesystem() |
140
|
|
|
{ |
141
|
|
|
$fs = $this->createFilesystemMock(); |
142
|
|
|
|
143
|
|
|
$path = 'foo'; |
144
|
|
|
|
145
|
|
|
$this->sut->registerFilesystem($fs); |
146
|
|
|
|
147
|
|
|
$fs->expects($this->once())->method('fileExists')->willReturn(true); |
148
|
|
|
|
149
|
|
|
$actualResult = $this->sut->fileExists($path); |
150
|
|
|
|
151
|
|
|
$this->assertTrue($actualResult); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testFileExistsWithOnlyVendorFilesystemImplicit() |
155
|
|
|
{ |
156
|
|
|
$fs = $this->createFilesystemMock(); |
157
|
|
|
|
158
|
|
|
$path = '/vendor-one/foo'; |
159
|
|
|
|
160
|
|
|
$this->sut->registerFilesystem($fs, 'vendor-one', 1); |
161
|
|
|
|
162
|
|
|
$fs->expects($this->once())->method('fileExists')->willReturn(true); |
163
|
|
|
|
164
|
|
|
$actualResult = $this->sut->fileExists($path); |
165
|
|
|
|
166
|
|
|
$this->assertTrue($actualResult); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testFileExistsWithOnlyVendorFilesystemExplicit() |
170
|
|
|
{ |
171
|
|
|
$fs = $this->createFilesystemMock(); |
172
|
|
|
|
173
|
|
|
$path = 'foo'; |
174
|
|
|
|
175
|
|
|
$this->sut->registerFilesystem($fs, 'vendor-one', 1); |
176
|
|
|
|
177
|
|
|
$fs->expects($this->once())->method('fileExists')->willReturn(true); |
178
|
|
|
|
179
|
|
|
$actualResult = $this->sut->fileExists($path, 'vendor-one'); |
180
|
|
|
|
181
|
|
|
$this->assertTrue($actualResult); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|