‪TYPO3CMS  ‪main
CropVariantCollectionTest.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\Test;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
27 final class ‪CropVariantCollectionTest extends UnitTestCase
28 {
29  private static array ‪$tca = [
30  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.crop_variant.default',
31  'cropArea' => [
32  'x' => 0.0,
33  'y' => 0.0,
34  'width' => 1.0,
35  'height' => 1.0,
36  ],
37  'allowedAspectRatios' => [
38  '16:9' => [
39  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.16_9',
40  'value' => 1.777777777777777,
41  ],
42  '4:3' => [
43  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.4_3',
44  'value' => 1.333333333333333,
45  ],
46  '1:1' => [
47  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.1_1',
48  'value' => 1.0,
49  ],
50  'free' => [
51  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.free',
52  'value' => 0.0,
53  ],
54  ],
55  'selectedRatio' => '16:9',
56  'focusArea' => [
57  'x' => 0.4,
58  'y' => 0.4,
59  'width' => 0.6,
60  'height' => 0.6,
61  ],
62  'coverAreas' => [
63  [
64  'x' => 0.0,
65  'y' => 0.8,
66  'width' => 1.0,
67  'height' => 0.2,
68  ],
69  ],
70  ];
71 
72  #[Test]
73  public function ‪createFromJsonWorks(): void
74  {
75  $cropVariant1 = ‪self::$tca;
76  $cropVariant2 = ‪self::$tca;
77  $cropVariantCollection = ‪CropVariantCollection::create(json_encode(['default' => $cropVariant1, 'Second' => $cropVariant2]));
78  self::assertInstanceOf(CropVariantCollection::class, $cropVariantCollection);
79 
80  $assertSameValues = function ($expected, $actual) use (&$assertSameValues) {
81  if (is_array($expected)) {
82  foreach ($expected as $key => $value) {
83  $this->assertArrayHasKey($key, $actual);
84  $assertSameValues($expected[$key], $actual[$key]);
85  }
86  } else {
87  $this->assertSame($expected, $actual);
88  }
89  };
90  // assertSame does not work here, because the fuzz for float is not applied for array values
91  $assertSameValues(['default' => $cropVariant1, 'Second' => $cropVariant2], $cropVariantCollection->asArray());
92  }
93 
94  #[Test]
95  public function ‪duplicateIdThrowsException(): void
96  {
97  $this->expectException(InvalidConfigurationException::class);
98  $cropVariant1 = new ‪CropVariant('foo', 'title 1', new ‪Area(0.0, 0.0, 1.0, 1.0));
99  $cropVariant2 = new ‪CropVariant('foo', 'title 2', new ‪Area(0.0, 0.0, 0.5, 0.5));
100  new ‪CropVariantCollection([$cropVariant1, $cropVariant2]);
101  }
102 
103  #[Test]
104  public function ‪createEmptyWorks(): void
105  {
106  self::assertTrue(‪CropVariantCollection::create('')->getCropArea()->isEmpty());
107  }
108 
109  #[Test]
111  {
112  $variants = new ‪CropVariantCollection([]);
113  self::assertSame('[]', (string)$variants);
114  }
115 }
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageManipulation
Definition: AreaTest.php:18
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:37
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant
Definition: CropVariant.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:23
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageManipulation\CropVariantCollectionTest\castToStringReturnsJsonArrayOnEmptyInput
‪castToStringReturnsJsonArrayOnEmptyInput()
Definition: CropVariantCollectionTest.php:110
‪TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
Definition: InvalidConfigurationException.php:23
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageManipulation\CropVariantCollectionTest\createEmptyWorks
‪createEmptyWorks()
Definition: CropVariantCollectionTest.php:104
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:23
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageManipulation\CropVariantCollectionTest\duplicateIdThrowsException
‪duplicateIdThrowsException()
Definition: CropVariantCollectionTest.php:95
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageManipulation\CropVariantCollectionTest\createFromJsonWorks
‪createFromJsonWorks()
Definition: CropVariantCollectionTest.php:73
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageManipulation\CropVariantCollectionTest\$tca
‪static array $tca
Definition: CropVariantCollectionTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Imaging\ImageManipulation\CropVariantCollectionTest
Definition: CropVariantCollectionTest.php:28