‪TYPO3CMS  10.4
ImageDimensionTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
33 class ‪ImageDimensionTest extends UnitTestCase
34 {
35  protected ‪$resetSingletonInstances = true;
36 
38  {
39  return [
40  'max width is applied' => [
41  $this->‪createTask(
42  [
43  'maxWidth' => 100,
44  ],
45  new ‪ImageDimension(1000, 500),
46  'jpg'
47  ),
48  new ‪ImageDimension(100, 50),
49  ],
50  'max width is applied when provided in width' => [
51  $this->‪createTask(
52  [
53  'width' => '100m',
54  ],
55  new ‪ImageDimension(1000, 500),
56  'jpg'
57  ),
58  new ‪ImageDimension(100, 50),
59  ],
60  'max height is applied' => [
61  $this->‪createTask(
62  [
63  'maxHeight' => 100,
64  ],
65  new ‪ImageDimension(1000, 500),
66  'jpg'
67  ),
68  new ‪ImageDimension(200, 100),
69  ],
70  'max height is applied when provided in height' => [
71  $this->‪createTask(
72  [
73  'height' => '100m',
74  ],
75  new ‪ImageDimension(1000, 500),
76  'jpg'
77  ),
78  new ‪ImageDimension(200, 100),
79  ],
80  'crop scale is applied' => [
81  $this->‪createTask(
82  [
83  'width' => 100,
84  'height' => '100c',
85  ],
86  new ‪ImageDimension(1000, 500),
87  'jpg'
88  ),
89  new ‪ImageDimension(100, 100),
90  ],
91  'maxWidth higher than crop scale' => [
92  $this->‪createTask(
93  [
94  'width' => 100,
95  'height' => '100c',
96  'maxWidth' => 200,
97  ],
98  new ‪ImageDimension(1000, 500),
99  'jpg'
100  ),
101  new ‪ImageDimension(100, 100),
102  ],
103  'maxWidth lower than crop scale (crop scale is ignored)' => [
104  $this->‪createTask(
105  [
106  'width' => 100,
107  'height' => '100c',
108  'maxWidth' => 50,
109  ],
110  new ‪ImageDimension(1000, 500),
111  'jpg'
112  ),
113  new ‪ImageDimension(50, 25),
114  ],
115  'width and height are applied as given' => [
116  $this->‪createTask(
117  [
118  'width' => 100,
119  'height' => 125,
120  ],
121  new ‪ImageDimension(1000, 500),
122  'jpg'
123  ),
124  new ‪ImageDimension(100, 125),
125  ],
126  'cropping is applied before scaling' => [
127  $this->‪createTask(
128  [
129  'maxWidth' => 100,
130  'crop' => new ‪Area(0, 0, 121.8, 45.3)
131  ],
132  new ‪ImageDimension(1000, 500),
133  'jpg'
134  ),
135  new ‪ImageDimension(100, 37),
136  ],
137  'width and height act as maxWidth and maxHeight for previews' => [
138  $this->‪createTask(
139  [
140  'width' => 100,
141  'height' => 125,
142  ],
143  new ‪ImageDimension(1000, 500),
144  'jpg',
145  ImagePreviewTask::class
146  ),
147  new ‪ImageDimension(100, 50),
148  ],
149  'width and height act as maxWidth and maxHeight for previews, max height' => [
150  $this->‪createTask(
151  [
152  'width' => 100,
153  'height' => 125,
154  ],
155  new ‪ImageDimension(500, 1000),
156  'jpg',
157  ImagePreviewTask::class
158  ),
159  new ‪ImageDimension(63, 125),
160  ],
161  'SVGs are scaled when crop scale is applied' => [
162  $this->‪createTask(
163  [
164  'width' => 100,
165  'height' => '100c',
166  ],
167  new ‪ImageDimension(1000, 500),
168  'svg'
169  ),
170  new ‪ImageDimension(100, 50),
171  ],
172  'cropping is applied on SVGs' => [
173  $this->‪createTask(
174  [
175  'crop' => new ‪Area(0, 0, 121.8, 45.3)
176  ],
177  new ‪ImageDimension(1000, 500),
178  'svg'
179  ),
180  new ‪ImageDimension(122, 45),
181  ],
182  ];
183  }
184 
192  {
193  $calculatedDimension = ‪ImageDimension::fromProcessingTask($task);
194  self::assertEquals($expectedImageDimension, $calculatedDimension);
195  }
196 
197  private function ‪createTask(array $processingConfiguration, ‪ImageDimension $originalImageDimension, string $fileExtension, string $taskClass = ImageCropScaleMaskTask::class): ‪TaskInterface
198  {
199  $originalFileMock = $this->getMockBuilder(File::class)
200  ->disableOriginalConstructor()
201  ->getMock();
202  $originalFileMock->expects(self::any())
203  ->method('getExtension')
204  ->willReturn($fileExtension);
205  $originalFileMock->expects(self::exactly(2))
206  ->method('getProperty')
207  ->withConsecutive(['width'], ['height'])
208  ->willReturnOnConsecutiveCalls($originalImageDimension->‪getWidth(), $originalImageDimension->‪getHeight());
209  $processedFileMock = $this->getMockBuilder(ProcessedFile::class)
210  ->disableOriginalConstructor()
211  ->getMock();
212  $processedFileMock->expects(self::any())
213  ->method('getOriginalFile')
214  ->willReturn($originalFileMock);
215 
217  $task = new $taskClass(
218  $processedFileMock,
219  $processingConfiguration
220  );
221 
222  $processedFileMock->expects(self::any())
223  ->method('getTaskIdentifier')
224  ->willReturn($task->getType() . '.' . $task->getName());
225 
226  return $task;
227  }
228 }
‪TYPO3\CMS\Core\Imaging\ImageDimension\getHeight
‪getHeight()
Definition: ImageDimension.php:51
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:33
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:23
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageDimensionTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: ImageDimensionTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Imaging
Definition: DimensionTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageDimensionTest\givenProcessingInstructionsCalculatesCorrectDimension
‪givenProcessingInstructionsCalculatesCorrectDimension(TaskInterface $task, ImageDimension $expectedImageDimension)
Definition: ImageDimensionTest.php:191
‪TYPO3\CMS\Core\Imaging\ImageDimension\fromProcessingTask
‪static fromProcessingTask(TaskInterface $task)
Definition: ImageDimension.php:56
‪TYPO3\CMS\Core\Resource\Processing\ImagePreviewTask
Definition: ImagePreviewTask.php:22
‪TYPO3\CMS\Core\Resource\Processing\ImageCropScaleMaskTask
Definition: ImageCropScaleMaskTask.php:22
‪TYPO3\CMS\Core\Imaging\ImageDimension\getWidth
‪getWidth()
Definition: ImageDimension.php:46
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageDimensionTest\createTask
‪createTask(array $processingConfiguration, ImageDimension $originalImageDimension, string $fileExtension, string $taskClass=ImageCropScaleMaskTask::class)
Definition: ImageDimensionTest.php:197
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageDimensionTest\givenProcessingInstructionsCalculatesCorrectDimensionDataProvider
‪givenProcessingInstructionsCalculatesCorrectDimensionDataProvider()
Definition: ImageDimensionTest.php:37
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:44
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageDimensionTest
Definition: ImageDimensionTest.php:34
‪TYPO3\CMS\Core\Imaging\ImageDimension
Definition: ImageDimension.php:31