packages/sql-faker/tests/Unit/PostgreSql/Generation/Tokenization/PgQuotingTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\SqlFaker\PostgreSql\Generation\Tokenization;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\DataProvider;
9use PHPUnit\Framework\TestCase;
10use SqlFaker\PostgreSql\Generation\Tokenization\PgQuoting;
11
12#[CoversClass(PgQuoting::class)]
13#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Derivation\CompletionCosts::class)]
14#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Derivation\DerivationNode::class)]
15final class PgQuotingTest extends TestCase
16{
17 #[DataProvider('providerIdentifier')]
18 public function testIdentifierEncodesTheChosenBody(string $body, string $expected): void
19 {
20 self::assertSame($expected, PgQuoting::identifier($body));
21 }
22
23 /**
24 * @return iterable<string, array{string, string}>
25 */
26 public static function providerIdentifier(): iterable
27 {
28 yield 'plain' => ['users', '"users"'];
29 yield 'keyword' => ['select', '"select"'];
30 yield 'embedded delimiter' => ['a"b', '"a""b"'];
31 yield 'multiple delimiters' => ['"x"', '"""x"""'];
32 yield 'unicode' => ['利用者', '"利用者"'];
33 }
34
35 #[DataProvider('providerStringLiteral')]
36 public function testStringLiteralEncodesTheChosenBody(string $body, string $expected): void
37 {
38 self::assertSame($expected, PgQuoting::stringLiteral($body));
39 }
40
41 /**
42 * @return iterable<string, array{string, string}>
43 */
44 public static function providerStringLiteral(): iterable
45 {
46 yield 'empty' => ['', '\'\''];
47 yield 'plain' => ['text', '\'text\''];
48 yield 'apostrophe' => ['a\'b', '\'a\'\'b\''];
49 yield 'consecutive quotes' => ['\'\'', '\'\'\'\'\'\''];
50 yield 'backslash' => ['a\\b', '\'a\\b\''];
51 yield 'unicode' => ['日本語', '\'日本語\''];
52 }
53}
54