Files
WP-LIT-MCP/tests/Unit/Support/AssertsToolErrorsTest.php
janniclas.loosenandClaude Opus 5.5 60290c85a5 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>
2026-09-26 17:55:35 +02:00

45 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Support;
use LIT\MCP\Abilities\Errors;
use LIT\MCP\Tests\Unit\TestCase;
use PHPUnit\Framework\AssertionFailedError;
use WP_Error;
final class AssertsToolErrorsTest extends TestCase
{
public function testReturnsTheClientPayloadOfAMatchingError(): void
{
$payload = self::assertToolError(Errors::invalidInput('Titel fehlt.', ['title' => 'Pflichtfeld']), 'validation_failed');
self::assertSame('Titel fehlt.', $payload['message']);
self::assertSame(['title' => 'Pflichtfeld'], self::errorFields(Errors::invalidInput('x', ['title' => 'Pflichtfeld'])));
}
/** @return array<string, array{callable(): mixed}> Built in the test: Errors needs the stubs of setUp(). */
public static function notTheError(): array
{
return [
'another code' => [static fn () => Errors::notFound('weg')],
'no JSON payload' => [static fn () => new WP_Error('lit_validation_failed', 'kein JSON')],
'no error at all' => [static fn () => ['ok' => true]],
];
}
/** @dataProvider notTheError */
public function testFailsForAnythingElse(callable $result): void
{
$this->expectException(AssertionFailedError::class);
self::assertToolError($result(), 'validation_failed');
}
public function testErrorFieldsFailsWithoutFields(): void
{
$this->expectException(AssertionFailedError::class);
self::errorFields(Errors::invalidInput('x'));
}
}