‪TYPO3CMS  ‪main
AspectFactoryTest.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;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
29 final class ‪AspectFactoryTest extends UnitTestCase
30 {
31  protected function ‪setUp(): void
32  {
33  parent::setUp();
34  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects'] = [
35  'Persisted' => get_class($this->createMock(PersistedMappableAspectInterface::class)),
36  'Aspect' => get_class($this->createMock(AspectInterface::class)),
37  ];
38  }
39 
40  #[Test]
42  {
43  $aspectFactory = new ‪AspectFactory();
44  $this->expectException(\InvalidArgumentException::class);
45  $this->expectExceptionCode(1538079481);
46  $aspectFactory->createAspects(
47  ['a' => []],
48  $this->createMock(SiteLanguage::class),
49  $this->createMock(Site::class)
50  );
51  }
52 
53  #[Test]
55  {
56  $aspectFactory = new ‪AspectFactory();
57  $this->expectException(\OutOfRangeException::class);
58  $this->expectExceptionCode(1538079482);
59  $aspectFactory->createAspects(
60  ['a' => ['type' => 'Undefined']],
61  $this->createMock(SiteLanguage::class),
62  $this->createMock(Site::class)
63  );
64  }
65 
66  public static function ‪aspectsDataProvider(): array
67  {
68  return [
69  'single aspect' => [
70  [
71  'a' => ['type' => 'Aspect'],
72  ],
73  [
74  'a' => AspectInterface::class,
75  ],
76  ],
77  'both non-persisted' => [
78  [
79  'a' => ['type' => 'Aspect'],
80  'b' => ['type' => 'Aspect'],
81  ],
82  [
83  'a' => AspectInterface::class,
84  'b' => AspectInterface::class,
85  ],
86  ],
87  'both persisted' => [
88  [
89  'a' => ['type' => 'Persisted'],
90  'b' => ['type' => 'Persisted'],
91  ],
92  [
93  'a' => PersistedMappableAspectInterface::class,
94  'b' => PersistedMappableAspectInterface::class,
95  ],
96  ],
97  // persisted shall be sorted to the end
98  'first persisted, second non-persisted' => [
99  [
100  'a' => ['type' => 'Persisted'],
101  'b' => ['type' => 'Aspect'],
102  ],
103  [
104  'b' => AspectInterface::class,
105  'a' => PersistedMappableAspectInterface::class,
106  ],
107  ],
108  // persisted shall be sorted to the end
109  'many persisted, many non-persisted' => [
110  [
111  'a' => ['type' => 'Persisted'],
112  'b' => ['type' => 'Aspect'],
113  'c' => ['type' => 'Persisted'],
114  'd' => ['type' => 'Aspect'],
115  ],
116  [
117  'b' => AspectInterface::class,
118  'd' => AspectInterface::class,
119  'a' => PersistedMappableAspectInterface::class,
120  'c' => PersistedMappableAspectInterface::class,
121  ],
122  ],
123  ];
124  }
125 
129  #[DataProvider('aspectsDataProvider')]
130  #[Test]
131  public function ‪aspectsAreCreatedAndSorted(array $settings, array $expectation): void
132  {
133  $aspectFactory = new ‪AspectFactory();
134  $aspects = $aspectFactory->createAspects(
135  $settings,
136  $this->createMock(SiteLanguage::class),
137  $this->createMock(Site::class)
138  );
139  self::assertSame(array_keys($aspects), array_keys($expectation));
140  array_walk($aspects, static function ($aspect, $key) use ($expectation) {
141  static::assertInstanceOf($expectation[$key], $aspect);
142  });
143  }
144 }
‪TYPO3\CMS\Core\Routing\Aspect\AspectInterface
Definition: AspectInterface.php:23
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect
Definition: AspectFactoryTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest
Definition: AspectFactoryTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\aspectsAreCreatedAndSorted
‪aspectsAreCreatedAndSorted(array $settings, array $expectation)
Definition: AspectFactoryTest.php:131
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\createAspectsThrowsExceptionOnUnregisteredType
‪createAspectsThrowsExceptionOnUnregisteredType()
Definition: AspectFactoryTest.php:54
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\createAspectsThrowsExceptionOnMissingType
‪createAspectsThrowsExceptionOnMissingType()
Definition: AspectFactoryTest.php:41
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:27
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\aspectsDataProvider
‪static aspectsDataProvider()
Definition: AspectFactoryTest.php:66
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory
Definition: AspectFactory.php:30
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\setUp
‪setUp()
Definition: AspectFactoryTest.php:31
‪TYPO3\CMS\Core\Routing\Aspect\PersistedMappableAspectInterface
Definition: PersistedMappableAspectInterface.php:24