3611ffa1d24a8d5c0f902c6de27f6ba8f249f79f
[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 script belongs to the TYPO3 Flow framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 use TYPO3\CMS\Core\Core\ApplicationContext;
15
16 /**
17 * Testcase for the ApplicationContext class
18 */
19 class ApplicationContextTest extends \TYPO3\Components\TestingFramework\Core\UnitTestCase
20 {
21 /**
22 * Data provider with allowed contexts.
23 *
24 * @return array
25 */
26 public function allowedContexts()
27 {
28 return [
29 ['Production'],
30 ['Testing'],
31 ['Development'],
32
33 ['Development/MyLocalComputer'],
34 ['Development/MyLocalComputer/Foo'],
35 ['Production/SpecialDeployment/LiveSystem'],
36 ];
37 }
38
39 /**
40 * @test
41 * @dataProvider allowedContexts
42 */
43 public function contextStringCanBeSetInConstructorAndReadByCallingToString($allowedContext)
44 {
45 $context = new ApplicationContext($allowedContext);
46 $this->assertSame($allowedContext, (string)$context);
47 }
48
49 /**
50 * Data provider with forbidden contexts.
51 *
52 * @return array
53 */
54 public function forbiddenContexts()
55 {
56 return [
57 ['MySpecialContexz'],
58 ['Testing123'],
59 ['DevelopmentStuff'],
60 ['DevelopmentStuff/FooBar'],
61 ];
62 }
63
64 /**
65 * @test
66 * @dataProvider forbiddenContexts
67 */
68 public function constructorThrowsExceptionIfMainContextIsForbidden($forbiddenContext)
69 {
70 $this->expectException(\TYPO3\CMS\Core\Exception::class);
71 $this->expectExceptionCode(1335436551);
72
73 new ApplicationContext($forbiddenContext);
74 }
75
76 /**
77 * Data provider with expected is*() values for various contexts.
78 *
79 * @return array
80 */
81 public function isMethods()
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 /**
132 * @test
133 * @dataProvider isMethods
134 */
135 public function contextMethodsReturnTheCorrectValues($contextName, $isDevelopment, $isProduction, $isTesting, $parentContext)
136 {
137 $context = new ApplicationContext($contextName);
138 $this->assertSame($isDevelopment, $context->isDevelopment());
139 $this->assertSame($isProduction, $context->isProduction());
140 $this->assertSame($isTesting, $context->isTesting());
141 $this->assertSame((string)$parentContext, (string)$context->getParent());
142 }
143
144 /**
145 * @test
146 */
147 public function parentContextIsConnectedRecursively()
148 {
149 $context = new ApplicationContext('Production/Foo/Bar');
150 $parentContext = $context->getParent();
151 $this->assertSame('Production/Foo', (string)$parentContext);
152
153 $rootContext = $parentContext->getParent();
154 $this->assertSame('Production', (string)$rootContext);
155 }
156 }