aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Preview/GeneratorTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/Preview/GeneratorTest.php')
-rw-r--r--tests/lib/Preview/GeneratorTest.php192
1 files changed, 43 insertions, 149 deletions
diff --git a/tests/lib/Preview/GeneratorTest.php b/tests/lib/Preview/GeneratorTest.php
index 1f6f43dce1e..edf5418da6e 100644
--- a/tests/lib/Preview/GeneratorTest.php
+++ b/tests/lib/Preview/GeneratorTest.php
@@ -1,24 +1,8 @@
<?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;
@@ -32,34 +16,33 @@ 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 IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
+ /** @var IEventDispatcher&\PHPUnit\Framework\MockObject\MockObject */
private $eventDispatcher;
- /** @var EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject */
- private $legacyEventDispatcher;
-
/** @var Generator */
private $generator;
+ private LoggerInterface&\PHPUnit\Framework\MockObject\MockObject $logger;
+
protected function setUp(): void {
parent::setUp();
@@ -68,19 +51,19 @@ class GeneratorTest extends \Test\TestCase {
$this->appData = $this->createMock(IAppData::class);
$this->helper = $this->createMock(GeneratorHelper::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
- $this->legacyEventDispatcher = $this->createMock(EventDispatcherInterface::class);
+ $this->logger = $this->createMock(LoggerInterface::class);
$this->generator = new Generator(
$this->config,
$this->previewManager,
$this->appData,
$this->helper,
- $this->legacyEventDispatcher,
- $this->eventDispatcher
+ $this->eventDispatcher,
+ $this->logger,
);
}
- public function testGetCachedPreview() {
+ public function testGetCachedPreview(): void {
$file = $this->createMock(File::class);
$file->method('isReadable')
->willReturn(true);
@@ -105,36 +88,22 @@ class GeneratorTest extends \Test\TestCase {
$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);
-
- $this->legacyEventDispatcher->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;
- })
- );
+ $previewFolder->method('getDirectoryListing')
+ ->willReturn([$maxPreview, $previewFile]);
$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
- ->with(new BeforePreviewFetchedEvent($file));
+ ->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);
@@ -199,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);
@@ -228,18 +197,10 @@ class GeneratorTest extends \Test\TestCase {
$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'))
@@ -250,30 +211,15 @@ class GeneratorTest extends \Test\TestCase {
->with($this->equalTo($maxPreview))
->willReturn($image);
- $previewFile->expects($this->once())
- ->method('putContent')
- ->with('my resized data');
-
- $this->legacyEventDispatcher->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;
- })
- );
-
$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
- ->with(new BeforePreviewFetchedEvent($file));
+ ->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);
@@ -304,27 +250,14 @@ class GeneratorTest extends \Test\TestCase {
->with($this->equalTo('1024-512-crop.png'))
->willThrowException(new NotFoundException());
- $this->legacyEventDispatcher->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;
- })
- );
-
$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
- ->with(new BeforePreviewFetchedEvent($file));
+ ->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);
@@ -344,40 +277,25 @@ class GeneratorTest extends \Test\TestCase {
$maxPreview->method('getMimeType')
->willReturn('image/png');
- $previewFolder->method('getDirectoryListing')
- ->willReturn([$maxPreview]);
-
$preview = $this->createMock(ISimpleFile::class);
$preview->method('getSize')->willReturn(1000);
- $previewFolder->method('getFile')
- ->with($this->equalTo('1024-512-crop.png'))
- ->willReturn($preview);
+ $preview->method('getName')->willReturn('1024-512-crop.png');
+
+ $previewFolder->method('getDirectoryListing')
+ ->willReturn([$maxPreview, $preview]);
$this->previewManager->expects($this->never())
->method('isMimeSupported');
- $this->legacyEventDispatcher->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;
- })
- );
-
$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
- ->with(new BeforePreviewFetchedEvent($file));
+ ->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);
@@ -397,27 +315,16 @@ class GeneratorTest extends \Test\TestCase {
$this->previewManager->method('getProviders')
->willReturn([]);
- $this->legacyEventDispatcher->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;
- })
- );
-
$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
- ->with(new BeforePreviewFetchedEvent($file));
+ ->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);
@@ -437,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],
@@ -474,7 +381,6 @@ class GeneratorTest extends \Test\TestCase {
}
/**
- * @dataProvider dataSize
*
* @param int $maxX
* @param int $maxY
@@ -485,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);
@@ -533,22 +440,9 @@ class GeneratorTest extends \Test\TestCase {
->with($this->equalTo($filename))
->willReturn($preview);
- $this->legacyEventDispatcher->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;
- })
- );
-
$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
- ->with(new BeforePreviewFetchedEvent($file));
+ ->with(new BeforePreviewFetchedEvent($file, $reqX, $reqY, $crop, $mode, null));
$result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode);
if ($expectedX === $maxX && $expectedY === $maxY) {
@@ -558,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);