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:
@@ -13,6 +13,8 @@ use LIT\MCP\Integrations\WordPress\Content\ContentUpdateAbility;
|
||||
use LIT\MCP\Integrations\WordPress\Content\PostPresenter;
|
||||
use LIT\MCP\Integrations\WordPress\Content\PostWriter;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||||
use LIT\MCP\Tests\Support\SchemaFixture;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use RuntimeException;
|
||||
@@ -39,15 +41,14 @@ final class ContentUpdateAbilityTest extends TestCase
|
||||
|
||||
private function schema(): ContentSchema
|
||||
{
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
$schema = SchemaFixture::managed();
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
private function ability(PostWriter $writer, ?ChangeLog $log = null): ContentUpdateAbility
|
||||
{
|
||||
return new ContentUpdateAbility($this->schema(), $writer, $log ?? Mockery::mock(ChangeLog::class));
|
||||
return new ContentUpdateAbility($this->schema(), $writer, $log ?? FakeChangeLog::forbidden());
|
||||
}
|
||||
|
||||
public function testRevIsRequired(): void
|
||||
@@ -155,8 +156,7 @@ final class ContentUpdateAbilityTest extends TestCase
|
||||
Actions\expectDone('lit_mcp_after_save')->once()->with(5, 'lit/content-update', $input);
|
||||
Functions\when('get_preview_post_link')->justReturn('https://kunde.de/team/');
|
||||
Functions\when('get_edit_post_link')->justReturn('https://kunde.de/wp-admin/post.php?post=5&action=edit');
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/content-update', 'page', '5', Mockery::type('string'), 811)->andReturn(4711);
|
||||
$log = new FakeChangeLog(4711);
|
||||
|
||||
$result = $this->ability($writer, $log)->execute($input);
|
||||
|
||||
@@ -166,6 +166,12 @@ final class ContentUpdateAbilityTest extends TestCase
|
||||
self::assertSame('updated', $result['changed'][0]['action']);
|
||||
self::assertSame(Rev::ofPost($this->post), $result['rev']);
|
||||
self::assertStringContainsString('Status unverändert: publish', $result['summary']);
|
||||
$log->assertRecorded([
|
||||
'ability' => 'lit/content-update',
|
||||
'object_type' => 'page',
|
||||
'object_id' => '5',
|
||||
'revision_id' => 811,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -197,8 +203,7 @@ final class ContentUpdateAbilityTest extends TestCase
|
||||
private function executeExpectingFailure(PostWriter $writer, array $input, ?ChangeLog $log = null): WriteFailed
|
||||
{
|
||||
if ($log === null) {
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->never();
|
||||
$log = FakeChangeLog::forbidden();
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -298,14 +303,14 @@ final class ContentUpdateAbilityTest extends TestCase
|
||||
});
|
||||
Functions\when('wp_revisions_enabled')->justReturn(true);
|
||||
Functions\expect('wp_restore_post_revision')->never();
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('recordPartial');
|
||||
$log = FakeChangeLog::partialsOnly();
|
||||
|
||||
$failure = $this->executeExpectingFailure($writer, $input, $log);
|
||||
|
||||
self::assertFalse($failure->rolledBack);
|
||||
self::assertCount(1, $updates);
|
||||
self::assertStringContainsString('publish', $failure->getMessage());
|
||||
$log->assertPartials([]);
|
||||
}
|
||||
|
||||
public function testAnIncompleteRollbackIsAPartialWriteAndLogged(): void
|
||||
@@ -314,10 +319,7 @@ final class ContentUpdateAbilityTest extends TestCase
|
||||
$updates = [];
|
||||
$this->captureUpdates($updates);
|
||||
Functions\when('wp_revisions_enabled')->justReturn(false);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('recordPartial')
|
||||
->with('lit/content-update', 'page', '5', Mockery::type('string'), 811)
|
||||
->andReturn(12);
|
||||
$log = FakeChangeLog::partialsOnly();
|
||||
|
||||
$failure = $this->executeExpectingFailure($writer, $input, $log);
|
||||
|
||||
@@ -327,6 +329,12 @@ final class ContentUpdateAbilityTest extends TestCase
|
||||
self::assertSame('lit_partial_write', $error->get_error_code());
|
||||
self::assertSame(5, $error->get_error_data()['post_id']);
|
||||
self::assertSame(811, $error->get_error_data()['revision_id']);
|
||||
$log->assertPartials([
|
||||
'ability' => 'lit/content-update',
|
||||
'object_type' => 'page',
|
||||
'object_id' => '5',
|
||||
'revision_id' => 811,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testFailuresAfterTheWriteOnlyAddWarnings(): void
|
||||
@@ -345,8 +353,7 @@ final class ContentUpdateAbilityTest extends TestCase
|
||||
Actions\expectDone('lit_mcp_after_save')->once()->whenHappen(static function (): void {
|
||||
throw new RuntimeException('listener kaputt');
|
||||
});
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->andThrow(new RuntimeException('db weg'));
|
||||
$log = new FakeChangeLog(1, new RuntimeException('db weg'));
|
||||
$file = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
|
||||
$previous = ini_set('error_log', $file);
|
||||
|
||||
@@ -361,5 +368,6 @@ final class ContentUpdateAbilityTest extends TestCase
|
||||
self::assertCount(2, $result['warnings']);
|
||||
self::assertStringContainsString('listener kaputt', (string) file_get_contents($file));
|
||||
unlink($file);
|
||||
$log->assertRecorded([]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user