‪TYPO3CMS  11.5
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 Prophecy\PhpUnit\ProphecyTrait;
21 use Prophecy\Prophecy\ObjectProphecy;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
30 class ‪AspectFactoryTest extends UnitTestCase
31 {
32  use ProphecyTrait;
33 
34  protected ?‪AspectFactory ‪$subject;
35 
37  protected ObjectProphecy ‪$languageProphecy;
38 
40  protected ObjectProphecy ‪$siteProphecy;
41 
42  protected ?string ‪$persistedMockClass;
43  protected ?string ‪$aspectMockClass;
44 
45  protected function ‪setUp(): void
46  {
47  parent::setUp();
48  $this->languageProphecy = $this->prophesize(
49  SiteLanguage::class
50  );
51  $this->siteProphecy = $this->prophesize(
52  Site::class
53  );
54  $this->persistedMockClass = get_class($this->createMock(
55  PersistedMappableAspectInterface::class
56  ));
57  $this->aspectMockClass = get_class($this->createMock(
58  AspectInterface::class
59  ));
60  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects'] = [
61  'Persisted' => ‪$this->persistedMockClass,
62  'Aspect' => ‪$this->aspectMockClass,
63  ];
64  $contextMock = $this->getMockBuilder(Context::class)
65  ->disableOriginalConstructor()
66  ->getMock();
67  $this->subject = new ‪AspectFactory($contextMock);
68  }
69 
70  protected function ‪tearDown(): void
71  {
72  unset($this->subject, $this->languageProphecy, $this->persistedMockClass, $this->aspectMockClass);
73  parent::tearDown();
74  }
75 
79  public function ‪createAspectsThrowsExceptionOnMissingType(): void
80  {
81  $this->expectException(\InvalidArgumentException::class);
82  $this->expectExceptionCode(1538079481);
83  $this->subject->createAspects(
84  ['a' => []],
85  $this->languageProphecy->reveal(),
86  $this->siteProphecy->reveal()
87  );
88  }
89 
94  {
95  $this->expectException(\OutOfRangeException::class);
96  $this->expectExceptionCode(1538079482);
97  $this->subject->createAspects(
98  ['a' => ['type' => 'Undefined']],
99  $this->languageProphecy->reveal(),
100  $this->siteProphecy->reveal()
101  );
102  }
103 
107  public function ‪aspectsDataProvider(): array
108  {
109  return [
110  'single aspect' => [
111  [
112  'a' => ['type' => 'Aspect'],
113  ],
114  [
115  'a' => AspectInterface::class,
116  ],
117  ],
118  'both non-persisted' => [
119  [
120  'a' => ['type' => 'Aspect'],
121  'b' => ['type' => 'Aspect'],
122  ],
123  [
124  'a' => AspectInterface::class,
125  'b' => AspectInterface::class,
126  ],
127  ],
128  'both persisted' => [
129  [
130  'a' => ['type' => 'Persisted'],
131  'b' => ['type' => 'Persisted'],
132  ],
133  [
134  'a' => PersistedMappableAspectInterface::class,
135  'b' => PersistedMappableAspectInterface::class,
136  ],
137  ],
138  // persisted shall be sorted to the end
139  'first persisted, second non-persisted' => [
140  [
141  'a' => ['type' => 'Persisted'],
142  'b' => ['type' => 'Aspect'],
143  ],
144  [
145  'b' => AspectInterface::class,
146  'a' => PersistedMappableAspectInterface::class,
147  ],
148  ],
149  // persisted shall be sorted to the end
150  'many persisted, many non-persisted' => [
151  [
152  'a' => ['type' => 'Persisted'],
153  'b' => ['type' => 'Aspect'],
154  'c' => ['type' => 'Persisted'],
155  'd' => ['type' => 'Aspect'],
156  ],
157  [
158  'b' => AspectInterface::class,
159  'd' => AspectInterface::class,
160  'a' => PersistedMappableAspectInterface::class,
161  'c' => PersistedMappableAspectInterface::class,
162  ],
163  ],
164  ];
165  }
166 
174  public function ‪aspectsAreCreatedAndSorted(array $settings, array $expectation): void
175  {
176  $aspects = $this->subject->createAspects(
177  $settings,
178  $this->languageProphecy->reveal(),
179  $this->siteProphecy->reveal()
180  );
181  self::assertSame(array_keys($aspects), array_keys($expectation));
182  array_walk($aspects, static function ($aspect, $key) use ($expectation) {
183  static::assertInstanceOf($expectation[$key], $aspect);
184  });
185  }
186 }
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\$siteProphecy
‪ObjectProphecy $siteProphecy
Definition: AspectFactoryTest.php:39
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\$aspectMockClass
‪string $aspectMockClass
Definition: AspectFactoryTest.php:42
‪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:31
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\$languageProphecy
‪ObjectProphecy $languageProphecy
Definition: AspectFactoryTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\aspectsAreCreatedAndSorted
‪aspectsAreCreatedAndSorted(array $settings, array $expectation)
Definition: AspectFactoryTest.php:173
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\createAspectsThrowsExceptionOnUnregisteredType
‪createAspectsThrowsExceptionOnUnregisteredType()
Definition: AspectFactoryTest.php:92
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\createAspectsThrowsExceptionOnMissingType
‪createAspectsThrowsExceptionOnMissingType()
Definition: AspectFactoryTest.php:78
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:26
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\$subject
‪AspectFactory $subject
Definition: AspectFactoryTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\tearDown
‪tearDown()
Definition: AspectFactoryTest.php:69
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\$persistedMockClass
‪string $persistedMockClass
Definition: AspectFactoryTest.php:41
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\aspectsDataProvider
‪array aspectsDataProvider()
Definition: AspectFactoryTest.php:106
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory
Definition: AspectFactory.php:33
‪TYPO3\CMS\Core\Tests\Unit\Routing\Aspect\AspectFactoryTest\setUp
‪setUp()
Definition: AspectFactoryTest.php:44
‪TYPO3\CMS\Core\Routing\Aspect\PersistedMappableAspectInterface
Definition: PersistedMappableAspectInterface.php:24