[TASK] Add unit tests for ImageInfo
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Type / File / ImageInfoTest.php
1 <?php
2
3 namespace TYPO3\CMS\Core\Tests\Unit\Type\File;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use org\bovigo\vfs\vfsStream;
19 use TYPO3\CMS\Core\Imaging\GraphicalFunctions;
20 use TYPO3\CMS\Core\Type\File\ImageInfo;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Test case
25 */
26 class ImageInfoTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
27 {
28
29 /**
30 * @test
31 */
32 public function classImageInfoCanBeInstantiated()
33 {
34 $className = \TYPO3\CMS\Core\Type\File\ImageInfo::class;
35 $classInstance = new \TYPO3\CMS\Core\Type\File\ImageInfo('FooFileName');
36 $this->assertInstanceOf($className, $classInstance);
37 }
38
39 /**
40 * @test
41 */
42 public function doesNotBreakOnImageInfoWithInvalidSvg()
43 {
44 $root = vfsStream::setup('root');
45 $testFile = 'test.svg';
46 vfsStream::newFile($testFile)->at($root)->setContent('Invalid XML.');
47
48 $GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType'] = [
49 'svg' => 'image/svg+xml',
50 'youtube' => 'video/youtube',
51 'vimeo' => 'video/vimeo',
52 ];
53
54 $graphicalFunctionsProphecy = $this->prophesize(GraphicalFunctions::class);
55 $graphicalFunctionsProphecy->init()->shouldBeCalled();
56 $graphicalFunctionsProphecy->imageMagickIdentify($root->url() . '/' . $testFile)->willReturn(null);
57 GeneralUtility::addInstance(GraphicalFunctions::class, $graphicalFunctionsProphecy->reveal());
58
59 $imageInfo = new ImageInfo($root->url() . '/' . $testFile);
60
61 $this->assertEquals(0, $imageInfo->getWidth());
62 $this->assertEquals(0, $imageInfo->getHeight());
63 }
64
65 /**
66 * @return array
67 */
68 public function canDetectImageSizesDataProvider(): array
69 {
70 return [
71 'svg' => ['test.svg', 80, 80],
72 'jpg' => ['test.jpg', 600, 388],
73 'png' => ['test.png', 600, 388],
74 ];
75 }
76
77 /**
78 * @test
79 * @dataProvider canDetectImageSizesDataProvider
80 */
81 public function canDetectImageSizes($file, $width, $height)
82 {
83 $imageInfo = new ImageInfo(__DIR__ . '/../Fixture/' . $file);
84
85 $this->assertEquals($width, $imageInfo->getWidth());
86 $this->assertEquals($height, $imageInfo->getHeight());
87 }
88 }