Continued with unit tests:
[core.git] / tests / framework / bootstrap / class_FrameworkBootstrapTest.php
1 <?php
2
3 // Same namespace as target class
4 namespace CoreFramework\Bootstrap;
5
6 // Inport framework stuff
7 use CoreFramework\Console\Tools\ConsoleTools;
8 use CoreFramework\Loader\ClassLoader;
9 use CoreFramework\Generic\NullPointerException;
10 use CoreFramework\Generic\UnsupportedOperationException;
11
12 // Import PHPUnit stuff
13 use PHPUnit\Framework\Error\Notice;
14 use PHPUnit\Framework\TestCase;
15
16 // Import SPL stuff
17 use \InvalidArgumentException;
18
19 /*
20  * Copyright (C) 2017 Roland Haeder<roland@mxchange.org>
21  *
22  * This program is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34  */
35 class FrameworkBootstrapTest extends TestCase {
36
37         /**
38          * Own IP address
39          */
40         private static $ipAddress = FALSE;
41
42         /**
43          * Setup test case
44          */
45         public function setUp() {
46                 // Trace message
47                 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
48
49                 // Call parent method
50                 parent::setUp();
51
52                 // Trace message
53                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
54
55         }
56
57         /**
58          * Setup test case
59          */
60         public static function setUpBeforeClass() {
61                 // Trace message
62                 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
63
64                 // Call parent method
65                 parent::setUpBeforeClass();
66
67                 /*
68                  * Disable strict naming-convention check in own class loader, because
69                  * PHP_Invoker doesn't have namespaces.
70                  */
71                 ClassLoader::enableStrictNamingConventionCheck(FALSE);
72
73                 // Lookup own IP address
74                 self::$ipAddress = ConsoleTools::acquireSelfIpAddress();
75
76                 // Trace message
77                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
78         }
79
80         /**
81          * Tests setting a NULL default timezone
82          */
83         public function testSettingNullDefaultTimezone () {
84                 // Will throw this exception
85                 $this->expectException(NullPointerException::class);
86
87                 // Test it
88                 FrameworkBootstrap::setDefaultTimezone(NULL);
89         }
90
91         /**
92          * Tests setting a boolean default timezone
93          */
94         public function testSettingBooleanDefaultTimezone () {
95                 // Will throw this exception
96                 $this->expectException(InvalidArgumentException::class);
97
98                 // Test it
99                 FrameworkBootstrap::setDefaultTimezone(FALSE);
100         }
101
102         /**
103          * Tests setting a decimal default timezone
104          */
105         public function testSettingDecimalDefaultTimezone () {
106                 // Will throw this exception
107                 $this->expectException(InvalidArgumentException::class);
108
109                 // Test it
110                 FrameworkBootstrap::setDefaultTimezone(12345);
111         }
112
113         /**
114          * Tests setting a float default timezone
115          */
116         public function testSettingFloatDefaultTimezone () {
117                 // Will throw this exception
118                 $this->expectException(InvalidArgumentException::class);
119
120                 // Test it
121                 FrameworkBootstrap::setDefaultTimezone(123.45);
122         }
123
124         /**
125          * Tests setting an array default timezone
126          */
127         public function testSettingArrayDefaultTimezone () {
128                 // Will throw this exception
129                 $this->expectException(InvalidArgumentException::class);
130
131                 // Test it
132                 FrameworkBootstrap::setDefaultTimezone(array());
133         }
134
135         /**
136          * Tests setting an object default timezone
137          */
138         public function testSettingObjectDefaultTimezone () {
139                 // Will throw this exception
140                 $this->expectException(InvalidArgumentException::class);
141
142                 // Test it
143                 FrameworkBootstrap::setDefaultTimezone($this);
144         }
145
146         /**
147          * Tests setting a resource default timezone
148          */
149         public function testSettingResourceDefaultTimezone () {
150                 // Will throw this exception
151                 $this->expectException(InvalidArgumentException::class);
152
153                 // Init some resource
154                 $resource = fopen(__FILE__, 'r');
155
156                 // Test it
157                 FrameworkBootstrap::setDefaultTimezone($resource);
158         }
159
160         /**
161          * Tests setting an empty default timezone
162          */
163         public function testSettingEmptyDefaultTimezone () {
164                 // Will throw this exception
165                 $this->expectException(InvalidArgumentException::class);
166
167                 // Test it
168                 FrameworkBootstrap::setDefaultTimezone('');
169         }
170
171         /**
172          * Tests setting invalid timezone
173          */
174         public function testSettingInvalidDefaultTimezone () {
175                 // Expect Notice
176                 $this->expectException(Notice::class);
177
178                 // Try to set it
179                 FrameworkBootstrap::setDefaultTimezone('!invalid!');
180         }
181
182         /**
183          * Tests setting valid timezone
184          */
185         public function testSettingValidDefaultTimezone () {
186                 // Will be true
187                 $this->assertTrue(FrameworkBootstrap::setDefaultTimezone('Europe/Berlin'));
188         }
189
190         /**
191          * Tests if detectServerAddress() is returning what it should for tests.
192          * This will always be 127.0.0.1.
193          */
194         public function testConfigDetectServerAddress () {
195                 // Call it
196                 $serverAddress = FrameworkBootstrap::detectServerAddress();
197
198                 // Should be the same
199                 $this->assertEquals(self::$ipAddress, $serverAddress);
200         }
201
202         /**
203          * Re-tests if detectServerAddress() is returning what it should for tests.
204          * This will always be 127.0.0.1. This call should not invoke
205          * ConsoleTools's method as the configuration entry is already cached.
206          */
207         public function testConfigDetectServerAddressCached () {
208                 // Call it
209                 $serverAddress = FrameworkBootstrap::detectServerAddress();
210
211                 // Should be the same
212                 $this->assertEquals(self::$ipAddress, $serverAddress);
213         }
214
215 }