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>
This commit is contained in:
2026-09-26 17:53:54 +02:00
co-authored by Claude Opus 5.5
parent 3d96a41366
commit 72eeaa46ad
34 changed files with 725 additions and 353 deletions
@@ -5,10 +5,10 @@ declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Divi;
use LIT\MCP\Abilities\Ability;
use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\Integrations\Divi\DiviIntegration;
use LIT\MCP\Integrations\Divi\Pages\DiviContentComposer;
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
use LIT\MCP\Tests\Support\FakeChangeLog;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
@@ -30,7 +30,7 @@ final class DiviIntegrationTest extends TestCase
public function testProvidesTheDiviAbilitiesOnlyWithDivi5(): void
{
$integration = new DiviIntegration(Mockery::mock(ContentSchema::class), Mockery::mock(ChangeLog::class));
$integration = new DiviIntegration(Mockery::mock(ContentSchema::class), FakeChangeLog::forbidden());
self::assertSame('divi', $integration->id());
self::assertFalse($integration->isAvailable(), 'no Divi classes in unit tests');
@@ -48,7 +48,7 @@ final class DiviIntegrationTest extends TestCase
public function testGivesContentCreateTheDiviShortForm(): void
{
$integration = new DiviIntegration(Mockery::mock(ContentSchema::class), Mockery::mock(ChangeLog::class));
$integration = new DiviIntegration(Mockery::mock(ContentSchema::class), FakeChangeLog::forbidden());
$composer = $integration->contentComposer();
@@ -19,6 +19,7 @@ use LIT\MCP\Integrations\Divi\Pages\DiviWriter;
use LIT\MCP\Integrations\Divi\Pages\DynamicContentMap;
use LIT\MCP\Tests\Support\DiviFixtures;
use LIT\MCP\Tests\Support\DiviWriterFixture;
use LIT\MCP\Tests\Support\FakeChangeLog;
use LIT\MCP\Tests\Unit\TestCase;
use Mockery;
use WP_Post;
@@ -50,7 +51,7 @@ final class DiviWriterTest extends TestCase
{
$content = DiviFixtures::markup('synthetic-basic.html');
DiviFixtures::givenPost($content);
$writer = $this->writer(Mockery::mock(DiviSaver::class), Mockery::mock(ChangeLog::class));
$writer = $this->writer(Mockery::mock(DiviSaver::class), FakeChangeLog::forbidden());
self::assertInstanceOf(DiviPage::class, $writer->page(['post_id' => 7, 'tree_rev' => Rev::of($content)]));
self::assertSame('lit_rev_mismatch', $writer->page(['post_id' => 7])->get_error_code());
@@ -66,7 +67,7 @@ final class DiviWriterTest extends TestCase
$writer = new DiviWriter(
new DiviPageLoader(DiviFixtures::schema(false)),
Mockery::mock(DiviSaver::class),
Mockery::mock(ChangeLog::class),
FakeChangeLog::forbidden(),
Mockery::mock(DesignSystemReader::class),
Mockery::mock(DiviComposition::class)
);
@@ -87,7 +88,7 @@ final class DiviWriterTest extends TestCase
{
$content = DiviFixtures::markup('synthetic-basic.html');
DiviFixtures::givenPost($content);
$writer = $this->writer(Mockery::mock(DiviSaver::class), Mockery::mock(ChangeLog::class));
$writer = $this->writer(Mockery::mock(DiviSaver::class), FakeChangeLog::forbidden());
$page = $writer->page(['post_id' => 7, 'tree_rev' => Rev::of($content)]);
$writer->composition($page)->placeholders();
@@ -101,7 +102,7 @@ final class DiviWriterTest extends TestCase
DiviFixtures::givenPost($content);
$saver = Mockery::mock(DiviSaver::class);
$saver->expects('save')->never();
$writer = $this->writer($saver, Mockery::mock(ChangeLog::class));
$writer = $this->writer($saver, FakeChangeLog::forbidden());
$page = $writer->page(['post_id' => 7, 'tree_rev' => Rev::of($content)]);
$result = $writer->save('lit/divi-update', $page, new DiviChange('nichts'), []);
@@ -122,9 +123,7 @@ final class DiviWriterTest extends TestCase
return ['revision_id' => 811, 'warnings' => ['vom Speichern']];
});
$log = Mockery::mock(ChangeLog::class);
$log->expects('record')->with('lit/divi-update', 'page', '7', 'Divi "Start": Button geändert', 811)
->andReturn(55);
$log = new FakeChangeLog(55);
$input = ['post_id' => 7, 'tree_rev' => Rev::of($content)];
Functions\expect('do_action')->once()->with('lit_mcp_after_save', 7, 'lit/divi-update', $input);
Functions\when('get_preview_post_link')->justReturn('https://kunde.test/?page_id=7&preview=true');
@@ -158,6 +157,13 @@ final class DiviWriterTest extends TestCase
self::assertContains('eigene Warnung', $result['warnings']);
self::assertContains('vom Speichern', $result['warnings']);
self::assertSame(Rev::ofPost(DiviFixtures::givenPost($stored)), $result['rev']);
$log->assertRecorded([
'ability' => 'lit/divi-update',
'object_type' => 'page',
'object_id' => '7',
'summary' => 'Divi "Start": Button geändert',
'revision_id' => 811,
]);
}
public function testFailedRollbackIsLoggedAsPartial(): void
@@ -166,14 +172,24 @@ final class DiviWriterTest extends TestCase
DiviFixtures::givenPost($content);
$saver = Mockery::mock(DiviSaver::class);
$saver->allows('save')->andThrow(WriteFailed::partial(DiviSaver::REASON, postId: 7, revisionId: 811));
$log = Mockery::mock(ChangeLog::class);
$log->expects('recordPartial')->with('lit/divi-delete', 'page', '7', 'Divi "Start": Änderung nur teilweise zurückgenommen', 811);
$log = FakeChangeLog::partialsOnly();
$writer = $this->writer($saver, $log);
$page = $writer->page(['post_id' => 7, 'tree_rev' => Rev::of($content)]);
$page->document->children()[0]->removeChild($page->index->find('s1'));
$this->expectException(WriteFailed::class);
$writer->save('lit/divi-delete', $page, new DiviChange('s1 gelöscht'), []);
try {
$writer->save('lit/divi-delete', $page, new DiviChange('s1 gelöscht'), []);
self::fail('WriteFailed expected');
} catch (WriteFailed $e) {
self::assertFalse($e->rolledBack);
}
$log->assertPartials([
'ability' => 'lit/divi-delete',
'object_type' => 'page',
'object_id' => '7',
'summary' => 'Divi "Start": Änderung nur teilweise zurückgenommen',
'revision_id' => 811,
]);
}
public function testCleanFailureIsNotLogged(): void
@@ -182,14 +198,18 @@ final class DiviWriterTest extends TestCase
DiviFixtures::givenPost($content);
$saver = Mockery::mock(DiviSaver::class);
$saver->allows('save')->andThrow(WriteFailed::rolledBack(DiviSaver::REASON, postId: 7, revisionId: 811));
$log = Mockery::mock(ChangeLog::class);
$log->expects('recordPartial')->never();
$log->expects('record')->never();
$log = FakeChangeLog::forbidden();
$writer = $this->writer($saver, $log);
$page = $writer->page(['post_id' => 7, 'tree_rev' => Rev::of($content)]);
$page->document->children()[0]->removeChild($page->index->find('s1'));
$this->expectException(WriteFailed::class);
$writer->save('lit/divi-delete', $page, new DiviChange('s1 gelöscht'), []);
try {
$writer->save('lit/divi-delete', $page, new DiviChange('s1 gelöscht'), []);
self::fail('WriteFailed expected');
} catch (WriteFailed $e) {
self::assertTrue($e->rolledBack);
}
$log->assertRecorded();
$log->assertPartials();
}
}