]> git.mxchange.org Git - core.git/blobdiff - tests/framework/bootstrap/class_FrameworkBootstrapTest.php
Continued:
[core.git] / tests / framework / bootstrap / class_FrameworkBootstrapTest.php
index e7a8ccfb0635c0c5f30e5cf9e6282af001eaa15e..289c2433393fcbda642b555bac8bc66c449251e0 100644 (file)
@@ -6,6 +6,7 @@ namespace Org\Mxchange\CoreFramework\Bootstrap;
 use Org\Mxchange\CoreFramework\Console\Tools\ConsoleTools;
 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
+use Org\Mxchange\CoreFramework\Loader\ClassLoader;
 
 // Import PHPUnit stuff
 use PHPUnit\Framework\Error\Notice;
@@ -13,6 +14,7 @@ use PHPUnit\Framework\TestCase;
 
 // Import SPL stuff
 use \InvalidArgumentException;
+use \SplFileInfo;
 
 /*
  * Copyright (C) 2017 - 2023 Core Developer Team
@@ -35,7 +37,12 @@ class FrameworkBootstrapTest extends TestCase {
        /**
         * Own IP address
         */
-       private static $ipAddress = FALSE;
+       private static $ipAddress = '0.0.0.0';
+
+       /**
+        * Own host name
+        */
+       private static $hostname = 'host.invalid';
 
        /**
         * Setup test case
@@ -49,7 +56,6 @@ class FrameworkBootstrapTest extends TestCase {
 
                // Trace message
                //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
-
        }
 
        /**
@@ -57,16 +63,19 @@ class FrameworkBootstrapTest extends TestCase {
         */
        public static function setUpBeforeClass() {
                // Trace message
-               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
+               /* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
 
                // Call parent method
                parent::setUpBeforeClass();
 
+               // Lookup own hostname
+               self::$hostname = ConsoleTools::acquireHostname();
+
                // Lookup own IP address
                self::$ipAddress = ConsoleTools::acquireSelfIpAddress();
 
                // Trace message
-               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
+               /* NOISY-DEBUG: */ printf('[%s:%d]: self::ipAddress[%s]=%s,self::hostname=%s - EXIT!' . PHP_EOL, __METHOD__, __LINE__, gettype(self::$ipAddress), self::$ipAddress, self::$hostname);
        }
 
        /**
@@ -103,7 +112,7 @@ class FrameworkBootstrapTest extends TestCase {
         * Tests if detectServerAddress() is returning what it should for tests.
         * This will always be 127.0.0.1.
         */
-       public function testConfigDetectServerAddress () {
+       public function testBootstrapDetectServerAddress () {
                // Call it
                $serverAddress = FrameworkBootstrap::detectServerAddress();
 
@@ -116,12 +125,113 @@ class FrameworkBootstrapTest extends TestCase {
         * This will always be 127.0.0.1. This method should not invoke
         * ConsoleTools's method as the configuration entry is already cached.
         */
-       public function testConfigDetectServerAddressCached () {
+       public function testBootstrapDetectServerAddressCached () {
                // Call it
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
                $serverAddress = FrameworkBootstrap::detectServerAddress();
 
                // Should be the same
+               //* NOISY-DEBUG: */ printf('[%s:%d]: self::ipAddress=%s,serverAddress=%s' . PHP_EOL, __METHOD__, __LINE__, self::$ipAddress, $serverAddress);
                $this->assertEquals(self::$ipAddress, $serverAddress);
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
+       }
+
+       /**
+        * Tests method BootstrapFramework::isReachableFilePath() with a
+        * non-existing path. $isReachable should always return TRUE as nothing is
+        * restricting PHP.
+        */
+       public function testBootstrapIsReachableFilePathUnrestrictedNotExisting () {
+               // Init SPL file info instance
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
+               $infoInstance = new SplFileInfo('/does/not/exist/');
+
+               // Invoke method
+               /* NOISY-DEBUG: */ printf('[%s:%d]: infoInstance=%s' . PHP_EOL, __METHOD__, __LINE__, get_class($infoInstance));
+               $isReachable = FrameworkBootstrap::isReachableFilePath($infoInstance);
+
+               // Test if it is not reachable
+               /* NOISY-DEBUG: */ printf('[%s:%d]: isReachable=%d' . PHP_EOL, __METHOD__, __LINE__, intval($isReachable));
+               $this->assertTrue($isReachable, 'Returned true on a non-existing path');
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
+       }
+
+       /**
+        * Tests method BootstrapFramework::isReachableFilePath() with a
+        * non-existing path. $isReachable should be FALSE here as it is always
+        * outside the scope of open_basedir.
+        */
+       public function testBootstrapIsReachableFilePathRestrictedNotExisting () {
+               // Init SPL file info instance
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
+               $infoInstance = new SplFileInfo('/does/not/exist/');
+
+               // "Detect" root path
+               //* NOISY-DEBUG: */ printf('[%s:%d]: infoInstance=%s' . PHP_EOL, __METHOD__, __LINE__, get_class($infoInstance));
+               $rootScriptPath = realpath(dirname(dirname(FrameworkBootstrap::detectScriptPath())));
+
+               // Set it
+               /* NOISY-DEBUG: */ printf('[%s:%d]: open_basedir=%s,rootScriptPath[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, ini_get('open_basedir'), gettype($rootScriptPath), $rootScriptPath);
+               $result = ini_set('open_basedir', $rootScriptPath . ':/etc/');
+
+               // Was it set?
+               //* NOISY-DEBUG: */ printf('[%s:%d]: result[]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($result));
+               if ($result === FALSE) {
+                       // Didn't work
+                       $this->failed(sprintf('Cannot set open_basepath=%s', $rootScriptPath));
+               }
+
+               // Invoke method
+               //* NOISY-DEBUG: */ printf('[%s:%d]: Testing method FrameworkBootstrap::isReachableFilePath(%s) ...' . PHP_EOL, __METHOD__, __LINE__, get_class($infoInstance));
+               $isReachable = FrameworkBootstrap::isReachableFilePath($infoInstance);
+
+               // Test if 
+               //* NOISY-DEBUG: */ printf('[%s:%d]: isReachable=%d - Testing method ...' . PHP_EOL, __METHOD__, __LINE__, intval($isReachable));
+               $this->assertTrue(!$isReachable, 'Returned true on a non-existing path');
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
+       }
+
+       /**
+        * Tests method BootstrapFramework::isReachableFilePath() with an
+        * existing path. $isReachable should be TRUE here as it is within the scope
+        * of open_basedir.
+        */
+       public function testBootstrapIsReachableFilePathRestrictedExisting () {
+               // Init SPL file info instance
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
+               $infoInstance = new SplFileInfo(__DIR__);
+
+               // "Detect" root path
+               //* NOISY-DEBUG: */ printf('[%s:%d]: infoInstance=%s' . PHP_EOL, __METHOD__, __LINE__, get_class($infoInstance));
+               $rootScriptPath = realpath(dirname(dirname(FrameworkBootstrap::detectScriptPath())));
+
+               // Set it
+               //* NOISY-DEBUG: */ printf('[%s:%d]: rootScriptPath[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($rootScriptPath), $rootScriptPath);
+               $result = ini_set('open_basedir', $rootScriptPath . ':/etc/');
+
+               // Was it set?
+               //* NOISY-DEBUG: */ printf('[%s:%d]: result[]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($result));
+               if ($result === FALSE) {
+                       // Didn't work
+                       $this->failed(sprintf('Cannot set open_basepath=%s', $rootScriptPath));
+               }
+
+               // Invoke method
+               //* NOISY-DEBUG: */ printf('[%s:%d]: Testing method FrameworkBootstrap::isReachableFilePath(%s) ...' . PHP_EOL, __METHOD__, __LINE__, get_class($infoInstance));
+               $isReachable = FrameworkBootstrap::isReachableFilePath($infoInstance);
+
+               // Test if 
+               //* NOISY-DEBUG: */ printf('[%s:%d]: isReachable=%d - Testing method ...' . PHP_EOL, __METHOD__, __LINE__, intval($isReachable));
+               $this->assertTrue($isReachable, 'Returned true on a non-existing path');
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
        }
 
 }