Files
WP-LIT-MCP/tests/Unit/Integrations/WordPress/Content/NewDraftTest.php
T
janniclas.loosenandClaude Opus 5.5 72eeaa46ad test: FakeChangeLog and SchemaFixture instead of repeated mocks (T2)
- tests/Support/FakeChangeLog extends ChangeLog: records record()/recordPartial()
  calls, increasing ids, optional failure; new FakeChangeLog(id) / partialsOnly() /
  any(id) / forbidden() mirror what the replaced mocks allowed. assertRecorded() /
  assertPartials() check count and the named arguments. Calls a fake does not allow
  throw and are also verified in TestCase::tearDown (one assertion per test that
  uses a fake), so swallowed change-log calls still fail like a never() expectation.
- All ChangeLog mocks of ability/integration tests replaced (the admin tests that
  stub query/purge/clear keep their mocks); expectException tests that checked the
  partial log now catch and assert.
- SchemaFixture::managed(?divi) replaces the 21 isManaged-only ContentSchema mocks;
  DiviFixtures::schema() uses it.
Tests 1397 -> 1404 (FakeChangeLogTest); no test has fewer assertions.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 17:53:54 +02:00

66 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Content;
use Brain\Monkey\Functions;
use LIT\MCP\Abilities\WriteFailed;
use LIT\MCP\Integrations\WordPress\Content\NewDraft;
use LIT\MCP\Integrations\WordPress\Content\PostWriter;
use LIT\MCP\Tests\Support\FakeChangeLog;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use RuntimeException;
final class NewDraftTest extends TestCase
{
public function testADeleteThatThrowsIsNamedInTheLogDetailAndTheOtherStepsStillRun(): void
{
$writer = Mockery::mock(PostWriter::class);
$writer->expects('deleteCreatedTerms')->andThrow(new RuntimeException('term 15 bleibt'));
Functions\when('wp_delete_post')->alias(static function (): never {
throw new RuntimeException('delete_post hook kaputt');
});
$log = FakeChangeLog::partialsOnly();
$draft = new NewDraft($writer, $log, 'lit/content-create', 'page');
try {
$draft->discard(42, new RuntimeException('layout kaputt'), 'Grund für den Client.');
self::fail('WriteFailed expected');
} catch (WriteFailed $e) {
self::assertFalse($e->rolledBack);
self::assertSame(42, $e->postId);
self::assertSame('Grund für den Client.', $e->reason);
self::assertSame(
'Extras of new post 42 could not be written: layout kaputt; rollback: term 15 bleibt; '
. 'draft 42 could not be deleted: delete_post hook kaputt [rollback incomplete]',
$e->getMessage()
);
}
$log->assertPartials([
'ability' => 'lit/content-create',
'object_type' => 'page',
'object_id' => '42',
'summary' => 'Entwurf 42: Anlegen nur teilweise zurückgenommen',
]);
}
public function testACleanDiscardIsRolledBackWithTheGivenReason(): void
{
$writer = Mockery::mock(PostWriter::class);
$writer->expects('deleteCreatedTerms');
Functions\expect('wp_delete_post')->once()->with(42, true)->andReturn(true);
$log = FakeChangeLog::forbidden();
$draft = new NewDraft($writer, $log, 'lit/content-create', 'page');
try {
$draft->discard(42, new RuntimeException('meta kaputt'), NewDraft::REASON_EXTRAS);
self::fail('WriteFailed expected');
} catch (WriteFailed $e) {
self::assertTrue($e->rolledBack);
self::assertSame(NewDraft::REASON_EXTRAS, $e->reason);
}
}
}