diff options
Diffstat (limited to 'tests/lib/Preview/GeneratorTest.php')
-rw-r--r-- | tests/lib/Preview/GeneratorTest.php | 197 |
1 files changed, 67 insertions, 130 deletions
diff --git a/tests/lib/Preview/GeneratorTest.php b/tests/lib/Preview/GeneratorTest.php index 43f5c1e0d36..edf5418da6e 100644 --- a/tests/lib/Preview/GeneratorTest.php +++ b/tests/lib/Preview/GeneratorTest.php @@ -1,61 +1,48 @@ <?php + /** - * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Preview; use OC\Preview\Generator; use OC\Preview\GeneratorHelper; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\File; use OCP\Files\IAppData; use OCP\Files\NotFoundException; use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFolder; use OCP\IConfig; +use OCP\IImage; use OCP\IPreview; +use OCP\Preview\BeforePreviewFetchedEvent; use OCP\Preview\IProviderV2; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; +use Psr\Log\LoggerInterface; class GeneratorTest extends \Test\TestCase { - - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IConfig&\PHPUnit\Framework\MockObject\MockObject */ private $config; - /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IPreview&\PHPUnit\Framework\MockObject\MockObject */ private $previewManager; - /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IAppData&\PHPUnit\Framework\MockObject\MockObject */ private $appData; - /** @var GeneratorHelper|\PHPUnit\Framework\MockObject\MockObject */ + /** @var GeneratorHelper&\PHPUnit\Framework\MockObject\MockObject */ private $helper; - /** @var EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IEventDispatcher&\PHPUnit\Framework\MockObject\MockObject */ private $eventDispatcher; /** @var Generator */ private $generator; + private LoggerInterface&\PHPUnit\Framework\MockObject\MockObject $logger; + protected function setUp(): void { parent::setUp(); @@ -63,18 +50,20 @@ class GeneratorTest extends \Test\TestCase { $this->previewManager = $this->createMock(IPreview::class); $this->appData = $this->createMock(IAppData::class); $this->helper = $this->createMock(GeneratorHelper::class); - $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); + $this->logger = $this->createMock(LoggerInterface::class); $this->generator = new Generator( $this->config, $this->previewManager, $this->appData, $this->helper, - $this->eventDispatcher + $this->eventDispatcher, + $this->logger, ); } - public function testGetCachedPreview() { + public function testGetCachedPreview(): void { $file = $this->createMock(File::class); $file->method('isReadable') ->willReturn(true); @@ -95,34 +84,26 @@ class GeneratorTest extends \Test\TestCase { $maxPreview = $this->createMock(ISimpleFile::class); $maxPreview->method('getName') ->willReturn('1000-1000-max.png'); + $maxPreview->method('getSize')->willReturn(1000); $maxPreview->method('getMimeType') ->willReturn('image/png'); - $previewFolder->method('getDirectoryListing') - ->willReturn([$maxPreview]); - $previewFile = $this->createMock(ISimpleFile::class); + $previewFile->method('getSize')->willReturn(1000); + $previewFile->method('getName')->willReturn('256-256.png'); - $previewFolder->method('getFile') - ->with($this->equalTo('256-256.png')) - ->willReturn($previewFile); + $previewFolder->method('getDirectoryListing') + ->willReturn([$maxPreview, $previewFile]); $this->eventDispatcher->expects($this->once()) - ->method('dispatch') - ->with( - $this->equalTo(IPreview::EVENT), - $this->callback(function (GenericEvent $event) use ($file) { - return $event->getSubject() === $file && - $event->getArgument('width') === 100 && - $event->getArgument('height') === 100; - }) - ); + ->method('dispatchTyped') + ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null)); $result = $this->generator->getPreview($file, 100, 100); $this->assertSame($previewFile, $result); } - public function testGetNewPreview() { + public function testGetNewPreview(): void { $file = $this->createMock(File::class); $file->method('isReadable') ->willReturn(true); @@ -145,8 +126,13 @@ class GeneratorTest extends \Test\TestCase { ->willReturn($previewFolder); $this->config->method('getSystemValue') - ->willReturnCallback(function ($key, $defult) { - return $defult; + ->willReturnCallback(function ($key, $default) { + return $default; + }); + + $this->config->method('getSystemValueInt') + ->willReturnCallback(function ($key, $default) { + return $default; }); $invalidProvider = $this->createMock(IProviderV2::class); @@ -182,7 +168,7 @@ class GeneratorTest extends \Test\TestCase { $this->fail('Unexpected provider requested'); }); - $image = $this->createMock(\OC_Image::class); + $image = $this->createMock(IImage::class); $image->method('width')->willReturn(2048); $image->method('height')->willReturn(2048); $image->method('valid')->willReturn(true); @@ -203,24 +189,18 @@ class GeneratorTest extends \Test\TestCase { $maxPreview = $this->createMock(ISimpleFile::class); $maxPreview->method('getName')->willReturn('2048-2048-max.png'); $maxPreview->method('getMimeType')->willReturn('image/png'); + $maxPreview->method('getSize')->willReturn(1000); $previewFile = $this->createMock(ISimpleFile::class); + $previewFile->method('getSize')->willReturn(1000); $previewFolder->method('getDirectoryListing') ->willReturn([]); $previewFolder->method('newFile') - ->willReturnCallback(function ($filename) use ($maxPreview, $previewFile) { - if ($filename === '2048-2048-max.png') { - return $maxPreview; - } elseif ($filename === '256-256.png') { - return $previewFile; - } - $this->fail('Unexpected file'); - }); - - $maxPreview->expects($this->once()) - ->method('putContent') - ->with($this->equalTo('my data')); + ->willReturnMap([ + ['2048-2048-max.png', 'my data', $maxPreview], + ['256-256.png', 'my resized data', $previewFile], + ]); $previewFolder->method('getFile') ->with($this->equalTo('256-256.png')) @@ -231,26 +211,15 @@ class GeneratorTest extends \Test\TestCase { ->with($this->equalTo($maxPreview)) ->willReturn($image); - $previewFile->expects($this->once()) - ->method('putContent') - ->with('my resized data'); - $this->eventDispatcher->expects($this->once()) - ->method('dispatch') - ->with( - $this->equalTo(IPreview::EVENT), - $this->callback(function (GenericEvent $event) use ($file) { - return $event->getSubject() === $file && - $event->getArgument('width') === 100 && - $event->getArgument('height') === 100; - }) - ); + ->method('dispatchTyped') + ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null)); $result = $this->generator->getPreview($file, 100, 100); $this->assertSame($previewFile, $result); } - public function testInvalidMimeType() { + public function testInvalidMimeType(): void { $this->expectException(NotFoundException::class); $file = $this->createMock(File::class); @@ -282,22 +251,13 @@ class GeneratorTest extends \Test\TestCase { ->willThrowException(new NotFoundException()); $this->eventDispatcher->expects($this->once()) - ->method('dispatch') - ->with( - $this->equalTo(IPreview::EVENT), - $this->callback(function (GenericEvent $event) use ($file) { - return $event->getSubject() === $file && - $event->getArgument('width') === 1024 && - $event->getArgument('height') === 512 && - $event->getArgument('crop') === true && - $event->getArgument('mode') === IPreview::MODE_COVER; - }) - ); + ->method('dispatchTyped') + ->with(new BeforePreviewFetchedEvent($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType')); $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType'); } - public function testReturnCachedPreviewsWithoutCheckingSupportedMimetype() { + public function testReturnCachedPreviewsWithoutCheckingSupportedMimetype(): void { $file = $this->createMock(File::class); $file->method('isReadable') ->willReturn(true); @@ -313,38 +273,29 @@ class GeneratorTest extends \Test\TestCase { $maxPreview = $this->createMock(ISimpleFile::class); $maxPreview->method('getName') ->willReturn('2048-2048-max.png'); + $maxPreview->method('getSize')->willReturn(1000); $maxPreview->method('getMimeType') ->willReturn('image/png'); - $previewFolder->method('getDirectoryListing') - ->willReturn([$maxPreview]); - $preview = $this->createMock(ISimpleFile::class); - $previewFolder->method('getFile') - ->with($this->equalTo('1024-512-crop.png')) - ->willReturn($preview); + $preview->method('getSize')->willReturn(1000); + $preview->method('getName')->willReturn('1024-512-crop.png'); + + $previewFolder->method('getDirectoryListing') + ->willReturn([$maxPreview, $preview]); $this->previewManager->expects($this->never()) ->method('isMimeSupported'); $this->eventDispatcher->expects($this->once()) - ->method('dispatch') - ->with( - $this->equalTo(IPreview::EVENT), - $this->callback(function (GenericEvent $event) use ($file) { - return $event->getSubject() === $file && - $event->getArgument('width') === 1024 && - $event->getArgument('height') === 512 && - $event->getArgument('crop') === true && - $event->getArgument('mode') === IPreview::MODE_COVER; - }) - ); + ->method('dispatchTyped') + ->with(new BeforePreviewFetchedEvent($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType')); $result = $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType'); $this->assertSame($preview, $result); } - public function testNoProvider() { + public function testNoProvider(): void { $file = $this->createMock(File::class); $file->method('isReadable') ->willReturn(true); @@ -365,22 +316,15 @@ class GeneratorTest extends \Test\TestCase { ->willReturn([]); $this->eventDispatcher->expects($this->once()) - ->method('dispatch') - ->with( - $this->equalTo(IPreview::EVENT), - $this->callback(function (GenericEvent $event) use ($file) { - return $event->getSubject() === $file && - $event->getArgument('width') === 100 && - $event->getArgument('height') === 100; - }) - ); + ->method('dispatchTyped') + ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null)); $this->expectException(NotFoundException::class); $this->generator->getPreview($file, 100, 100); } private function getMockImage($width, $height, $data = null) { - $image = $this->createMock(\OC_Image::class); + $image = $this->createMock(IImage::class); $image->method('height')->willReturn($width); $image->method('width')->willReturn($height); $image->method('valid')->willReturn(true); @@ -400,7 +344,7 @@ class GeneratorTest extends \Test\TestCase { return $image; } - public function dataSize() { + public static function dataSize(): array { return [ [1024, 2048, 512, 512, false, IPreview::MODE_FILL, 256, 512], [1024, 2048, 512, 512, false, IPreview::MODE_COVER, 512, 1024], @@ -437,7 +381,6 @@ class GeneratorTest extends \Test\TestCase { } /** - * @dataProvider dataSize * * @param int $maxX * @param int $maxY @@ -448,7 +391,8 @@ class GeneratorTest extends \Test\TestCase { * @param int $expectedX * @param int $expectedY */ - public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expectedX, $expectedY) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataSize')] + public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expectedX, $expectedY): void { $file = $this->createMock(File::class); $file->method('isReadable') ->willReturn(true); @@ -471,6 +415,7 @@ class GeneratorTest extends \Test\TestCase { ->willReturn($maxX . '-' . $maxY . '-max.png'); $maxPreview->method('getMimeType') ->willReturn('image/png'); + $maxPreview->method('getSize')->willReturn(1000); $previewFolder->method('getDirectoryListing') ->willReturn([$maxPreview]); @@ -490,22 +435,14 @@ class GeneratorTest extends \Test\TestCase { ->willReturn($image); $preview = $this->createMock(ISimpleFile::class); + $preview->method('getSize')->willReturn(1000); $previewFolder->method('newFile') ->with($this->equalTo($filename)) ->willReturn($preview); $this->eventDispatcher->expects($this->once()) - ->method('dispatch') - ->with( - $this->equalTo(IPreview::EVENT), - $this->callback(function (GenericEvent $event) use ($file, $reqX, $reqY, $crop, $mode) { - return $event->getSubject() === $file && - $event->getArgument('width') === $reqX && - $event->getArgument('height') === $reqY && - $event->getArgument('crop') === $crop && - $event->getArgument('mode') === $mode; - }) - ); + ->method('dispatchTyped') + ->with(new BeforePreviewFetchedEvent($file, $reqX, $reqY, $crop, $mode, null)); $result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode); if ($expectedX === $maxX && $expectedY === $maxY) { @@ -515,7 +452,7 @@ class GeneratorTest extends \Test\TestCase { } } - public function testUnreadbleFile() { + public function testUnreadbleFile(): void { $file = $this->createMock(File::class); $file->method('isReadable') ->willReturn(false); |