Files
WP-LIT-MCP/tests/Support/DiviWriterFixture.php
T
janniclas.loosenandClaude Opus 5.5 72eeaa46ad 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>
2026-09-26 17:53:54 +02:00

114 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Support;
use LIT\MCP\Integrations\Divi\Design\DesignSystemReader;
use LIT\MCP\Integrations\Divi\Pages\DiviComposition;
use LIT\MCP\Integrations\Divi\Pages\DiviPageLoader;
use LIT\MCP\Integrations\Divi\Pages\DiviSaver;
use LIT\MCP\Integrations\Divi\Pages\DiviWriter;
use LIT\MCP\Integrations\Divi\Pages\DynamicContentMap;
use LIT\MCP\Integrations\Divi\Schema\DiviModules;
use Mockery;
use Throwable;
use WP_Post;
/**
* The Divi write stack of the page tests: DiviModules and DesignSystemReader serve the sample
* catalog (builder 5.2.1) and design system, DiviComposition uses ComposeFixture's sanitizer and
* media, and the saver records what it saves instead of touching the database.
*/
final class DiviWriterFixture
{
public static function modules(): DiviModules
{
$modules = Mockery::mock(DiviModules::class);
$modules->allows('catalog')->andReturn(DiviFixtures::catalog());
$modules->allows('builderVersion')->andReturn('5.2.1');
return $modules;
}
public static function designReader(): DesignSystemReader
{
$design = Mockery::mock(DesignSystemReader::class);
$design->allows('read')->andReturn(DiviFixtures::designSystem());
return $design;
}
/**
* $design is the reader the writer gets too (one instance, like the live wiring); without
* $registry the composition uses DynamicContentMap's default (VariableRef's built-in map in
* unit tests, where Divi's classes are missing).
*/
public static function composition(
DesignSystemReader $design,
bool $unfiltered = false,
?DynamicContentMap $registry = null
): DiviComposition {
return new DiviComposition(
self::modules(),
$design,
ComposeFixture::sanitizer($unfiltered),
ComposeFixture::media(),
$registry ?? new DynamicContentMap()
);
}
/**
* A DiviSaver whose save() appends the content to $saved, puts it on the post and reports
* revision 811. Each call first takes the next entry of $failures and throws it, if any.
*
* @param list<string> $saved
* @param list<Throwable> $failures
*/
public static function saver(array &$saved, array &$failures = []): DiviSaver
{
$saver = Mockery::mock(DiviSaver::class);
$saver->allows('save')->andReturnUsing(static function (WP_Post $post, string $content) use (&$saved, &$failures): array {
$failure = array_shift($failures);
if ($failure !== null) {
throw $failure;
}
$saved[] = $content;
$post->post_content = $content;
return ['revision_id' => 811, 'warnings' => []];
});
return $saver;
}
/** A ChangeLog that accepts records (log ids from 4711). */
public static function log(): FakeChangeLog
{
return new FakeChangeLog(4711);
}
/**
* The writer over the page of DiviFixtures::givenPost(), saving into $saved (see saver()).
*
* @param list<string> $saved
* @param list<Throwable> $failures
*/
public static function writer(
array &$saved,
array &$failures = [],
bool $unfiltered = false,
?DiviPageLoader $loader = null
): DiviWriter {
$design = self::designReader();
return new DiviWriter(
$loader ?? new DiviPageLoader(DiviFixtures::schema()),
self::saver($saved, $failures),
self::log(),
$design,
self::composition($design, $unfiltered)
);
}
}