php-sdk/tests/Unit/Schema/JsonRpc/RequestTest.php

95 lines
2.4 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\Schema\JsonRpc;
use Mcp\Schema\JsonRpc\Request;
use PHPUnit\Framework\TestCase;
final class RequestTest extends TestCase
{
public function testMetaAndIdAreLoopedThrough(): void
{
$requestImplementation = new class extends Request {
public static function getMethod(): string
{
return 'foo/bar';
}
public static function fromParams(?array $params): static
{
return new self();
}
protected function getParams(): ?array
{
return null;
}
};
$notification = $requestImplementation::fromArray([
'jsonrpc' => '2.0',
'id' => '12345',
'method' => 'foo/bar',
'params' => [
'_meta' => ['key' => 'value'],
],
]);
$expectedMeta = [
'jsonrpc' => '2.0',
'id' => '12345',
'method' => 'foo/bar',
'params' => [
'_meta' => ['key' => 'value'],
],
];
$this->assertSame($expectedMeta, $notification->jsonSerialize());
}
public function testMetaDoesNotOverrideMetaFromParams(): void
{
$requestImplementation = new class extends Request {
public static function getMethod(): string
{
return 'foo/bar';
}
public static function fromParams(?array $params): static
{
return new self();
}
protected function getParams(): array
{
return [
'_meta' => ['key' => 'from-params'],
];
}
};
$request = $requestImplementation::fromParams(null)
->withId('12345')
->withMeta(['key' => 'from-meta']);
$expected = [
'jsonrpc' => '2.0',
'id' => '12345',
'method' => 'foo/bar',
'params' => [
'_meta' => ['key' => 'from-params'],
],
];
$this->assertSame($expected, $request->jsonSerialize());
}
}