Skip to content

Commit 9698d9e

Browse files
Roman HaninRoman Hanin
authored andcommitted
B2B-1610 Upgrade to Flysystem 2.0
- tests fix
1 parent 9c9bc4d commit 9698d9e

File tree

2 files changed

+51
-48
lines changed

2 files changed

+51
-48
lines changed

app/code/Magento/AwsS3/Driver/AwsS3.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -443,16 +443,34 @@ public function isDirectory($path): bool
443443
return true;
444444
}
445445

446+
$path = $this->normalizeRelativePath($path, true);
447+
446448
try {
447-
return iterator_count(
448-
$this->adapter->listContents(
449-
$this->normalizeRelativePath($path, true),
450-
false)
451-
) > 0;
452-
} catch (\Throwable $e) {
453-
// catch closed iterator
454-
return false;
449+
return $this->isMetadataTypeDirectory($path);
450+
} catch (UnableToRetrieveMetadata $e) {
451+
try {
452+
return iterator_count($this->adapter->listContents($path, false)) > 0;
453+
} catch (\Throwable $e) {
454+
// catch closed iterator
455+
return false;
456+
}
457+
}
458+
}
459+
460+
/**
461+
* Check is given path a directory in metadata.
462+
*
463+
* @param string $path
464+
* @return bool
465+
* @throws UnableToRetrieveMetadata
466+
*/
467+
private function isMetadataTypeDirectory($path)
468+
{
469+
$meta = $this->metadataProvider->getMetadata($path);
470+
if (isset($meta['type']) && $meta['type'] === self::TYPE_DIR) {
471+
return true;
455472
}
473+
return false;
456474
}
457475

458476
/**
@@ -553,7 +571,7 @@ public function stat($path): array
553571
$metaInfo = $this->metadataProvider->getMetadata($path);
554572
} catch (UnableToRetrieveMetadata $exception) {
555573
if ($this->directoryExists($path)) {
556-
$result['type'] = 'dir';
574+
$result['type'] = self::TYPE_DIR;
557575
}
558576
return $result;
559577
}

app/code/Magento/AwsS3/Test/Unit/Driver/AwsS3Test.php

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -217,28 +217,25 @@ public function getRelativePathDataProvider(): array
217217
public function testIsDirectory(
218218
string $path,
219219
string $normalizedPath,
220-
bool $has,
221220
array $metadata,
222221
bool $expected,
223222
iterable $listContents,
224-
\Exception $metadataException = null
223+
\Throwable $listContentsException = null
225224
): void {
226-
$this->adapterMock->method('fileExists')
227-
->with($normalizedPath)
228-
->willReturn($has);
229-
$this->adapterMock->method('listContents')
230-
->with($normalizedPath)
231-
->willReturn($listContents);
232-
if (!$metadataException) {
225+
if (!empty($metadata)) {
233226
$this->metadataProviderMock->method('getMetadata')
234227
->with($normalizedPath)
235228
->willReturn($metadata);
229+
}
230+
if ($listContentsException) {
231+
$this->adapterMock->method('listContents')
232+
->with($normalizedPath)
233+
->willThrowException($listContentsException);
236234
} else {
237-
$this->metadataProviderMock->method('getMetadata')
235+
$this->adapterMock->method('listContents')
238236
->with($normalizedPath)
239-
->willThrowException($metadataException);
237+
->willReturn($listContents);
240238
}
241-
242239
self::assertSame($expected, $this->driver->isDirectory($path));
243240
}
244241

@@ -251,16 +248,14 @@ public function isDirectoryDataProvider(): array
251248
'empty metadata' => [
252249
'some_directory/',
253250
'some_directory',
254-
false,
255251
[],
256252
false,
257253
new \ArrayIterator([]),
258-
new UnableToRetrieveMetadata('Can not fetch metadata.')
254+
new \Exception('Closed iterator'),
259255
],
260256
[
261257
'some_directory',
262258
'some_directory',
263-
true,
264259
[
265260
'type' => AwsS3::TYPE_DIR
266261
],
@@ -270,38 +265,29 @@ public function isDirectoryDataProvider(): array
270265
[
271266
self::URL . 'some_directory',
272267
'some_directory',
273-
true,
274268
[
275269
'type' => AwsS3::TYPE_DIR
276270
],
277271
true,
278272
new \ArrayIterator(['some_directory']),
279273
],
280-
[
281-
self::URL . 'some_directory',
282-
'some_directory',
283-
true,
284-
[
285-
'type' => AwsS3::TYPE_FILE
286-
],
287-
false,
288-
new \ArrayIterator(['some_directory']),
289-
],
290274
[
291275
'',
292276
'',
277+
[
278+
'type' => AwsS3::TYPE_DIR
279+
],
293280
true,
294-
[],
295-
true,
296-
new \ArrayIterator(['some_directory']),
281+
new \ArrayIterator(['']),
297282
],
298283
[
299284
'/',
300285
'',
286+
[
287+
'type' => AwsS3::TYPE_DIR
288+
],
301289
true,
302-
[],
303-
true,
304-
new \ArrayIterator(['some_directory']),
290+
new \ArrayIterator(['']),
305291
],
306292
];
307293
}
@@ -433,19 +419,18 @@ public function testSearchDirectory(): void
433419
$expression = '/*';
434420
$path = 'path';
435421
$subPaths = [
436-
new \League\Flysystem\DirectoryAttributes('path/1'),
437-
new \League\Flysystem\DirectoryAttributes('path/2')
422+
new \League\Flysystem\DirectoryAttributes('path/1/'),
423+
new \League\Flysystem\DirectoryAttributes('path/2/')
438424
];
439-
$expectedResult = [self::URL . 'path/1', self::URL . 'path/2'];
440-
$this->metadataProviderMock->expects(self::atLeastOnce())->method('getMetadata')
425+
$expectedResult = [self::URL . 'path/1/', self::URL . 'path/2/'];
426+
$this->metadataProviderMock->expects(self::any())->method('getMetadata')
441427
->willReturnMap([
442428
['path', ['type' => AwsS3::TYPE_DIR]],
443429
['path/1', ['type' => AwsS3::TYPE_FILE]],
444430
['path/2', ['type' => AwsS3::TYPE_FILE]],
445431
]);
446432
$this->adapterMock->expects(self::atLeastOnce())->method('listContents')
447-
->with($path, false)
448-
->willReturn($subPaths);
433+
->willReturn(new \ArrayIterator($subPaths));
449434

450435
self::assertEquals($expectedResult, $this->driver->search($expression, $path));
451436
}
@@ -468,7 +453,7 @@ public function testSearchFiles(): void
468453
['path/1.jpg', ['type' => AwsS3::TYPE_FILE]],
469454
['path/2.png', ['type' => AwsS3::TYPE_FILE]],
470455
]);
471-
$this->adapterMock->expects(self::atLeastOnce())->method('listContents')->with($path, false)
456+
$this->adapterMock->expects(self::atLeastOnce())->method('listContents')
472457
->willReturn($subPaths);
473458

474459
self::assertEquals($expectedResult, $this->driver->search($expression, $path));

0 commit comments

Comments
 (0)