‪TYPO3CMS  10.4
ContextTest.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 
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
27 class ‪ContextTest extends UnitTestCase
28 {
34  public function ‪validAspectKeysDataProvider(): array
35  {
36  return [
37  ['myfirst'],
38  ['mysecond'],
39  ['date'],
40  ['visibility'],
41  ['backend.user'],
42  ['frontend.user'],
43  ['workspace'],
44  ];
45  }
46 
52  public function ‪hasAspectReturnsTrueOnExistingAspect(string $aspectName)
53  {
54  $subject = new ‪Context([
55  'myfirst' => new ‪UserAspect(),
56  'mysecond' => new ‪UserAspect(),
57  ]);
58  self::assertTrue($subject->hasAspect($aspectName));
59  }
60 
66  public function ‪invalidAspectKeysDataProvider(): array
67  {
68  return [
69  ['visible'],
70  ['frontenduser'],
71  ['compatibility'],
72  ];
73  }
74 
80  public function ‪hasAspectReturnsFalseOnNonExistingAspect(string $aspectName)
81  {
82  $subject = new ‪Context([
83  'myfirst' => new ‪UserAspect(),
84  'mysecond' => new ‪UserAspect(),
85  ]);
86  self::assertFalse($subject->hasAspect($aspectName));
87  }
88 
93  {
94  $subject = new ‪Context([
95  'coolio' => new ‪UserAspect(),
96  'uncoolio' => new ‪Registry()
97  ]);
98  self::assertTrue($subject->hasAspect('coolio'));
99  self::assertFalse($subject->hasAspect('uncoolio'));
100  }
101 
106  {
107  $aspect = new ‪UserAspect();
108  $subject = new ‪Context([
109  'coolio' => $aspect
110  ]);
111 
112  $this->expectException(AspectNotFoundException::class);
113  $this->expectExceptionCode(1527777641);
114  $subject->getAspect('uncoolio');
115  }
116 
121  {
122  $aspect = new ‪UserAspect();
123  $subject = new ‪Context([
124  'coolio' => $aspect
125  ]);
126 
127  self::assertSame($aspect, $subject->getAspect('coolio'));
128  }
129 
134  {
135  $aspect = new ‪UserAspect();
136  $subject = new ‪Context([
137  'coolio' => $aspect
138  ]);
139 
140  $this->expectException(AspectNotFoundException::class);
141  $this->expectExceptionCode(1527777868);
142  $subject->getPropertyFromAspect('uncoolio', 'does not matter');
143  }
144 
149  {
150  $defaultValue = 'default value';
151  $aspect = new ‪UserAspect();
152  $subject = new ‪Context([
153  'coolio' => $aspect
154  ]);
155 
156  $result = $subject->getPropertyFromAspect('coolio', 'unknownproperty', $defaultValue);
157  self::assertEquals($defaultValue, $result);
158  }
159 
164  {
165  $aspect = new ‪WorkspaceAspect(13);
166  $subject = new ‪Context([
167  'coolio' => $aspect
168  ]);
169 
170  $result = $subject->getPropertyFromAspect('coolio', 'id');
171  self::assertEquals(13, $result);
172  }
173 
178  {
179  $aspect = new ‪UserAspect();
180  $subject = new ‪Context();
181 
182  $subject->setAspect('coolio', $aspect);
183  self::assertSame($aspect, $subject->getAspect('coolio'));
184  }
185 
190  {
191  $initialAspect = new ‪UserAspect();
192  $aspectOverride = new ‪UserAspect();
193  $subject = new ‪Context([
194  'coolio' => $initialAspect
195  ]);
196 
197  $subject->setAspect('coolio', $aspectOverride);
198  self::assertNotSame($initialAspect, $subject->getAspect('coolio'));
199  self::assertSame($aspectOverride, $subject->getAspect('coolio'));
200  }
201 }
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\setAspectOverridesAnExisting
‪setAspectOverridesAnExisting()
Definition: ContextTest.php:189
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\invalidPropertyFromgetPropertyFromAspectReturnsDefault
‪invalidPropertyFromgetPropertyFromAspectReturnsDefault()
Definition: ContextTest.php:148
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\invalidAspectKeysDataProvider
‪array invalidAspectKeysDataProvider()
Definition: ContextTest.php:66
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:31
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\validPropertyFromgetPropertyFromAspectReturnsValue
‪validPropertyFromgetPropertyFromAspectReturnsValue()
Definition: ContextTest.php:163
‪TYPO3\CMS\Core\Registry
Definition: Registry.php:33
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\hasAspectReturnsTrueOnExistingAspect
‪hasAspectReturnsTrueOnExistingAspect(string $aspectName)
Definition: ContextTest.php:52
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\constructorAddsValidAspect
‪constructorAddsValidAspect()
Definition: ContextTest.php:92
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\invalidAspectFromgetPropertyFromAspectThrowsException
‪invalidAspectFromgetPropertyFromAspectThrowsException()
Definition: ContextTest.php:133
‪TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
Definition: AspectNotFoundException.php:26
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\getAspectThrowsExceptionOnInvalidAspect
‪getAspectThrowsExceptionOnInvalidAspect()
Definition: ContextTest.php:105
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\hasAspectReturnsFalseOnNonExistingAspect
‪hasAspectReturnsFalseOnNonExistingAspect(string $aspectName)
Definition: ContextTest.php:80
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\setAspectSetsAnAspectAndCanReturnIt
‪setAspectSetsAnAspectAndCanReturnIt()
Definition: ContextTest.php:177
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest
Definition: ContextTest.php:28
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:38
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\validAspectKeysDataProvider
‪array validAspectKeysDataProvider()
Definition: ContextTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\Context
Definition: ContextTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\getAspectReturnsValidAspect
‪getAspectReturnsValidAspect()
Definition: ContextTest.php:120