packages/sql-semantics-sqlite/tests/Unit/Statement/Relation/JoinStepTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Relation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Operation;
13use SqlSemantics\Platform\Sqlite\Semantics;
14use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
15use SqlSemantics\Platform\Sqlite\Statement\Query\Star;
16use SqlSemantics\Platform\Sqlite\Statement\Relation\JoinChain;
17use SqlSemantics\Platform\Sqlite\Statement\Relation\JoinKeyword;
18use SqlSemantics\Platform\Sqlite\Statement\Relation\JoinOperator;
19use SqlSemantics\Platform\Sqlite\Statement\Relation\JoinStep;
20use SqlSemantics\Platform\Sqlite\Statement\Relation\JoinUsing;
21use SqlSemantics\Platform\Sqlite\Statement\Relation\TableInput;
22
23#[CoversClass(JoinStep::class)]
24#[Medium]
25final class JoinStepTest extends TestCase
26{
27    public function testRenderWritesOperatorRelationAndConstraint(): void
28    {
29        $semantics = new Semantics();
30        $step = new JoinStep(new JoinOperator(false, [JoinKeyword::Left]), new TableInput(new QualifiedName(new Name('u')), new Name('x')), new JoinUsing([new Name('a')]));
31        $built = new Operation($semantics->context(), new Select([new Star()], new JoinChain(new TableInput(new QualifiedName(new Name('t'))), [$step])));
32        $query = $semantics->analyze('select * from t left join u using (a) join u as u2');
33
34        self::assertSame('SELECT * FROM t LEFT JOIN u AS x USING (a)', $built->toString());
35        self::assertSame('SELECT * FROM t LEFT JOIN u USING (a) JOIN u AS u2', $query->toString());
36        self::assertInstanceOf(Select::class, $query->statement);
37        self::assertInstanceOf(JoinChain::class, $query->statement->from);
38        self::assertSame([JoinKeyword::Left], $query->statement->from->steps[0]->operator->words);
39        self::assertInstanceOf(TableInput::class, $query->statement->from->steps[0]->relation);
40        self::assertInstanceOf(JoinUsing::class, $query->statement->from->steps[0]->constraint);
41        self::assertNull($query->statement->from->steps[1]->constraint);
42        $second = $query->statement->from->steps[1]->relation;
43        self::assertInstanceOf(TableInput::class, $second);
44        self::assertSame('u2', $second->alias?->value);
45    }
46
47    public function testRejectsAJoinChainAsTheJoinedRelation(): void
48    {
49        $chain = new JoinChain(new TableInput(new QualifiedName(new Name('t'))), [new JoinStep(new JoinOperator(), new TableInput(new QualifiedName(new Name('u'))))]);
50
51        $this->expectExceptionMessage('A join chain used as a term is written in parentheses.');
52
53        new JoinStep(new JoinOperator(true), $chain);
54    }
55}
56