php-sdk/tests/Unit/Capability/Formatter/PromptResultFormatterTest.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

49 lines
1.5 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\PromptResultFormatter;
use Mcp\Schema\Content\PromptMessage;
use Mcp\Schema\Content\TextContent;
use Mcp\Schema\Enum\Role;
use PHPUnit\Framework\TestCase;
class PromptResultFormatterTest extends TestCase
{
public function testFormatPromptMessage(): void
{
$message = new PromptMessage(Role::User, new TextContent('hello'));
$result = (new PromptResultFormatter())->format($message);
$this->assertCount(1, $result);
$this->assertSame($message, $result[0]);
}
public function testFormatUserAssistantShorthand(): void
{
$result = (new PromptResultFormatter())->format([
'user' => 'Hello',
'assistant' => 'Hi there',
]);
$this->assertCount(2, $result);
$this->assertSame(Role::User, $result[0]->role);
$this->assertSame(Role::Assistant, $result[1]->role);
}
public function testFormatRoleContentArray(): void
{
$result = (new PromptResultFormatter())->format([
['role' => 'user', 'content' => 'Hello'],
]);
$this->assertCount(1, $result);
$this->assertSame(Role::User, $result[0]->role);
}
}