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:
@@ -8,10 +8,10 @@ use Brain\Monkey\Functions;
|
||||
use DateTimeZone;
|
||||
use LIT\MCP\Abilities\WriteFailed;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Schema\ContentSchema;
|
||||
use LIT\MCP\Integrations\WordPress\Status\PostScheduleAbility;
|
||||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||||
use LIT\MCP\Tests\Support\SchemaFixture;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
use RuntimeException;
|
||||
use WP_Post;
|
||||
|
||||
@@ -29,8 +29,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
|
||||
private function ability(ChangeLog $log): PostScheduleAbility
|
||||
{
|
||||
$schema = Mockery::mock(ContentSchema::class);
|
||||
$schema->allows('isManaged')->andReturn(true);
|
||||
$schema = SchemaFixture::managed();
|
||||
|
||||
return new PostScheduleAbility($schema, $log);
|
||||
}
|
||||
@@ -45,13 +44,13 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
'edit_date' => true,
|
||||
], true)->andReturn(5);
|
||||
Functions\when('get_post_status')->justReturn('future');
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')->andReturn(4);
|
||||
$log = new FakeChangeLog(4);
|
||||
|
||||
$result = $this->ability($log)->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
|
||||
|
||||
self::assertSame('scheduled', $result['changed'][0]['action']);
|
||||
self::assertStringContainsString('2099-06-01 09:30', $result['summary']);
|
||||
$log->assertRecorded([]);
|
||||
}
|
||||
|
||||
public function testWarnsThatAPrivatePostBecomesPublic(): void
|
||||
@@ -59,8 +58,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 5, 'post_type' => 'post', 'post_title' => 'Intern', 'post_status' => 'private']));
|
||||
Functions\when('wp_update_post')->justReturn(5);
|
||||
Functions\when('get_post_status')->justReturn('future');
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(4);
|
||||
$log = new FakeChangeLog(4);
|
||||
|
||||
$result = $this->ability($log)->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
|
||||
|
||||
@@ -73,8 +71,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
{
|
||||
Functions\when('wp_update_post')->justReturn(5);
|
||||
Functions\when('get_post_status')->justReturn('future');
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(4);
|
||||
$log = new FakeChangeLog(4);
|
||||
|
||||
$result = $this->ability($log)->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
|
||||
|
||||
@@ -86,8 +83,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
Functions\when('wp_update_post')->alias(static function (): void {
|
||||
throw new RuntimeException('save_post-Hook kaputt');
|
||||
});
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('recordPartial')->with('lit/post-schedule', 'post', '5', Mockery::type('string'));
|
||||
$log = FakeChangeLog::partialsOnly();
|
||||
|
||||
try {
|
||||
$this->ability($log)->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
|
||||
@@ -98,6 +94,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
self::assertStringContainsString('save_post-Hook kaputt', $e->getMessage());
|
||||
self::assertSame('lit_partial_write', $e->toError()->get_error_code());
|
||||
}
|
||||
$log->assertPartials(['ability' => 'lit/post-schedule', 'object_type' => 'post', 'object_id' => '5']);
|
||||
}
|
||||
|
||||
public function testATrashedPostIsRefused(): void
|
||||
@@ -105,7 +102,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
Functions\when('get_post')->justReturn(new WP_Post(['ID' => 5, 'post_type' => 'post', 'post_status' => 'trash']));
|
||||
Functions\expect('wp_update_post')->never();
|
||||
|
||||
$result = $this->ability(Mockery::mock(ChangeLog::class))->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
|
||||
$result = $this->ability(FakeChangeLog::forbidden())->execute(['post_id' => 5, 'date' => '2099-06-01 09:30']);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
}
|
||||
@@ -113,7 +110,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
public function testPastAndImpossibleDatesAreRejected(): void
|
||||
{
|
||||
Functions\expect('wp_update_post')->never();
|
||||
$ability = $this->ability(Mockery::mock(ChangeLog::class));
|
||||
$ability = $this->ability(FakeChangeLog::forbidden());
|
||||
|
||||
$past = $ability->execute(['post_id' => 5, 'date' => '2020-01-01 10:00']);
|
||||
$impossible = $ability->execute(['post_id' => 5, 'date' => '2099-13-45 10:00']);
|
||||
@@ -131,7 +128,7 @@ final class PostScheduleAbilityTest extends TestCase
|
||||
->setTimezone(new DateTimeZone('Europe/Berlin'));
|
||||
Functions\when('get_post_status')->justReturn('future');
|
||||
|
||||
$result = $this->ability(Mockery::mock(ChangeLog::class))
|
||||
$result = $this->ability(FakeChangeLog::forbidden())
|
||||
->execute(['post_id' => 5, 'date' => $soon->format('Y-m-d H:i')]);
|
||||
|
||||
self::assertSame('lit_validation_failed', $result->get_error_code());
|
||||
|
||||
Reference in New Issue
Block a user