‪TYPO3CMS  9.5
ImageViewHelperTest.php
Go to the documentation of this file.
1 <?php
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 
21 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
23 
27 class ‪ImageViewHelperTest extends ViewHelperBaseTestcase
28 {
32  protected ‪$resetSingletonInstances = true;
33 
37  protected ‪$viewHelper;
38 
39  protected function ‪setUp()
40  {
41  parent::setUp();
42  $this->viewHelper = new ‪ImageViewHelper();
43  $this->injectDependenciesIntoViewHelper($this->viewHelper);
44  }
45 
49  public function ‪getInvalidArguments()
50  {
51  return [
52  [['image' => null]],
53  [['src' => null]],
54  [['src' => 'something', 'image' => 'something']],
55  ];
56  }
57 
63  public function ‪renderMethodThrowsExceptionOnInvalidArguments(array $arguments)
64  {
65  $this->setArgumentsUnderTest($this->viewHelper, $arguments);
66 
67  $this->expectException(\‪TYPO3Fluid\Fluid\Core\ViewHelper\Exception::class);
68  $this->expectExceptionCode(1382284106);
69 
70  $this->viewHelper->render();
71  }
72 
76  public function ‪getRenderMethodTestValues()
77  {
78  return [
79  [
80  [
81  'src' => 'test',
82  'width' => 100,
83  'height' => 200,
84  'minWidth' => 300,
85  'maxWidth' => 400,
86  'minHeight' => 500,
87  'maxHeight' => 600,
88  'crop' => false
89  ],
90  [
91  'src' => 'test.png',
92  'width' => '100',
93  'height' => '200',
94  'alt' => 'alternative',
95  'title' => 'title'
96  ]
97  ],
98  [
99  [
100  'src' => 'test',
101  'width' => 100,
102  'height' => 200,
103  'minWidth' => 300,
104  'maxWidth' => 400,
105  'minHeight' => 500,
106  'maxHeight' => 600,
107  'crop' => null
108  ],
109  [
110  'src' => 'test.png',
111  'width' => '100',
112  'height' => '200',
113  'alt' => 'alternative',
114  'title' => 'title'
115  ]
116  ],
117  ];
118  }
119 
126  public function ‪renderMethodCreatesExpectedTag(array $arguments, array $expected)
127  {
128  $this->setArgumentsUnderTest($this->viewHelper, $arguments);
129 
130  $image = $this->getMockBuilder(FileReference::class)
131  ->setMethods(['getProperty'])
132  ->disableOriginalConstructor()
133  ->getMock();
134  $image->expects($this->any())->method('getProperty')->willReturnMap([
135  ['width', $arguments['width']],
136  ['height', $arguments['height']],
137  ['alternative', 'alternative'],
138  ['title', 'title'],
139  ['crop', 'crop']
140  ]);
141  $originalFile = $this->getMockBuilder(File::class)
142  ->disableOriginalConstructor()
143  ->getMock();
144  $originalFile->expects($this->any())->method('getProperties')->willReturn([]);
145  $this->inject($image, 'originalFile', $originalFile);
146  $this->inject($image, 'propertiesOfFileReference', []);
147  $imageService = $this->createMock(ImageService::class);
148  $imageService->expects($this->once())->method('getImage')->willReturn($image);
149  $imageService->expects($this->once())->method('applyProcessingInstructions')->with($image, $this->anything())->willReturn($image);
150  $imageService->expects($this->once())->method('getImageUri')->with($image)->willReturn('test.png');
151 
152  $this->inject($this->viewHelper, 'imageService', $imageService);
153 
154  $tagBuilder = $this->getMockBuilder(TagBuilder::class)
155  ->setMethods(['addAttribute', 'render'])
156  ->getMock();
157  $index = -1;
158  foreach ($expected as $expectedAttribute => $expectedValue) {
159  $tagBuilder->expects($this->at(++ $index))->method('addAttribute')->with($expectedAttribute, $expectedValue);
160  }
161  $tagBuilder->expects($this->once())->method('render');
162  $this->inject($this->viewHelper, 'tag', $tagBuilder);
163 
164  $this->viewHelper->render();
165  }
166 }
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\renderMethodThrowsExceptionOnInvalidArguments
‪renderMethodThrowsExceptionOnInvalidArguments(array $arguments)
Definition: ImageViewHelperTest.php:61
‪TYPO3Fluid
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:31
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper
Definition: ImageViewHelper.php:87
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:30
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\getInvalidArguments
‪array getInvalidArguments()
Definition: ImageViewHelperTest.php:47
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\setUp
‪setUp()
Definition: ImageViewHelperTest.php:37
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ImageViewHelperTest.php:31
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\getRenderMethodTestValues
‪array getRenderMethodTestValues()
Definition: ImageViewHelperTest.php:74
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\$viewHelper
‪ImageViewHelper $viewHelper
Definition: ImageViewHelperTest.php:35
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest
Definition: ImageViewHelperTest.php:28
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers
Definition: BaseViewHelperTest.php:2
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ImageViewHelperTest\renderMethodCreatesExpectedTag
‪renderMethodCreatesExpectedTag(array $arguments, array $expected)
Definition: ImageViewHelperTest.php:124