- DiviFixtures: markup(), givenPost(), schema(), designSystem(), catalog(), dynamicContentRegistry(), maliciousRefs() - moved verbatim out of DiviPageLoaderTest, DesignSystemTest, ModuleCatalogTest, DynamicContentMapTest and VariableRefTest (its malicious() provider now returns maliciousRefs()). - ComposeFixture moves to tests/Support. - DiviWriterFixture: modules(), designReader(), composition(), a recording saver(), log() and writer(&$saved, &$failures, unfiltered, loader) for the five tests that wired the Divi write stack by hand. No test cases or assertions changed (1397 / 5834). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
118 lines
3.7 KiB
PHP
118 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Support;
|
|
|
|
use LIT\MCP\ChangeLog\ChangeLog;
|
|
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 every record (log id 4711). */
|
|
public static function log(): ChangeLog
|
|
{
|
|
$log = Mockery::mock(ChangeLog::class);
|
|
$log->allows('record')->andReturn(4711);
|
|
|
|
return $log;
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
);
|
|
}
|
|
}
|