packages/sql-semantics-mysql/tests/Unit/Statement/Query/WithClauseTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query;
6
7use PHPUnit\Framework\Attributes\CoversNothing;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
11use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\MySql\Model\Statement\Node;
13use SqlSemantics\Platform\MySql\Semantics;
14use SqlSemantics\Platform\MySql\Statement\Query\QueryExpression;
15use SqlSemantics\Platform\MySql\Statement\Query\WithClause;
16
17#[CoversNothing]
18#[Medium]
19final class WithClauseTest extends TestCase
20{
21    public function testAWithClauseIsARenderableNode(): void
22    {
23        self::assertTrue(interface_exists(WithClause::class));
24        self::assertContains(Node::class, class_implements(WithClause::class));
25    }
26
27    public function testBindAnswersTheScopeWithTheCommonTables(): void
28    {
29        $semantics = new Semantics();
30        $operation = $semantics->analyze('WITH c AS (SELECT 1 AS x) SELECT x FROM c');
31        $with = $operation->statement instanceof QueryExpression ? $operation->statement->with : null;
32        $derivation = new Derivation($semantics->context());
33
34        self::assertNotNull($with);
35        self::assertNotNull($with->bind($derivation, $derivation->environment())->commonTable(new Name('c')));
36    }
37}
38