Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
203 lines
6.4 KiB
PHP
203 lines
6.4 KiB
PHP
<?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');
|
|
}
|
|
}
|
|
}
|