Skip to content

Commit 020a303

Browse files
committed
Fix error handling in loadFromFile methods and update exception types in tests
1 parent bde1440 commit 020a303

File tree

5 files changed

+14
-14
lines changed

5 files changed

+14
-14
lines changed

src/StaticPHP/Config/ArtifactConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static function loadFromDir(string $dir): void
3131
*/
3232
public static function loadFromFile(string $file): void
3333
{
34-
$content = file_get_contents($file);
34+
$content = @file_get_contents($file);
3535
if ($content === false) {
3636
throw new WrongUsageException("Failed to read artifact config file: {$file}");
3737
}

src/StaticPHP/Config/PackageConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static function loadFromDir(string $dir): void
3838
*/
3939
public static function loadFromFile(string $file): void
4040
{
41-
$content = file_get_contents($file);
41+
$content = @file_get_contents($file);
4242
if ($content === false) {
4343
throw new WrongUsageException("Failed to read package config file: {$file}");
4444
}

tests/StaticPHP/Config/ArtifactConfigTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected function setUp(): void
2626
$reflection = new \ReflectionClass(ArtifactConfig::class);
2727
$property = $reflection->getProperty('artifact_configs');
2828
$property->setAccessible(true);
29-
$property->setValue([]);
29+
$property->setValue(null, []);
3030
}
3131

3232
/** @noinspection PhpExpressionResultUnusedInspection */
@@ -42,7 +42,7 @@ protected function tearDown(): void
4242
$reflection = new \ReflectionClass(ArtifactConfig::class);
4343
$property = $reflection->getProperty('artifact_configs');
4444
$property->setAccessible(true);
45-
$property->setValue([]);
45+
$property->setValue(null, []);
4646
}
4747

4848
public function testLoadFromDirThrowsExceptionWhenDirectoryDoesNotExist(): void

tests/StaticPHP/Config/PackageConfigTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected function setUp(): void
2626
$reflection = new \ReflectionClass(PackageConfig::class);
2727
$property = $reflection->getProperty('package_configs');
2828
$property->setAccessible(true);
29-
$property->setValue([]);
29+
$property->setValue(null, []);
3030
}
3131

3232
protected function tearDown(): void
@@ -41,7 +41,7 @@ protected function tearDown(): void
4141
$reflection = new \ReflectionClass(PackageConfig::class);
4242
$property = $reflection->getProperty('package_configs');
4343
$property->setAccessible(true);
44-
$property->setValue([]);
44+
$property->setValue(null, []);
4545
}
4646

4747
public function testLoadFromDirThrowsExceptionWhenDirectoryDoesNotExist(): void

tests/StaticPHP/DI/CallbackInvokerTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
use DI\Container;
88
use PHPUnit\Framework\TestCase;
99
use StaticPHP\DI\CallbackInvoker;
10+
use StaticPHP\Exception\SPCInternalException;
1011

1112
/**
1213
* Helper class that requires constructor parameters for testing
1314
*/
14-
class UnresolvableTestClass
15+
readonly class UnresolvableTestClass
1516
{
16-
public function __construct(
17-
private string $requiredParam
18-
) {}
17+
/** @noinspection PhpPropertyOnlyWrittenInspection */
18+
public function __construct(private string $requiredParam) {}
1919
}
2020

2121
/**
@@ -92,7 +92,7 @@ public function testInvokeCallbackWithContainerResolution(): void
9292

9393
// Should not resolve from container as 'test.service' is not a type
9494
// Will try default value or null
95-
$this->expectException(\RuntimeException::class);
95+
$this->expectException(SPCInternalException::class);
9696
$this->invoker->invoke($callback);
9797
}
9898

@@ -139,7 +139,7 @@ public function testInvokeCallbackThrowsExceptionForUnresolvableParameter(): voi
139139
return $required;
140140
};
141141

142-
$this->expectException(\RuntimeException::class);
142+
$this->expectException(SPCInternalException::class);
143143
$this->expectExceptionMessage("Cannot resolve parameter 'required' of type 'string'");
144144
$this->invoker->invoke($callback);
145145
}
@@ -527,7 +527,7 @@ public function testInvokeWithUnionTypeThrowsException(): void
527527
$callback = eval('return function (string|int $param) { return $param; };');
528528

529529
// Union types are not ReflectionNamedType, should not be resolved from container
530-
$this->expectException(\RuntimeException::class);
530+
$this->expectException(SPCInternalException::class);
531531
$this->invoker->invoke($callback);
532532
}
533533

@@ -594,7 +594,7 @@ public function testInvokeWithContainerExceptionAndNoFallback(): void
594594
return $obj;
595595
};
596596

597-
$this->expectException(\RuntimeException::class);
597+
$this->expectException(SPCInternalException::class);
598598
$this->expectExceptionMessage("Cannot resolve parameter 'obj'");
599599

600600
$this->invoker->invoke($callback);

0 commit comments

Comments
 (0)