2 // Same namespace as target class
3 namespace Org\Mxchange\CoreFramework\Bootstrap;
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;
11 // Import PHPUnit stuff
12 use PHPUnit\Framework\Error\Notice;
13 use PHPUnit\Framework\TestCase;
16 use \InvalidArgumentException;
20 * Copyright (C) 2017 - 2023 Core Developer Team
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.
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.
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/>.
35 class FrameworkBootstrapTest extends TestCase {
40 private static $ipAddress = '0.0.0.0';
45 private static $hostname = 'host.invalid';
50 public function setUp() {
52 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
58 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
64 public static function setUpBeforeClass() {
66 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
67 parent::setUpBeforeClass();
69 // Lookup own hostname
70 self::$hostname = ConsoleTools::acquireHostname();
72 // Lookup own IP address
73 self::$ipAddress = ConsoleTools::acquireSelfIpAddress();
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);
80 * Tests setting an empty default timezone
82 public function testSettingEmptyDefaultTimezone () {
83 // Will throw this exception
84 $this->expectException(InvalidArgumentException::class);
87 FrameworkBootstrap::setDefaultTimezone('');
91 * Tests setting invalid timezone
93 public function testSettingInvalidDefaultTimezone () {
95 $this->expectException(Notice::class);
98 FrameworkBootstrap::setDefaultTimezone('!invalid!');
102 * Tests setting valid timezone
104 public function testSettingValidDefaultTimezone () {
106 $this->assertTrue(FrameworkBootstrap::setDefaultTimezone('Europe/Berlin'));
110 * Tests if detectServerAddress() is returning what it should for tests.
111 * This will always be 127.0.0.1.
113 public function testBootstrapDetectServerAddress () {
115 $serverAddress = FrameworkBootstrap::detectServerAddress();
117 // Should be the same
118 $this->assertEquals(self::$ipAddress, $serverAddress);
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.
126 public function testBootstrapDetectServerAddressCached () {
128 //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
129 $serverAddress = FrameworkBootstrap::detectServerAddress();
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);
136 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
140 * Tests method BootstrapFramework::isReachableFilePath() with a
141 * non-existing path. $isReachable should always return TRUE as nothing is
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/');
150 //* NOISY-DEBUG: */ printf('[%s:%d]: infoInstance=%s' . PHP_EOL, __METHOD__, __LINE__, get_class($infoInstance));
151 $isReachable = FrameworkBootstrap::isReachableFilePath($infoInstance);
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');
158 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
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.
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/');
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())));
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/');
180 //* NOISY-DEBUG: */ printf('[%s:%d]: result[]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($result));
181 if ($result === FALSE) {
183 $this->failed(sprintf('Cannot set open_basepath=%s', $rootScriptPath));
187 //* NOISY-DEBUG: */ printf('[%s:%d]: Testing method FrameworkBootstrap::isReachableFilePath(%s) ...' . PHP_EOL, __METHOD__, __LINE__, get_class($infoInstance));
188 $isReachable = FrameworkBootstrap::isReachableFilePath($infoInstance);
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');
195 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
199 * Tests method BootstrapFramework::isReachableFilePath() with an
200 * existing path. $isReachable should be TRUE here as it is within the scope
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__);
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())));
213 //* NOISY-DEBUG: */ printf('[%s:%d]: rootScriptPath[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($rootScriptPath), $rootScriptPath);
214 $result = ini_set('open_basedir', $rootScriptPath . ':/etc/');
217 //* NOISY-DEBUG: */ printf('[%s:%d]: result[]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($result));
218 if ($result === FALSE) {
220 $this->failed(sprintf('Cannot set open_basepath=%s', $rootScriptPath));
224 //* NOISY-DEBUG: */ printf('[%s:%d]: Testing method FrameworkBootstrap::isReachableFilePath(%s) ...' . PHP_EOL, __METHOD__, __LINE__, get_class($infoInstance));
225 $isReachable = FrameworkBootstrap::isReachableFilePath($infoInstance);
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');
232 //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);