Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Sep 12, 2024
1 parent 9c31de0 commit 8004746
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Registry/ElementRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use function array_merge_recursive;
use function dirname;
use function file_exists;

final class ElementRegistry
{
Expand All @@ -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);
}
}
}
}
Expand Down
58 changes: 58 additions & 0 deletions tests/Registry/ElementRegistryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\XML;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use SimpleSAML\XML\Registry\ElementRegistry;

/**
* @package simplesamlphp\xml-common
*/
#[CoversClass(ElementRegistry::class)]
#[Group('registry')]
final class ElementRegistryTest extends TestCase
{
/** @var \SimpleSAML\XML\Registry\ElementRegistry */
protected static ElementRegistry $registry;


/**
*/
public static function setUpBeforeClass(): void
{
self::$registry = ElementRegistry::getInstance();
self::$registry->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);
}
}

0 comments on commit 8004746

Please sign in to comment.