packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/RaiseTest.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\Type\Nullability;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Type\NullOnly;
13use SqlSemantics\Platform\Sqlite\Semantics;
14use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\TextLiteral;
15use SqlSemantics\Platform\Sqlite\Statement\Expression\Raise;
16use SqlSemantics\Platform\Sqlite\Statement\Expression\RaiseAction;
17use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\Misuse;
18use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\MisuseRule;
19use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
20use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
21use SqlSemantics\Platform\Sqlite\Statement\Trigger\CreateTrigger;
22
23#[CoversClass(Raise::class)]
24#[Medium]
25final class RaiseTest extends TestCase
26{
27    public function testDeriveScalarYieldsNoValueInsideATrigger(): void
28    {
29        $semantics = new Semantics();
30        $create = $semantics->inspect('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)');
31        $operation = $semantics->inspect("CREATE TRIGGER r BEFORE DELETE ON t BEGIN SELECT RAISE(FAIL, 'no'); END", [$create]);
32        $statement = $operation->statement;
33
34        self::assertInstanceOf(CreateTrigger::class, $statement);
35        self::assertInstanceOf(Select::class, $statement->steps[0]);
36        self::assertInstanceOf(ResultColumn::class, $statement->steps[0]->columns[0]);
37        self::assertInstanceOf(Raise::class, $statement->steps[0]->columns[0]->expression);
38        $raise = $statement->steps[0]->columns[0]->expression;
39        self::assertSame(RaiseAction::Fail, $raise->action);
40        self::assertInstanceOf(TextLiteral::class, $raise->message);
41        self::assertSame('no', $raise->message->value);
42        $fact = $operation->facts->scalar($raise);
43        self::assertInstanceOf(NullOnly::class, $fact->type);
44        self::assertSame(Nullability::Nullable, $fact->nullability);
45        self::assertTrue($operation->facts->covers($raise->message));
46        self::assertSame([], $operation->facts->diagnostics);
47    }
48
49    public function testDeriveScalarOutsideATriggerIsReportedByTheStatement(): void
50    {
51        $operation = (new Semantics())->inspect('SELECT RAISE(IGNORE)', []);
52
53        self::assertEquals([new Misuse(MisuseRule::RaiseOutsideTrigger)], $operation->facts->diagnostics);
54        self::assertInstanceOf(NullOnly::class, $operation->field(0)->type);
55    }
56
57    public function testRenderWritesTheActionAndTheMessage(): void
58    {
59        $operation = (new Semantics())->inspect('create trigger r before delete on t begin select raise(ignore) AS c1; select raise(rollback, \'r\') AS c2, raise(abort, \'a\') AS c3, raise(fail, \'f\') AS c4; end');
60
61        self::assertSame('CREATE TRIGGER r BEFORE DELETE ON t BEGIN SELECT RAISE(IGNORE) AS c1; SELECT RAISE(ROLLBACK, \'r\') AS c2, RAISE(ABORT, \'a\') AS c3, RAISE(FAIL, \'f\') AS c4; END', $operation->toString());
62    }
63
64    public function testRenderWritesANewlyBuiltRaise(): void
65    {
66        $semantics = new Semantics();
67        $operation = new Operation($semantics->context(), new Select([new ResultColumn(new Raise(RaiseAction::Ignore)), new ResultColumn(new Raise(RaiseAction::Abort, new TextLiteral('stop')))]));
68
69        self::assertSame("SELECT RAISE(IGNORE), RAISE(ABORT, 'stop')", $operation->toString());
70    }
71}
72