From 3a400f92d1936b2b752d813cbb27632d6acb9904 Mon Sep 17 00:00:00 2001 From: Julius Haertl Date: Mon, 17 Oct 2016 16:31:07 +0200 Subject: Replace null return with NotFoundException Signed-off-by: Julius Haertl --- apps/theming/lib/Controller/IconController.php | 18 +++++++++++------- apps/theming/lib/ImageManager.php | 9 +++------ apps/theming/tests/Controller/IconControllerTest.php | 7 +++++++ apps/theming/tests/ImageManagerTest.php | 14 ++++++-------- 4 files changed, 27 insertions(+), 21 deletions(-) (limited to 'apps') diff --git a/apps/theming/lib/Controller/IconController.php b/apps/theming/lib/Controller/IconController.php index 887dba8a68a..08d5b50120f 100644 --- a/apps/theming/lib/Controller/IconController.php +++ b/apps/theming/lib/Controller/IconController.php @@ -30,6 +30,7 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataDisplayResponse; use OCP\AppFramework\Http\FileDisplayResponse; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Files\NotFoundException; use OCP\IRequest; use OCA\Theming\Util; use OCP\IConfig; @@ -89,8 +90,9 @@ class IconController extends Controller { * @return FileDisplayResponse */ public function getThemedIcon($app, $image) { - $iconFile = $this->imageManager->getCachedImage("icon-" . $app . '-' . str_replace("/","_",$image)); - if ($iconFile === null) { + try { + $iconFile = $this->imageManager->getCachedImage("icon-" . $app . '-' . str_replace("/","_",$image)); + } catch (NotFoundException $exception) { $icon = $this->iconBuilder->colorSvg($app, $image); $iconFile = $this->imageManager->setCachedImage("icon-" . $app . '-' . str_replace("/","_",$image), $icon); } @@ -115,8 +117,9 @@ class IconController extends Controller { */ public function getFavicon($app = "core") { if ($this->themingDefaults->shouldReplaceIcons()) { - $iconFile = $this->imageManager->getCachedImage('favIcon-' . $app); - if($iconFile === null) { + try { + $iconFile = $this->imageManager->getCachedImage('favIcon-' . $app); + } catch (NotFoundException $exception) { $icon = $this->iconBuilder->getFavicon($app); $iconFile = $this->imageManager->setCachedImage('favIcon-' . $app, $icon); } @@ -146,8 +149,9 @@ class IconController extends Controller { */ public function getTouchIcon($app = "core") { if ($this->themingDefaults->shouldReplaceIcons()) { - $iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app); - if ($iconFile === null) { + try { + $iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app); + } catch (NotFoundException $exception) { $icon = $this->iconBuilder->getTouchIcon($app); $iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app, $icon); } @@ -165,4 +169,4 @@ class IconController extends Controller { } return $response; } -} \ No newline at end of file +} diff --git a/apps/theming/lib/ImageManager.php b/apps/theming/lib/ImageManager.php index 5e1b61e3a92..e7dcfa92190 100644 --- a/apps/theming/lib/ImageManager.php +++ b/apps/theming/lib/ImageManager.php @@ -71,15 +71,12 @@ class ImageManager { * Get a file from AppData * * @param string $filename - * @return null|\OCP\Files\SimpleFS\ISimpleFile + * @throws NotFoundException + * @return \OCP\Files\SimpleFS\ISimpleFile */ public function getCachedImage($filename) { $currentFolder = $this->getCacheFolder(); - if($currentFolder->fileExists($filename)) { - return $currentFolder->getFile($filename); - } else { - return null; - } + return $currentFolder->getFile($filename); } /** diff --git a/apps/theming/tests/Controller/IconControllerTest.php b/apps/theming/tests/Controller/IconControllerTest.php index 98fa4f1a243..69bbfa0e45e 100644 --- a/apps/theming/tests/Controller/IconControllerTest.php +++ b/apps/theming/tests/Controller/IconControllerTest.php @@ -28,6 +28,7 @@ use OCA\Theming\ImageManager; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataDisplayResponse; use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; @@ -130,6 +131,9 @@ class IconControllerTest extends TestCase { ->with('core') ->willReturn('filecontent'); $file = $this->iconFileMock('filename', 'filecontent'); + $this->imageManager->expects($this->once()) + ->method('getCachedImage') + ->will($this->throwException(new NotFoundException())); $this->imageManager->expects($this->once()) ->method('setCachedImage') ->willReturn($file); @@ -171,6 +175,9 @@ class IconControllerTest extends TestCase { ->with('core') ->willReturn('filecontent'); $file = $this->iconFileMock('filename', 'filecontent'); + $this->imageManager->expects($this->once()) + ->method('getCachedImage') + ->will($this->throwException(new NotFoundException())); $this->imageManager->expects($this->once()) ->method('setCachedImage') ->willReturn($file); diff --git a/apps/theming/tests/ImageManagerTest.php b/apps/theming/tests/ImageManagerTest.php index b52d557ab67..4df49633d80 100644 --- a/apps/theming/tests/ImageManagerTest.php +++ b/apps/theming/tests/ImageManagerTest.php @@ -85,10 +85,6 @@ class ImageManager extends TestCase { public function testGetCachedImage() { $folder = $this->setupCacheFolder(); - $folder->expects($this->once()) - ->method('fileExists') - ->with('filename') - ->willReturn(true); $folder->expects($this->once()) ->method('getFile') ->with('filename') @@ -97,14 +93,16 @@ class ImageManager extends TestCase { $this->assertEquals($expected, $this->imageManager->getCachedImage('filename')); } + /** + * @expectedException \OCP\Files\NotFoundException + */ public function testGetCachedImageNotFound() { $folder = $this->setupCacheFolder(); $folder->expects($this->once()) - ->method('fileExists') + ->method('getFile') ->with('filename') - ->willReturn(false); - $expected = null; - $this->assertEquals($expected, $this->imageManager->getCachedImage('filename')); + ->will($this->throwException(new \OCP\Files\NotFoundException())); + $image = $this->imageManager->getCachedImage('filename'); } public function testSetCachedImage() { -- cgit v1.2.3