packages/sql-semantics-mysql/fuzz/Target/SchemaTarget.php

1<?php
2
3declare(strict_types=1);
4
5namespace Fuzz\Target;
6
7use Error;
8use SqlSemantics\Platform\MySql\Model\Validation\Equivalence;
9use SqlSemantics\Platform\MySql\Semantics;
10use Throwable;
11
12/**
13 * Every generated table definition must declare one table, round-trip structurally, declare the same table from its rendering, and stay the same structure under any context.
14 */
15final class SchemaTarget
16{
17    public function __construct(private readonly Semantics $semantics, private readonly string $grammarVersion)
18    {
19    }
20
21    /**
22     * No generated input or semantic rejection is filtered out.
23     */
24    public function verify(string $sql, string $input): void
25    {
26        try {
27            $operation = $this->semantics->inspect($sql, []);
28            if (count($operation->declarations()) !== 1) {
29                throw new Error('A table definition must declare exactly one table; ' . count($operation->declarations()) . ' declared.');
30            }
31            $rendered = $operation->toString();
32            $again = $this->semantics->inspect($rendered, []);
33            $difference = (new Equivalence())->difference($operation->statement, $again->statement);
34            if ($difference !== null) {
35                throw new Error('The structure of the rendered definition differs from the structure of the input at ' . $difference);
36            }
37            if ($again->toString() !== $rendered) {
38                throw new Error('The rendering is not stable: ' . $again->toString());
39            }
40            if (count($again->declarations()) !== 1) {
41                throw new Error('The rendered definition must declare exactly one table; ' . count($again->declarations()) . ' declared.');
42            }
43            $difference = (new Equivalence())->difference($operation->declarations()[0], $again->declarations()[0]);
44            if ($difference !== null) {
45                throw new Error('The table declared by the rendered definition differs from the table declared by the input at ' . $difference);
46            }
47            $dependent = $this->semantics->inspect($rendered, [$operation]);
48            $difference = (new Equivalence())->difference($operation->statement, $dependent->statement);
49            if ($difference !== null) {
50                throw new Error('The declaration context changed the structure of the definition at ' . $difference);
51            }
52            if (count($dependent->declarations()) !== 1) {
53                throw new Error('A table definition analyzed with its own declaration in the context must still declare one table.');
54            }
55            $difference = (new Equivalence())->difference($operation->declarations()[0], $dependent->declarations()[0]);
56            if ($difference !== null) {
57                throw new Error('The declaration context changed the declared table at ' . $difference);
58            }
59        } catch (Throwable $error) {
60            throw new Error("Schema property failed\nGrammar: {$this->grammarVersion}\nInput (hex): " . bin2hex($input) . "\nSQL: {$sql}\n{$error->getMessage()}", 0, $error);
61        }
62    }
63}
64