feat: Signatur-Fingerabdruck der Divi-Aufrufe (Reflection); fingerprints.json enthält sig:-Einträge, divi-check --update-reference schreibt sie; Referenz für Divi 5.15.1
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
392fe7c569
commit
710444e88d
@@ -4,25 +4,99 @@ declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Release;
|
||||
|
||||
use LIT\MCP\Integrations\Divi\Release\Call;
|
||||
use LIT\MCP\Integrations\Divi\Release\DiviAdapters;
|
||||
use LIT\MCP\Integrations\Divi\Release\DiviReference;
|
||||
use LIT\MCP\Integrations\Divi\Release\SignatureFingerprint;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RuntimeException;
|
||||
|
||||
final class DiviReferenceTest extends TestCase
|
||||
{
|
||||
public function testEveryMirrorHasAnExpectedFingerprintAndAReferenceSource(): void
|
||||
public function testEveryMirrorAndEveryCallHasAnExpectedFingerprintAndEveryMirrorAReferenceSource(): void
|
||||
{
|
||||
$ids = array_map(static fn ($mirror): string => $mirror->id, DiviAdapters::mirrors());
|
||||
sort($ids);
|
||||
$mirrorIds = array_map(static fn ($mirror): string => $mirror->id, DiviAdapters::mirrors());
|
||||
$signatureKeys = [];
|
||||
foreach (DiviAdapters::manifests() as $manifest) {
|
||||
foreach ($manifest->calls as $call) {
|
||||
$signatureKeys[] = 'sig:' . ltrim($call->symbol, '\\');
|
||||
}
|
||||
}
|
||||
$keys = array_values(array_unique(array_merge($mirrorIds, $signatureKeys)));
|
||||
sort($keys);
|
||||
$expected = DiviReference::expected();
|
||||
|
||||
self::assertSame($ids, array_keys($expected));
|
||||
foreach ($expected as $id => $hash) {
|
||||
self::assertMatchesRegularExpression('/^[0-9a-f]{64}$/', $hash, $id);
|
||||
self::assertSame($keys, array_keys($expected));
|
||||
foreach ($expected as $key => $hash) {
|
||||
self::assertMatchesRegularExpression('/^[0-9a-f]{64}$/', $hash, $key);
|
||||
}
|
||||
foreach ($mirrorIds as $id) {
|
||||
self::assertNotNull(DiviReference::readSource($id, (string) DiviReference::sourceDir()), $id);
|
||||
}
|
||||
}
|
||||
|
||||
public function testEverySignatureKeyStartsWithThePrefixNoMirrorIdUses(): void
|
||||
{
|
||||
foreach (DiviAdapters::mirrors() as $mirror) {
|
||||
self::assertStringStartsNotWith(DiviReference::SIGNATURE_PREFIX, $mirror->id);
|
||||
}
|
||||
self::assertSame('sig:et_pb_is_allowed', DiviReference::signatureKey(new Call('et_pb_is_allowed', 1)));
|
||||
self::assertSame(
|
||||
'sig:ET_Core_PageResource::remove_static_resources',
|
||||
DiviReference::signatureKey(new Call('\\ET_Core_PageResource::remove_static_resources', 3))
|
||||
);
|
||||
}
|
||||
|
||||
public function testCallsListsEverySymbolOnceAcrossTheManifests(): void
|
||||
{
|
||||
$symbols = array_map(static fn (Call $call): string => ltrim($call->symbol, '\\'), DiviAdapters::calls());
|
||||
|
||||
self::assertSame($symbols, array_values(array_unique($symbols)));
|
||||
self::assertContains('ET_Core_PageResource::remove_static_resources', $symbols);
|
||||
self::assertCount(1, array_keys($symbols, 'ET_Core_PageResource::remove_static_resources', true));
|
||||
self::assertContains('et_pb_is_allowed', $symbols);
|
||||
}
|
||||
|
||||
public function testCurrentSignaturesAreSortedAndKeyedLikeTheReference(): void
|
||||
{
|
||||
$signatures = DiviReference::currentSignatures([
|
||||
new Call('strlen', 1),
|
||||
new Call('\\str_replace', 3),
|
||||
new Call('strlen', 1),
|
||||
]);
|
||||
|
||||
self::assertSame(['sig:str_replace', 'sig:strlen'], array_keys($signatures));
|
||||
self::assertSame(SignatureFingerprint::of(new Call('strlen')), $signatures['sig:strlen']);
|
||||
}
|
||||
|
||||
public function testCurrentSignaturesRefuseASymbolThatIsNotLoaded(): void
|
||||
{
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->expectExceptionMessage('signature of lit_mcp_missing_function(): symbol is not loaded');
|
||||
|
||||
DiviReference::currentSignatures([new Call('strlen', 1), new Call('lit_mcp_missing_function', 0)]);
|
||||
}
|
||||
|
||||
public function testSignatureKeysAreWrittenAndReadLikeMirrorIds(): void
|
||||
{
|
||||
$file = (string) tempnam(sys_get_temp_dir(), 'lit');
|
||||
|
||||
try {
|
||||
DiviReference::writeExpected([
|
||||
'sig:Z\\Klass::run' => str_repeat('c', 64),
|
||||
'tb-api-save' => str_repeat('b', 64),
|
||||
'sig:a_function' => str_repeat('a', 64),
|
||||
], $file);
|
||||
|
||||
self::assertSame(
|
||||
['sig:Z\\Klass::run', 'sig:a_function', 'tb-api-save'],
|
||||
array_keys(DiviReference::expected($file))
|
||||
);
|
||||
} finally {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
public function testWritesAndReadsExpectedFingerprintsSorted(): void
|
||||
{
|
||||
$file = (string) tempnam(sys_get_temp_dir(), 'lit');
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Release;
|
||||
|
||||
use ArrayObject;
|
||||
use LIT\MCP\Integrations\Divi\Release\Call;
|
||||
use LIT\MCP\Integrations\Divi\Release\SignatureFingerprint;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/** Stand-in for a Divi class: one baseline method and one variant per property the fingerprint covers. */
|
||||
final class SignatureFixture
|
||||
{
|
||||
public static function base(int $id, string|array $data = [], &$ref = null, ...$rest): ?bool
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Same signature, other parameter names and other default values: no change. */
|
||||
public static function renamed(int $post, string|array $payload = '', &$out = 5, ...$more): ?bool
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function extraRequired(int $id, string|array $data, &$ref = null, ...$rest): ?bool
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function fewer(int $id, string|array $data = []): ?bool
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function typeChanged(string $id, string|array $data = [], &$ref = null, ...$rest): ?bool
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function unionWidened(int $id, string|array|int $data = [], &$ref = null, ...$rest): ?bool
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function notNullable(int $id, string|array $data = [], &$ref = null, ...$rest): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function byValue(int $id, string|array $data = [], $ref = null, ...$rest): ?bool
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function notVariadic(int $id, string|array $data = [], &$ref = null, $rest = null): ?bool
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function defaultRemoved(int $id, string|array $data, &$ref, ...$rest): ?bool
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function returnChanged(int $id, string|array $data = [], &$ref = null, ...$rest): ?int
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function notStatic(int $id, string|array $data = [], &$ref = null, ...$rest): ?bool
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
protected static function notPublic(int $id, string|array $data = [], &$ref = null, ...$rest): ?bool
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function untyped($id, $data = [])
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function mixedTyped(mixed $id, mixed $data = []): mixed
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
final class SignatureFingerprintTest extends TestCase
|
||||
{
|
||||
private const FIXTURE = SignatureFixture::class;
|
||||
|
||||
private static function method(string $name): Call
|
||||
{
|
||||
return new Call(self::FIXTURE . '::' . $name);
|
||||
}
|
||||
|
||||
public function testIsASha256OfAnExistingFunctionAndStable(): void
|
||||
{
|
||||
$hash = SignatureFingerprint::of(new Call('strlen', 1));
|
||||
|
||||
self::assertMatchesRegularExpression('/^[0-9a-f]{64}$/', (string) $hash);
|
||||
self::assertSame($hash, SignatureFingerprint::of(new Call('strlen', 1)));
|
||||
self::assertNotSame($hash, SignatureFingerprint::of(new Call('str_replace', 3)));
|
||||
}
|
||||
|
||||
public function testMissingSymbolsGiveNull(): void
|
||||
{
|
||||
self::assertNull(SignatureFingerprint::of(new Call('lit_mcp_missing_function')));
|
||||
self::assertNull(SignatureFingerprint::of(new Call('Missing\Klass::run')));
|
||||
self::assertNull(SignatureFingerprint::of(new Call(ArrayObject::class . '::missing')));
|
||||
self::assertNull(SignatureFingerprint::canonical(new Call('lit_mcp_missing_function')));
|
||||
}
|
||||
|
||||
public function testCanonicalTextDescribesTheSignature(): void
|
||||
{
|
||||
self::assertSame(
|
||||
'public static (int, array|string=, &mixed=, ...mixed): ?bool',
|
||||
SignatureFingerprint::canonical(self::method('base'))
|
||||
);
|
||||
self::assertSame('function (string): int', SignatureFingerprint::canonical(new Call('strlen')));
|
||||
self::assertSame(
|
||||
'public (int, array|string=, &mixed=, ...mixed): ?bool',
|
||||
SignatureFingerprint::canonical(self::method('notStatic'))
|
||||
);
|
||||
}
|
||||
|
||||
/** @return array<string, array{string}> */
|
||||
public static function changedSignatures(): array
|
||||
{
|
||||
return [
|
||||
'extra required parameter' => ['extraRequired'],
|
||||
'fewer parameters' => ['fewer'],
|
||||
'parameter type' => ['typeChanged'],
|
||||
'wider union' => ['unionWidened'],
|
||||
'return type loses null' => ['notNullable'],
|
||||
'by reference lost' => ['byValue'],
|
||||
'variadic lost' => ['notVariadic'],
|
||||
'default removed' => ['defaultRemoved'],
|
||||
'return type' => ['returnChanged'],
|
||||
'no longer static' => ['notStatic'],
|
||||
'no longer public' => ['notPublic'],
|
||||
];
|
||||
}
|
||||
|
||||
/** @dataProvider changedSignatures */
|
||||
public function testAnyChangeOfTheInterfaceChangesTheHash(string $variant): void
|
||||
{
|
||||
self::assertNotSame(
|
||||
SignatureFingerprint::of(self::method('base')),
|
||||
SignatureFingerprint::of(self::method($variant)),
|
||||
$variant
|
||||
);
|
||||
}
|
||||
|
||||
public function testRenamedParametersAndOtherDefaultValuesKeepTheHash(): void
|
||||
{
|
||||
self::assertSame(
|
||||
SignatureFingerprint::of(self::method('base')),
|
||||
SignatureFingerprint::of(self::method('renamed'))
|
||||
);
|
||||
}
|
||||
|
||||
public function testNoTypeReadsAsMixed(): void
|
||||
{
|
||||
self::assertSame(
|
||||
SignatureFingerprint::of(self::method('untyped')),
|
||||
SignatureFingerprint::of(self::method('mixedTyped'))
|
||||
);
|
||||
}
|
||||
|
||||
public function testTheCallsOwnArgumentCountAndFeaturesAreNotPartOfIt(): void
|
||||
{
|
||||
self::assertSame(
|
||||
SignatureFingerprint::of(new Call(self::FIXTURE . '::base', 2)),
|
||||
SignatureFingerprint::of(new Call('\\' . self::FIXTURE . '::base', null, []))
|
||||
);
|
||||
}
|
||||
|
||||
public function testAFunctionAndAMethodOfTheSameShapeDiffer(): void
|
||||
{
|
||||
self::assertNotSame(
|
||||
SignatureFingerprint::of(new Call('strlen')),
|
||||
SignatureFingerprint::of(new Call(ArrayObject::class . '::count'))
|
||||
);
|
||||
}
|
||||
|
||||
public function testTheClassNeverIncludesAFile(): void
|
||||
{
|
||||
$source = (string) file_get_contents(
|
||||
dirname(__DIR__, 5) . '/src/Integrations/Divi/Release/SignatureFingerprint.php'
|
||||
);
|
||||
$forbidden = [T_INCLUDE, T_INCLUDE_ONCE, T_REQUIRE, T_REQUIRE_ONCE, T_EVAL];
|
||||
|
||||
foreach (token_get_all($source) as $token) {
|
||||
self::assertFalse(is_array($token) && in_array($token[0], $forbidden, true), 'no include, require or eval');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user