a1384dd515458ef728b35d2ccff04c6e56d61aa3
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / ImageViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Resource\File;
18 use TYPO3\CMS\Core\Resource\FileReference;
19 use TYPO3\CMS\Core\Resource\ProcessedFile;
20 use TYPO3\CMS\Extbase\Service\ImageService;
21 use TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper;
22 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
24
25 /**
26 * Test case
27 */
28 class ImageViewHelperTest extends ViewHelperBaseTestcase
29 {
30 /**
31 * @var bool Reset singletons created by subject
32 */
33 protected $resetSingletonInstances = true;
34
35 /**
36 * @var ImageViewHelper
37 */
38 protected $viewHelper;
39
40 protected function setUp()
41 {
42 parent::setUp();
43 $this->viewHelper = new ImageViewHelper();
44 $this->injectDependenciesIntoViewHelper($this->viewHelper);
45 }
46
47 /**
48 * @return array
49 */
50 public function getInvalidArguments()
51 {
52 return [
53 [['image' => null]],
54 [['src' => '']],
55 [['src' => 'something', 'image' => 'something']],
56 ];
57 }
58
59 /**
60 * @test
61 * @dataProvider getInvalidArguments
62 * @param array $arguments
63 */
64 public function renderMethodThrowsExceptionOnInvalidArguments(array $arguments)
65 {
66 $this->setArgumentsUnderTest($this->viewHelper, $arguments);
67
68 $this->expectException(\TYPO3Fluid\Fluid\Core\ViewHelper\Exception::class);
69 $this->expectExceptionCode(1382284106);
70
71 $this->viewHelper->render();
72 }
73
74 /**
75 * @return array
76 */
77 public function getRenderMethodTestValues()
78 {
79 return [
80 [
81 [
82 'src' => 'test',
83 'width' => 100,
84 'height' => 200,
85 'minWidth' => 300,
86 'maxWidth' => 400,
87 'minHeight' => 500,
88 'maxHeight' => 600,
89 'crop' => false
90 ],
91 [
92 'src' => 'test.png',
93 'width' => '100',
94 'height' => '200',
95 'alt' => 'alternative',
96 'title' => 'title'
97 ]
98 ],
99 [
100 [
101 'src' => 'test',
102 'width' => 100,
103 'height' => 200,
104 'minWidth' => 300,
105 'maxWidth' => 400,
106 'minHeight' => 500,
107 'maxHeight' => 600,
108 'crop' => null
109 ],
110 [
111 'src' => 'test.png',
112 'width' => '100',
113 'height' => '200',
114 'alt' => 'alternative',
115 'title' => 'title'
116 ]
117 ],
118 ];
119 }
120
121 /**
122 * @test
123 * @dataProvider getRenderMethodTestValues
124 * @param array $arguments
125 * @param array $expected
126 */
127 public function renderMethodCreatesExpectedTag(array $arguments, array $expected)
128 {
129 $this->setArgumentsUnderTest($this->viewHelper, $arguments);
130
131 $image = $this->getMockBuilder(FileReference::class)
132 ->setMethods(['getProperty'])
133 ->disableOriginalConstructor()
134 ->getMock();
135 $image->expects($this->any())->method('getProperty')->willReturnMap([
136 ['width', $arguments['width']],
137 ['height', $arguments['height']],
138 ['alternative', 'alternative'],
139 ['title', 'title'],
140 ['crop', 'crop']
141 ]);
142 $originalFile = $this->getMockBuilder(File::class)
143 ->disableOriginalConstructor()
144 ->getMock();
145 $originalFile->expects($this->any())->method('getProperties')->willReturn([]);
146
147 $processedFile = $this->getMockBuilder(ProcessedFile::class)
148 ->disableOriginalConstructor()
149 ->getMock();
150
151 $processedFile->expects($this->any())->method('getProperty')->willReturnMap([
152 ['width', $arguments['width']],
153 ['height', $arguments['height']],
154 ]);
155
156 $this->inject($image, 'originalFile', $originalFile);
157 $this->inject($image, 'propertiesOfFileReference', []);
158 $imageService = $this->getMockBuilder(ImageService::class)
159 ->setMethods(['getImage', 'applyProcessingInstructions', 'getImageUri'])
160 ->getMock();
161 $imageService->expects($this->once())->method('getImage')->willReturn($image);
162 $imageService->expects($this->once())->method('applyProcessingInstructions')->with($image, $this->anything())->willReturn($processedFile);
163 $imageService->expects($this->once())->method('getImageUri')->with($processedFile)->willReturn('test.png');
164
165 $this->inject($this->viewHelper, 'imageService', $imageService);
166
167 $tagBuilder = $this->getMockBuilder(TagBuilder::class)
168 ->setMethods(['addAttribute', 'render'])
169 ->getMock();
170 $index = -1;
171 foreach ($expected as $expectedAttribute => $expectedValue) {
172 $tagBuilder->expects($this->at(++ $index))->method('addAttribute')->with($expectedAttribute, $expectedValue);
173 }
174 $tagBuilder->expects($this->once())->method('render');
175 $this->inject($this->viewHelper, 'tag', $tagBuilder);
176
177 $this->viewHelper->render();
178 }
179 }