php-sdk/tests/Unit/Capability/Formatter/ToolResultFormatterTest.php
Mateu Aguiló Bosch ab4f686e18
refactor: put formatters into dedicated classes (#224)
Split formatting logic from Reference classes into dedicated formatter classes:
- ToolResultFormatter: Formats tool execution results into Content items
- PromptResultFormatter: Formats prompt results into PromptMessage items
- ResourceResultFormatter: Formats resource results into ResourceContents items

This refactor:
- Eliminates 122 lines of code duplication between ResourceReference and
  ResourceTemplateReference (identical formatting logic)
- Improves code organization by separating formatting concerns from
  capability reference management
- Makes formatters independently testable and reusable
- Reduces Reference classes by ~450 lines while maintaining all functionality

Changes:
- Create src/Capability/Formatter/* with three formatter classes
- Update Reference classes to delegate to formatters instead of inline logic
- Add comprehensive test coverage for all formatter classes
- All existing tests pass; no behavioral changes
2026-01-18 22:17:44 +01:00

58 lines
1.8 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\Formatter;
use Mcp\Capability\Formatter\ToolResultFormatter;
use Mcp\Schema\Content\TextContent;
use PHPUnit\Framework\TestCase;
class ToolResultFormatterTest extends TestCase
{
public function testFormatStringResult(): void
{
$result = (new ToolResultFormatter())->format('hello');
$this->assertCount(1, $result);
$this->assertInstanceOf(TextContent::class, $result[0]);
$this->assertSame('hello', $result[0]->text);
}
public function testFormatContentResult(): void
{
$content = new TextContent('test');
$result = (new ToolResultFormatter())->format($content);
$this->assertSame([$content], $result);
}
public function testFormatArrayResult(): void
{
$result = (new ToolResultFormatter())->format(['key' => 'value']);
$this->assertCount(1, $result);
$this->assertInstanceOf(TextContent::class, $result[0]);
$this->assertStringContainsString('value', $result[0]->text);
}
public function testFormatNullResult(): void
{
$result = (new ToolResultFormatter())->format(null);
$this->assertCount(1, $result);
$this->assertInstanceOf(TextContent::class, $result[0]);
$this->assertSame('(null)', $result[0]->text);
}
public function testFormatBoolResult(): void
{
$result = (new ToolResultFormatter())->format(true);
$this->assertCount(1, $result);
$this->assertInstanceOf(TextContent::class, $result[0]);
$this->assertSame('true', $result[0]->text);
}
}