packages/sql-semantics-postgres/tests/Unit/Lowering/Manipulation/CopyOptionRuleTest.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\CopyOptionRule::class)]
12#[Medium]
13final class CopyOptionRuleTest extends TestCase
14{
15 public function testOptionsLowersTheOldSyntax(): void
16 {
17 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
18 $query = $semantics->inspect("COPY t TO STDOUT WITH CSV HEADER QUOTE '\"' FORCE QUOTE *");
19 $statement = $query->statement;
20 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Copy\CopyTable::class, $statement);
21 self::assertCount(4, $statement->legacy);
22 }
23
24 public function testItemLowersAForceColumnList(): void
25 {
26 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
27 $query = $semantics->inspect('COPY t FROM STDIN CSV FORCE NOT NULL a, b');
28 $statement = $query->statement;
29 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Copy\CopyTable::class, $statement);
30 $force = $statement->legacy[1];
31 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Copy\CopyForce::class, $force);
32 self::assertCount(2, $force->columns);
33 }
34
35 public function testOptionLowersTheName(): void
36 {
37 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
38 $query = $semantics->inspect('COPY t FROM STDIN ("Format" csv)');
39 $statement = $query->statement;
40 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Copy\CopyTable::class, $statement);
41 self::assertSame('Format', $statement->options[0]->name->value);
42 }
43
44 public function testArgumentLowersEveryForm(): void
45 {
46 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
47 $query = $semantics->inspect("COPY t FROM STDIN (a, b 1, c *, d DEFAULT, e (x), f 'y', g true)");
48 $statement = $query->statement;
49 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Copy\CopyTable::class, $statement);
50 self::assertSame(7, count($statement->options));
51 }
52
53 public function testArgumentsLowersTheItems(): void
54 {
55 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
56 $query = $semantics->inspect("COPY t FROM STDIN (force_null (a, 'b', off))");
57 $statement = $query->statement;
58 self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Copy\CopyTable::class, $statement);
59 self::assertCount(3, $statement->options[0]->argument instanceof \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\Copy\CopyArguments ? $statement->options[0]->argument->items : []);
60 }
61}
62