Skip to content

Commit dd16a5a

Browse files
test: Add tests for various handler types in RegisteredElement
1 parent 9e30fd2 commit dd16a5a

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

tests/Unit/Elements/RegisteredElementTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@
1212
use Psr\Container\ContainerInterface;
1313
use stdClass;
1414

15+
// --- Test Fixtures for Handler Types ---
16+
17+
class MyInvokableTestHandler
18+
{
19+
public function __invoke(string $name): string
20+
{
21+
return "Hello, {$name}!";
22+
}
23+
}
24+
25+
class MyStaticMethodTestHandler
26+
{
27+
public static function myStaticMethod(int $a, int $b): int
28+
{
29+
return $a + $b;
30+
}
31+
}
32+
33+
function my_global_test_function(bool $flag): string
34+
{
35+
return $flag ? 'on' : 'off';
36+
}
37+
38+
1539
beforeEach(function () {
1640
$this->container = Mockery::mock(ContainerInterface::class);
1741
$this->container->shouldReceive('get')->with(VariousTypesHandler::class)->andReturn(new VariousTypesHandler());
@@ -232,3 +256,40 @@
232256
$element = new RegisteredElement([VariousTypesHandler::class, 'nonExistentMethod']);
233257
$element->handle($this->container, []);
234258
})->throws(\ReflectionException::class, "VariousTypesHandler::nonExistentMethod() does not exist");
259+
260+
261+
describe('Handler Types', function () {
262+
it('handles invokable class handler', function () {
263+
$this->container->shouldReceive('get')
264+
->with(MyInvokableTestHandler::class)
265+
->andReturn(new MyInvokableTestHandler());
266+
267+
$element = new RegisteredElement(MyInvokableTestHandler::class);
268+
$result = $element->handle($this->container, ['name' => 'World']);
269+
270+
expect($result)->toBe('Hello, World!');
271+
});
272+
273+
it('handles closure handler', function () {
274+
$closure = function (string $a, string $b) {
275+
return $a . $b;
276+
};
277+
$element = new RegisteredElement($closure);
278+
$result = $element->handle($this->container, ['a' => 'foo', 'b' => 'bar']);
279+
expect($result)->toBe('foobar');
280+
});
281+
282+
it('handles static method handler', function () {
283+
$handler = [MyStaticMethodTestHandler::class, 'myStaticMethod'];
284+
$element = new RegisteredElement($handler);
285+
$result = $element->handle($this->container, ['a' => 5, 'b' => 10]);
286+
expect($result)->toBe(15);
287+
});
288+
289+
it('handles global function name handler', function () {
290+
$handler = 'PhpMcp\Server\Tests\Unit\Elements\my_global_test_function';
291+
$element = new RegisteredElement($handler);
292+
$result = $element->handle($this->container, ['flag' => true]);
293+
expect($result)->toBe('on');
294+
});
295+
});

0 commit comments

Comments
 (0)