V3 fix/phpunit (#1024)

This commit is contained in:
Jerry Ma 2026-02-06 17:02:36 +08:00 committed by GitHub
parent 7c3ac484b3
commit 041b08f10f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 139 additions and 131 deletions

View File

@ -7,6 +7,7 @@ namespace StaticPHP\Registry;
use StaticPHP\Config\ArtifactConfig; use StaticPHP\Config\ArtifactConfig;
use StaticPHP\Config\PackageConfig; use StaticPHP\Config\PackageConfig;
use StaticPHP\ConsoleApplication; use StaticPHP\ConsoleApplication;
use StaticPHP\Exception\FileSystemException;
use StaticPHP\Exception\RegistryException; use StaticPHP\Exception\RegistryException;
use StaticPHP\Util\FileSystem; use StaticPHP\Util\FileSystem;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
@ -87,116 +88,121 @@ class Registry
self::$current_registry_name = $registry_name; self::$current_registry_name = $registry_name;
// Load composer autoload if specified (for external registries with their own dependencies) try {
if (isset($data['autoload']) && is_string($data['autoload'])) { // Load composer autoload if specified (for external registries with their own dependencies)
$autoload_path = FileSystem::fullpath($data['autoload'], dirname($registry_file)); if (isset($data['autoload']) && is_string($data['autoload'])) {
if (file_exists($autoload_path)) { $autoload_path = FileSystem::fullpath($data['autoload'], dirname($registry_file));
logger()->debug("Loading external autoload from: {$autoload_path}"); if (file_exists($autoload_path)) {
require_once $autoload_path; logger()->debug("Loading external autoload from: {$autoload_path}");
} else { require_once $autoload_path;
logger()->warning("Autoload file not found: {$autoload_path}"); } else {
} logger()->warning("Autoload file not found: {$autoload_path}");
}
// load package configs
if (isset($data['package']['config']) && is_array($data['package']['config'])) {
foreach ($data['package']['config'] as $path) {
$path = FileSystem::fullpath($path, dirname($registry_file));
if (is_file($path)) {
self::$loaded_package_configs[] = PackageConfig::loadFromFile($path, $registry_name);
} elseif (is_dir($path)) {
self::$loaded_package_configs = array_merge(self::$loaded_package_configs, PackageConfig::loadFromDir($path, $registry_name));
} }
} }
}
// load artifact configs // load package configs
if (isset($data['artifact']['config']) && is_array($data['artifact']['config'])) { if (isset($data['package']['config']) && is_array($data['package']['config'])) {
foreach ($data['artifact']['config'] as $path) { foreach ($data['package']['config'] as $path) {
$path = FileSystem::fullpath($path, dirname($registry_file)); $path = FileSystem::fullpath($path, dirname($registry_file));
if (is_file($path)) { if (is_file($path)) {
self::$loaded_artifact_configs[] = ArtifactConfig::loadFromFile($path, $registry_name); self::$loaded_package_configs[] = PackageConfig::loadFromFile($path, $registry_name);
} elseif (is_dir($path)) { } elseif (is_dir($path)) {
self::$loaded_package_configs = array_merge(self::$loaded_package_configs, ArtifactConfig::loadFromDir($path, $registry_name)); self::$loaded_package_configs = array_merge(self::$loaded_package_configs, PackageConfig::loadFromDir($path, $registry_name));
}
} }
} }
}
// load doctor items from PSR-4 directories // load artifact configs
if (isset($data['doctor']['psr-4']) && is_assoc_array($data['doctor']['psr-4'])) { if (isset($data['artifact']['config']) && is_array($data['artifact']['config'])) {
foreach ($data['doctor']['psr-4'] as $namespace => $path) { foreach ($data['artifact']['config'] as $path) {
$path = FileSystem::fullpath($path, dirname($registry_file)); $path = FileSystem::fullpath($path, dirname($registry_file));
DoctorLoader::loadFromPsr4Dir($path, $namespace, $auto_require); if (is_file($path)) {
self::$loaded_artifact_configs[] = ArtifactConfig::loadFromFile($path, $registry_name);
} elseif (is_dir($path)) {
self::$loaded_package_configs = array_merge(self::$loaded_package_configs, ArtifactConfig::loadFromDir($path, $registry_name));
}
}
} }
}
// load doctor items from specific classes // load doctor items from PSR-4 directories
// Supports both array format ["ClassName"] and map format {"ClassName": "path/to/file.php"} if (isset($data['doctor']['psr-4']) && is_assoc_array($data['doctor']['psr-4'])) {
if (isset($data['doctor']['classes']) && is_array($data['doctor']['classes'])) { foreach ($data['doctor']['psr-4'] as $namespace => $path) {
foreach ($data['doctor']['classes'] as $key => $value) { $path = FileSystem::fullpath($path, dirname($registry_file));
[$class, $file] = self::parseClassEntry($key, $value); DoctorLoader::loadFromPsr4Dir($path, $namespace, $auto_require);
self::requireClassFile($class, $file, dirname($registry_file), $auto_require); }
DoctorLoader::loadFromClass($class);
} }
}
// load packages from PSR-4 directories // load doctor items from specific classes
if (isset($data['package']['psr-4']) && is_assoc_array($data['package']['psr-4'])) { // Supports both array format ["ClassName"] and map format {"ClassName": "path/to/file.php"}
foreach ($data['package']['psr-4'] as $namespace => $path) { if (isset($data['doctor']['classes']) && is_array($data['doctor']['classes'])) {
$path = FileSystem::fullpath($path, dirname($registry_file)); foreach ($data['doctor']['classes'] as $key => $value) {
PackageLoader::loadFromPsr4Dir($path, $namespace, $auto_require); [$class, $file] = self::parseClassEntry($key, $value);
self::requireClassFile($class, $file, dirname($registry_file), $auto_require);
DoctorLoader::loadFromClass($class);
}
} }
}
// load packages from specific classes // load packages from PSR-4 directories
// Supports both array format ["ClassName"] and map format {"ClassName": "path/to/file.php"} if (isset($data['package']['psr-4']) && is_assoc_array($data['package']['psr-4'])) {
if (isset($data['package']['classes']) && is_array($data['package']['classes'])) { foreach ($data['package']['psr-4'] as $namespace => $path) {
foreach ($data['package']['classes'] as $key => $value) { $path = FileSystem::fullpath($path, dirname($registry_file));
[$class, $file] = self::parseClassEntry($key, $value); PackageLoader::loadFromPsr4Dir($path, $namespace, $auto_require);
self::requireClassFile($class, $file, dirname($registry_file), $auto_require); }
PackageLoader::loadFromClass($class);
} }
}
// load artifacts from PSR-4 directories // load packages from specific classes
if (isset($data['artifact']['psr-4']) && is_assoc_array($data['artifact']['psr-4'])) { // Supports both array format ["ClassName"] and map format {"ClassName": "path/to/file.php"}
foreach ($data['artifact']['psr-4'] as $namespace => $path) { if (isset($data['package']['classes']) && is_array($data['package']['classes'])) {
$path = FileSystem::fullpath($path, dirname($registry_file)); foreach ($data['package']['classes'] as $key => $value) {
ArtifactLoader::loadFromPsr4Dir($path, $namespace, $auto_require); [$class, $file] = self::parseClassEntry($key, $value);
self::requireClassFile($class, $file, dirname($registry_file), $auto_require);
PackageLoader::loadFromClass($class);
}
} }
}
// load artifacts from specific classes // load artifacts from PSR-4 directories
// Supports both array format ["ClassName"] and map format {"ClassName": "path/to/file.php"} if (isset($data['artifact']['psr-4']) && is_assoc_array($data['artifact']['psr-4'])) {
if (isset($data['artifact']['classes']) && is_array($data['artifact']['classes'])) { foreach ($data['artifact']['psr-4'] as $namespace => $path) {
foreach ($data['artifact']['classes'] as $key => $value) { $path = FileSystem::fullpath($path, dirname($registry_file));
[$class, $file] = self::parseClassEntry($key, $value); ArtifactLoader::loadFromPsr4Dir($path, $namespace, $auto_require);
self::requireClassFile($class, $file, dirname($registry_file), $auto_require); }
ArtifactLoader::loadFromClass($class);
} }
}
// load additional commands from PSR-4 directories // load artifacts from specific classes
if (isset($data['command']['psr-4']) && is_assoc_array($data['command']['psr-4'])) { // Supports both array format ["ClassName"] and map format {"ClassName": "path/to/file.php"}
foreach ($data['command']['psr-4'] as $namespace => $path) { if (isset($data['artifact']['classes']) && is_array($data['artifact']['classes'])) {
$path = FileSystem::fullpath($path, dirname($registry_file)); foreach ($data['artifact']['classes'] as $key => $value) {
$classes = FileSystem::getClassesPsr4($path, $namespace, auto_require: $auto_require); [$class, $file] = self::parseClassEntry($key, $value);
$instances = array_map(fn ($x) => new $x(), $classes); self::requireClassFile($class, $file, dirname($registry_file), $auto_require);
ArtifactLoader::loadFromClass($class);
}
}
// load additional commands from PSR-4 directories
if (isset($data['command']['psr-4']) && is_assoc_array($data['command']['psr-4'])) {
foreach ($data['command']['psr-4'] as $namespace => $path) {
$path = FileSystem::fullpath($path, dirname($registry_file));
$classes = FileSystem::getClassesPsr4($path, $namespace, auto_require: $auto_require);
$instances = array_map(fn ($x) => new $x(), $classes);
ConsoleApplication::_addAdditionalCommands($instances);
}
}
// load additional commands from specific classes
// Supports both array format ["ClassName"] and map format {"ClassName": "path/to/file.php"}
if (isset($data['command']['classes']) && is_array($data['command']['classes'])) {
$instances = [];
foreach ($data['command']['classes'] as $key => $value) {
[$class, $file] = self::parseClassEntry($key, $value);
self::requireClassFile($class, $file, dirname($registry_file), $auto_require);
$instances[] = new $class();
}
ConsoleApplication::_addAdditionalCommands($instances); ConsoleApplication::_addAdditionalCommands($instances);
} }
} catch (FileSystemException $e) {
throw new RegistryException($e->getMessage(), 0, $e);
} }
// load additional commands from specific classes
// Supports both array format ["ClassName"] and map format {"ClassName": "path/to/file.php"}
if (isset($data['command']['classes']) && is_array($data['command']['classes'])) {
$instances = [];
foreach ($data['command']['classes'] as $key => $value) {
[$class, $file] = self::parseClassEntry($key, $value);
self::requireClassFile($class, $file, dirname($registry_file), $auto_require);
$instances[] = new $class();
}
ConsoleApplication::_addAdditionalCommands($instances);
}
self::$current_registry_name = null; self::$current_registry_name = null;
} }

View File

@ -50,7 +50,7 @@ class ArtifactConfigTest extends TestCase
$this->expectException(WrongUsageException::class); $this->expectException(WrongUsageException::class);
$this->expectExceptionMessage('Directory /nonexistent/path does not exist, cannot load artifact config.'); $this->expectExceptionMessage('Directory /nonexistent/path does not exist, cannot load artifact config.');
ArtifactConfig::loadFromDir('/nonexistent/path'); ArtifactConfig::loadFromDir('/nonexistent/path', 'test');
} }
public function testLoadFromDirWithValidArtifactJson(): void public function testLoadFromDirWithValidArtifactJson(): void
@ -63,7 +63,7 @@ class ArtifactConfigTest extends TestCase
file_put_contents($this->tempDir . '/artifact.json', $artifactContent); file_put_contents($this->tempDir . '/artifact.json', $artifactContent);
ArtifactConfig::loadFromDir($this->tempDir); ArtifactConfig::loadFromDir($this->tempDir, 'test');
$config = ArtifactConfig::get('test-artifact'); $config = ArtifactConfig::get('test-artifact');
$this->assertIsArray($config); $this->assertIsArray($config);
@ -88,7 +88,7 @@ class ArtifactConfigTest extends TestCase
file_put_contents($this->tempDir . '/artifact.lib.json', $artifact2Content); file_put_contents($this->tempDir . '/artifact.lib.json', $artifact2Content);
file_put_contents($this->tempDir . '/artifact.json', json_encode(['artifact-3' => ['source' => 'custom']])); file_put_contents($this->tempDir . '/artifact.json', json_encode(['artifact-3' => ['source' => 'custom']]));
ArtifactConfig::loadFromDir($this->tempDir); ArtifactConfig::loadFromDir($this->tempDir, 'test');
$this->assertNotNull(ArtifactConfig::get('artifact-1')); $this->assertNotNull(ArtifactConfig::get('artifact-1'));
$this->assertNotNull(ArtifactConfig::get('artifact-2')); $this->assertNotNull(ArtifactConfig::get('artifact-2'));
@ -100,7 +100,7 @@ class ArtifactConfigTest extends TestCase
$this->expectException(WrongUsageException::class); $this->expectException(WrongUsageException::class);
$this->expectExceptionMessage('Failed to read artifact config file:'); $this->expectExceptionMessage('Failed to read artifact config file:');
ArtifactConfig::loadFromFile('/nonexistent/file.json'); ArtifactConfig::loadFromFile('/nonexistent/file.json', 'test');
} }
public function testLoadFromFileThrowsExceptionWhenJsonIsInvalid(): void public function testLoadFromFileThrowsExceptionWhenJsonIsInvalid(): void
@ -111,7 +111,7 @@ class ArtifactConfigTest extends TestCase
$this->expectException(WrongUsageException::class); $this->expectException(WrongUsageException::class);
$this->expectExceptionMessage('Invalid JSON format in artifact config file:'); $this->expectExceptionMessage('Invalid JSON format in artifact config file:');
ArtifactConfig::loadFromFile($file); ArtifactConfig::loadFromFile($file, 'test');
} }
public function testLoadFromFileWithValidJson(): void public function testLoadFromFileWithValidJson(): void
@ -127,7 +127,7 @@ class ArtifactConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
ArtifactConfig::loadFromFile($file); ArtifactConfig::loadFromFile($file, 'test');
$config = ArtifactConfig::get('my-artifact'); $config = ArtifactConfig::get('my-artifact');
$this->assertIsArray($config); $this->assertIsArray($config);
@ -144,7 +144,7 @@ class ArtifactConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
ArtifactConfig::loadFromFile($file); ArtifactConfig::loadFromFile($file, 'test');
$all = ArtifactConfig::getAll(); $all = ArtifactConfig::getAll();
$this->assertIsArray($all); $this->assertIsArray($all);
@ -170,7 +170,7 @@ class ArtifactConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
ArtifactConfig::loadFromFile($file); ArtifactConfig::loadFromFile($file, 'test');
$config = ArtifactConfig::get('test-artifact'); $config = ArtifactConfig::get('test-artifact');
$this->assertIsArray($config); $this->assertIsArray($config);
@ -188,7 +188,7 @@ class ArtifactConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
ArtifactConfig::loadFromFile($file); ArtifactConfig::loadFromFile($file, 'test');
$config = ArtifactConfig::get('test-artifact'); $config = ArtifactConfig::get('test-artifact');
$this->assertIsArray($config); $this->assertIsArray($config);
@ -208,7 +208,7 @@ class ArtifactConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
ArtifactConfig::loadFromFile($file); ArtifactConfig::loadFromFile($file, 'test');
$config = ArtifactConfig::get('test-artifact'); $config = ArtifactConfig::get('test-artifact');
$this->assertIsArray($config['binary']); $this->assertIsArray($config['binary']);
@ -228,7 +228,7 @@ class ArtifactConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
ArtifactConfig::loadFromFile($file); ArtifactConfig::loadFromFile($file, 'test');
$config = ArtifactConfig::get('test-artifact'); $config = ArtifactConfig::get('test-artifact');
$this->assertIsArray($config['binary']); $this->assertIsArray($config['binary']);
@ -253,7 +253,7 @@ class ArtifactConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
ArtifactConfig::loadFromFile($file); ArtifactConfig::loadFromFile($file, 'test');
$config = ArtifactConfig::get('test-artifact'); $config = ArtifactConfig::get('test-artifact');
$this->assertIsArray($config['binary']); $this->assertIsArray($config['binary']);
@ -266,7 +266,7 @@ class ArtifactConfigTest extends TestCase
public function testLoadFromDirWithEmptyDirectory(): void public function testLoadFromDirWithEmptyDirectory(): void
{ {
// Empty directory should not throw exception // Empty directory should not throw exception
ArtifactConfig::loadFromDir($this->tempDir); ArtifactConfig::loadFromDir($this->tempDir, 'test');
$this->assertEquals([], ArtifactConfig::getAll()); $this->assertEquals([], ArtifactConfig::getAll());
} }
@ -279,8 +279,8 @@ class ArtifactConfigTest extends TestCase
file_put_contents($file1, json_encode(['art1' => ['source' => 'custom']])); file_put_contents($file1, json_encode(['art1' => ['source' => 'custom']]));
file_put_contents($file2, json_encode(['art2' => ['source' => 'custom']])); file_put_contents($file2, json_encode(['art2' => ['source' => 'custom']]));
ArtifactConfig::loadFromFile($file1); ArtifactConfig::loadFromFile($file1, 'test');
ArtifactConfig::loadFromFile($file2); ArtifactConfig::loadFromFile($file2, 'test');
$all = ArtifactConfig::getAll(); $all = ArtifactConfig::getAll();
$this->assertCount(2, $all); $this->assertCount(2, $all);

View File

@ -582,7 +582,7 @@ class ConfigValidatorTest extends TestCase
public function testValidateAndLintPackagesThrowsExceptionForWrongTypeString(): void public function testValidateAndLintPackagesThrowsExceptionForWrongTypeString(): void
{ {
$this->expectException(ValidationException::class); $this->expectException(ValidationException::class);
$this->expectExceptionMessage('Package test-pkg [artifact] must be string'); $this->expectExceptionMessage('Package test-pkg [artifact] has invalid type specification');
$data = [ $data = [
'test-pkg' => [ 'test-pkg' => [

View File

@ -6,6 +6,7 @@ namespace Tests\StaticPHP\Config;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use StaticPHP\Config\PackageConfig; use StaticPHP\Config\PackageConfig;
use StaticPHP\Exception\ValidationException;
use StaticPHP\Exception\WrongUsageException; use StaticPHP\Exception\WrongUsageException;
use StaticPHP\Runtime\SystemTarget; use StaticPHP\Runtime\SystemTarget;
@ -49,7 +50,7 @@ class PackageConfigTest extends TestCase
$this->expectException(WrongUsageException::class); $this->expectException(WrongUsageException::class);
$this->expectExceptionMessage('Directory /nonexistent/path does not exist, cannot load pkg.json config.'); $this->expectExceptionMessage('Directory /nonexistent/path does not exist, cannot load pkg.json config.');
PackageConfig::loadFromDir('/nonexistent/path'); PackageConfig::loadFromDir('/nonexistent/path', 'test');
} }
public function testLoadFromDirWithValidPkgJson(): void public function testLoadFromDirWithValidPkgJson(): void
@ -63,7 +64,7 @@ class PackageConfigTest extends TestCase
file_put_contents($this->tempDir . '/pkg.json', $packageContent); file_put_contents($this->tempDir . '/pkg.json', $packageContent);
PackageConfig::loadFromDir($this->tempDir); PackageConfig::loadFromDir($this->tempDir, 'test');
$this->assertTrue(PackageConfig::isPackageExists('test-pkg')); $this->assertTrue(PackageConfig::isPackageExists('test-pkg'));
} }
@ -87,7 +88,7 @@ class PackageConfigTest extends TestCase
file_put_contents($this->tempDir . '/pkg.lib.json', $pkg2Content); file_put_contents($this->tempDir . '/pkg.lib.json', $pkg2Content);
file_put_contents($this->tempDir . '/pkg.json', json_encode(['pkg-3' => ['type' => 'virtual-target']])); file_put_contents($this->tempDir . '/pkg.json', json_encode(['pkg-3' => ['type' => 'virtual-target']]));
PackageConfig::loadFromDir($this->tempDir); PackageConfig::loadFromDir($this->tempDir, 'test');
$this->assertTrue(PackageConfig::isPackageExists('pkg-1')); $this->assertTrue(PackageConfig::isPackageExists('pkg-1'));
$this->assertTrue(PackageConfig::isPackageExists('pkg-2')); $this->assertTrue(PackageConfig::isPackageExists('pkg-2'));
@ -99,7 +100,7 @@ class PackageConfigTest extends TestCase
$this->expectException(WrongUsageException::class); $this->expectException(WrongUsageException::class);
$this->expectExceptionMessage('Failed to read package config file:'); $this->expectExceptionMessage('Failed to read package config file:');
PackageConfig::loadFromFile('/nonexistent/file.json'); PackageConfig::loadFromFile('/nonexistent/file.json', 'test');
} }
public function testLoadFromFileThrowsExceptionWhenJsonIsInvalid(): void public function testLoadFromFileThrowsExceptionWhenJsonIsInvalid(): void
@ -107,10 +108,10 @@ class PackageConfigTest extends TestCase
$file = $this->tempDir . '/invalid.json'; $file = $this->tempDir . '/invalid.json';
file_put_contents($file, 'not valid json{'); file_put_contents($file, 'not valid json{');
$this->expectException(WrongUsageException::class); $this->expectException(ValidationException::class);
$this->expectExceptionMessage('Invalid JSON format in package config file:'); $this->expectExceptionMessage('invalid.json is broken');
PackageConfig::loadFromFile($file); PackageConfig::loadFromFile($file, 'test');
} }
public function testLoadFromFileWithValidJson(): void public function testLoadFromFileWithValidJson(): void
@ -124,7 +125,7 @@ class PackageConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
PackageConfig::loadFromFile($file); PackageConfig::loadFromFile($file, 'test');
$this->assertTrue(PackageConfig::isPackageExists('my-pkg')); $this->assertTrue(PackageConfig::isPackageExists('my-pkg'));
} }
@ -145,7 +146,7 @@ class PackageConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
PackageConfig::loadFromFile($file); PackageConfig::loadFromFile($file, 'test');
$this->assertTrue(PackageConfig::isPackageExists('test-pkg')); $this->assertTrue(PackageConfig::isPackageExists('test-pkg'));
} }
@ -160,7 +161,7 @@ class PackageConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
PackageConfig::loadFromFile($file); PackageConfig::loadFromFile($file, 'test');
$all = PackageConfig::getAll(); $all = PackageConfig::getAll();
$this->assertIsArray($all); $this->assertIsArray($all);
@ -189,7 +190,7 @@ class PackageConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
PackageConfig::loadFromFile($file); PackageConfig::loadFromFile($file, 'test');
$result = PackageConfig::get('test-pkg'); $result = PackageConfig::get('test-pkg');
$this->assertIsArray($result); $this->assertIsArray($result);
@ -208,7 +209,7 @@ class PackageConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
PackageConfig::loadFromFile($file); PackageConfig::loadFromFile($file, 'test');
$result = PackageConfig::get('test-pkg', 'artifact'); $result = PackageConfig::get('test-pkg', 'artifact');
$this->assertEquals('test-artifact', $result); $this->assertEquals('test-artifact', $result);
@ -225,7 +226,7 @@ class PackageConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
PackageConfig::loadFromFile($file); PackageConfig::loadFromFile($file, 'test');
$result = PackageConfig::get('test-pkg', 'non-existent-field', 'default'); $result = PackageConfig::get('test-pkg', 'non-existent-field', 'default');
$this->assertEquals('default', $result); $this->assertEquals('default', $result);
@ -251,7 +252,7 @@ class PackageConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
PackageConfig::loadFromFile($file); PackageConfig::loadFromFile($file, 'test');
// The get method will check SystemTarget::getTargetOS() // The get method will check SystemTarget::getTargetOS()
// On real Linux systems, it should return 'depends@linux' first // On real Linux systems, it should return 'depends@linux' first
@ -273,7 +274,7 @@ class PackageConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
PackageConfig::loadFromFile($file); PackageConfig::loadFromFile($file, 'test');
$result = PackageConfig::get('test-pkg', 'depends'); $result = PackageConfig::get('test-pkg', 'depends');
$this->assertEquals(['base-dep'], $result); $this->assertEquals(['base-dep'], $result);
@ -291,7 +292,7 @@ class PackageConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
PackageConfig::loadFromFile($file); PackageConfig::loadFromFile($file, 'test');
// 'artifact' is not in SUFFIX_ALLOWED_FIELDS, so it won't check suffixes // 'artifact' is not in SUFFIX_ALLOWED_FIELDS, so it won't check suffixes
$result = PackageConfig::get('test-pkg', 'artifact'); $result = PackageConfig::get('test-pkg', 'artifact');
@ -314,7 +315,7 @@ class PackageConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
PackageConfig::loadFromFile($file); PackageConfig::loadFromFile($file, 'test');
// These are all suffix-allowed fields // These are all suffix-allowed fields
$pkg = PackageConfig::get('test-pkg'); $pkg = PackageConfig::get('test-pkg');
@ -328,7 +329,7 @@ class PackageConfigTest extends TestCase
public function testLoadFromDirWithEmptyDirectory(): void public function testLoadFromDirWithEmptyDirectory(): void
{ {
// Empty directory should not throw exception // Empty directory should not throw exception
PackageConfig::loadFromDir($this->tempDir); PackageConfig::loadFromDir($this->tempDir, 'test');
$this->assertEquals([], PackageConfig::getAll()); $this->assertEquals([], PackageConfig::getAll());
} }
@ -341,8 +342,8 @@ class PackageConfigTest extends TestCase
file_put_contents($file1, json_encode(['pkg1' => ['type' => 'virtual-target']])); file_put_contents($file1, json_encode(['pkg1' => ['type' => 'virtual-target']]));
file_put_contents($file2, json_encode(['pkg2' => ['type' => 'virtual-target']])); file_put_contents($file2, json_encode(['pkg2' => ['type' => 'virtual-target']]));
PackageConfig::loadFromFile($file1); PackageConfig::loadFromFile($file1, 'test');
PackageConfig::loadFromFile($file2); PackageConfig::loadFromFile($file2, 'test');
$all = PackageConfig::getAll(); $all = PackageConfig::getAll();
$this->assertCount(2, $all); $this->assertCount(2, $all);
@ -366,7 +367,7 @@ class PackageConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
PackageConfig::loadFromFile($file); PackageConfig::loadFromFile($file, 'test');
$phpExt = PackageConfig::get('test-ext', 'php-extension'); $phpExt = PackageConfig::get('test-ext', 'php-extension');
$this->assertIsArray($phpExt); $this->assertIsArray($phpExt);
@ -384,7 +385,7 @@ class PackageConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
PackageConfig::loadFromFile($file); PackageConfig::loadFromFile($file, 'test');
$result = PackageConfig::get('test-pkg', 'non-existent'); $result = PackageConfig::get('test-pkg', 'non-existent');
$this->assertNull($result); $this->assertNull($result);
@ -411,7 +412,7 @@ class PackageConfigTest extends TestCase
]); ]);
file_put_contents($file, $content); file_put_contents($file, $content);
PackageConfig::loadFromFile($file); PackageConfig::loadFromFile($file, 'test');
$this->assertTrue(PackageConfig::isPackageExists('library-pkg')); $this->assertTrue(PackageConfig::isPackageExists('library-pkg'));
$this->assertTrue(PackageConfig::isPackageExists('extension-pkg')); $this->assertTrue(PackageConfig::isPackageExists('extension-pkg'));

View File

@ -7,6 +7,7 @@ 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
@ -92,7 +93,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 +140,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 +528,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 +595,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);