packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/BindParameterTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Operation;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Missing\UnboundParameter;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Dependent;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
14use SqlSemantics\Platform\Sqlite\Semantics;
15use SqlSemantics\Platform\Sqlite\Statement\Expression\BindParameter;
16use SqlSemantics\Platform\Sqlite\Statement\Expression\ParameterPrefix;
17use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
18use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
19
20#[CoversClass(BindParameter::class)]
21#[Medium]
22final class BindParameterTest extends TestCase
23{
24    public function testMarkerJoinsThePrefixAndTheLabel(): void
25    {
26        self::assertSame('?', (new BindParameter(ParameterPrefix::Question))->marker());
27        self::assertSame('?12', (new BindParameter(ParameterPrefix::Question, '12'))->marker());
28        self::assertSame(':id', (new BindParameter(ParameterPrefix::Colon, 'id'))->marker());
29        self::assertSame('@a::b(1)', (new BindParameter(ParameterPrefix::At, 'a::b(1)'))->marker());
30        self::assertSame('$x', (new BindParameter(ParameterPrefix::Dollar, 'x'))->marker());
31    }
32
33    public function testMarkerReadsEveryWrittenPrefix(): void
34    {
35        $statement = (new Semantics())->analyze('SELECT ?, ?7, :a, @b, $c')->statement;
36
37        self::assertInstanceOf(Select::class, $statement);
38        $parameters = array_map(static function (object $column): BindParameter {
39            self::assertInstanceOf(ResultColumn::class, $column);
40            self::assertInstanceOf(BindParameter::class, $column->expression);
41
42            return $column->expression;
43        }, $statement->columns);
44        self::assertSame(['?', '?7', ':a', '@b', '$c'], array_map(static fn (BindParameter $parameter): string => $parameter->marker(), $parameters));
45        self::assertSame(['', '7', 'a', 'b', 'c'], array_map(static fn (BindParameter $parameter): string => $parameter->label, $parameters));
46        self::assertSame([ParameterPrefix::Question, ParameterPrefix::Question, ParameterPrefix::Colon, ParameterPrefix::At, ParameterPrefix::Dollar], array_map(static fn (BindParameter $parameter): ParameterPrefix => $parameter->prefix, $parameters));
47    }
48
49    public function testDeriveScalarDependsOnTheBoundValue(): void
50    {
51        $operation = (new Semantics())->analyze('SELECT ?2', []);
52        $statement = $operation->statement;
53
54        self::assertInstanceOf(Select::class, $statement);
55        self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
56        $fact = $operation->facts->scalar($statement->columns[0]->expression);
57        self::assertEquals(new Dependent([new UnboundParameter('?2')]), $fact->type);
58        self::assertSame(Nullability::Dependent, $fact->nullability);
59        self::assertNull($fact->resolution);
60        self::assertSame(Nullability::Dependent, $operation->field(0)->nullability);
61        self::assertSame([], $operation->facts->diagnostics);
62    }
63
64    public function testDeriveScalarNamesTheMarkerOfANamedParameter(): void
65    {
66        $operation = (new Semantics())->analyze('SELECT :name', []);
67        $type = $operation->field(0)->type;
68
69        self::assertInstanceOf(Dependent::class, $type);
70        self::assertInstanceOf(UnboundParameter::class, $type->missing[0]);
71        self::assertSame(':name', $type->missing[0]->marker);
72        self::assertSame('the value bound to parameter :name', $type->missing[0]->describe());
73    }
74
75    public function testRenderWritesTheMarkerAsWritten(): void
76    {
77        self::assertSame('SELECT ?, ?7, :a, @b, $c', (new Semantics())->analyze('select ?, ?7, :a, @b, $c')->toString());
78    }
79
80    public function testRenderWritesANewlyBuiltParameter(): void
81    {
82        $semantics = new Semantics();
83        $operation = new Operation($semantics->context(), new Select([new ResultColumn(new BindParameter(ParameterPrefix::Question)), new ResultColumn(new BindParameter(ParameterPrefix::At, 'x'))]));
84
85        self::assertSame('SELECT ?, @x', $operation->toString());
86    }
87}
88