‪TYPO3CMS  ‪main
ApplicationContextTest.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;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 final class ‪ApplicationContextTest extends UnitTestCase
30 {
34  public static function ‪allowedContexts(): array
35  {
36  return [
37  ['Production'],
38  ['Testing'],
39  ['Development'],
40 
41  ['Development/MyLocalComputer'],
42  ['Development/MyLocalComputer/Foo'],
43  ['Production/SpecialDeployment/LiveSystem'],
44  ];
45  }
46 
47  #[DataProvider('allowedContexts')]
48  #[Test]
50  {
51  $context = new ‪ApplicationContext($allowedContext);
52  self::assertSame($allowedContext, (string)$context);
53  }
54 
58  public static function ‪forbiddenContexts(): array
59  {
60  return [
61  ['MySpecialContext'],
62  ['Testing123'],
63  ['DevelopmentStuff'],
64  ['DevelopmentStuff/FooBar'],
65  ];
66  }
67 
68  #[DataProvider('forbiddenContexts')]
69  #[Test]
70  public function ‪constructorThrowsExceptionIfMainContextIsForbidden($forbiddenContext): void
71  {
72  $this->expectException(Exception::class);
73  $this->expectExceptionCode(1335436551);
74 
75  new ‪ApplicationContext($forbiddenContext);
76  }
77 
81  public static function ‪isMethods(): array
82  {
83  return [
84  'Development' => [
85  'contextName' => 'Development',
86  'isDevelopment' => true,
87  'isProduction' => false,
88  'isTesting' => false,
89  'parentContext' => null,
90  ],
91  'Development/YourSpecialContext' => [
92  'contextName' => 'Development/YourSpecialContext',
93  'isDevelopment' => true,
94  'isProduction' => false,
95  'isTesting' => false,
96  'parentContext' => 'Development',
97  ],
98 
99  'Production' => [
100  'contextName' => 'Production',
101  'isDevelopment' => false,
102  'isProduction' => true,
103  'isTesting' => false,
104  'parentContext' => null,
105  ],
106  'Production/MySpecialContext' => [
107  'contextName' => 'Production/MySpecialContext',
108  'isDevelopment' => false,
109  'isProduction' => true,
110  'isTesting' => false,
111  'parentContext' => 'Production',
112  ],
113 
114  'Testing' => [
115  'contextName' => 'Testing',
116  'isDevelopment' => false,
117  'isProduction' => false,
118  'isTesting' => true,
119  'parentContext' => null,
120  ],
121  'Testing/MySpecialContext' => [
122  'contextName' => 'Testing/MySpecialContext',
123  'isDevelopment' => false,
124  'isProduction' => false,
125  'isTesting' => true,
126  'parentContext' => 'Testing',
127  ],
128  ];
129  }
130 
131  #[DataProvider('isMethods')]
132  #[Test]
133  public function ‪contextMethodsReturnTheCorrectValues($contextName, $isDevelopment, $isProduction, $isTesting, $parentContext): void
134  {
135  $context = new ‪ApplicationContext($contextName);
136  self::assertSame($isDevelopment, $context->isDevelopment());
137  self::assertSame($isProduction, $context->isProduction());
138  self::assertSame($isTesting, $context->isTesting());
139  self::assertSame((string)$parentContext, (string)$context->getParent());
140  }
141 
142  #[Test]
144  {
145  $context = new ‪ApplicationContext('Production/Foo/Bar');
146  $parentContext = $context->getParent();
147  self::assertSame('Production/Foo', (string)$parentContext);
148 
149  $rootContext = $parentContext->getParent();
150  self::assertSame('Production', (string)$rootContext);
151  }
152 }
‪TYPO3\CMS\Core\Core\ApplicationContext
Definition: ApplicationContext.php:39
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Core\Tests\Unit\Core\ApplicationContextTest\parentContextIsConnectedRecursively
‪parentContextIsConnectedRecursively()
Definition: ApplicationContextTest.php:143
‪TYPO3\CMS\Core\Tests\Unit\Core\ApplicationContextTest\forbiddenContexts
‪static forbiddenContexts()
Definition: ApplicationContextTest.php:58
‪TYPO3\CMS\Core\Tests\Unit\Core\ApplicationContextTest\allowedContexts
‪static allowedContexts()
Definition: ApplicationContextTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\Core\ApplicationContextTest\constructorThrowsExceptionIfMainContextIsForbidden
‪constructorThrowsExceptionIfMainContextIsForbidden($forbiddenContext)
Definition: ApplicationContextTest.php:70
‪TYPO3\CMS\Core\Tests\Unit\Core
Definition: ApplicationContextTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Core\ApplicationContextTest
Definition: ApplicationContextTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Core\ApplicationContextTest\isMethods
‪static isMethods()
Definition: ApplicationContextTest.php:81
‪TYPO3\CMS\Core\Tests\Unit\Core\ApplicationContextTest\contextMethodsReturnTheCorrectValues
‪contextMethodsReturnTheCorrectValues($contextName, $isDevelopment, $isProduction, $isTesting, $parentContext)
Definition: ApplicationContextTest.php:133
‪TYPO3\CMS\Core\Tests\Unit\Core\ApplicationContextTest\contextStringCanBeSetInConstructorAndReadByCallingToString
‪contextStringCanBeSetInConstructorAndReadByCallingToString($allowedContext)
Definition: ApplicationContextTest.php:49