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