Skip to content

Commit

Permalink
Change getPathToMediaFile to be working on string arguments
Browse files Browse the repository at this point in the history
The old method is renamed to getPathToMedia instead and is a convinient alias for the new one

Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Feb 14, 2024
1 parent 0e3f86c commit 6787393
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
12 changes: 10 additions & 2 deletions src/Image/Service/ImageResourceRefactored.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ public function getUrlToMedia(string $folder = '', string $fileName = ''): strin
);
}

public function getPathToMediaFile(MediaInterface $media): string
public function getPathToMedia(MediaInterface $media): string
{
return $this->getPathToMediaFiles($media->getFolderName()) . '/' . $media->getFileName();
return $this->getPathToMediaFile($media->getFolderName(), $media->getFileName());
}

public function getPathToMediaFile(string $folderName = '', string $fileName = ''): string
{
return Path::join(
$this->getPathToMediaFiles($folderName),
$fileName
);
}

public function getPossibleMediaFilePath(string $folderName = '', string $fileName = ''): FilePathInterface
Expand Down
4 changes: 3 additions & 1 deletion src/Image/Service/ImageResourceRefactoredInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ interface ImageResourceRefactoredInterface
{
public function getPathToMediaFiles(string $folder = ''): string;

public function getPathToMediaFile(MediaInterface $media): string;
public function getPathToMedia(MediaInterface $media): string;

public function getPathToMediaFile(string $folderName = '', string $fileName = ''): string;

public function getUrlToMedia(string $folder = '', string $fileName = ''): string;

Expand Down
17 changes: 3 additions & 14 deletions src/Media/Service/MediaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function rename(string $mediaId, string $newMediaName): MediaInterface
$this->thumbnailService->deleteMediaThumbnails($currentMedia);

$this->fileSystemService->rename(
$this->imageResourceRefactored->getPathToMediaFile($currentMedia),
$this->imageResourceRefactored->getPathToMedia($currentMedia),
$uniqueFileName->getPath()
);

Expand All @@ -103,7 +103,7 @@ public function moveToFolder(string $mediaId, string $folderId): void
}

$this->fileSystemService->rename(
$this->imageResourceRefactored->getPathToMediaFile($media),
$this->imageResourceRefactored->getPathToMedia($media),
$uniqueFileName->getPath()
);

Expand All @@ -118,20 +118,9 @@ public function delete(array $ids): void
}
}

/**
* @param $sSourcePath
* @param array|string $sDestPath
*
* @return bool
*/
protected function moveUploadedFile($sSourcePath, array|string $sDestPath): bool
{
return move_uploaded_file($sSourcePath, $sDestPath);
}

public function deleteMedia(MediaInterface $media): void
{
$this->fileSystemService->delete($this->imageResourceRefactored->getPathToMediaFile($media));
$this->fileSystemService->delete($this->imageResourceRefactored->getPathToMedia($media));
$this->thumbnailService->deleteMediaThumbnails($media);
$this->mediaRepository->deleteMedia($media->getOxid());
}
Expand Down
17 changes: 15 additions & 2 deletions tests/Unit/Image/Service/ImageResourceRefactoredTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function testGetUrlToMedia(
$this->assertSame($expectedResult, $sut->getUrlToMedia($folder, $fileName));
}

public function testGetPathToMediaFile(): void
public function testGetPathToMedia(): void
{
$mediaFileName = uniqid();
$directoryName = uniqid();
Expand All @@ -111,7 +111,20 @@ public function testGetPathToMediaFile(): void
$sut->method('getPathToMediaFiles')->with($directoryName)->willReturn($examplePath);

$expectedPath = $examplePath . '/' . $mediaFileName;
$this->assertSame($expectedPath, $sut->getPathToMediaFile($exampleMedia));
$this->assertSame($expectedPath, $sut->getPathToMedia($exampleMedia));
}

public function testGetPathToMediaFile(): void
{
$mediaFileName = uniqid();
$directoryName = uniqid();

$examplePath = 'examplePathWithConcreteDirectory';
$sut = $this->createPartialMock(ImageResourceRefactored::class, ['getPathToMediaFiles']);
$sut->method('getPathToMediaFiles')->with($directoryName)->willReturn($examplePath);

$expectedPath = $examplePath . '/' . $mediaFileName;
$this->assertSame($expectedPath, $sut->getPathToMediaFile($directoryName, $mediaFileName));
}

public function testCalculateUniqueFilePath(): void
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Media/Service/MediaServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testDeleteRegularMedia(): void
);

$mediaFilePath = 'exampleMediaFilePath';
$imageResource->method('getPathToMediaFile')->with($exampleMedia)->willReturn($mediaFilePath);
$imageResource->method('getPathToMedia')->with($exampleMedia)->willReturn($mediaFilePath);

$thumbnailServiceSpy->expects($this->once())->method('deleteMediaThumbnails')->with($exampleMedia);

Expand Down Expand Up @@ -107,7 +107,7 @@ public function testRename(): void

$oldPath = 'exampleOldFilePath';
$mediaFolderPath = 'mediaFolderPath';
$imageResource->method('getPathToMediaFile')->with($mediaStub)->willReturn($oldPath);
$imageResource->method('getPathToMedia')->with($mediaStub)->willReturn($oldPath);
$imageResource->method('getPathToMediaFiles')->with($mediaFolderName)->willReturn($mediaFolderPath);

$newMediaNameInput = 'someFileName';
Expand Down Expand Up @@ -167,7 +167,7 @@ public function testMoveToFolder(): void
$oldPath = 'exampleOldFilePath';
$mediaFolderPath = 'mediaFolderPath';

$imageResource->method('getPathToMediaFile')->with($mediaStub)->willReturn($oldPath);
$imageResource->method('getPathToMedia')->with($mediaStub)->willReturn($oldPath);
$imageResource->method('getPathToMediaFiles')->with($newFolderName)->willReturn($mediaFolderPath);

$newUniquePath = $mediaFolderPath . '/someUniqueFileName.txt';
Expand Down

0 comments on commit 6787393

Please sign in to comment.