-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Refactor moveFileFromTmp to not use a static method call #27581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
magento-engcom-team
merged 11 commits into
magento:2.4-develop
from
dmanners:refactor-moveFileFromTmp
Sep 30, 2020
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c3d14d1
Create a new class for getting new names of files plus a test class i…
dmanners f2f88a4
Update the test cases to cover the case when multiple files with the …
dmanners 25a9bdf
Update the Catalog ImageUploader to use our new nameing class
dmanners 7a92462
Revert the constructor comment and update the content of the name fil…
dmanners b1676da
Update the class name to add a description and declare strict type
dmanners 69aa7bf
Merge branch '2.4-develop' into refactor-moveFileFromTmp
engcom-Charlie e4d45a8
Merge branch '2.4-develop' into refactor-moveFileFromTmp
engcom-Charlie 05abef2
Merge branch '2.4-develop' into refactor-moveFileFromTmp
engcom-Charlie 5c31521
Merge branch '2.4-develop' into refactor-moveFileFromTmp
engcom-Charlie 988f31b
add return type
engcom-Charlie 84b22c1
fix static
engcom-Charlie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Framework\File; | ||
|
||
use Magento\Framework\App\Filesystem\DirectoryList; | ||
use Magento\Framework\Exception\FileSystemException; | ||
use Magento\Framework\Validation\ValidationException; | ||
|
||
/** | ||
* Utility for generating a unique file name | ||
*/ | ||
class Name | ||
{ | ||
/** | ||
* Get new file name if the given name is in use | ||
* | ||
* @param string $destinationFile | ||
* @return string | ||
*/ | ||
public function getNewFileName(string $destinationFile) | ||
{ | ||
$fileInfo = $this->getPathInfo($destinationFile); | ||
if ($this->fileExist($destinationFile)) { | ||
$index = 1; | ||
$baseName = $fileInfo['filename'] . '.' . $fileInfo['extension']; | ||
while ($this->fileExist($fileInfo['dirname'] . '/' . $baseName)) { | ||
$baseName = $fileInfo['filename'] . '_' . $index . '.' . $fileInfo['extension']; | ||
$index++; | ||
} | ||
$destFileName = $baseName; | ||
} else { | ||
return $fileInfo['basename']; | ||
} | ||
|
||
return $destFileName; | ||
} | ||
|
||
/** | ||
* Get the path information from a given file | ||
* | ||
* @param string $destinationFile | ||
* @return string|string[] | ||
*/ | ||
private function getPathInfo(string $destinationFile) | ||
{ | ||
return pathinfo($destinationFile); | ||
} | ||
|
||
/** | ||
* Check to see if a given file exists | ||
* | ||
* @param string $destinationFile | ||
* @return bool | ||
*/ | ||
private function fileExist(string $destinationFile) | ||
{ | ||
return file_exists($destinationFile); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
lib/internal/Magento/Framework/Test/Unit/File/NameTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Framework\Test\Unit\File; | ||
|
||
use Magento\Framework\File\Name; | ||
|
||
class NameTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $existingFilePath; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $nonExistingFilePath; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $multipleExistingFilePath; | ||
|
||
/** | ||
* @var Name | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->name = new Name(); | ||
$this->existingFilePath = __DIR__ . '/../_files/source.txt'; | ||
$this->multipleExistingFilePath = __DIR__ . '/../_files/name.txt'; | ||
$this->nonExistingFilePath = __DIR__ . '/../_files/file.txt'; | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function testGetNewFileNameWhenOneFileExists() | ||
{ | ||
$this->assertEquals('source_1.txt', $this->name->getNewFileName($this->existingFilePath)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function testGetNewFileNameWhenTwoFileExists() | ||
{ | ||
$this->assertEquals('name_2.txt', $this->name->getNewFileName($this->multipleExistingFilePath)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function testGetNewFileNameWhenFileDoesNotExist() | ||
{ | ||
$this->assertEquals('file.txt', $this->name->getNewFileName($this->nonExistingFilePath)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
name_1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.