Skip to content

Commit 26edafb

Browse files
authored
Merge pull request #7243 from magento-arcticfoxes/B2B-2053
B2B-1986, B2B-1990, B2B-2018, B2B-2036, B2B-2052, B2B-2053, B2B-2081
2 parents 42fe223 + 3efbe0b commit 26edafb

17 files changed

+962
-32
lines changed
Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\AwsS3\Test\Mftf\Helper;
9+
10+
use Codeception\Lib\ModuleContainer;
11+
use Magento\Catalog\Test\Mftf\Helper\LocalFileAssertions;
12+
use Magento\FunctionalTestingFramework\Helper\Helper;
13+
14+
/**
15+
* File assertions proxy.
16+
*
17+
* Accepts plain file paths or json structure with paths by storage type in case there are different storage types.
18+
*
19+
* Sample structure:
20+
* {
21+
* "local":"path/to/file.txt",
22+
* "s3":"custom_path/s3_specific/file.txt"
23+
* }
24+
*
25+
* Storage type driver is identified by ENV variable 'MEDIA_STORAGE_DRIVER'.
26+
* Use 'MEDIA_STORAGE_DRIVER=local' for running tests against local filesystem.
27+
* Use 'MEDIA_STORAGE_DRIVER=s3' for running tests against AWS S3 filesystem.
28+
*/
29+
class FileAssertions extends Helper implements FileAssertionsInterface
30+
{
31+
/**
32+
* Filesystem types.
33+
*/
34+
private const STORAGE_TYPE_LOCAL = 'local';
35+
private const STORAGE_TYPE_S3 = 's3';
36+
37+
/**
38+
* @var FileAssertionsInterface
39+
*/
40+
private $helperInstance;
41+
42+
/**
43+
* Type of storage.
44+
*
45+
* @var string
46+
*/
47+
private $storageType;
48+
49+
/**
50+
* Call the parent constructor then create the driver from environment variables
51+
*
52+
* @param ModuleContainer $moduleContainer
53+
* @param array|null $config
54+
* @return void
55+
*/
56+
public function __construct(ModuleContainer $moduleContainer, ?array $config = null)
57+
{
58+
parent::__construct($moduleContainer, $config);
59+
$this->storageType = getenv('MEDIA_STORAGE_DRIVER') ?: self::STORAGE_TYPE_LOCAL;
60+
if ($this->storageType === self::STORAGE_TYPE_S3) {
61+
$this->helperInstance = new S3FileAssertions($moduleContainer, $config);
62+
} else {
63+
$this->helperInstance = new LocalFileAssertions($moduleContainer, $config);
64+
}
65+
}
66+
67+
/**
68+
* Create a file in the storage.
69+
*
70+
* @param string $filePath - path to file or json structure with paths by storage type.
71+
* @param string $text
72+
* @throws \Magento\Framework\Exception\FileSystemException
73+
*/
74+
public function createTextFile($filePath, $text): void
75+
{
76+
$this->helperInstance->createTextFile($this->extractFilePath($filePath), $text);
77+
}
78+
79+
/**
80+
* Delete a file from the storage if it exists
81+
*
82+
* @param string $filePath - path to file or json structure with paths by storage type.
83+
* @throws \Magento\Framework\Exception\FileSystemException
84+
*/
85+
public function deleteFileIfExists($filePath): void
86+
{
87+
$this->helperInstance->deleteFileIfExists($this->extractFilePath($filePath));
88+
}
89+
90+
/**
91+
* Copy source into destination
92+
*
93+
* @param string $source - path to file or json structure with paths by storage type.
94+
* @param string $destination - path to file or json structure with paths by storage type.
95+
* @throws \Magento\Framework\Exception\FileSystemException
96+
*/
97+
public function copy($source, $destination): void
98+
{
99+
$this->helperInstance->copy($this->extractFilePath($source), $this->extractFilePath($destination));
100+
}
101+
102+
/**
103+
* Create directory in the storage
104+
*
105+
* @param string $path - path to file or json structure with paths by storage type.
106+
* @param int $permissions
107+
* @throws \Magento\Framework\Exception\FileSystemException
108+
*/
109+
public function createDirectory($path, $permissions = 0777): void
110+
{
111+
$this->helperInstance->createDirectory($this->extractFilePath($path), $permissions);
112+
}
113+
114+
/**
115+
* Recursive delete directory in the storage
116+
*
117+
* @param string $path - path to file or json structure with paths by storage type.
118+
* @throws \Magento\Framework\Exception\FileSystemException
119+
*/
120+
public function deleteDirectory($path): void
121+
{
122+
$this->helperInstance->deleteDirectory($this->extractFilePath($path));
123+
}
124+
125+
/**
126+
* Assert a file exists in the storage
127+
*
128+
* @param string $filePath - path to file or json structure with paths by storage type.
129+
* @param string $message
130+
* @throws \Magento\Framework\Exception\FileSystemException
131+
*/
132+
public function assertFileExists($filePath, $message = ''): void
133+
{
134+
$this->helperInstance->assertFileExists($this->extractFilePath($filePath), $message);
135+
}
136+
137+
/**
138+
* Asserts that a file with the given glob pattern exists in the given path in the storage
139+
*
140+
* @param string $path - path to file or json structure with paths by storage type.
141+
* @param string $pattern
142+
* @param string $message
143+
* @throws \Magento\Framework\Exception\FileSystemException
144+
*/
145+
public function assertGlobbedFileExists($path, $pattern, $message = ''): void
146+
{
147+
$this->helperInstance->assertGlobbedFileExists($this->extractFilePath($path), $pattern, $message);
148+
}
149+
150+
/**
151+
* Asserts that a directory exists in the storage
152+
*
153+
* @param string $path - path to file or json structure with paths by storage type.
154+
* @param string $message
155+
* @throws \Magento\Framework\Exception\FileSystemException
156+
*/
157+
public function assertDirectoryExists($path, $message = ''): void
158+
{
159+
$this->helperInstance->assertDirectoryExists($this->extractFilePath($path), $message);
160+
}
161+
162+
/**
163+
* Asserts that a directory does not exist in the storage
164+
*
165+
* @param string $path - path to file or json structure with paths by storage type.
166+
* @param string $message
167+
* @throws \Magento\Framework\Exception\FileSystemException
168+
*/
169+
public function assertDirectoryDoesNotExist($path, $message = ''): void
170+
{
171+
$this->helperInstance->assertDirectoryDoesNotExist($this->extractFilePath($path), $message);
172+
}
173+
174+
/**
175+
* Assert a file does not exist in the storage
176+
*
177+
* @param string $filePath - path to file or json structure with paths by storage type.
178+
* @param string $message
179+
* @throws \Magento\Framework\Exception\FileSystemException
180+
*/
181+
public function assertFileDoesNotExist($filePath, $message = ''): void
182+
{
183+
$this->helperInstance->assertFileDoesNotExist($this->extractFilePath($filePath), $message);
184+
}
185+
186+
/**
187+
* Assert a file in the storage has no contents
188+
*
189+
* @param string $filePath - path to file or json structure with paths by storage type.
190+
* @param string $message
191+
* @throws \Magento\Framework\Exception\FileSystemException
192+
*/
193+
public function assertFileEmpty($filePath, $message = ''): void
194+
{
195+
$this->helperInstance->assertFileEmpty($this->extractFilePath($filePath), $message);
196+
}
197+
198+
/**
199+
* Assert a file in the storage is not empty
200+
*
201+
* @param string $filePath - path to file or json structure with paths by storage type.
202+
* @param string $message
203+
* @throws \Magento\Framework\Exception\FileSystemException
204+
*/
205+
public function assertFileNotEmpty($filePath, $message = ''): void
206+
{
207+
$this->helperInstance->assertFileNotEmpty($this->extractFilePath($filePath), $message);
208+
}
209+
210+
/**
211+
* Assert a file in the storage contains a given string
212+
*
213+
* @param string $filePath - path to file or json structure with paths by storage type.
214+
* @param string $text
215+
* @param string $message
216+
* @throws \Magento\Framework\Exception\FileSystemException
217+
*/
218+
public function assertFileContainsString($filePath, $text, $message = ''): void
219+
{
220+
$this->helperInstance->assertFileContainsString($this->extractFilePath($filePath), $text, $message);
221+
}
222+
223+
/**
224+
* Asserts that a file with the given glob pattern at the given path in the storage contains a given string
225+
*
226+
* @param string $path - path to file or json structure with paths by storage type.
227+
* @param string $pattern
228+
* @param string $text
229+
* @param int $fileIndex
230+
* @param string $message
231+
* @throws \Magento\Framework\Exception\FileSystemException
232+
*/
233+
public function assertGlobbedFileContainsString($path, $pattern, $text, $fileIndex = 0, $message = ''): void
234+
{
235+
$this->helperInstance->assertGlobbedFileContainsString(
236+
$this->extractFilePath($path),
237+
$pattern,
238+
$text,
239+
$fileIndex,
240+
$message
241+
);
242+
}
243+
244+
/**
245+
* Assert a file in the storage does not contain a given string
246+
*
247+
* @param string $filePath - path to file or json structure with paths by storage type.
248+
* @param string $text
249+
* @param string $message
250+
* @throws \Magento\Framework\Exception\FileSystemException
251+
*/
252+
public function assertFileDoesNotContainString($filePath, $text, $message = ''): void
253+
{
254+
$this->helperInstance->assertFileDoesNotContainString($this->extractFilePath($filePath), $text, $message);
255+
}
256+
257+
/**
258+
* Asserts that a directory in the storage is empty
259+
*
260+
* @param string $path - path to file or json structure with paths by storage type.
261+
* @param string $message
262+
* @throws \Magento\Framework\Exception\FileSystemException
263+
*/
264+
public function assertDirectoryEmpty($path, $message = ''): void
265+
{
266+
$this->helperInstance->assertDirectoryEmpty($this->extractFilePath($path), $message);
267+
}
268+
269+
/**
270+
* Asserts that a directory in the storage is not empty
271+
*
272+
* @param string $path - path to file or json structure with paths by storage type.
273+
* @param string $message
274+
* @throws \Magento\Framework\Exception\FileSystemException
275+
*/
276+
public function assertDirectoryNotEmpty($path, $message = ''): void
277+
{
278+
$this->helperInstance->assertDirectoryNotEmpty($this->extractFilePath($path), $message);
279+
}
280+
281+
/**
282+
* Extract file path from json string relevant for current storage type.
283+
* Returns given argument unchanged if no file path for current storage or given value is not a json structure.
284+
*
285+
* @param string $filePathJson - path to file or json structure with paths by storage type.
286+
* @return mixed
287+
*/
288+
private function extractFilePath($filePathJson)
289+
{
290+
$filePathArgs = json_decode($filePathJson, true);
291+
if (isset($filePathArgs[$this->storageType])) {
292+
return $filePathArgs[$this->storageType];
293+
}
294+
return $filePathJson;
295+
}
296+
}

0 commit comments

Comments
 (0)