From 8004746d830d3d6d7a11ec36c9987bd480feffce Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Thu, 12 Sep 2024 18:25:55 +0200 Subject: [PATCH] Add unit tests --- src/Registry/ElementRegistry.php | 13 +++--- tests/Registry/ElementRegistryTest.php | 58 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 tests/Registry/ElementRegistryTest.php diff --git a/src/Registry/ElementRegistry.php b/src/Registry/ElementRegistry.php index ec1ad5f..1d1b6ed 100644 --- a/src/Registry/ElementRegistry.php +++ b/src/Registry/ElementRegistry.php @@ -11,6 +11,7 @@ use function array_merge_recursive; use function dirname; +use function file_exists; final class ElementRegistry { @@ -26,11 +27,13 @@ final private function __construct() // Initialize the registry with all the elements we know $classesDir = dirname(__FILE__, 3) . '/vendor/simplesamlphp/composer-xmlprovider-installer/classes'; - $finder = Finder::create()->files()->name('element.registry.*.php')->in($classesDir); - if ($finder->hasResults()) { - foreach ($finder as $file) { - $elements = include($file); - $this->registry = array_merge_recursive($this->registry, $elements); + if (file_exists($classesDir) === true) { + $finder = Finder::create()->files()->name('element.registry.*.php')->in($classesDir); + if ($finder->hasResults()) { + foreach ($finder as $file) { + $elements = include($file); + $this->registry = array_merge_recursive($this->registry, $elements); + } } } } diff --git a/tests/Registry/ElementRegistryTest.php b/tests/Registry/ElementRegistryTest.php new file mode 100644 index 0000000..f38acf3 --- /dev/null +++ b/tests/Registry/ElementRegistryTest.php @@ -0,0 +1,58 @@ +registerElementHandler('\SimpleSAML\Test\XML\Element'); + } + + + /** + */ + public function testFetchingHandlerWorks(): void + { + $handler = self::$registry->getElementHandler('urn:x-simplesamlphp:namespace', 'Element'); + $this->assertEquals($handler, '\SimpleSAML\Test\XML\Element'); + } + + + /** + */ + public function testAddingHandlerWorks(): void + { + self::$registry->registerElementHandler('\SimpleSAML\Test\XML\ExtendableElement'); + $handler = self::$registry->getElementHandler('urn:x-simplesamlphp:namespace', 'ExtendableElement'); + $this->assertEquals($handler, '\SimpleSAML\Test\XML\ExtendableElement'); + } + + + /** + */ + public function testUnknownHandlerReturnsNull(): void + { + $handler = self::$registry->getElementHandler('urn:x-simplesamlphp:namespace', 'UnknownElement'); + $this->assertNull($handler); + } +}