|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Laravel\Boost\Mcp\Methods\CallToolWithExecutor; |
| 6 | +use Laravel\Boost\Mcp\ToolExecutor; |
| 7 | +use Laravel\Boost\Mcp\Tools\GetConfig; |
| 8 | +use Laravel\Mcp\Response; |
| 9 | +use Laravel\Mcp\Server\ServerContext; |
| 10 | +use Laravel\Mcp\Server\Transport\JsonRpcRequest; |
| 11 | +use Laravel\Mcp\Server\Transport\JsonRpcResponse; |
| 12 | + |
| 13 | +test('handles tool execution with ResponseFactory correctly', function (): void { |
| 14 | + // Mock ToolExecutor to return a Response |
| 15 | + $executor = Mockery::mock(ToolExecutor::class); |
| 16 | + $response = Response::json(['key' => 'app.name', 'value' => 'Laravel']); |
| 17 | + $executor->shouldReceive('execute') |
| 18 | + ->once() |
| 19 | + ->with(GetConfig::class, ['key' => 'app.name']) |
| 20 | + ->andReturn($response); |
| 21 | + |
| 22 | + // Create a tool instance |
| 23 | + $tool = new GetConfig; |
| 24 | + |
| 25 | + // Create ServerContext with the tool |
| 26 | + $context = Mockery::mock(ServerContext::class); |
| 27 | + $context->shouldReceive('tools') |
| 28 | + ->once() |
| 29 | + ->andReturn(collect([$tool])); |
| 30 | + |
| 31 | + // Create JsonRpcRequest mock |
| 32 | + $request = Mockery::mock(JsonRpcRequest::class); |
| 33 | + $request->shouldReceive('get') |
| 34 | + ->with('name') |
| 35 | + ->andReturn($tool->name()); |
| 36 | + $request->params = [ |
| 37 | + 'name' => $tool->name(), |
| 38 | + 'arguments' => ['key' => 'app.name'], |
| 39 | + ]; |
| 40 | + $request->id = 'test-request-1'; |
| 41 | + |
| 42 | + // Create CallToolWithExecutor instance |
| 43 | + $handler = new CallToolWithExecutor($executor); |
| 44 | + |
| 45 | + // Execute - this should not throw BadMethodCallException |
| 46 | + // The fix ensures we call ->responses() before ->map() on ResponseFactory |
| 47 | + $jsonRpcResponse = $handler->handle($request, $context); |
| 48 | + |
| 49 | + // Verify it returns a JsonRpcResponse without throwing BadMethodCallException |
| 50 | + expect($jsonRpcResponse)->toBeInstanceOf(JsonRpcResponse::class); |
| 51 | +}); |
| 52 | + |
| 53 | +test('handles tool execution error correctly', function (): void { |
| 54 | + // Mock ToolExecutor to throw an exception |
| 55 | + $executor = Mockery::mock(ToolExecutor::class); |
| 56 | + $executor->shouldReceive('execute') |
| 57 | + ->once() |
| 58 | + ->with(GetConfig::class, ['key' => 'app.name']) |
| 59 | + ->andThrow(new RuntimeException('Test error')); |
| 60 | + |
| 61 | + // Create a tool instance |
| 62 | + $tool = new GetConfig; |
| 63 | + |
| 64 | + // Create ServerContext with the tool |
| 65 | + $context = Mockery::mock(ServerContext::class); |
| 66 | + $context->shouldReceive('tools') |
| 67 | + ->once() |
| 68 | + ->andReturn(collect([$tool])); |
| 69 | + |
| 70 | + // Create JsonRpcRequest mock |
| 71 | + $request = Mockery::mock(JsonRpcRequest::class); |
| 72 | + $request->shouldReceive('get') |
| 73 | + ->with('name') |
| 74 | + ->andReturn($tool->name()); |
| 75 | + $request->params = [ |
| 76 | + 'name' => $tool->name(), |
| 77 | + 'arguments' => ['key' => 'app.name'], |
| 78 | + ]; |
| 79 | + $request->id = 'test-request-2'; |
| 80 | + |
| 81 | + // Create CallToolWithExecutor instance |
| 82 | + $handler = new CallToolWithExecutor($executor); |
| 83 | + |
| 84 | + // Execute - should handle the exception gracefully without throwing BadMethodCallException |
| 85 | + $jsonRpcResponse = $handler->handle($request, $context); |
| 86 | + |
| 87 | + // Verify it returns a JsonRpcResponse |
| 88 | + expect($jsonRpcResponse)->toBeInstanceOf(JsonRpcResponse::class); |
| 89 | +}); |
| 90 | + |
| 91 | +test('handles ResponseFactory with multiple responses correctly', function (): void { |
| 92 | + // Mock ToolExecutor to return a Response |
| 93 | + $executor = Mockery::mock(ToolExecutor::class); |
| 94 | + $response = Response::json(['key' => 'app.name', 'value' => 'Laravel']); |
| 95 | + $executor->shouldReceive('execute') |
| 96 | + ->once() |
| 97 | + ->with(GetConfig::class, ['key' => 'app.name']) |
| 98 | + ->andReturn($response); |
| 99 | + |
| 100 | + // Create a tool instance |
| 101 | + $tool = new GetConfig; |
| 102 | + |
| 103 | + // Create ServerContext with the tool |
| 104 | + $context = Mockery::mock(ServerContext::class); |
| 105 | + $context->shouldReceive('tools') |
| 106 | + ->once() |
| 107 | + ->andReturn(collect([$tool])); |
| 108 | + |
| 109 | + // Create JsonRpcRequest mock |
| 110 | + $request = Mockery::mock(JsonRpcRequest::class); |
| 111 | + $request->shouldReceive('get') |
| 112 | + ->with('name') |
| 113 | + ->andReturn($tool->name()); |
| 114 | + $request->params = [ |
| 115 | + 'name' => $tool->name(), |
| 116 | + 'arguments' => ['key' => 'app.name'], |
| 117 | + ]; |
| 118 | + $request->id = 'test-request-3'; |
| 119 | + |
| 120 | + // Create CallToolWithExecutor instance |
| 121 | + $handler = new CallToolWithExecutor($executor); |
| 122 | + |
| 123 | + // Execute - this specifically tests that ->responses()->map() works |
| 124 | + // The fix ensures we call ->responses() before ->map() on ResponseFactory |
| 125 | + // This should not throw BadMethodCallException |
| 126 | + $jsonRpcResponse = $handler->handle($request, $context); |
| 127 | + |
| 128 | + // Verify it returns a JsonRpcResponse without throwing BadMethodCallException |
| 129 | + expect($jsonRpcResponse)->toBeInstanceOf(JsonRpcResponse::class); |
| 130 | +}); |
| 131 | + |
| 132 | +test('throws JsonRpcException when tool name is missing', function (): void { |
| 133 | + $executor = Mockery::mock(ToolExecutor::class); |
| 134 | + $context = Mockery::mock(ServerContext::class); |
| 135 | + |
| 136 | + // Create JsonRpcRequest mock without 'name' parameter |
| 137 | + $request = Mockery::mock(JsonRpcRequest::class); |
| 138 | + $request->shouldReceive('get') |
| 139 | + ->with('name') |
| 140 | + ->andReturn(null); |
| 141 | + $request->id = 'test-request-4'; |
| 142 | + |
| 143 | + $handler = new CallToolWithExecutor($executor); |
| 144 | + |
| 145 | + try { |
| 146 | + $handler->handle($request, $context); |
| 147 | + expect(false)->toBeTrue('Expected JsonRpcException to be thrown'); |
| 148 | + } catch (\Laravel\Mcp\Server\Exceptions\JsonRpcException $e) { |
| 149 | + expect($e->getMessage())->toContain('Missing [name] parameter'); |
| 150 | + } |
| 151 | +}); |
| 152 | + |
| 153 | +test('throws JsonRpcException when tool is not found', function (): void { |
| 154 | + $executor = Mockery::mock(ToolExecutor::class); |
| 155 | + |
| 156 | + // Create ServerContext with no tools |
| 157 | + $context = Mockery::mock(ServerContext::class); |
| 158 | + $context->shouldReceive('tools') |
| 159 | + ->once() |
| 160 | + ->andReturn(collect([])); |
| 161 | + |
| 162 | + // Create JsonRpcRequest mock with non-existent tool |
| 163 | + $request = Mockery::mock(JsonRpcRequest::class); |
| 164 | + $request->shouldReceive('get') |
| 165 | + ->with('name') |
| 166 | + ->andReturn('non-existent-tool'); |
| 167 | + $request->params = [ |
| 168 | + 'name' => 'non-existent-tool', |
| 169 | + 'arguments' => [], |
| 170 | + ]; |
| 171 | + $request->id = 'test-request-5'; |
| 172 | + |
| 173 | + $handler = new CallToolWithExecutor($executor); |
| 174 | + |
| 175 | + try { |
| 176 | + $handler->handle($request, $context); |
| 177 | + expect(false)->toBeTrue('Expected JsonRpcException to be thrown'); |
| 178 | + } catch (\Laravel\Mcp\Server\Exceptions\JsonRpcException $e) { |
| 179 | + expect($e->getMessage())->toContain('Tool [non-existent-tool] not found'); |
| 180 | + } |
| 181 | +}); |
| 182 | + |
0 commit comments