diff options
author | Olivier Paroz <github@oparoz.com> | 2015-08-25 03:43:50 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-08-25 03:43:50 +0300 |
commit | 182938dee9444c7744a6cbf0e63b22b96da142c3 (patch) | |
tree | f348a38a795615c9c11d82354084d515891844b9 /tests/unit | |
parent | be0c66adc70fbf372acfc08cced7e59a8861a351 (diff) |
Some tests for the Preview class
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/preview/PreviewTest.php | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/tests/unit/preview/PreviewTest.php b/tests/unit/preview/PreviewTest.php new file mode 100644 index 00000000..4d51fc34 --- /dev/null +++ b/tests/unit/preview/PreviewTest.php @@ -0,0 +1,192 @@ +<?php +/** + * ownCloud - gallery + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Olivier Paroz <owncloud@interfasys.ch> + * + * @copyright Olivier Paroz 2015 + */ + +namespace OCA\Gallery\Preview; + +use OCP\IConfig; +use OCP\IPreview; +use OCP\ILogger; + +use OCA\Gallery\AppInfo\Application; + + +/** + * Class PreviewTest + * + * @package OCA\Gallery\Environment + */ +class PreviewTest extends \Test\TestCase { + + /** @var IConfig */ + private $config; + /** @var IPreview */ + private $corePreviewManager; + /** @var ILogger */ + private $logger; + /** @var Preview */ + private $previewManager; + + /** + * Test set up + */ + public function setUp() { + parent::setUp(); + + $this->config = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->corePreviewManager = $this->getMockBuilder('\OCP\IPreview') + ->disableOriginalConstructor() + ->getMock(); + $this->logger = $this->getMockBuilder('\OCP\ILogger') + ->disableOriginalConstructor() + ->getMock(); + $this->previewManager = new Preview( + $this->config, + $this->corePreviewManager, + $this->logger + ); + } + + public function providesPreviewValidatorData() { + $maxWidth = 400; + $maxHeight = 200; + $maxHeightSquare = 400; + $correction = 59; + + return [ + [ + $maxWidth, $maxHeightSquare, $maxWidth, $maxHeightSquare, true, $maxWidth, + $maxHeightSquare + ], + [ + $maxWidth, $maxHeightSquare, $maxWidth + $correction, + $maxHeightSquare + $correction, true, + $maxWidth, + $maxHeightSquare + ], + [ + $maxWidth, $maxHeightSquare, $maxWidth + $correction, + $maxHeightSquare - $correction, true, + $maxWidth, + $maxHeightSquare + ], + [ + $maxWidth, $maxHeightSquare, $maxWidth - $correction, + $maxHeightSquare + $correction, true, + $maxWidth, + $maxHeightSquare + ], + [ + $maxWidth, $maxHeight, $maxWidth, $maxHeight, false, $maxWidth, + $maxHeight + ], + [ + $maxWidth, $maxHeight, $maxWidth + $correction, $maxHeight + $correction, false, + floor($maxHeight * (($maxWidth + $correction) / ($maxHeight + $correction))), + $maxHeight + ], + [ + $maxWidth, $maxHeight, $maxWidth + $correction, $maxHeight - $correction, false, + $maxWidth, + floor($maxWidth / (($maxWidth + $correction) / ($maxHeight - $correction))) + ], + [ + $maxWidth, $maxHeight, $maxWidth - $correction, $maxHeight + $correction, false, + floor($maxHeight * (($maxWidth - $correction) / ($maxHeight + $correction))), + $maxHeight + ], + ]; + } + + /** + * @dataProvider providesPreviewValidatorData + */ + public function testPreviewValidator( + $maxWidth, $maxHeight, $width, $height, $square, $expectedWidth, $expectedHeight + ) { + self::invokePrivate($this->previewManager, 'userId', ['tester']); + $app = new Application(); + $dataDir = $app->getContainer() + ->getServer() + ->getConfig() + ->getSystemValue('datadirectory'); + self::invokePrivate($this->previewManager, 'dataDir', [$dataDir]); + $fileId = 12345; + $file = $this->mockFile($fileId); + self::invokePrivate($this->previewManager, 'file', [$file]); + self::invokePrivate($this->previewManager, 'dims', [[$maxWidth, $maxHeight]]); + $preview = $this->mockGetPreview($fileId, $width, $height); + self::invokePrivate($this->previewManager, 'preview', [$preview]); + + $response = $this->previewManager->previewValidator($square); + + $this->assertEquals( + [$expectedWidth, $expectedHeight], [$response->width(), $response->height()] + ); + } + + public function testGetPreviewFromCoreWithBrokenSystem() { + $keepAspect = true; // Doesn't matter + $exception = new \Exception('Encryption ate your file'); + $preview = $this->mockGetPreviewWithBrokenSetup($exception); + self::invokePrivate($this->previewManager, 'preview', [$preview]); + + $response = self::invokePrivate($this->previewManager, 'getPreviewFromCore', [$keepAspect]); + + $this->assertNull($response); + } + + protected function mockFile($fileId) { + $file = $this->getMockBuilder('OCP\Files\File') + ->disableOriginalConstructor() + ->getMock(); + $file->method('getId') + ->willReturn($fileId); + + return $file; + } + + private function mockGetPreview($fileId, $width, $height) { + $image = new \OC_Image(file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.jpg')); + $image->preciseResize($width, $height); + + $preview = $this->getMockBuilder('\OC\Preview') + ->disableOriginalConstructor() + ->getMock(); + $preview->method('getPreview') + ->willReturn($image); + $preview->method('isCached') + ->willReturn($fileId); + + return $preview; + } + + private function mockGetPreviewWithBrokenSetup($exception) { + $preview = $this->getMockBuilder('\OC\Preview') + ->disableOriginalConstructor() + ->getMock(); + $preview->method('setMaxX') + ->willReturn(null); + $preview->method('setMaxY') + ->willReturn(null); + $preview->method('setScalingUp') + ->willReturn(null); + $preview->method('setKeepAspect') + ->willReturn(null); + $preview->method('getPreview') + ->willThrowException($exception); + + return $preview; + } + +} |