From 002080963445c2efa8607f6b4230059e5e957803 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Mon, 17 Aug 2020 16:41:49 +0800 Subject: [PATCH] Additional work on tests, should pass now --- modules/system/classes/ImageResizer.php | 14 ++++ .../unit/system/classes/ImageResizerTest.php | 66 ++++++++++++++++--- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/modules/system/classes/ImageResizer.php b/modules/system/classes/ImageResizer.php index 7f8617c93..426cceb9a 100644 --- a/modules/system/classes/ImageResizer.php +++ b/modules/system/classes/ImageResizer.php @@ -252,6 +252,20 @@ class ImageResizer return static::$availableSources = $sources; } + /** + * Flushes the local sources cache. + * + * @return void + */ + public static function flushAvailableSources() + { + if (empty(static::$availableSources)) { + return; + } + + static::$availableSources = []; + } + /** * Get the current config * diff --git a/tests/unit/system/classes/ImageResizerTest.php b/tests/unit/system/classes/ImageResizerTest.php index 2a8187a70..f2ffbddfd 100644 --- a/tests/unit/system/classes/ImageResizerTest.php +++ b/tests/unit/system/classes/ImageResizerTest.php @@ -5,14 +5,26 @@ use System\Classes\MediaLibrary; use System\Models\File as FileModel; use Cms\Classes\Controller as CmsController; use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; +use October\Rain\Exception\SystemException; class ImageResizerTest extends PluginTestCase { use ArraySubsetAsserts; + public function setUp(): void + { + parent::setUp(); + + Event::flush('cms.theme.getActiveTheme'); + Event::listen('cms.theme.getActiveTheme', function () { + return 'test'; + }); + } + public function tearDown(): void { $this->removeMedia(); + ImageResizer::flushAvailableSources(); parent::tearDown(); } @@ -134,30 +146,68 @@ class ImageResizerTest extends PluginTestCase ); $this->assertEquals('png', $imageResizer->getConfig()['options']['extension']); - // Still to test - FileModel URL and FileModel instance + // Path of a FileModel instance + $fileModel = new FileModel(); + $fileModel->fromFile(base_path('tests/fixtures/plugins/database/tester/assets/images/avatar.png')); + $fileModel->save(); + + $imageResizer = new ImageResizer( + FileModel::first()->getPath(), + 100, + 100, + ); + $this->assertEquals('png', $imageResizer->getConfig()['options']['extension']); + + // FileModel instance itself + $imageResizer = new ImageResizer( + $fileModel, + 100, + 100, + ); + $this->assertEquals('png', $imageResizer->getConfig()['options']['extension']); + + // Remove FileModel instance + $fileModel->delete(); } - public function testInvalidInput() + public function testInvalidInputPath() { - $this->markTestIncomplete(); + $this->expectException(SystemException::class); + $this->expectExceptionMessageMatches('/^Unable to process the provided image/'); - $providedPath = '/plugins/october/demo/assets/NOTPRESENT.png'; + $imageResizer = new ImageResizer( + '/plugins/database/tester/assets/images/MISSING.png', + 100, + 100, + ); + } + + public function testInvalidInputFileModel() + { + $this->expectException(SystemException::class); + $this->expectExceptionMessageMatches('/^Unable to process the provided image/'); + + $imageResizer = new ImageResizer( + FileModel::first(), + 100, + 100, + ); } protected function setUpStorage() { $this->app->useStoragePath(base_path('storage/temp')); - config(['filesystems.disks.test_local' => [ + Config::set('filesystems.disks.test_local', [ 'driver' => 'local', 'root' => storage_path('app'), - ]]); + ]); - config(['cms.storage.media' => [ + Config::set('cms.storage.media', [ 'disk' => 'test_local', 'folder' => 'media', 'path' => '/storage/temp/app/media', - ]]); + ]); } protected function copyMedia()