|
12 | 12 | use Psr\Container\ContainerInterface;
|
13 | 13 | use stdClass;
|
14 | 14 |
|
| 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 | + |
15 | 39 | beforeEach(function () {
|
16 | 40 | $this->container = Mockery::mock(ContainerInterface::class);
|
17 | 41 | $this->container->shouldReceive('get')->with(VariousTypesHandler::class)->andReturn(new VariousTypesHandler());
|
|
232 | 256 | $element = new RegisteredElement([VariousTypesHandler::class, 'nonExistentMethod']);
|
233 | 257 | $element->handle($this->container, []);
|
234 | 258 | })->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