diff --git a/lib/OperatingSystems/FreeBSD.php b/lib/OperatingSystems/FreeBSD.php index 5535818b..db736eb8 100644 --- a/lib/OperatingSystems/FreeBSD.php +++ b/lib/OperatingSystems/FreeBSD.php @@ -119,7 +119,13 @@ public function getNetworkInfo(): array { public function getNetworkInterfaces(): array { $data = []; - foreach ($this->getNetInterfaces() as $interfaceName => $interface) { + try { + $interfaces = $this->getNetInterfaces(); + } catch (RuntimeException) { + return $data; + } + + foreach ($interfaces as $interfaceName => $interface) { $netInterface = new NetInterface($interfaceName, $interface['up']); $data[] = $netInterface; diff --git a/lib/OperatingSystems/Linux.php b/lib/OperatingSystems/Linux.php index bc886374..34440347 100644 --- a/lib/OperatingSystems/Linux.php +++ b/lib/OperatingSystems/Linux.php @@ -144,7 +144,13 @@ public function getNetworkInfo(): array { public function getNetworkInterfaces(): array { $data = []; - foreach ($this->getNetInterfaces() as $interfaceName => $interface) { + try { + $interfaces = $this->getNetInterfaces(); + } catch (RuntimeException) { + return $data; + } + + foreach ($interfaces as $interfaceName => $interface) { $netInterface = new NetInterface($interfaceName, $interface['up']); $data[] = $netInterface; diff --git a/tests/lib/FreeBSDTest.php b/tests/lib/FreeBSDTest.php index 9e1f320b..19f97056 100644 --- a/tests/lib/FreeBSDTest.php +++ b/tests/lib/FreeBSDTest.php @@ -134,6 +134,16 @@ public function testGetNetworkInterfaces(): void { $this->assertEquals($expected, $actual); } + public function testGetNetworkInterfacesError(): void { + $this->os->method('getNetInterfaces') + ->willThrowException(new RuntimeException('Unable to get network interfaces')); + + $expected = []; + $actual = $this->os->getNetworkInterfaces(); + + $this->assertEquals($expected, $actual); + } + public function testSupported(): void { $this->assertFalse($this->os->supported()); } diff --git a/tests/lib/LinuxTest.php b/tests/lib/LinuxTest.php index 4e184b4d..38361374 100644 --- a/tests/lib/LinuxTest.php +++ b/tests/lib/LinuxTest.php @@ -245,4 +245,14 @@ public function testGetNetworkInterfaces(): void { $this->assertEquals($expected, $actual); } + + public function testGetNetworkInterfacesError(): void { + $this->os->method('getNetInterfaces') + ->willThrowException(new RuntimeException('Unable to get network interfaces')); + + $expected = []; + $actual = $this->os->getNetworkInterfaces(); + + $this->assertEquals($expected, $actual); + } }