packages/sql-semantics-postgres/tests/Unit/Rules/Query/CarriersTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Query\Carriers::class)]
12#[Small]
13final class CarriersTest extends TestCase
14{
15 public function testCoreUnwrapsParenthesesAndClauses(): void
16 {
17 $values = new \SqlSemantics\Platform\PostgreSql\Statement\Query\ValuesList([new \SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\ValuesRow([new \SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant('1'))])]);
18 self::assertSame($values, (new \SqlSemantics\Platform\PostgreSql\Rules\Query\Carriers())->core(new \SqlSemantics\Platform\PostgreSql\Statement\Query\ParenthesizedQuery(new \SqlSemantics\Platform\PostgreSql\Statement\Query\QueryExpression(null, $values, new \SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\SelectOptions(readOnly: true)))));
19 }
20
21 public function testCarryAddsAnOccurrenceNoNameReaches(): void
22 {
23 $context = (new \SqlSemantics\Platform\PostgreSql\Platform())->context(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172), null, [], true);
24 $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($context);
25 $carrier = new \SqlSemantics\Platform\PostgreSql\Statement\Query\QueryExpression(null, new \SqlSemantics\Platform\PostgreSql\Statement\Query\ValuesList([new \SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\ValuesRow([new \SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant('1'))])]), new \SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\SelectOptions(readOnly: true));
26 $environment = (new \SqlSemantics\Platform\PostgreSql\Rules\Query\Carriers())->carry($derivation->environment(), $carrier);
27 self::assertSame([null, null, [], []], [$environment->relations[0]->alias, $environment->relations[0]->name, $environment->relations[0]->shape->slots, $environment->commonTables]);
28 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Rules\Query\Carrier::class, $environment->relations[0]->relation);
29 }
30
31 public function testTakeAnswersTheCarriersOfAQuery(): void
32 {
33 $context = (new \SqlSemantics\Platform\PostgreSql\Platform())->context(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172), null, [], true);
34 $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($context);
35 $values = new \SqlSemantics\Platform\PostgreSql\Statement\Query\ValuesList([new \SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\ValuesRow([new \SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant('1'))])]);
36 $carrier = new \SqlSemantics\Platform\PostgreSql\Statement\Query\QueryExpression(null, $values, new \SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\SelectOptions(readOnly: true));
37 [$environment, $carriers] = (new \SqlSemantics\Platform\PostgreSql\Rules\Query\Carriers())->take((new \SqlSemantics\Platform\PostgreSql\Rules\Query\Carriers())->carry($derivation->environment(), $carrier), $values);
38 self::assertSame([[], [$carrier]], [$environment->relations, $carriers]);
39 }
40}
41