‪TYPO3CMS  9.5
ContextTest.php
Go to the documentation of this file.
1 <?php
2 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 
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
25 class ‪ContextTest extends UnitTestCase
26 {
32  public function ‪validAspectKeysDataProvider(): array
33  {
34  return [
35  ['myfirst'],
36  ['mysecond'],
37  ['date'],
38  ['visibility'],
39  ['backend.user'],
40  ['frontend.user'],
41  ['workspace'],
42  ];
43  }
44 
50  public function ‪hasAspectReturnsTrueOnExistingAspect(string $aspectName)
51  {
52  $subject = new ‪Context([
53  'myfirst' => new ‪UserAspect(),
54  'mysecond' => new ‪UserAspect(),
55  ]);
56  $this->assertTrue($subject->hasAspect($aspectName));
57  }
58 
64  public function ‪invalidAspectKeysDataProvider(): array
65  {
66  return [
67  ['visible'],
68  ['frontenduser'],
69  ['compatibility'],
70  ];
71  }
72 
78  public function ‪hasAspectReturnsFalseOnNonExistingAspect(string $aspectName)
79  {
80  $subject = new ‪Context([
81  'myfirst' => new ‪UserAspect(),
82  'mysecond' => new ‪UserAspect(),
83  ]);
84  $this->assertFalse($subject->hasAspect($aspectName));
85  }
86 
91  {
92  $subject = new ‪Context([
93  'coolio' => new ‪UserAspect(),
94  'uncoolio' => new ‪Registry()
95  ]);
96  $this->assertTrue($subject->hasAspect('coolio'));
97  $this->assertFalse($subject->hasAspect('uncoolio'));
98  }
99 
104  {
105  $aspect = new ‪UserAspect();
106  $subject = new ‪Context([
107  'coolio' => $aspect
108  ]);
109 
110  $this->expectException(AspectNotFoundException::class);
111  $this->expectExceptionCode(1527777641);
112  $subject->getAspect('uncoolio');
113  }
114 
119  {
120  $aspect = new ‪UserAspect();
121  $subject = new ‪Context([
122  'coolio' => $aspect
123  ]);
124 
125  $this->assertSame($aspect, $subject->getAspect('coolio'));
126  }
127 
132  {
133  $aspect = new ‪UserAspect();
134  $subject = new ‪Context([
135  'coolio' => $aspect
136  ]);
137 
138  $this->expectException(AspectNotFoundException::class);
139  $this->expectExceptionCode(1527777868);
140  $subject->getPropertyFromAspect('uncoolio', 'does not matter');
141  }
142 
147  {
148  $defaultValue = 'default value';
149  $aspect = new ‪UserAspect();
150  $subject = new ‪Context([
151  'coolio' => $aspect
152  ]);
153 
154  $result = $subject->getPropertyFromAspect('coolio', 'unknownproperty', $defaultValue);
155  $this->assertEquals($defaultValue, $result);
156  }
157 
162  {
163  $aspect = new ‪WorkspaceAspect(13);
164  $subject = new ‪Context([
165  'coolio' => $aspect
166  ]);
167 
168  $result = $subject->getPropertyFromAspect('coolio', 'id');
169  $this->assertEquals(13, $result);
170  }
171 
176  {
177  $aspect = new ‪UserAspect();
178  $subject = new ‪Context();
179 
180  $subject->setAspect('coolio', $aspect);
181  $this->assertSame($aspect, $subject->getAspect('coolio'));
182  }
183 
188  {
189  $initialAspect = new ‪UserAspect();
190  $aspectOverride = new ‪UserAspect();
191  $subject = new ‪Context([
192  'coolio' => $initialAspect
193  ]);
194 
195  $subject->setAspect('coolio', $aspectOverride);
196  $this->assertNotSame($initialAspect, $subject->getAspect('coolio'));
197  $this->assertSame($aspectOverride, $subject->getAspect('coolio'));
198  }
199 }
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\setAspectOverridesAnExisting
‪setAspectOverridesAnExisting()
Definition: ContextTest.php:187
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\invalidPropertyFromgetPropertyFromAspectReturnsDefault
‪invalidPropertyFromgetPropertyFromAspectReturnsDefault()
Definition: ContextTest.php:146
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\invalidAspectKeysDataProvider
‪array invalidAspectKeysDataProvider()
Definition: ContextTest.php:64
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:29
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\validPropertyFromgetPropertyFromAspectReturnsValue
‪validPropertyFromgetPropertyFromAspectReturnsValue()
Definition: ContextTest.php:161
‪TYPO3\CMS\Core\Registry
Definition: Registry.php:32
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:49
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\hasAspectReturnsTrueOnExistingAspect
‪hasAspectReturnsTrueOnExistingAspect(string $aspectName)
Definition: ContextTest.php:50
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\constructorAddsValidAspect
‪constructorAddsValidAspect()
Definition: ContextTest.php:90
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\invalidAspectFromgetPropertyFromAspectThrowsException
‪invalidAspectFromgetPropertyFromAspectThrowsException()
Definition: ContextTest.php:131
‪TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
Definition: AspectNotFoundException.php:24
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\getAspectThrowsExceptionOnInvalidAspect
‪getAspectThrowsExceptionOnInvalidAspect()
Definition: ContextTest.php:103
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\hasAspectReturnsFalseOnNonExistingAspect
‪hasAspectReturnsFalseOnNonExistingAspect(string $aspectName)
Definition: ContextTest.php:78
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\setAspectSetsAnAspectAndCanReturnIt
‪setAspectSetsAnAspectAndCanReturnIt()
Definition: ContextTest.php:175
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest
Definition: ContextTest.php:26
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:36
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\validAspectKeysDataProvider
‪array validAspectKeysDataProvider()
Definition: ContextTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Context
Definition: ContextTest.php:3
‪TYPO3\CMS\Core\Tests\Unit\Context\ContextTest\getAspectReturnsValidAspect
‪getAspectReturnsValidAspect()
Definition: ContextTest.php:118