Skip to content

Commit f2f88a4

Browse files
committed
Update the test cases to cover the case when multiple files with the same name are in the directory
1 parent c3d14d1 commit f2f88a4

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/internal/Magento/Framework/File/Name.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function getNewFileName(string $destinationFile)
2828
if ($this->fileExist($destinationFile)) {
2929
$index = 1;
3030
$baseName = $fileInfo['filename'] . '.' . $fileInfo['extension'];
31-
while (file_exists($fileInfo['dirname'] . '/' . $baseName)) {
31+
while ($this->fileExist($fileInfo['dirname'] . '/' . $baseName)) {
3232
$baseName = $fileInfo['filename'] . '_' . $index . '.' . $fileInfo['extension'];
3333
$index++;
3434
}

lib/internal/Magento/Framework/Test/Unit/File/NameTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class NameTest extends \PHPUnit\Framework\TestCase
2020
*/
2121
private $nonExistingFilePath;
2222

23+
/**
24+
* @var string
25+
*/
26+
private $multipleExistingFilePath;
27+
2328
/**
2429
* @var Name
2530
*/
@@ -29,17 +34,26 @@ protected function setUp()
2934
{
3035
$this->name = new Name();
3136
$this->existingFilePath = __DIR__ . '/../_files/source.txt';
37+
$this->multipleExistingFilePath = __DIR__ . '/../_files/name.txt';
3238
$this->nonExistingFilePath = __DIR__ . '/../_files/file.txt';
3339
}
3440

3541
/**
3642
* @test
3743
*/
38-
public function testGetNewFileNameWhenFileExists()
44+
public function testGetNewFileNameWhenOneFileExists()
3945
{
4046
$this->assertEquals('source_1.txt', $this->name->getNewFileName($this->existingFilePath));
4147
}
4248

49+
/**
50+
* @test
51+
*/
52+
public function testGetNewFileNameWhenTwoFileExists()
53+
{
54+
$this->assertEquals('name_2.txt', $this->name->getNewFileName($this->multipleExistingFilePath));
55+
}
56+
4357
/**
4458
* @test
4559
*/

0 commit comments

Comments
 (0)