packages/sql-semantics-postgres/tests/Unit/Lowering/Manipulation/CursorRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Manipulation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Lowering\Manipulation\CursorRule::class)]
12#[Medium]
13final class CursorRuleTest extends TestCase
14{
15 public function testDeclareLowersTheHoldability(): void
16 {
17 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
18 $query = $semantics->analyze('DECLARE c CURSOR WITHOUT HOLD FOR SELECT 1');
19 $statement = $query->statement;
20 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Cursor\DeclareCursor::class, $statement);
21 self::assertSame(\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Cursor\Holdability::Without, $statement->hold);
22 }
23
24 public function testOptionsKeepsTheWrittenOrder(): void
25 {
26 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
27 $query = $semantics->analyze('DECLARE c SCROLL BINARY INSENSITIVE CURSOR FOR SELECT 1');
28 $statement = $query->statement;
29 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Cursor\DeclareCursor::class, $statement);
30 self::assertSame([\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Cursor\CursorOption::Scroll, \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Cursor\CursorOption::Binary, \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Cursor\CursorOption::Insensitive], $statement->options);
31 }
32
33 public function testFetchLowersTheMovementAndTheCount(): void
34 {
35 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
36 $query = $semantics->analyze('MOVE BACKWARD -2 IN c');
37 $statement = $query->statement;
38 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Cursor\Fetch::class, $statement);
39 self::assertSame([\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Cursor\FetchMovement::BackwardCount, true, true], [$statement->movement, $statement->move, $statement->count?->negative]);
40 }
41
42 public function testFromAcceptsTheNoiseWords(): void
43 {
44 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
45 $query = $semantics->analyze('FETCH ALL IN c');
46 self::assertSame('FETCH ALL c', $query->toString());
47 }
48
49 public function testCloseLowersTheCursor(): void
50 {
51 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
52 $query = $semantics->analyze('CLOSE c');
53 $statement = $query->statement;
54 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Cursor\Close::class, $statement);
55 self::assertSame('c', $statement->cursor?->value);
56 }
57}
58