]> git.mxchange.org Git - core.git/blob - tests/framework/bootstrap/class_FrameworkBootstrapTest.php
Continued:
[core.git] / tests / framework / bootstrap / class_FrameworkBootstrapTest.php
1 <?php
2 // Same namespace as target class
3 namespace Org\Mxchange\CoreFramework\Bootstrap;
4
5 // Inport framework stuff
6 use Org\Mxchange\CoreFramework\Console\Tools\ConsoleTools;
7 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
8 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
9 use Org\Mxchange\CoreFramework\Loader\ClassLoader;
10
11 // Import PHPUnit stuff
12 use PHPUnit\Framework\Error\Notice;
13 use PHPUnit\Framework\TestCase;
14
15 // Import SPL stuff
16 use \InvalidArgumentException;
17 use \SplFileInfo;
18
19 /*
20  * Copyright (C) 2017 - 2023 Core Developer Team
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 = '0.0.0.0';
41
42         /**
43          * Own host name
44          */
45         private static $hostname = 'host.invalid';
46
47         /**
48          * Setup test case
49          */
50         public function setUp() {
51                 // Trace message
52                 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
53
54                 // Call parent method
55                 parent::setUp();
56
57                 // Trace message
58                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
59         }
60
61         /**
62          * Setup test case
63          */
64         public static function setUpBeforeClass() {
65                 // Call parent method
66                 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
67                 parent::setUpBeforeClass();
68
69                 // Lookup own hostname
70                 self::$hostname = ConsoleTools::acquireHostname();
71
72                 // Lookup own IP address
73                 self::$ipAddress = ConsoleTools::acquireSelfIpAddress();
74
75                 // Trace message
76                 //* NOISY-DEBUG: */ printf('[%s:%d]: self::ipAddress[%s]=%s,self::hostname=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, gettype(self::$ipAddress), self::$ipAddress, self::$hostname);
77         }
78
79         /**
80          * Tests setting an empty default timezone
81          */
82         public function testSettingEmptyDefaultTimezone () {
83                 // Will throw this exception
84                 $this->expectException(InvalidArgumentException::class);
85
86                 // Test it
87                 FrameworkBootstrap::setDefaultTimezone('');
88         }
89
90         /**
91          * Tests setting invalid timezone
92          */
93         public function testSettingInvalidDefaultTimezone () {
94                 // Expect Notice
95                 $this->expectException(Notice::class);
96
97                 // Try to set it
98                 FrameworkBootstrap::setDefaultTimezone('!invalid!');
99         }
100
101         /**
102          * Tests setting valid timezone
103          */
104         public function testSettingValidDefaultTimezone () {
105                 // Will be true
106                 $this->assertTrue(FrameworkBootstrap::setDefaultTimezone('Europe/Berlin'));
107         }
108
109         /**
110          * Tests if detectServerAddress() is returning what it should for tests.
111          * This will always be 127.0.0.1.
112          */
113         public function testBootstrapDetectServerAddress () {
114                 // Call it
115                 $serverAddress = FrameworkBootstrap::detectServerAddress();
116
117                 // Should be the same
118                 $this->assertEquals(self::$ipAddress, $serverAddress);
119         }
120
121         /**
122          * Re-tests if detectServerAddress() is returning what it should for tests.
123          * This will always be 127.0.0.1. This method should not invoke
124          * ConsoleTools's method as the configuration entry is already cached.
125          */
126         public function testBootstrapDetectServerAddressCached () {
127                 // Call it
128                 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
129                 $serverAddress = FrameworkBootstrap::detectServerAddress();
130
131                 // Should be the same
132                 //* NOISY-DEBUG: */ printf('[%s:%d]: self::ipAddress=%s,serverAddress=%s' . PHP_EOL, __METHOD__, __LINE__, self::$ipAddress, $serverAddress);
133                 $this->assertEquals(self::$ipAddress, $serverAddress);
134
135                 // Trace message
136                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
137         }
138
139         /**
140          * Tests method BootstrapFramework::isReachableFilePath() with a
141          * non-existing path. $isReachable should always return TRUE as nothing is
142          * restricting PHP.
143          */
144         public function testBootstrapIsReachableFilePathUnrestrictedNotExisting () {
145                 // Init SPL file info instance
146                 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
147                 $infoInstance = new SplFileInfo('/does/not/exist/');
148
149                 // Invoke method
150                 //* NOISY-DEBUG: */ printf('[%s:%d]: infoInstance=%s' . PHP_EOL, __METHOD__, __LINE__, get_class($infoInstance));
151                 $isReachable = FrameworkBootstrap::isReachableFilePath($infoInstance);
152
153                 // Test if it is not reachable
154                 //* NOISY-DEBUG: */ printf('[%s:%d]: isReachable=%d' . PHP_EOL, __METHOD__, __LINE__, intval($isReachable));
155                 $this->assertTrue($isReachable, 'Returned true on a non-existing path');
156
157                 // Trace message
158                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
159         }
160
161         /**
162          * Tests method BootstrapFramework::isReachableFilePath() with a
163          * non-existing path. $isReachable should be FALSE here as it is always
164          * outside the scope of open_basedir.
165          */
166         public function testBootstrapIsReachableFilePathRestrictedNotExisting () {
167                 // Init SPL file info instance
168                 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
169                 $infoInstance = new SplFileInfo('/does/not/exist/');
170
171                 // "Detect" root path
172                 //* NOISY-DEBUG: */ printf('[%s:%d]: infoInstance=%s' . PHP_EOL, __METHOD__, __LINE__, get_class($infoInstance));
173                 $rootScriptPath = realpath(dirname(dirname(FrameworkBootstrap::detectScriptPath())));
174
175                 // Set it
176                 //* NOISY-DEBUG: */ printf('[%s:%d]: open_basedir=%s,rootScriptPath[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, ini_get('open_basedir'), gettype($rootScriptPath), $rootScriptPath);
177                 $result = ini_set('open_basedir', $rootScriptPath . ':/etc/');
178
179                 // Was it set?
180                 //* NOISY-DEBUG: */ printf('[%s:%d]: result[]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($result));
181                 if ($result === FALSE) {
182                         // Didn't work
183                         $this->failed(sprintf('Cannot set open_basepath=%s', $rootScriptPath));
184                 }
185
186                 // Invoke method
187                 //* NOISY-DEBUG: */ printf('[%s:%d]: Testing method FrameworkBootstrap::isReachableFilePath(%s) ...' . PHP_EOL, __METHOD__, __LINE__, get_class($infoInstance));
188                 $isReachable = FrameworkBootstrap::isReachableFilePath($infoInstance);
189
190                 // Test if 
191                 //* NOISY-DEBUG: */ printf('[%s:%d]: isReachable=%d - Testing method ...' . PHP_EOL, __METHOD__, __LINE__, intval($isReachable));
192                 $this->assertTrue(!$isReachable, 'Returned true on a non-existing path');
193
194                 // Trace message
195                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
196         }
197
198         /**
199          * Tests method BootstrapFramework::isReachableFilePath() with an
200          * existing path. $isReachable should be TRUE here as it is within the scope
201          * of open_basedir.
202          */
203         public function testBootstrapIsReachableFilePathRestrictedExisting () {
204                 // Init SPL file info instance
205                 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
206                 $infoInstance = new SplFileInfo(__DIR__);
207
208                 // "Detect" root path
209                 //* NOISY-DEBUG: */ printf('[%s:%d]: infoInstance=%s' . PHP_EOL, __METHOD__, __LINE__, get_class($infoInstance));
210                 $rootScriptPath = realpath(dirname(dirname(FrameworkBootstrap::detectScriptPath())));
211
212                 // Set it
213                 //* NOISY-DEBUG: */ printf('[%s:%d]: rootScriptPath[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($rootScriptPath), $rootScriptPath);
214                 $result = ini_set('open_basedir', $rootScriptPath . ':/etc/');
215
216                 // Was it set?
217                 //* NOISY-DEBUG: */ printf('[%s:%d]: result[]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($result));
218                 if ($result === FALSE) {
219                         // Didn't work
220                         $this->failed(sprintf('Cannot set open_basepath=%s', $rootScriptPath));
221                 }
222
223                 // Invoke method
224                 //* NOISY-DEBUG: */ printf('[%s:%d]: Testing method FrameworkBootstrap::isReachableFilePath(%s) ...' . PHP_EOL, __METHOD__, __LINE__, get_class($infoInstance));
225                 $isReachable = FrameworkBootstrap::isReachableFilePath($infoInstance);
226
227                 // Test if 
228                 //* NOISY-DEBUG: */ printf('[%s:%d]: isReachable=%d - Testing method ...' . PHP_EOL, __METHOD__, __LINE__, intval($isReachable));
229                 $this->assertTrue($isReachable, 'Returned true on a non-existing path');
230
231                 // Trace message
232                 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
233         }
234
235 }