packages/sql-catalog/tests/Unit/Core/Sql/DialectTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Core\Sql;
6
7use PHPUnit\Framework\TestCase;
8
9#[\PHPUnit\Framework\Attributes\CoversClass(\SqlCatalog\Extension\Laravel\Grammar::class)]
10#[\PHPUnit\Framework\Attributes\CoversClass(\SqlCatalog\Extension\Laravel\WriteCompiler::class)]
11#[\PHPUnit\Framework\Attributes\CoversClass(\SqlCatalog\Extension\Laravel\QueryState::class)]
12final class DialectTest extends TestCase
13{
14    public function testIdentifierQuoteCanBeSuppliedByAnApplication(): void
15    {
16        $policy = self::createStub(\SqlCatalog\Core\Sql\Dialect::class);
17        $policy->method('identifierQuote')->willReturn('!');
18        $grammar = new \SqlCatalog\Extension\Laravel\Grammar($policy);
19        self::assertSame('!a!.!b!', $grammar->wrap(\SqlCatalog\Core\Evaluation\Domain::literal('a.b'))->soleLiteral()?->value);
20    }
21
22    public function testInsertPrefixComesFromThePolicy(): void
23    {
24        $policy = self::createStub(\SqlCatalog\Core\Sql\Dialect::class);
25        $policy->method('identifierQuote')->willReturn('"');
26        $policy->method('insertPrefix')->willReturn('custom insert into ');
27        $state = new \SqlCatalog\Extension\Laravel\QueryState(['table' => \SqlCatalog\Core\Evaluation\Domain::literal('items')]);
28        $values = new \SqlCatalog\Core\Evaluation\ArrayTerm([new \SqlCatalog\Core\Evaluation\ArrayEntry(\SqlCatalog\Core\Evaluation\Domain::literal('id'), \SqlCatalog\Core\Evaluation\Domain::literal(1))]);
29        [$sql] = (new \SqlCatalog\Extension\Laravel\WriteCompiler(new \SqlCatalog\Extension\Laravel\Grammar($policy)))->insert($state, $values, true);
30        self::assertSame('custom insert into "items" ("id") values (?)', $sql->soleLiteral()?->value);
31    }
32
33    public function testInsertSuffixComesFromThePolicy(): void
34    {
35        $policy = self::createStub(\SqlCatalog\Core\Sql\Dialect::class);
36        $policy->method('identifierQuote')->willReturn('"');
37        $policy->method('insertPrefix')->willReturn('insert into ');
38        $policy->method('insertSuffix')->willReturn(' custom conflict policy');
39        $state = new \SqlCatalog\Extension\Laravel\QueryState(['table' => \SqlCatalog\Core\Evaluation\Domain::literal('items')]);
40        $values = new \SqlCatalog\Core\Evaluation\ArrayTerm([new \SqlCatalog\Core\Evaluation\ArrayEntry(\SqlCatalog\Core\Evaluation\Domain::literal('id'), \SqlCatalog\Core\Evaluation\Domain::literal(1))]);
41        [$sql] = (new \SqlCatalog\Extension\Laravel\WriteCompiler(new \SqlCatalog\Extension\Laravel\Grammar($policy)))->insert($state, $values, true);
42        self::assertSame('insert into "items" ("id") values (?) custom conflict policy', $sql->soleLiteral()?->value);
43    }
44
45
46    public function testReturningSuffixComesFromThePolicy(): void
47    {
48        $policy = self::createStub(\SqlCatalog\Core\Sql\Dialect::class);
49        $policy->method('identifierQuote')->willReturn('"');
50        $policy->method('insertPrefix')->willReturn('insert into ');
51        $policy->method('insertSuffix')->willReturn('');
52        $policy->method('returningSuffix')->willReturn(' custom returning ');
53        $state = new \SqlCatalog\Extension\Laravel\QueryState(['table' => \SqlCatalog\Core\Evaluation\Domain::literal('items')]);
54        $values = new \SqlCatalog\Core\Evaluation\ArrayTerm([new \SqlCatalog\Core\Evaluation\ArrayEntry(\SqlCatalog\Core\Evaluation\Domain::literal('id'), \SqlCatalog\Core\Evaluation\Domain::literal(1))]);
55        [$sql] = (new \SqlCatalog\Extension\Laravel\WriteCompiler(new \SqlCatalog\Extension\Laravel\Grammar($policy)))->insertGetId($state, $values, \SqlCatalog\Core\Evaluation\Domain::literal(null));
56        self::assertSame('insert into "items" ("id") values (?) custom returning "id"', $sql->soleLiteral()?->value);
57    }
58}
59