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,8 +13,9 @@ use LIT\MCP\Integrations\Media\MediaPresenter;
|
||||
use LIT\MCP\Integrations\Media\MediaUploadAbility;
|
||||
use LIT\MCP\Integrations\Media\MediaUploadFromUrlAbility;
|
||||
use LIT\MCP\Integrations\Media\MediaUploads;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Limits;
|
||||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||||
use LIT\MCP\Tests\Support\SchemaFixture;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use RuntimeException;
|
||||
@@ -26,10 +27,9 @@ final class MediaUploadAbilitiesTest extends TestCase
|
||||
{
|
||||
$presenter = Mockery::mock(MediaPresenter::class);
|
||||
$presenter->allows('summary')->andReturn(['id' => 55, 'title' => 'Logo']);
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
$schema = SchemaFixture::managed();
|
||||
|
||||
return new MediaUploads($presenter, $log ?? Mockery::mock(ChangeLog::class), $schema);
|
||||
return new MediaUploads($presenter, $log ?? FakeChangeLog::forbidden(), $schema);
|
||||
}
|
||||
|
||||
private function savedAttachment(): void
|
||||
@@ -43,8 +43,7 @@ final class MediaUploadAbilitiesTest extends TestCase
|
||||
$this->savedAttachment();
|
||||
$importer = Mockery::mock(MediaImporter::class);
|
||||
$importer->expects('fromUrl')->with('https://example.com/logo.png', null, ['alt' => 'Logo'])->andReturn(55);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/media-upload-from-url', 'attachment', '55', Mockery::type('string'))->andReturn(3);
|
||||
$log = new FakeChangeLog(3);
|
||||
|
||||
$result = (new MediaUploadFromUrlAbility($importer, $this->uploads($log)))
|
||||
->execute(['url' => 'https://example.com/logo.png', 'alt' => 'Logo']);
|
||||
@@ -54,6 +53,11 @@ final class MediaUploadAbilitiesTest extends TestCase
|
||||
self::assertSame(3, $result['log_id']);
|
||||
self::assertSame('https://kunde.de/wp-admin/post.php?post=55&action=edit', $result['edit_url']);
|
||||
self::assertSame([], $result['warnings']);
|
||||
$log->assertRecorded([
|
||||
'ability' => 'lit/media-upload-from-url',
|
||||
'object_type' => 'attachment',
|
||||
'object_id' => '55',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testBase64UploadPassesFilenameDataAndMeta(): void
|
||||
@@ -61,14 +65,14 @@ final class MediaUploadAbilitiesTest extends TestCase
|
||||
$this->savedAttachment();
|
||||
$importer = Mockery::mock(MediaImporter::class);
|
||||
$importer->expects('fromBase64')->with('AAAA', 'x.png', ['title' => 'Logo', 'post_id' => 5])->andReturn(55);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->with('lit/media-upload', 'attachment', '55', Mockery::type('string'))->andReturn(4);
|
||||
$log = new FakeChangeLog(4);
|
||||
|
||||
$result = (new MediaUploadAbility($importer, $this->uploads($log), new Limits()))
|
||||
->execute(['filename' => 'x.png', 'data' => 'AAAA', 'title' => 'Logo', 'post_id' => 5]);
|
||||
|
||||
self::assertTrue($result['ok']);
|
||||
self::assertSame(4, $result['log_id']);
|
||||
$log->assertRecorded(['ability' => 'lit/media-upload', 'object_type' => 'attachment', 'object_id' => '55']);
|
||||
}
|
||||
|
||||
public function testAFailingChangeLogIsOnlyAWarning(): void
|
||||
@@ -76,8 +80,7 @@ final class MediaUploadAbilitiesTest extends TestCase
|
||||
$this->savedAttachment();
|
||||
$importer = Mockery::mock(MediaImporter::class);
|
||||
$importer->allows('fromUrl')->andReturn(55);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andThrow(new RuntimeException('db down'));
|
||||
$log = new FakeChangeLog(1, new RuntimeException('db down'));
|
||||
// error_log() is a PHP internal and cannot be stubbed; redirect it to a temp file instead.
|
||||
$file = (string) tempnam(sys_get_temp_dir(), 'lit-log');
|
||||
$previous = ini_set('error_log', $file);
|
||||
@@ -111,11 +114,19 @@ final class MediaUploadAbilitiesTest extends TestCase
|
||||
$failure = WriteFailed::partial('Alt-Text fehlt.', postId: 55);
|
||||
$importer = Mockery::mock(MediaImporter::class);
|
||||
$importer->allows('fromUrl')->andThrow($failure);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('recordPartial')->with('lit/media-upload-from-url', 'attachment', '55', Mockery::type('string'));
|
||||
$log = FakeChangeLog::partialsOnly();
|
||||
|
||||
$this->expectExceptionObject($failure);
|
||||
(new MediaUploadFromUrlAbility($importer, $this->uploads($log)))->execute(['url' => 'https://example.com/a.png']);
|
||||
try {
|
||||
(new MediaUploadFromUrlAbility($importer, $this->uploads($log)))->execute(['url' => 'https://example.com/a.png']);
|
||||
self::fail('WriteFailed expected');
|
||||
} catch (WriteFailed $e) {
|
||||
self::assertSame($failure, $e);
|
||||
}
|
||||
$log->assertPartials([
|
||||
'ability' => 'lit/media-upload-from-url',
|
||||
'object_type' => 'attachment',
|
||||
'object_id' => '55',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testPermissionNeedsUploadRightsAndEditRightsOnTheParent(): void
|
||||
|
||||
Reference in New Issue
Block a user