[!!!][TASK] Extract testing framework for TYPO3
[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 use TYPO3\CMS\Core\Resource\FileReference;
17 use TYPO3\CMS\Extbase\Service\ImageService;
18 use TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
20
21 /**
22 * Test case
23 */
24 class ImageViewHelperTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
25 {
26 /**
27 * @test
28 */
29 public function registersExpectedArgumentsInInitializeArgumentsMethod()
30 {
31 $mock = $this->getMockBuilder(ImageViewHelper::class)
32 ->setMethods(['registerUniversalTagAttributes', 'registerTagAttribute'])
33 ->getMock();
34 $mock->expects($this->at(0))->method('registerUniversalTagAttributes');
35 $mock->expects($this->at(1))->method('registerTagAttribute')->with('alt', 'string', $this->anything(), false);
36 $mock->expects($this->at(2))->method('registerTagAttribute')->with('ismap', 'string', $this->anything(), false);
37 $mock->expects($this->at(3))->method('registerTagAttribute')->with('longdesc', 'string', $this->anything(), false);
38 $mock->expects($this->at(4))->method('registerTagAttribute')->with('usemap', 'string', $this->anything(), false);
39 $mock->initializeArguments();
40 }
41
42 /**
43 * @test
44 * @dataProvider getInvalidArguments
45 * @param array $arguments
46 */
47 public function renderMethodThrowsExceptionOnInvalidArguments(array $arguments)
48 {
49 $mock = $this->getMockBuilder(ImageViewHelper::class)
50 ->setMethods(['dummy'])
51 ->getMock();
52 $mock->setArguments($arguments);
53
54 $this->expectException(\TYPO3\CMS\Fluid\Core\ViewHelper\Exception::class);
55 $this->expectExceptionCode(1382284106);
56
57 $mock->render(
58 isset($arguments['src']) ? $arguments['src'] : null,
59 isset($arguments['width']) ? $arguments['width'] : null,
60 isset($arguments['height']) ? $arguments['height'] : null,
61 isset($arguments['minWidth']) ? $arguments['minWidth'] : null,
62 isset($arguments['minHeight']) ? $arguments['minHeight'] : null,
63 isset($arguments['maxWidth']) ? $arguments['maxWidth'] : null,
64 isset($arguments['maxHeight']) ? $arguments['maxHeight'] : null,
65 isset($arguments['treatIdAsReference']) ? $arguments['treatIdAsReference'] : null,
66 isset($arguments['image']) ? $arguments['image'] : null,
67 isset($arguments['crop']) ? $arguments['crop'] : null
68 );
69 }
70
71 /**
72 * @return array
73 */
74 public function getInvalidArguments()
75 {
76 return [
77 [['image' => null]],
78 [['src' => null]],
79 [['src' => 'something', 'image' => 'something']],
80 ];
81 }
82
83 /**
84 * @test
85 * @dataProvider getRenderMethodTestValues
86 * @param array $arguments
87 * @param array $expected
88 */
89 public function renderMethodCreatesExpectedTag(array $arguments, array $expected)
90 {
91 $image = $this->getMockBuilder(FileReference::class)
92 ->setMethods(['getProperty'])
93 ->disableOriginalConstructor()
94 ->getMock();
95 $image->expects($this->any())->method('getProperty')->willReturnMap([
96 ['width', $arguments['width']],
97 ['height', $arguments['height']],
98 ['alternative', 'alternative'],
99 ['title', 'title'],
100 ['crop', 'crop']
101 ]);
102 $imageService = $this->getMockBuilder(ImageService::class)
103 ->setMethods(['getImage', 'applyProcessingInstructions', 'getImageUri'])
104 ->getMock();
105 $imageService->expects($this->once())->method('getImage')->willReturn($image);
106 $imageService->expects($this->once())->method('applyProcessingInstructions')->with($image, $this->anything())->willReturn($image);
107 $imageService->expects($this->once())->method('getImageUri')->with($image)->willReturn('test.png');
108 $tagBuilder = $this->getMockBuilder(TagBuilder::class)
109 ->setMethods(['addAttribute', 'render'])
110 ->getMock();
111 $index = -1;
112 foreach ($expected as $expectedAttribute => $expectedValue) {
113 $tagBuilder->expects($this->at(++ $index))->method('addAttribute')->with($expectedAttribute, $expectedValue);
114 }
115 $tagBuilder->expects($this->once())->method('render');
116 $mock = $this->getAccessibleMock(ImageViewHelper::class, ['dummy'], [], '', false);
117 $mock->_set('imageService', $imageService);
118 $mock->_set('tag', $tagBuilder);
119 $mock->setArguments($arguments);
120 $mock->render(
121 isset($arguments['src']) ? $arguments['src'] : null,
122 isset($arguments['width']) ? $arguments['width'] : null,
123 isset($arguments['height']) ? $arguments['height'] : null,
124 isset($arguments['minWidth']) ? $arguments['minWidth'] : null,
125 isset($arguments['minHeight']) ? $arguments['minHeight'] : null,
126 isset($arguments['maxWidth']) ? $arguments['maxWidth'] : null,
127 isset($arguments['maxHeight']) ? $arguments['maxHeight'] : null,
128 isset($arguments['treatIdAsReference']) ? $arguments['treatIdAsReference'] : null,
129 isset($arguments['image']) ? $arguments['image'] : null,
130 isset($arguments['crop']) ? $arguments['crop'] : null
131 );
132 }
133
134 /**
135 * @return array
136 */
137 public function getRenderMethodTestValues()
138 {
139 return [
140 [
141 [
142 'src' => 'test',
143 'width' => 100,
144 'height' => 200,
145 'minWidth' => 300,
146 'maxWidth' => 400,
147 'minHeight' => 500,
148 'maxHeight' => 600,
149 'crop' => false
150 ],
151 [
152 'src' => 'test.png',
153 'width' => '100',
154 'height' => '200',
155 'alt' => 'alternative',
156 'title' => 'title'
157 ]
158 ],
159 [
160 [
161 'src' => 'test',
162 'width' => 100,
163 'height' => 200,
164 'minWidth' => 300,
165 'maxWidth' => 400,
166 'minHeight' => 500,
167 'maxHeight' => 600,
168 'crop' => null
169 ],
170 [
171 'src' => 'test.png',
172 'width' => '100',
173 'height' => '200',
174 'alt' => 'alternative',
175 'title' => 'title'
176 ]
177 ],
178 ];
179 }
180 }