From a1b71ed9e1f1bd11155382b590de4b77799848dc Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 10 Oct 2014 21:18:54 +0200 Subject: [PATCH] Disabled blank thumbnail saving --- src/Controllers/PostContentController.php | 3 +- src/Controllers/UserAvatarController.php | 3 +- src/Services/ThumbnailService.php | 34 ++++----- tests/Services/ThumbnailServiceTest.php | 86 ++++++++++++++--------- 4 files changed, 70 insertions(+), 56 deletions(-) diff --git a/src/Controllers/PostContentController.php b/src/Controllers/PostContentController.php index 7376593a..f0574379 100644 --- a/src/Controllers/PostContentController.php +++ b/src/Controllers/PostContentController.php @@ -57,8 +57,7 @@ final class PostContentController extends AbstractController if (!$this->fileDao->exists($sourceName)) $sourceName = $post->getContentPath(); - $this->thumbnailService->generateIfNeeded($sourceName, $size, $size); - $thumbnailName = $this->thumbnailService->getThumbnailName($sourceName, $size, $size); + $thumbnailName = $this->thumbnailService->generateIfNeeded($sourceName, $size, $size); $this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName)); } } diff --git a/src/Controllers/UserAvatarController.php b/src/Controllers/UserAvatarController.php index b4d1fc42..6cd9c4ae 100644 --- a/src/Controllers/UserAvatarController.php +++ b/src/Controllers/UserAvatarController.php @@ -71,8 +71,7 @@ final class UserAvatarController extends AbstractController private function serveFromFile($sourceName, $size) { - $this->thumbnailService->generateIfNeeded($sourceName, $size, $size); - $thumbnailName = $this->thumbnailService->getThumbnailName($sourceName, $size, $size); + $thumbnailName = $this->thumbnailService->generateIfNeeded($sourceName, $size, $size); $this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName)); } diff --git a/src/Services/ThumbnailService.php b/src/Services/ThumbnailService.php index 2f8dd0c1..9e7ec6f9 100644 --- a/src/Services/ThumbnailService.php +++ b/src/Services/ThumbnailService.php @@ -26,17 +26,19 @@ class ThumbnailService foreach ($this->getUsedThumbnailSizes() as $size) { list ($width, $height) = $size; - $target = $this->getThumbnailName($sourceName, $width, $height); + $target = $this->getTargetName($sourceName, $width, $height); $this->fileDao->delete($target); } } public function generateIfNeeded($sourceName, $width, $height) { - $thumbnailName = $this->getThumbnailName($sourceName, $width, $height); + $targetName = $this->getTargetName($sourceName, $width, $height); - if (!$this->fileDao->exists($thumbnailName)) - $this->generate($sourceName, $width, $height); + if (!$this->fileDao->exists($targetName)) + return $this->generate($sourceName, $width, $height); + + return $targetName; } public function generate($sourceName, $width, $height) @@ -55,21 +57,19 @@ class ThumbnailService throw new \InvalidArgumentException('Invalid thumbnail crop style'); } - $thumbnailName = $this->getThumbnailName($sourceName, $width, $height); - $source = $this->fileDao->load($sourceName); - $result = null; - - if (!$source) - $source = $this->fileDao->load($this->getBlankThumbnailName()); - if ($source) - $result = $this->thumbnailGenerator->generate($source, $width, $height, $cropStyle); + { + $thumbnailContent = $this->thumbnailGenerator->generate($source, $width, $height, $cropStyle); + if ($thumbnailContent) + { + $targetName = $this->getTargetName($sourceName, $width, $height); + $this->fileDao->save($targetName, $thumbnailContent); + return $targetName; + } + } - if (!$result) - $result = $this->fileDao->load($this->getBlankThumbnailName()); - - $this->fileDao->save($thumbnailName, $result); + return $this->getBlankThumbnailName(); } public function getUsedThumbnailSizes() @@ -89,7 +89,7 @@ class ThumbnailService } } - public function getThumbnailName($source, $width, $height) + public function getTargetName($source, $width, $height) { return 'thumbnails' . DIRECTORY_SEPARATOR . $width . 'x' . $height . DIRECTORY_SEPARATOR . $source; } diff --git a/tests/Services/ThumbnailServiceTest.php b/tests/Services/ThumbnailServiceTest.php index 1d84a80b..7561256f 100644 --- a/tests/Services/ThumbnailServiceTest.php +++ b/tests/Services/ThumbnailServiceTest.php @@ -64,33 +64,23 @@ final class ThumbnailServiceTest extends AbstractTestCase $this->configMock->set('misc/thumbnailCropStyle', 'outside'); $this->fileDaoMock - ->expects($this->exactly(2)) + ->expects($this->once()) ->method('load') - ->withConsecutive( - ['nope'], - ['thumbnails' . DIRECTORY_SEPARATOR . 'blank.png']) - ->will( - $this->onConsecutiveCalls( - null, - 'content of blank thumbnail')); + ->with('nope') + ->willReturn(null); $this->thumbnailGeneratorMock - ->expects($this->once()) - ->method('generate') - ->with( - 'content of blank thumbnail', - 100, - 100, - IThumbnailGenerator::CROP_OUTSIDE) - ->willReturn('generated thumbnail'); + ->expects($this->never()) + ->method('generate'); $this->fileDaoMock - ->expects($this->once()) - ->method('save') - ->with('thumbnails' . DIRECTORY_SEPARATOR . '100x100' . DIRECTORY_SEPARATOR . 'nope', 'generated thumbnail'); + ->expects($this->never()) + ->method('save'); $thumbnailService = $this->getThumbnailService(); - $thumbnailService->generate('nope', 100, 100); + $this->assertEquals( + 'thumbnails' . DIRECTORY_SEPARATOR . 'blank.png', + $thumbnailService->generate('nope', 100, 100)); } public function testThumbnailGeneratingFail() @@ -98,37 +88,63 @@ final class ThumbnailServiceTest extends AbstractTestCase $this->configMock->set('misc/thumbnailCropStyle', 'outside'); $this->fileDaoMock - ->expects($this->exactly(3)) + ->expects($this->once()) ->method('load') - ->withConsecutive( - ['nope'], - ['thumbnails' . DIRECTORY_SEPARATOR . 'blank.png'], - ['thumbnails' . DIRECTORY_SEPARATOR . 'blank.png']) - ->will( - $this->onConsecutiveCalls( - null, - 'content of blank thumbnail', - 'content of blank thumbnail (2)')); + ->with('nope') + ->willReturn('content of file'); $this->thumbnailGeneratorMock ->expects($this->once()) ->method('generate') ->with( - 'content of blank thumbnail', + 'content of file', 100, 100, IThumbnailGenerator::CROP_OUTSIDE) ->willReturn(null); $this->fileDaoMock - ->expects($this->once()) - ->method('save') - ->with('thumbnails' . DIRECTORY_SEPARATOR . '100x100' . DIRECTORY_SEPARATOR . 'nope', 'content of blank thumbnail (2)'); + ->expects($this->never()) + ->method('save'); $thumbnailService = $this->getThumbnailService(); - $thumbnailService->generate('nope', 100, 100); + $this->assertEquals( + 'thumbnails' . DIRECTORY_SEPARATOR . 'blank.png', + $thumbnailService->generate('nope', 100, 100)); } + public function testThumbnailGeneratingSuccess() + { + $this->configMock->set('misc/thumbnailCropStyle', 'outside'); + + $this->fileDaoMock + ->expects($this->once()) + ->method('load') + ->with('okay') + ->willReturn('content of file'); + + $this->thumbnailGeneratorMock + ->expects($this->once()) + ->method('generate') + ->with( + 'content of file', + 100, + 100, + IThumbnailGenerator::CROP_OUTSIDE) + ->willReturn('content of thumbnail'); + + $this->fileDaoMock + ->expects($this->once()) + ->method('save') + ->with( + 'thumbnails' . DIRECTORY_SEPARATOR . '100x100' . DIRECTORY_SEPARATOR . 'okay', + 'content of thumbnail'); + + $thumbnailService = $this->getThumbnailService(); + $this->assertEquals( + 'thumbnails' . DIRECTORY_SEPARATOR . '100x100' . DIRECTORY_SEPARATOR . 'okay', + $thumbnailService->generate('okay', 100, 100)); + } private function getThumbnailService() {