diff --git a/system/Common.php b/system/Common.php index 0483fd5934..9256ff1cf6 100644 --- a/system/Common.php +++ b/system/Common.php @@ -725,8 +725,18 @@ if (! function_exists('is_windows')) { /** * Detect if platform is running in Windows. */ - function is_windows(): bool + function is_windows(?bool $mock = null): bool { + static $mocked; + + if (func_num_args() === 1) { + $mocked = $mock; + } + + if (isset($mocked)) { + return $mocked; + } + return DIRECTORY_SEPARATOR === '\\'; } } diff --git a/tests/system/CommonFunctionsTest.php b/tests/system/CommonFunctionsTest.php index 0049973f36..2e8c3be0d6 100644 --- a/tests/system/CommonFunctionsTest.php +++ b/tests/system/CommonFunctionsTest.php @@ -706,4 +706,19 @@ final class CommonFunctionsTest extends CIUnitTestCase $this->assertSame(strpos(php_uname(), 'Windows') !== false, is_windows()); $this->assertSame(defined('PHP_WINDOWS_VERSION_MAJOR'), is_windows()); } + + public function testIsWindowsUsingMock() + { + is_windows(true); + $this->assertTrue(is_windows()); + $this->assertNotFalse(is_windows()); + + is_windows(false); + $this->assertFalse(is_windows()); + $this->assertNotTrue(is_windows()); + + is_windows(null); + $this->assertSame(strpos(php_uname(), 'Windows') !== false, is_windows()); + $this->assertSame(defined('PHP_WINDOWS_VERSION_MAJOR'), is_windows()); + } } diff --git a/user_guide_src/source/general/common_functions.rst b/user_guide_src/source/general/common_functions.rst index 1a3a4f2990..b9ed2317c7 100755 --- a/user_guide_src/source/general/common_functions.rst +++ b/user_guide_src/source/general/common_functions.rst @@ -281,12 +281,19 @@ Miscellaneous Functions :returns: true if you can write to the file, false otherwise. :rtype: bool -.. php:function:: is_windows() +.. php:function:: is_windows([$mock = null]) + :param bool|null $mock: If given and is a boolean then it will be used as the return value. :rtype: bool Detect if platform is running in Windows. + .. note:: The boolean value provided to $mock will persist in subsequent calls. To reset this + mock value, the user must pass an explicit ``null`` to the function call. This will + refresh the function to use auto-detection. + + .. literalinclude:: common_functions/012.php + .. php:function:: log_message($level, $message [, $context]) :param string $level: The level of severity diff --git a/user_guide_src/source/general/common_functions/012.php b/user_guide_src/source/general/common_functions/012.php new file mode 100644 index 0000000000..7c716dc0b3 --- /dev/null +++ b/user_guide_src/source/general/common_functions/012.php @@ -0,0 +1,11 @@ +