From 2dbd56aa66483257066389a6d1073434d38c9aa1 Mon Sep 17 00:00:00 2001 From: Madhav Gupta Date: Fri, 19 Dec 2025 21:24:21 +0530 Subject: [PATCH] feat: added Test Suite for Windows --- composer.json | 10 ++- phpunit.xml | 3 + src/System/System.php | 12 ++- tests/System/SystemTestWindows.php | 124 +++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 tests/System/SystemTestWindows.php diff --git a/composer.json b/composer.json index 0a00f92..fcf1f12 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,13 @@ "name": "utopia-php/system", "description": "A simple library for obtaining information about the host's system.", "type": "library", - "keywords": ["php","framework", "upf", "utopia", "system"], + "keywords": [ + "php", + "framework", + "upf", + "utopia", + "system" + ], "license": "MIT", "minimum-stability": "stable", "authors": [ @@ -33,4 +39,4 @@ "laravel/pint": "1.13.*", "phpstan/phpstan": "1.12.*" } -} +} \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index 7afd0dc..3755004 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -27,5 +27,8 @@ tests/System/SystemTestARMV8.php + + tests/System/SystemTestWindows.php + \ No newline at end of file diff --git a/src/System/System.php b/src/System/System.php index ca8b1da..d9dca54 100644 --- a/src/System/System.php +++ b/src/System/System.php @@ -16,7 +16,7 @@ class System public const ARMV8 = 'armv8'; - private const RegExX86 = '/(x86*|i386|i686)/'; + private const RegExX86 = '/(x86*|i386|i686|AMD64)/i'; private const RegexARM64 = '/(arm64|aarch64)/'; @@ -140,8 +140,14 @@ public static function getCPUCores(): int return count($matches[0]); case 'Darwin': return intval(shell_exec('sysctl -n hw.ncpu')); - case 'Windows': - return intval(shell_exec('wmic cpu get NumberOfCores')); + case 'Windows NT': + $output = shell_exec('wmic cpu get NumberOfCores'); + if ($output === null) { + throw new Exception('Unable to get CPU cores on Windows'); + } + // Parse output - wmic returns header line and value(s) + preg_match_all('/\d+/', $output, $matches); + return !empty($matches[0]) ? intval($matches[0][0]) : 0; default: throw new Exception(self::getOS().' not supported.'); } diff --git a/tests/System/SystemTestWindows.php b/tests/System/SystemTestWindows.php new file mode 100644 index 0000000..a3b2c3a --- /dev/null +++ b/tests/System/SystemTestWindows.php @@ -0,0 +1,124 @@ + + * + * @version 1.0 RC4 + * + * @license The MIT License (MIT) + */ + +namespace Utopia\Tests; + +use PHPUnit\Framework\TestCase; +use Utopia\System\System; + +class SystemTestWindows extends TestCase +{ + public function setUp(): void + { + if (System::getOS() !== 'Windows NT') { + $this->markTestSkipped('Windows-specific tests can only run on Windows'); + } + } + + public function tearDown(): void + { + } + + public function testOs(): void + { + $this->assertEquals('Windows NT', System::getOS()); + $this->assertIsString(System::getArch()); + $this->assertIsString(System::getArchEnum()); + $this->assertIsString(System::getHostname()); + } + + public function testGetCPUCores(): void + { + $cores = System::getCPUCores(); + $this->assertIsInt($cores); + $this->assertGreaterThan(0, $cores); + } + + public function testGetDiskTotal(): void + { + $total = System::getDiskTotal(); + $this->assertIsInt($total); + $this->assertGreaterThan(0, $total); + } + + public function testGetDiskFree(): void + { + $free = System::getDiskFree(); + $this->assertIsInt($free); + $this->assertGreaterThanOrEqual(0, $free); + + // Free space should be less than or equal to total + $this->assertLessThanOrEqual(System::getDiskTotal(), $free); + } + + public function testUnsupportedMethods(): void + { + // CPU Usage is not supported on Windows + $this->expectException(\Exception::class); + System::getCPUUsage(5); + } + + public function testGetMemoryTotalThrowsException(): void + { + $this->expectException(\Exception::class); + System::getMemoryTotal(); + } + + public function testGetMemoryFreeThrowsException(): void + { + $this->expectException(\Exception::class); + System::getMemoryFree(); + } + + public function testGetIOUsageThrowsException(): void + { + try { + System::getIOUsage(); + $this->fail('Expected exception was not thrown'); + } catch (\Exception $e) { + // Expected - method tries to read /proc/diskstats which doesn't exist on Windows + $this->assertTrue(true); + } catch (\Throwable $e) { + // Also acceptable - might throw warnings/errors + $this->assertTrue(true); + } + } + + public function testGetNetworkUsageThrowsException(): void + { + try { + System::getNetworkUsage(); + $this->fail('Expected exception was not thrown'); + } catch (\Exception $e) { + // Expected - method tries to access /sys/class/net which doesn't exist on Windows + $this->assertTrue(true); + } catch (\Throwable $e) { + // Also acceptable - might throw warnings/errors + $this->assertTrue(true); + } + } + + public function testGetEnv(): void + { + // Test with existing Windows environment variables + $path = System::getEnv('PATH', 'DEFAULT'); + $this->assertNotEquals('DEFAULT', $path); + $this->assertIsString($path); + + // Test with non-existent variable + $this->assertEquals('DEFAULT_VALUE', System::getEnv('NON_EXISTENT_VAR_12345', 'DEFAULT_VALUE')); + $this->assertNull(System::getEnv('NON_EXISTENT_VAR_12345')); + } +}