php-sdk/tests/Unit/Capability/Registry/Loader/ChainLoaderTest.php
Christopher Hertel 967c5cda4d
[Server] Untangle origin tracking from Registry (#302)
Drop the isManual flag from ElementReference and the discovery-state methods
from RegistryInterface. The Registry becomes a flat last-write-wins map;
DiscoveryLoader owns its own owned-set bookkeeping so re-running discovery
only removes elements it itself contributed. Adds ChainLoader for explicit
composition. Manual-over-discovered precedence is preserved via loader
ordering, not a flag on the reference.
2026-05-17 14:03:31 +02:00

57 lines
1.7 KiB
PHP

<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Tests\Unit\Capability\Registry\Loader;
use Mcp\Capability\Registry;
use Mcp\Capability\Registry\Loader\ChainLoader;
use Mcp\Tests\Unit\Capability\Registry\Loader\Stub\RecordingLoader;
use Mcp\Tests\Unit\Capability\Registry\Loader\Stub\ToolWriterLoader;
use PHPUnit\Framework\TestCase;
class ChainLoaderTest extends TestCase
{
public function testInvokesChildLoadersInOrder(): void
{
$calls = new \ArrayObject();
$a = new RecordingLoader('A', $calls);
$b = new RecordingLoader('B', $calls);
$c = new RecordingLoader('C', $calls);
(new ChainLoader([$a, $b, $c]))->load(new Registry());
$this->assertSame(['A', 'B', 'C'], $calls->getArrayCopy());
}
public function testLastWriterWinsForConflictingKeys(): void
{
$registry = new Registry();
$first = new ToolWriterLoader('shared', static fn () => 'first');
$second = new ToolWriterLoader('shared', static fn () => 'second');
(new ChainLoader([$first, $second]))->load($registry);
$this->assertSame('second', ($registry->getTool('shared')->handler)());
}
public function testEmptyChainIsNoop(): void
{
$registry = new Registry();
(new ChainLoader([]))->load($registry);
$this->assertFalse($registry->hasTools());
$this->assertFalse($registry->hasResources());
$this->assertFalse($registry->hasResourceTemplates());
$this->assertFalse($registry->hasPrompts());
}
}