Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -33,4 +39,4 @@
"laravel/pint": "1.13.*",
"phpstan/phpstan": "1.12.*"
}
}
}
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@
<testsuite name="ARMV8">
<file>tests/System/SystemTestARMV8.php</file>
</testsuite>
<testsuite name="Windows">
<file>tests/System/SystemTestWindows.php</file>
</testsuite>
</testsuites>
</phpunit>
12 changes: 9 additions & 3 deletions src/System/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)/';

Expand Down Expand Up @@ -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;
Comment on lines +143 to +150
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Returning 0 cores is misleading; throw an exception instead.

When preg_match_all finds no numeric matches, returning 0 is incorrect—no system has zero CPU cores. Throwing an exception would be consistent with the null-output case and more reliable for error handling.

Additionally, WMIC is deprecated as of Windows 10 version 21H1 and will be removed from Windows 11 version 25H2. Use PowerShell with WMI interfaces instead via shell_exec() for better long-term compatibility.

🔎 Proposed fix
             // Parse output - wmic returns header line and value(s)
             preg_match_all('/\d+/', $output, $matches);
-            return !empty($matches[0]) ? intval($matches[0][0]) : 0;
+            if (empty($matches[0])) {
+                throw new Exception('Unable to parse CPU cores from wmic output');
+            }
+            return intval($matches[0][0]);

default:
throw new Exception(self::getOS().' not supported.');
}
Expand Down
124 changes: 124 additions & 0 deletions tests/System/SystemTestWindows.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

/**
* Utopia PHP Framework
*
*
* @link https://github.com/utopia-php/framework
*
* @author Eldad Fux <eldad@appwrite.io>
*
* @version 1.0 RC4
*
* @license The MIT License (MIT) <http://www.opensource.org/licenses/mit-license.php>
*/

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'));
}
}