Fix error handling in loadFromFile methods and update exception types in tests

This commit is contained in:
crazywhalecc 2025-12-10 11:33:39 +08:00
parent bde1440617
commit 020a30315d
No known key found for this signature in database
GPG Key ID: 1F4BDD59391F2680
5 changed files with 14 additions and 14 deletions

View File

@ -31,7 +31,7 @@ class ArtifactConfig
*/ */
public static function loadFromFile(string $file): void public static function loadFromFile(string $file): void
{ {
$content = file_get_contents($file); $content = @file_get_contents($file);
if ($content === false) { if ($content === false) {
throw new WrongUsageException("Failed to read artifact config file: {$file}"); throw new WrongUsageException("Failed to read artifact config file: {$file}");
} }

View File

@ -38,7 +38,7 @@ class PackageConfig
*/ */
public static function loadFromFile(string $file): void public static function loadFromFile(string $file): void
{ {
$content = file_get_contents($file); $content = @file_get_contents($file);
if ($content === false) { if ($content === false) {
throw new WrongUsageException("Failed to read package config file: {$file}"); throw new WrongUsageException("Failed to read package config file: {$file}");
} }

View File

@ -26,7 +26,7 @@ class ArtifactConfigTest extends TestCase
$reflection = new \ReflectionClass(ArtifactConfig::class); $reflection = new \ReflectionClass(ArtifactConfig::class);
$property = $reflection->getProperty('artifact_configs'); $property = $reflection->getProperty('artifact_configs');
$property->setAccessible(true); $property->setAccessible(true);
$property->setValue([]); $property->setValue(null, []);
} }
/** @noinspection PhpExpressionResultUnusedInspection */ /** @noinspection PhpExpressionResultUnusedInspection */
@ -42,7 +42,7 @@ class ArtifactConfigTest extends TestCase
$reflection = new \ReflectionClass(ArtifactConfig::class); $reflection = new \ReflectionClass(ArtifactConfig::class);
$property = $reflection->getProperty('artifact_configs'); $property = $reflection->getProperty('artifact_configs');
$property->setAccessible(true); $property->setAccessible(true);
$property->setValue([]); $property->setValue(null, []);
} }
public function testLoadFromDirThrowsExceptionWhenDirectoryDoesNotExist(): void public function testLoadFromDirThrowsExceptionWhenDirectoryDoesNotExist(): void

View File

@ -26,7 +26,7 @@ class PackageConfigTest extends TestCase
$reflection = new \ReflectionClass(PackageConfig::class); $reflection = new \ReflectionClass(PackageConfig::class);
$property = $reflection->getProperty('package_configs'); $property = $reflection->getProperty('package_configs');
$property->setAccessible(true); $property->setAccessible(true);
$property->setValue([]); $property->setValue(null, []);
} }
protected function tearDown(): void protected function tearDown(): void
@ -41,7 +41,7 @@ class PackageConfigTest extends TestCase
$reflection = new \ReflectionClass(PackageConfig::class); $reflection = new \ReflectionClass(PackageConfig::class);
$property = $reflection->getProperty('package_configs'); $property = $reflection->getProperty('package_configs');
$property->setAccessible(true); $property->setAccessible(true);
$property->setValue([]); $property->setValue(null, []);
} }
public function testLoadFromDirThrowsExceptionWhenDirectoryDoesNotExist(): void public function testLoadFromDirThrowsExceptionWhenDirectoryDoesNotExist(): void

View File

@ -7,15 +7,15 @@ namespace Tests\StaticPHP\DI;
use DI\Container; use DI\Container;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use StaticPHP\DI\CallbackInvoker; use StaticPHP\DI\CallbackInvoker;
use StaticPHP\Exception\SPCInternalException;
/** /**
* Helper class that requires constructor parameters for testing * Helper class that requires constructor parameters for testing
*/ */
class UnresolvableTestClass readonly class UnresolvableTestClass
{ {
public function __construct( /** @noinspection PhpPropertyOnlyWrittenInspection */
private string $requiredParam public function __construct(private string $requiredParam) {}
) {}
} }
/** /**
@ -92,7 +92,7 @@ class CallbackInvokerTest extends TestCase
// Should not resolve from container as 'test.service' is not a type // Should not resolve from container as 'test.service' is not a type
// Will try default value or null // Will try default value or null
$this->expectException(\RuntimeException::class); $this->expectException(SPCInternalException::class);
$this->invoker->invoke($callback); $this->invoker->invoke($callback);
} }
@ -139,7 +139,7 @@ class CallbackInvokerTest extends TestCase
return $required; return $required;
}; };
$this->expectException(\RuntimeException::class); $this->expectException(SPCInternalException::class);
$this->expectExceptionMessage("Cannot resolve parameter 'required' of type 'string'"); $this->expectExceptionMessage("Cannot resolve parameter 'required' of type 'string'");
$this->invoker->invoke($callback); $this->invoker->invoke($callback);
} }
@ -527,7 +527,7 @@ class CallbackInvokerTest extends TestCase
$callback = eval('return function (string|int $param) { return $param; };'); $callback = eval('return function (string|int $param) { return $param; };');
// Union types are not ReflectionNamedType, should not be resolved from container // Union types are not ReflectionNamedType, should not be resolved from container
$this->expectException(\RuntimeException::class); $this->expectException(SPCInternalException::class);
$this->invoker->invoke($callback); $this->invoker->invoke($callback);
} }
@ -594,7 +594,7 @@ class CallbackInvokerTest extends TestCase
return $obj; return $obj;
}; };
$this->expectException(\RuntimeException::class); $this->expectException(SPCInternalException::class);
$this->expectExceptionMessage("Cannot resolve parameter 'obj'"); $this->expectExceptionMessage("Cannot resolve parameter 'obj'");
$this->invoker->invoke($callback); $this->invoker->invoke($callback);