‪TYPO3CMS  10.4
ImageViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
25 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
26 
30 class ‪ImageViewHelperTest extends ViewHelperBaseTestcase
31 {
35  protected ‪$resetSingletonInstances = true;
36 
40  protected ‪$viewHelper;
41 
42  protected function ‪setUp(): void
43  {
44  parent::setUp();
45  $this->viewHelper = new ‪ImageViewHelper();
46  $this->injectDependenciesIntoViewHelper($this->viewHelper);
47  }
48 
52  public function ‪getInvalidArguments()
53  {
54  return [
55  [['image' => null]],
56  [['src' => null]],
57  [['src' => '']],
58  [['src' => 'something', 'image' => 'something']],
59  ];
60  }
61 
67  public function ‪renderMethodThrowsExceptionOnInvalidArguments(array $arguments)
68  {
69  $this->setArgumentsUnderTest($this->viewHelper, $arguments);
70 
71  $this->expectException(Exception::class);
72  $this->expectExceptionCode(1382284106);
73 
74  $this->viewHelper->render();
75  }
76 
80  public function ‪getRenderMethodTestValues()
81  {
82  return [
83  [
84  [
85  'src' => 'test',
86  'width' => 100,
87  'height' => 200,
88  'minWidth' => 300,
89  'maxWidth' => 400,
90  'minHeight' => 500,
91  'maxHeight' => 600,
92  'crop' => false
93  ],
94  [
95  'src' => 'test.png',
96  'width' => '100',
97  'height' => '200',
98  'alt' => 'alternative',
99  'title' => 'title'
100  ]
101  ],
102  [
103  [
104  'src' => 'test',
105  'width' => 100,
106  'height' => 200,
107  'minWidth' => 300,
108  'maxWidth' => 400,
109  'minHeight' => 500,
110  'maxHeight' => 600,
111  'crop' => null
112  ],
113  [
114  'src' => 'test.png',
115  'width' => '100',
116  'height' => '200',
117  'alt' => 'alternative',
118  'title' => 'title'
119  ]
120  ],
121  [
122  [
123  'src' => 'test',
124  'width' => 100,
125  'height' => 200,
126  'minWidth' => 300,
127  'maxWidth' => 400,
128  'minHeight' => 500,
129  'maxHeight' => 600,
130  'crop' => null,
131  'fileExtension' => 'jpg'
132  ],
133  [
134  'src' => 'test.jpg',
135  'width' => '100',
136  'height' => '200',
137  'alt' => 'alternative',
138  'title' => 'title'
139  ]
140  ],
141  ];
142  }
143 
150  public function ‪renderMethodCreatesExpectedTag(array $arguments, array $expected)
151  {
152  $this->setArgumentsUnderTest($this->viewHelper, $arguments);
153 
154  $image = $this->getAccessibleMock(FileReference::class, ['getProperty', 'hasProperty'], [], '', false);
155  $image->expects(self::any())->method('hasProperty')->willReturn(true);
156  $image->expects(self::any())->method('getProperty')->willReturnMap([
157  ['width', $arguments['width']],
158  ['height', $arguments['height']],
159  ['alternative', 'alternative'],
160  ['title', 'title'],
161  ['crop', 'crop']
162  ]);
163  $originalFile = $this->getMockBuilder(File::class)
164  ->disableOriginalConstructor()
165  ->getMock();
166  $originalFile->expects(self::any())->method('getProperties')->willReturn([]);
167 
168  $processedFile = $this->getMockBuilder(ProcessedFile::class)
169  ->disableOriginalConstructor()
170  ->getMock();
171 
172  $processedFile->expects(self::any())->method('getProperty')->willReturnMap([
173  ['width', $arguments['width']],
174  ['height', $arguments['height']],
175  ]);
176 
177  $image->_set('originalFile', $originalFile);
178  $image->_set('propertiesOfFileReference', []);
179  $imageService = $this->createMock(ImageService::class);
180  $imageService->expects(self::once())->method('getImage')->willReturn($image);
181  $imageService->expects(self::once())->method('applyProcessingInstructions')->with($image, self::anything())->willReturn($processedFile);
182  $imageService->expects(self::once())->method('getImageUri')->with($processedFile)->willReturn($expected['src']);
183 
184  $this->viewHelper->injectImageService($imageService);
185 
186  $tagBuilder = $this->getMockBuilder(TagBuilder::class)
187  ->setMethods(['addAttribute', 'render'])
188  ->getMock();
189 
190  $tagBuilder->expects(self::exactly(count($expected)))
191  ->method('addAttribute')
192  ->withConsecutive(
193  ...$this->‪formatCallParameterExpectations($expected)
194  );
195  $tagBuilder->expects(self::once())->method('render');
196  $this->viewHelper->setTagBuilder($tagBuilder);
197 
198  $this->viewHelper->render();
199  }
200 
207  private function ‪formatCallParameterExpectations(array $expectations): array
208  {
209  $callParameters = [];
210  foreach ($expectations as $expectedAttribute => $expectedValue) {
211  $callParameters[] = [$expectedAttribute, $expectedValue];
212  }
213  return $callParameters;
214  }
215 
220  {
221  return [
222  [
223  [
224  'src' => 'test',
225  'width' => 100,
226  'height' => 200,
227  'minWidth' => 300,
228  'maxWidth' => 400,
229  'minHeight' => 500,
230  'maxHeight' => 600,
231  'crop' => false
232  ],
233  [
234  'src' => 'test.png',
235  'width' => '100',
236  'height' => '200',
237  'alt' => ''
238  ]
239  ],
240  [
241  [
242  'src' => 'test',
243  'width' => 100,
244  'height' => 200,
245  'minWidth' => 300,
246  'maxWidth' => 400,
247  'minHeight' => 500,
248  'maxHeight' => 600,
249  'crop' => null
250  ],
251  [
252  'src' => 'test.png',
253  'width' => '100',
254  'height' => '200',
255  'alt' => '',
256  ]
257  ],
258  ];
259  }
260 
267  public function ‪renderMethodCreatesExpectedTagWithoutFallbackProperties(array $arguments, array $expected)
268  {
269  $this->setArgumentsUnderTest($this->viewHelper, $arguments);
270 
271  $image = $this->getAccessibleMock(FileReference::class, ['getProperty', 'hasProperty'], [], '', false);
272  $image->expects(self::any())->method('hasProperty')->willReturn(false);
273 
274  $e = new \InvalidArgumentException('', 1556282257);
275  $image->expects(self::any())->method('getProperty')->willThrowException($e);
276 
277  $originalFile = $this->getMockBuilder(File::class)
278  ->disableOriginalConstructor()
279  ->getMock();
280  $originalFile->expects(self::any())->method('getProperties')->willReturn([]);
281 
282  $processedFile = $this->getMockBuilder(ProcessedFile::class)
283  ->disableOriginalConstructor()
284  ->getMock();
285 
286  $processedFile->expects(self::any())->method('getProperty')->willReturnMap([
287  ['width', $arguments['width']],
288  ['height', $arguments['height']],
289  ]);
290 
291  $image->_set('originalFile', $originalFile);
292  $image->_set('propertiesOfFileReference', []);
293  $imageService = $this->createMock(ImageService::class);
294  $imageService->expects(self::once())->method('getImage')->willReturn($image);
295  $imageService->expects(self::once())->method('applyProcessingInstructions')->with($image, self::anything())->willReturn($processedFile);
296  $imageService->expects(self::once())->method('getImageUri')->with($processedFile)->willReturn('test.png');
297 
298  $this->viewHelper->injectImageService($imageService);
299 
300  $tagBuilder = $this->getMockBuilder(TagBuilder::class)
301  ->setMethods(['addAttribute', 'render'])
302  ->getMock();
303  $tagBuilder->expects(self::exactly(count($expected)))
304  ->method('addAttribute')
305  ->withConsecutive(
306  ...$this->‪formatCallParameterExpectations($expected)
307  );
308  $tagBuilder->expects(self::once())->method('render');
309  $this->viewHelper->setTagBuilder($tagBuilder);
310 
311  $this->viewHelper->render();
312  }
313 }
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\formatCallParameterExpectations
‪array formatCallParameterExpectations(array $expectations)
Definition: ImageViewHelperTest.php:205
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\renderMethodThrowsExceptionOnInvalidArguments
‪renderMethodThrowsExceptionOnInvalidArguments(array $arguments)
Definition: ImageViewHelperTest.php:65
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper
Definition: ImageViewHelper.php:91
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:35
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\getInvalidArguments
‪array getInvalidArguments()
Definition: ImageViewHelperTest.php:50
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\setUp
‪setUp()
Definition: ImageViewHelperTest.php:40
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\getRenderMethodTestValuesWithoutFallbackProperties
‪array getRenderMethodTestValuesWithoutFallbackProperties()
Definition: ImageViewHelperTest.php:217
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ImageViewHelperTest.php:34
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\getRenderMethodTestValues
‪array getRenderMethodTestValues()
Definition: ImageViewHelperTest.php:78
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:44
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\$viewHelper
‪ImageViewHelper $viewHelper
Definition: ImageViewHelperTest.php:38
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\renderMethodCreatesExpectedTagWithoutFallbackProperties
‪renderMethodCreatesExpectedTagWithoutFallbackProperties(array $arguments, array $expected)
Definition: ImageViewHelperTest.php:265
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest
Definition: ImageViewHelperTest.php:31
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\renderMethodCreatesExpectedTag
‪renderMethodCreatesExpectedTag(array $arguments, array $expected)
Definition: ImageViewHelperTest.php:148