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:
@@ -5,10 +5,9 @@ declare(strict_types=1);
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Menus;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
use LIT\MCP\ChangeLog\ChangeLog;
|
||||
use LIT\MCP\Integrations\WordPress\Menus\MenuAssignLocationAbility;
|
||||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||||
use LIT\MCP\Tests\Unit\TestCase;
|
||||
use Mockery;
|
||||
|
||||
final class MenuAssignLocationAbilityTest extends TestCase
|
||||
{
|
||||
@@ -29,28 +28,26 @@ final class MenuAssignLocationAbilityTest extends TestCase
|
||||
public function testAssignsAndReportsThePreviousMenu(): void
|
||||
{
|
||||
Functions\expect('set_theme_mod')->once()->with('nav_menu_locations', ['primary-menu' => 8]);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')
|
||||
->with(
|
||||
'lit/menu-assign-location',
|
||||
'menu_location',
|
||||
'primary-menu',
|
||||
'Menü 8 der Position primary-menu zugewiesen'
|
||||
)
|
||||
->andReturn(6);
|
||||
$log = new FakeChangeLog(6);
|
||||
|
||||
$result = (new MenuAssignLocationAbility($log))->execute(['location' => 'primary-menu', 'menu_id' => 8]);
|
||||
|
||||
self::assertSame(3, $result['previous_menu_id']);
|
||||
self::assertSame('assigned', $result['changed'][0]['action']);
|
||||
self::assertSame(6, $result['log_id']);
|
||||
$log->assertRecorded([
|
||||
'ability' => 'lit/menu-assign-location',
|
||||
'object_type' => 'menu_location',
|
||||
'object_id' => 'primary-menu',
|
||||
'summary' => 'Menü 8 der Position primary-menu zugewiesen',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testRemovingAnAssignmentNeedsConfirmation(): void
|
||||
{
|
||||
Functions\expect('set_theme_mod')->never();
|
||||
|
||||
$result = (new MenuAssignLocationAbility(Mockery::mock(ChangeLog::class)))
|
||||
$result = (new MenuAssignLocationAbility(FakeChangeLog::forbidden()))
|
||||
->execute(['location' => 'primary-menu', 'menu_id' => 0]);
|
||||
|
||||
self::assertSame('lit_confirm_required', $result->get_error_code());
|
||||
@@ -63,15 +60,7 @@ final class MenuAssignLocationAbilityTest extends TestCase
|
||||
public function testZeroRemovesTheAssignmentAndNamesTheRemovedMenu(): void
|
||||
{
|
||||
Functions\expect('set_theme_mod')->once()->with('nav_menu_locations', []);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->expects('record')
|
||||
->with(
|
||||
'lit/menu-assign-location',
|
||||
'menu_location',
|
||||
'primary-menu',
|
||||
'Menü "Hauptmenü" (ID 3) von der Position primary-menu entfernt'
|
||||
)
|
||||
->andReturn(7);
|
||||
$log = new FakeChangeLog(7);
|
||||
|
||||
$result = (new MenuAssignLocationAbility($log))
|
||||
->execute(['location' => 'primary-menu', 'menu_id' => 0, 'confirm' => true]);
|
||||
@@ -80,14 +69,19 @@ final class MenuAssignLocationAbilityTest extends TestCase
|
||||
self::assertSame(3, $result['previous_menu_id']);
|
||||
self::assertStringContainsString('"Hauptmenü" (ID 3)', $result['summary']);
|
||||
self::assertSame(7, $result['log_id']);
|
||||
$log->assertRecorded([
|
||||
'ability' => 'lit/menu-assign-location',
|
||||
'object_type' => 'menu_location',
|
||||
'object_id' => 'primary-menu',
|
||||
'summary' => 'Menü "Hauptmenü" (ID 3) von der Position primary-menu entfernt',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testRemovingFromAnEmptyLocationNeedsNoConfirmation(): void
|
||||
{
|
||||
Functions\when('get_nav_menu_locations')->justReturn([]);
|
||||
Functions\expect('set_theme_mod')->once()->with('nav_menu_locations', []);
|
||||
$log = Mockery::mock(ChangeLog::class);
|
||||
$log->allows('record')->andReturn(8);
|
||||
$log = new FakeChangeLog(8);
|
||||
|
||||
$result = (new MenuAssignLocationAbility($log))->execute(['location' => 'primary-menu', 'menu_id' => 0]);
|
||||
|
||||
@@ -97,7 +91,7 @@ final class MenuAssignLocationAbilityTest extends TestCase
|
||||
|
||||
public function testDescribesTheConfirmationForRemoval(): void
|
||||
{
|
||||
$ability = new MenuAssignLocationAbility(Mockery::mock(ChangeLog::class));
|
||||
$ability = new MenuAssignLocationAbility(FakeChangeLog::forbidden());
|
||||
|
||||
self::assertArrayHasKey('confirm', $ability->inputSchema()['properties']);
|
||||
self::assertStringContainsString('confirm: true', $ability->description());
|
||||
@@ -107,7 +101,7 @@ final class MenuAssignLocationAbilityTest extends TestCase
|
||||
{
|
||||
Functions\expect('set_theme_mod')->never();
|
||||
|
||||
$result = (new MenuAssignLocationAbility(Mockery::mock(ChangeLog::class)))
|
||||
$result = (new MenuAssignLocationAbility(FakeChangeLog::forbidden()))
|
||||
->execute(['location' => 'primary-menu', 'menu_id' => 99]);
|
||||
|
||||
self::assertSame('lit_not_found', $result->get_error_code());
|
||||
@@ -117,7 +111,7 @@ final class MenuAssignLocationAbilityTest extends TestCase
|
||||
{
|
||||
Functions\when('current_user_can')->justReturn(false);
|
||||
|
||||
$ability = new MenuAssignLocationAbility(Mockery::mock(ChangeLog::class));
|
||||
$ability = new MenuAssignLocationAbility(FakeChangeLog::forbidden());
|
||||
$input = ['location' => 'primary-menu', 'menu_id' => 8];
|
||||
|
||||
self::assertSame('lit_forbidden', $ability->permission($input)->get_error_code());
|
||||
@@ -125,7 +119,7 @@ final class MenuAssignLocationAbilityTest extends TestCase
|
||||
|
||||
public function testIsDestructiveBecauseItReplacesTheMenuOfTheLocation(): void
|
||||
{
|
||||
$ability = new MenuAssignLocationAbility(Mockery::mock(ChangeLog::class));
|
||||
$ability = new MenuAssignLocationAbility(FakeChangeLog::forbidden());
|
||||
|
||||
self::assertSame(['readonly' => false, 'destructive' => true, 'idempotent' => true], $ability->annotations());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user