[TASK] Anonymize token in Exception handlers
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Core / ApplicationContextTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Core;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Core\ApplicationContext;
18
19 /**
20 * Testcase for the ApplicationContext class
21 */
22 class ApplicationContextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
24 /**
25 * Data provider with allowed contexts.
26 *
27 * @return array
28 */
29 public function allowedContexts()
30 {
31 return [
32 ['Production'],
33 ['Testing'],
34 ['Development'],
35
36 ['Development/MyLocalComputer'],
37 ['Development/MyLocalComputer/Foo'],
38 ['Production/SpecialDeployment/LiveSystem'],
39 ];
40 }
41
42 /**
43 * @test
44 * @dataProvider allowedContexts
45 */
46 public function contextStringCanBeSetInConstructorAndReadByCallingToString($allowedContext)
47 {
48 $context = new ApplicationContext($allowedContext);
49 $this->assertSame($allowedContext, (string)$context);
50 }
51
52 /**
53 * Data provider with forbidden contexts.
54 *
55 * @return array
56 */
57 public function forbiddenContexts()
58 {
59 return [
60 ['MySpecialContexz'],
61 ['Testing123'],
62 ['DevelopmentStuff'],
63 ['DevelopmentStuff/FooBar'],
64 ];
65 }
66
67 /**
68 * @test
69 * @dataProvider forbiddenContexts
70 */
71 public function constructorThrowsExceptionIfMainContextIsForbidden($forbiddenContext)
72 {
73 $this->expectException(\TYPO3\CMS\Core\Exception::class);
74 $this->expectExceptionCode(1335436551);
75
76 new ApplicationContext($forbiddenContext);
77 }
78
79 /**
80 * Data provider with expected is*() values for various contexts.
81 *
82 * @return array
83 */
84 public function isMethods()
85 {
86 return [
87 'Development' => [
88 'contextName' => 'Development',
89 'isDevelopment' => true,
90 'isProduction' => false,
91 'isTesting' => false,
92 'parentContext' => null
93 ],
94 'Development/YourSpecialContext' => [
95 'contextName' => 'Development/YourSpecialContext',
96 'isDevelopment' => true,
97 'isProduction' => false,
98 'isTesting' => false,
99 'parentContext' => 'Development'
100 ],
101
102 'Production' => [
103 'contextName' => 'Production',
104 'isDevelopment' => false,
105 'isProduction' => true,
106 'isTesting' => false,
107 'parentContext' => null
108 ],
109 'Production/MySpecialContext' => [
110 'contextName' => 'Production/MySpecialContext',
111 'isDevelopment' => false,
112 'isProduction' => true,
113 'isTesting' => false,
114 'parentContext' => 'Production'
115 ],
116
117 'Testing' => [
118 'contextName' => 'Testing',
119 'isDevelopment' => false,
120 'isProduction' => false,
121 'isTesting' => true,
122 'parentContext' => null
123 ],
124 'Testing/MySpecialContext' => [
125 'contextName' => 'Testing/MySpecialContext',
126 'isDevelopment' => false,
127 'isProduction' => false,
128 'isTesting' => true,
129 'parentContext' => 'Testing'
130 ]
131 ];
132 }
133
134 /**
135 * @test
136 * @dataProvider isMethods
137 */
138 public function contextMethodsReturnTheCorrectValues($contextName, $isDevelopment, $isProduction, $isTesting, $parentContext)
139 {
140 $context = new ApplicationContext($contextName);
141 $this->assertSame($isDevelopment, $context->isDevelopment());
142 $this->assertSame($isProduction, $context->isProduction());
143 $this->assertSame($isTesting, $context->isTesting());
144 $this->assertSame((string)$parentContext, (string)$context->getParent());
145 }
146
147 /**
148 * @test
149 */
150 public function parentContextIsConnectedRecursively()
151 {
152 $context = new ApplicationContext('Production/Foo/Bar');
153 $parentContext = $context->getParent();
154 $this->assertSame('Production/Foo', (string)$parentContext);
155
156 $rootContext = $parentContext->getParent();
157 $this->assertSame('Production', (string)$rootContext);
158 }
159 }