6daaa7c1659da2dd293b83fd2cd294b68897095e
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Imaging / ImageManipulation / CropVariantCollectionTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Imaging\ImageManipulation;
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
18 use TYPO3\CMS\Core\Imaging\ImageManipulation\Area;
19 use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant;
20 use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
21 use TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 class CropVariantCollectionTest extends UnitTestCase
25 {
26 /**
27 * @var array
28 */
29 private static $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 /**
73 * @test
74 */
75 public function createFromJsonWorks()
76 {
77 $cropVariant1 = self::$tca;
78 $cropVariant2 = self::$tca;
79 $cropVariantCollection = CropVariantCollection::create(json_encode(['default' => $cropVariant1, 'Second' => $cropVariant2]));
80 $this->assertInstanceOf(CropVariantCollection::class, $cropVariantCollection);
81
82 $assertSameValues = function ($expected, $actual) use (&$assertSameValues) {
83 if (is_array($expected)) {
84 foreach ($expected as $key => $value) {
85 $this->assertArrayHasKey($key, $actual);
86 $assertSameValues($expected[$key], $actual[$key]);
87 }
88 } else {
89 $this->assertSame($expected, $actual);
90 }
91 };
92 // assertSame does not work here, because the fuzz for float is not applied for array values
93 $assertSameValues(['default' => $cropVariant1, 'Second' => $cropVariant2], $cropVariantCollection->asArray());
94 }
95
96 /**
97 * @test
98 */
99 public function duplicateIdThrowsException()
100 {
101 $this->expectException(InvalidConfigurationException::class);
102 $cropVariant1 = new CropVariant('foo', 'title 1', new Area(0.0, 0.0, 1.0, 1.0));
103 $cropVariant2 = new CropVariant('foo', 'title 2', new Area(0.0, 0.0, 0.5, 0.5));
104 new CropVariantCollection([$cropVariant1, $cropVariant2]);
105 }
106
107 /**
108 * @test
109 */
110 public function createEmptyWorks()
111 {
112 $this->assertTrue(CropVariantCollection::create('')->getCropArea()->isEmpty());
113 }
114 }