TYPO3 CMS  TYPO3_8-7
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 
23 
24 class ImageViewHelperTest extends ViewHelperBaseTestcase
25 {
29  protected $viewHelper;
30 
31  protected function setUp()
32  {
33  parent::setUp();
34  $this->viewHelper = new ImageViewHelper();
35  $this->injectDependenciesIntoViewHelper($this->viewHelper);
36  }
37 
41  public function getInvalidArguments()
42  {
43  return [
44  [['image' => null]],
45  [['src' => null]],
46  [['src' => 'something', 'image' => 'something']],
47  ];
48  }
49 
55  public function renderMethodThrowsExceptionOnInvalidArguments(array $arguments)
56  {
57  $this->setArgumentsUnderTest($this->viewHelper, $arguments);
58 
59  $this->expectException(\TYPO3\CMS\Fluid\Core\ViewHelper\Exception::class);
60  $this->expectExceptionCode(1382284106);
61 
62  $this->viewHelper->render();
63  }
64 
68  public function getRenderMethodTestValues()
69  {
70  return [
71  [
72  [
73  'src' => 'test',
74  'width' => 100,
75  'height' => 200,
76  'minWidth' => 300,
77  'maxWidth' => 400,
78  'minHeight' => 500,
79  'maxHeight' => 600,
80  'crop' => false
81  ],
82  [
83  'src' => 'test.png',
84  'width' => '100',
85  'height' => '200',
86  'alt' => 'alternative',
87  'title' => 'title'
88  ]
89  ],
90  [
91  [
92  'src' => 'test',
93  'width' => 100,
94  'height' => 200,
95  'minWidth' => 300,
96  'maxWidth' => 400,
97  'minHeight' => 500,
98  'maxHeight' => 600,
99  'crop' => null
100  ],
101  [
102  'src' => 'test.png',
103  'width' => '100',
104  'height' => '200',
105  'alt' => 'alternative',
106  'title' => 'title'
107  ]
108  ],
109  ];
110  }
111 
118  public function renderMethodCreatesExpectedTag(array $arguments, array $expected)
119  {
120  $this->setArgumentsUnderTest($this->viewHelper, $arguments);
121 
122  $image = $this->getMockBuilder(FileReference::class)
123  ->setMethods(['getProperty'])
124  ->disableOriginalConstructor()
125  ->getMock();
126  $image->expects($this->any())->method('getProperty')->willReturnMap([
127  ['width', $arguments['width']],
128  ['height', $arguments['height']],
129  ['alternative', 'alternative'],
130  ['title', 'title'],
131  ['crop', 'crop']
132  ]);
133  $originalFile = $this->getMockBuilder(File::class)
134  ->disableOriginalConstructor()
135  ->getMock();
136  $originalFile->expects($this->any())->method('getProperties')->willReturn([]);
137  $this->inject($image, 'originalFile', $originalFile);
138  $this->inject($image, 'propertiesOfFileReference', []);
139  $imageService = $this->getMockBuilder(ImageService::class)
140  ->setMethods(['getImage', 'applyProcessingInstructions', 'getImageUri'])
141  ->getMock();
142  $imageService->expects($this->once())->method('getImage')->willReturn($image);
143  $imageService->expects($this->once())->method('applyProcessingInstructions')->with($image, $this->anything())->willReturn($image);
144  $imageService->expects($this->once())->method('getImageUri')->with($image)->willReturn('test.png');
145 
146  $this->inject($this->viewHelper, 'imageService', $imageService);
147 
148  $tagBuilder = $this->getMockBuilder(TagBuilder::class)
149  ->setMethods(['addAttribute', 'render'])
150  ->getMock();
151  $index = -1;
152  foreach ($expected as $expectedAttribute => $expectedValue) {
153  $tagBuilder->expects($this->at(++ $index))->method('addAttribute')->with($expectedAttribute, $expectedValue);
154  }
155  $tagBuilder->expects($this->once())->method('render');
156  $this->inject($this->viewHelper, 'tag', $tagBuilder);
157 
158  $this->viewHelper->render();
159  }
160 }