‪TYPO3CMS  ‪main
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 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
35 final class ‪ImageDimensionTest extends UnitTestCase
36 {
37  protected bool ‪$resetSingletonInstances = true;
38 
40  {
41  return [
42  'max width is applied' => [
43  [
44  'maxWidth' => 100,
45  ],
46  new ‪ImageDimension(1000, 500),
47  'jpg',
48  ImageCropScaleMaskTask::class,
49  new ‪ImageDimension(100, 50),
50  ],
51  'max width is applied when provided in width' => [
52  [
53  'width' => '100m',
54  ],
55  new ‪ImageDimension(1000, 500),
56  'jpg',
57  ImageCropScaleMaskTask::class,
58  new ‪ImageDimension(100, 50),
59  ],
60  'max height is applied' => [
61  [
62  'maxHeight' => 100,
63  ],
64  new ‪ImageDimension(1000, 500),
65  'jpg',
66  ImageCropScaleMaskTask::class,
67  new ‪ImageDimension(200, 100),
68  ],
69  'max height is applied when provided in height' => [
70  [
71  'height' => '100m',
72  ],
73  new ‪ImageDimension(1000, 500),
74  'jpg',
75  ImageCropScaleMaskTask::class,
76  new ‪ImageDimension(200, 100),
77  ],
78  'crop scale is applied' => [
79  [
80  'width' => 100,
81  'height' => '100c',
82  ],
83  new ‪ImageDimension(1000, 500),
84  'jpg',
85  ImageCropScaleMaskTask::class,
86  new ‪ImageDimension(100, 100),
87  ],
88  'maxWidth higher than crop scale' => [
89  [
90  'width' => 100,
91  'height' => '100c',
92  'maxWidth' => 200,
93  ],
94  new ‪ImageDimension(1000, 500),
95  'jpg',
96  ImageCropScaleMaskTask::class,
97  new ‪ImageDimension(100, 100),
98  ],
99  'maxWidth lower than crop scale (crop scale is ignored)' => [
100  [
101  'width' => 100,
102  'height' => '100c',
103  'maxWidth' => 50,
104  ],
105  new ‪ImageDimension(1000, 500),
106  'jpg',
107  ImageCropScaleMaskTask::class,
108  new ‪ImageDimension(50, 50),
109  ],
110  'width and height are applied as given' => [
111  [
112  'width' => 100,
113  'height' => 125,
114  ],
115  new ‪ImageDimension(1000, 500),
116  'jpg',
117  ImageCropScaleMaskTask::class,
118  new ‪ImageDimension(100, 125),
119  ],
120  'cropping is applied before scaling' => [
121  [
122  'maxWidth' => 100,
123  'crop' => new ‪Area(0, 0, 121.8, 45.3),
124  ],
125  new ‪ImageDimension(1000, 500),
126  'jpg',
127  ImageCropScaleMaskTask::class,
128  new ‪ImageDimension(100, 37),
129  ],
130  'width and height act as maxWidth and maxHeight for previews' => [
131  [
132  'width' => 100,
133  'height' => 125,
134  ],
135  new ‪ImageDimension(1000, 500),
136  'jpg',
137  ImagePreviewTask::class,
138  new ‪ImageDimension(100, 50),
139  ],
140  'width and height act as maxWidth and maxHeight for previews, max height' => [
141  [
142  'width' => 100,
143  'height' => 125,
144  ],
145  new ‪ImageDimension(500, 1000),
146  'jpg',
147  ImagePreviewTask::class,
148  new ‪ImageDimension(63, 125),
149  ],
150  'SVGs are scaled when crop scale is applied' => [
151  [
152  'width' => 100,
153  'height' => '100c',
154  ],
155  new ‪ImageDimension(1000, 500),
156  'svg',
157  ImageCropScaleMaskTask::class,
158  new ‪ImageDimension(100, 100),
159  ],
160  'cropping is applied on SVGs' => [
161  [
162  'crop' => new ‪Area(0, 0, 121.8, 45.3),
163  ],
164  new ‪ImageDimension(1000, 500),
165  'svg',
166  ImageCropScaleMaskTask::class,
167  new ‪ImageDimension(122, 45),
168  ],
169  ];
170  }
171 
172  #[DataProvider('givenProcessingInstructionsCalculatesCorrectDimensionDataProvider')]
173  #[Test]
175  array $processingConfiguration,
176  ‪ImageDimension $originalImageDimension,
177  string $fileExtension,
178  string $taskClass,
179  ‪ImageDimension $expectedImageDimension
180  ): void {
181  $task = $this->‪createTask($processingConfiguration, $originalImageDimension, $fileExtension, $taskClass);
182  $calculatedDimension = ‪ImageDimension::fromProcessingTask($task);
183  self::assertEquals($expectedImageDimension, $calculatedDimension);
184  }
185 
186  private function ‪createTask(array $processingConfiguration, ‪ImageDimension $originalImageDimension, string $fileExtension, string $taskClass = ImageCropScaleMaskTask::class): ‪TaskInterface
187  {
188  $originalFileMock = $this->getMockBuilder(File::class)->disableOriginalConstructor()->getMock();
189  $originalFileMock->method('getExtension')->willReturn($fileExtension);
190  $originalFileMock->method('getProperty')->willReturnOnConsecutiveCalls($originalImageDimension->getWidth(), $originalImageDimension->getHeight());
191  $processedFileMock = $this->getMockBuilder(ProcessedFile::class)->disableOriginalConstructor()->getMock();
192  $processedFileMock->method('getOriginalFile')->willReturn($originalFileMock);
193 
195  $task = new $taskClass(
196  $processedFileMock,
197  $processingConfiguration
198  );
199 
200  $processedFileMock->method('getTaskIdentifier')->willReturn($task->getType() . '.' . $task->getName());
201 
202  return $task;
203  }
204 }
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageDimensionTest\givenProcessingInstructionsCalculatesCorrectDimension
‪givenProcessingInstructionsCalculatesCorrectDimension(array $processingConfiguration, ImageDimension $originalImageDimension, string $fileExtension, string $taskClass, ImageDimension $expectedImageDimension)
Definition: ImageDimensionTest.php:174
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:34
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:23
‪TYPO3\CMS\Core\Tests\Unit\Imaging
Definition: DimensionTest.php:18
‪TYPO3\CMS\Core\Imaging\ImageDimension\fromProcessingTask
‪static fromProcessingTask(TaskInterface $task)
Definition: ImageDimension.php:57
‪TYPO3\CMS\Core\Resource\Processing\ImagePreviewTask
Definition: ImagePreviewTask.php:27
‪TYPO3\CMS\Core\Resource\Processing\ImageCropScaleMaskTask
Definition: ImageCropScaleMaskTask.php:26
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageDimensionTest\createTask
‪createTask(array $processingConfiguration, ImageDimension $originalImageDimension, string $fileExtension, string $taskClass=ImageCropScaleMaskTask::class)
Definition: ImageDimensionTest.php:186
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageDimensionTest\givenProcessingInstructionsCalculatesCorrectDimensionDataProvider
‪static givenProcessingInstructionsCalculatesCorrectDimensionDataProvider()
Definition: ImageDimensionTest.php:39
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageDimensionTest
Definition: ImageDimensionTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageDimensionTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ImageDimensionTest.php:37
‪TYPO3\CMS\Core\Imaging\ImageDimension
Definition: ImageDimension.php:28