Skip to content

Commit ce01870

Browse files
author
klapaudius
committed
Add test cases for tool execution success and failure
Introduce tests to verify tool execution behavior for both success and failure scenarios. These tests ensure proper handling of valid inputs and clear error reporting when execution fails. Also, update `injectPrivateProperty` to set property accessibility explicitly.
1 parent d1f90f9 commit ce01870

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

tests/Command/TestMcpToolCommandTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ private function invokeGetToolInstanceMethod(): ToolInterface
213213
private function injectPrivateProperty(object $object, string $propertyName, mixed $value): void
214214
{
215215
$reflection = new \ReflectionProperty($object, $propertyName);
216+
$reflection->setAccessible(true);
216217
$reflection->setValue($object, $value);
217218
}
218219

@@ -457,6 +458,66 @@ public function test_list_all_tools_when_no_tools_are_configured_displays_warnin
457458
$this->assertEquals(Command::SUCCESS, $this->command->execute($this->inputMock, $this->outputMock));
458459
}
459460

461+
/**
462+
* Tests that when valid input is provided, the tool is executed, and the result is displayed.
463+
*/
464+
public function test_test_tool_valid_input_process_tool(): void
465+
{
466+
$toolMock = $this->createMock(ToolInterface::class);
467+
$toolMock->method('getInputSchema')->willReturn([]);
468+
$toolMock->method('execute')->with([])->willReturn(['success' => true]);
469+
470+
$this->containerMock
471+
->method('get')
472+
->willReturn($toolMock);
473+
474+
$this->containerMock
475+
->method('getParameter')
476+
->willReturn([HelloWorldTool::class]);
477+
478+
$this->inputMock
479+
->method('getArgument')
480+
->with('tool')
481+
->willReturn(HelloWorldTool::class);
482+
483+
$this->ioMock
484+
->expects($this->once())
485+
->method('success')
486+
->with('Tool executed successfully!');
487+
488+
$this->assertEquals(Command::SUCCESS, $this->command->execute($this->inputMock, $this->outputMock));
489+
}
490+
491+
/**
492+
* Tests that when the tool execution fails, the error message and stack trace are displayed.
493+
*/
494+
public function test_test_tool_execution_failure_handles_error(): void
495+
{
496+
$toolMock = $this->createMock(ToolInterface::class);
497+
$toolMock->method('getInputSchema')->willReturn([]);
498+
$toolMock->method('execute')->willThrowException(new \RuntimeException('Execution error.'));
499+
500+
$this->containerMock
501+
->method('get')
502+
->willReturn($toolMock);
503+
504+
$this->containerMock
505+
->method('getParameter')
506+
->willReturn([HelloWorldTool::class]);
507+
508+
$this->inputMock
509+
->method('getArgument')
510+
->with('tool')
511+
->willReturn(HelloWorldTool::class);
512+
513+
$this->ioMock
514+
->expects($this->once())
515+
->method('error')
516+
->with($this->stringContains('Error executing tool'));
517+
518+
$this->assertEquals(Command::FAILURE, $this->command->execute($this->inputMock, $this->outputMock));
519+
}
520+
460521
/**
461522
* Tests that when valid tools are present, they are displayed in a table.
462523
*/

0 commit comments

Comments
 (0)