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