test: AssertsToolErrors helper for tool error assertions (T3)
tests/Support/AssertsToolErrors (used by TestCase): assertToolError($result, code)
checks the WP_Error code lit_<code> and the code of the client JSON and returns the
payload; clientError() decodes the payload; errorFields() returns the fields and
asserts they exist. Replaces the private clientError() decoders of
DiviWriteAbilitiesTest/DiviReplaceTextAbilityTest, 216 single-line
assertSame('lit_…', …->get_error_code()) assertions, 24 get_error_data()['fields']
digs and two json_decode(message) reads. ErrorsTest keeps json_decode: it tests
the format itself. Tests 1404 -> 1409 (AssertsToolErrorsTest); no test has fewer
assertions.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Support;
|
||||
|
||||
use PHPUnit\Framework\Assert;
|
||||
use WP_Error;
|
||||
|
||||
/**
|
||||
* Assertions on the errors abilities return (Errors::make(): WP_Error code "lit_<code>", the
|
||||
* message carries the client JSON {code, message, hint, fields?, …}).
|
||||
*/
|
||||
trait AssertsToolErrors
|
||||
{
|
||||
/**
|
||||
* Asserts a tool error with $code as the client sees it (without "lit_"): the WP_Error code
|
||||
* and the code in the JSON payload.
|
||||
*
|
||||
* @return array<string, mixed> The client payload.
|
||||
*/
|
||||
public static function assertToolError(mixed $result, string $code, string $message = ''): array
|
||||
{
|
||||
Assert::assertInstanceOf(WP_Error::class, $result, $message);
|
||||
Assert::assertSame('lit_' . $code, $result->get_error_code(), $message);
|
||||
$payload = self::clientError($result);
|
||||
Assert::assertSame($code, $payload['code'] ?? null, $message);
|
||||
|
||||
return $payload;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> The structured error the client sees (message JSON, spec §8.2). */
|
||||
public static function clientError(WP_Error $error): array
|
||||
{
|
||||
return (array) json_decode($error->get_error_message(), true);
|
||||
}
|
||||
|
||||
/** @return array<string, string> The error's fields (input path => problem); asserts it has some. */
|
||||
public static function errorFields(WP_Error $error): array
|
||||
{
|
||||
$data = $error->get_error_data();
|
||||
Assert::assertIsArray($data, 'error data');
|
||||
Assert::assertIsArray($data['fields'] ?? null, 'error fields');
|
||||
|
||||
return $data['fields'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user