|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lamoda\Codeception\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use Codeception\Util\Shared\Asserts; |
|
6
|
|
|
use League\Flysystem\FileExistsException; |
|
7
|
|
|
use League\Flysystem\FileNotFoundException; |
|
8
|
|
|
use League\Flysystem\FilesystemInterface; |
|
9
|
|
|
|
|
10
|
|
|
class FileSystem |
|
11
|
|
|
{ |
|
12
|
|
|
use Asserts; |
|
13
|
|
|
|
|
14
|
|
|
/** @var FilesystemInterface */ |
|
15
|
|
|
protected $flySystem; |
|
16
|
|
|
|
|
17
|
30 |
|
public function __construct(FilesystemInterface $flySystem) |
|
18
|
|
|
{ |
|
19
|
30 |
|
$this->flySystem = $flySystem; |
|
20
|
30 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $path |
|
24
|
|
|
* @param string $contents |
|
25
|
|
|
* @param array $config |
|
26
|
|
|
* |
|
27
|
|
|
* @throws FileExistsException |
|
28
|
|
|
* |
|
29
|
|
|
* @return bool |
|
30
|
|
|
*/ |
|
31
|
3 |
|
public function writeFile($path, $contents, array $config = []) |
|
32
|
|
|
{ |
|
33
|
3 |
|
return $this->flySystem->write($path, $contents, $config); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $path |
|
38
|
|
|
* |
|
39
|
|
|
* @throws FileNotFoundException |
|
40
|
|
|
* |
|
41
|
|
|
* @return bool |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function deleteFile($path) |
|
44
|
|
|
{ |
|
45
|
2 |
|
return $this->flySystem->delete($path); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $path |
|
50
|
|
|
* |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
3 |
|
public function clearDir($path) |
|
54
|
|
|
{ |
|
55
|
3 |
|
if (!$this->flySystem->deleteDir($path)) { |
|
56
|
1 |
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
return $this->flySystem->createDir($path); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $path |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
2 |
|
public function createDir($path) |
|
68
|
|
|
{ |
|
69
|
2 |
|
return $this->flySystem->createDir($path); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $path |
|
74
|
|
|
* @param string $newPath |
|
75
|
|
|
* |
|
76
|
|
|
* @throws FileExistsException |
|
77
|
|
|
* @throws FileNotFoundException |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function copyFile($path, $newPath) |
|
82
|
|
|
{ |
|
83
|
2 |
|
return $this->flySystem->copy($path, $newPath); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $path |
|
88
|
|
|
* |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
12 |
|
public function grabFileList($path = '') |
|
92
|
|
|
{ |
|
93
|
12 |
|
$list = []; |
|
94
|
12 |
|
$content = $this->flySystem->listContents($path); |
|
95
|
|
|
|
|
96
|
12 |
|
foreach ($content as $file) { |
|
97
|
8 |
|
if ('file' === $file['type']) { |
|
98
|
8 |
|
$list[] = $file['path']; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
12 |
|
return $list; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $regex |
|
107
|
|
|
* @param string $path |
|
108
|
|
|
*/ |
|
109
|
3 |
|
public function dontSeeFileFoundMatches($regex, $path = '') |
|
110
|
|
|
{ |
|
111
|
3 |
|
foreach ($this->grabFileList($path) as $filename) { |
|
112
|
2 |
|
preg_match($regex, $filename, $matches); |
|
113
|
2 |
|
if (!empty($matches)) { |
|
114
|
2 |
|
$this->fail("File matches found for '{$regex}'"); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
2 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $regex |
|
121
|
|
|
* @param string $path |
|
122
|
|
|
*/ |
|
123
|
3 |
|
public function seeFileFoundMatches($regex, $path = '') |
|
124
|
|
|
{ |
|
125
|
3 |
|
foreach ($this->grabFileList($path) as $filename) { |
|
126
|
2 |
|
preg_match($regex, $filename, $matches); |
|
127
|
2 |
|
if (!empty($matches)) { |
|
128
|
2 |
|
return; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
2 |
|
$this->fail("no file matches found for '{$regex}'"); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $path |
|
136
|
|
|
* @param string $needle |
|
137
|
|
|
* |
|
138
|
|
|
* @throws FileNotFoundException |
|
139
|
|
|
*/ |
|
140
|
3 |
|
public function seeInFile($path, $needle) |
|
141
|
|
|
{ |
|
142
|
3 |
|
$content = $this->flySystem->read($path); |
|
143
|
|
|
|
|
144
|
3 |
|
if (!$content) { |
|
145
|
1 |
|
$this->fail("can't read file '{$path}'"); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
2 |
|
if (false === strpos($content, $needle)) { |
|
149
|
1 |
|
$this->fail("file '{$path}' does not contain search content"); |
|
150
|
|
|
} |
|
151
|
1 |
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param string $path |
|
155
|
|
|
* @param string $count |
|
156
|
|
|
*/ |
|
157
|
3 |
|
public function seeFilesCount($path, $count) |
|
158
|
|
|
{ |
|
159
|
3 |
|
$list = $this->grabFileList($path); |
|
160
|
3 |
|
$countList = count($list); |
|
161
|
|
|
|
|
162
|
3 |
|
if ($countList !== $count) { |
|
163
|
1 |
|
$this->fail("see '{$countList} file'"); |
|
164
|
|
|
} |
|
165
|
2 |
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $path |
|
169
|
|
|
*/ |
|
170
|
2 |
|
public function canSeeFile($path) |
|
171
|
|
|
{ |
|
172
|
|
|
try { |
|
173
|
2 |
|
$this->flySystem->read($path); |
|
174
|
1 |
|
} catch (FileNotFoundException $e) { |
|
175
|
1 |
|
$this->fail("can't see file '{$path}'"); |
|
176
|
|
|
} |
|
177
|
1 |
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param string $path |
|
181
|
|
|
*/ |
|
182
|
|
|
public function cantSeeFile($path) |
|
183
|
|
|
{ |
|
184
|
|
|
try { |
|
185
|
|
|
$this->flySystem->read($path); |
|
186
|
|
|
} catch (FileNotFoundException $e) { |
|
187
|
|
|
return; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$this->fail("file is exist '{$path}'"); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|