packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/RaiseActionTest.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\Semantics;
11use SqlSemantics\Platform\Sqlite\Statement\Expression\Raise;
12use SqlSemantics\Platform\Sqlite\Statement\Expression\RaiseAction;
13use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
14use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
15use SqlSemantics\Platform\Sqlite\Statement\Trigger\CreateTrigger;
16
17#[CoversClass(RaiseAction::class)]
18#[Medium]
19final class RaiseActionTest extends TestCase
20{
21    public function testFromReadsEachKeyword(): void
22    {
23        self::assertSame(RaiseAction::Ignore, RaiseAction::from('IGNORE'));
24        self::assertSame(RaiseAction::Rollback, RaiseAction::from('ROLLBACK'));
25        self::assertSame(RaiseAction::Abort, RaiseAction::from('ABORT'));
26        self::assertSame(RaiseAction::Fail, RaiseAction::from('FAIL'));
27    }
28
29    public function testCasesListTheFourActions(): void
30    {
31        self::assertSame(['IGNORE', 'ROLLBACK', 'ABORT', 'FAIL'], array_map(static fn (RaiseAction $action): string => $action->value, RaiseAction::cases()));
32    }
33
34    public function testFromMatchesTheActionOfAnAnalyzedRaise(): void
35    {
36        $statement = (new Semantics())->analyze("CREATE TRIGGER r BEFORE DELETE ON t BEGIN SELECT RAISE(abort, 'no'); END")->statement;
37
38        self::assertInstanceOf(CreateTrigger::class, $statement);
39        self::assertInstanceOf(Select::class, $statement->steps[0]);
40        self::assertInstanceOf(ResultColumn::class, $statement->steps[0]->columns[0]);
41        self::assertInstanceOf(Raise::class, $statement->steps[0]->columns[0]->expression);
42        self::assertSame(RaiseAction::Abort, $statement->steps[0]->columns[0]->expression->action);
43    }
44}
45