0e12c97c616e462d33ea93e71542e1f15829a819
[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' => null]],
55 [['src' => '']],
56 [['src' => 'something', 'image' => 'something']],
57 ];
58 }
59
60 /**
61 * @test
62 * @dataProvider getInvalidArguments
63 * @param array $arguments
64 */
65 public function renderMethodThrowsExceptionOnInvalidArguments(array $arguments)
66 {
67 $this->setArgumentsUnderTest($this->viewHelper, $arguments);
68
69 $this->expectException(\TYPO3Fluid\Fluid\Core\ViewHelper\Exception::class);
70 $this->expectExceptionCode(1382284106);
71
72 $this->viewHelper->render();
73 }
74
75 /**
76 * @return array
77 */
78 public function getRenderMethodTestValues()
79 {
80 return [
81 [
82 [
83 'src' => 'test',
84 'width' => 100,
85 'height' => 200,
86 'minWidth' => 300,
87 'maxWidth' => 400,
88 'minHeight' => 500,
89 'maxHeight' => 600,
90 'crop' => false
91 ],
92 [
93 'src' => 'test.png',
94 'width' => '100',
95 'height' => '200',
96 'alt' => 'alternative',
97 'title' => 'title'
98 ]
99 ],
100 [
101 [
102 'src' => 'test',
103 'width' => 100,
104 'height' => 200,
105 'minWidth' => 300,
106 'maxWidth' => 400,
107 'minHeight' => 500,
108 'maxHeight' => 600,
109 'crop' => null
110 ],
111 [
112 'src' => 'test.png',
113 'width' => '100',
114 'height' => '200',
115 'alt' => 'alternative',
116 'title' => 'title'
117 ]
118 ],
119 ];
120 }
121
122 /**
123 * @test
124 * @dataProvider getRenderMethodTestValues
125 * @param array $arguments
126 * @param array $expected
127 */
128 public function renderMethodCreatesExpectedTag(array $arguments, array $expected)
129 {
130 $this->setArgumentsUnderTest($this->viewHelper, $arguments);
131
132 $image = $this->getMockBuilder(FileReference::class)
133 ->setMethods(['getProperty'])
134 ->disableOriginalConstructor()
135 ->getMock();
136 $image->expects($this->any())->method('getProperty')->willReturnMap([
137 ['width', $arguments['width']],
138 ['height', $arguments['height']],
139 ['alternative', 'alternative'],
140 ['title', 'title'],
141 ['crop', 'crop']
142 ]);
143 $originalFile = $this->getMockBuilder(File::class)
144 ->disableOriginalConstructor()
145 ->getMock();
146 $originalFile->expects($this->any())->method('getProperties')->willReturn([]);
147
148 $processedFile = $this->getMockBuilder(ProcessedFile::class)
149 ->disableOriginalConstructor()
150 ->getMock();
151
152 $processedFile->expects($this->any())->method('getProperty')->willReturnMap([
153 ['width', $arguments['width']],
154 ['height', $arguments['height']],
155 ]);
156
157 $this->inject($image, 'originalFile', $originalFile);
158 $this->inject($image, 'propertiesOfFileReference', []);
159 $imageService = $this->getMockBuilder(ImageService::class)
160 ->setMethods(['getImage', 'applyProcessingInstructions', 'getImageUri'])
161 ->getMock();
162 $imageService->expects($this->once())->method('getImage')->willReturn($image);
163 $imageService->expects($this->once())->method('applyProcessingInstructions')->with($image, $this->anything())->willReturn($processedFile);
164 $imageService->expects($this->once())->method('getImageUri')->with($processedFile)->willReturn('test.png');
165
166 $this->inject($this->viewHelper, 'imageService', $imageService);
167
168 $tagBuilder = $this->getMockBuilder(TagBuilder::class)
169 ->setMethods(['addAttribute', 'render'])
170 ->getMock();
171 $index = -1;
172 foreach ($expected as $expectedAttribute => $expectedValue) {
173 $tagBuilder->expects($this->at(++ $index))->method('addAttribute')->with($expectedAttribute, $expectedValue);
174 }
175 $tagBuilder->expects($this->once())->method('render');
176 $this->inject($this->viewHelper, 'tag', $tagBuilder);
177
178 $this->viewHelper->render();
179 }
180 }