packages/sql-faker/tests/Unit/Sqlite/Generation/Tokenization/SqliteQuotingTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\SqlFaker\Sqlite\Generation\Tokenization;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\DataProvider;
9use PHPUnit\Framework\TestCase;
10use SqlFaker\Sqlite\Generation\Tokenization\SqliteQuoting;
11
12#[CoversClass(SqliteQuoting::class)]
13#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Derivation\CompletionCosts::class)]
14#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFaker\Generation\Derivation\DerivationNode::class)]
15final class SqliteQuotingTest extends TestCase
16{
17    #[DataProvider('providerIdentifier')]
18    public function testIdentifierEncodesTheChosenBody(string $body, string $expected): void
19    {
20        self::assertSame($expected, SqliteQuoting::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, SqliteQuoting::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    #[DataProvider('providerBacktickIdentifier')]
55    public function testBacktickIdentifierEncodesTheChosenBody(string $body, string $expected): void
56    {
57        self::assertSame($expected, SqliteQuoting::backtickIdentifier($body));
58    }
59
60    /**
61     * @return iterable<string, array{string, string}>
62     */
63    public static function providerBacktickIdentifier(): iterable
64    {
65        yield 'plain' => ['users', '`users`'];
66        yield 'embedded' => ['a`b', '`a``b`'];
67        yield 'multiple' => ['`a`', '```a```'];
68    }
69
70    #[DataProvider('providerBracketIdentifier')]
71    public function testBracketIdentifierEncodesTheChosenBody(string $body, string $expected): void
72    {
73        self::assertSame($expected, SqliteQuoting::bracketIdentifier($body));
74    }
75
76    /**
77     * @return iterable<string, array{string, string}>
78     */
79    public static function providerBracketIdentifier(): iterable
80    {
81        yield 'plain' => ['users', '[users]'];
82        yield 'closing bracket' => ['a]b', '[ab]'];
83        yield 'multiple closing brackets' => [']]a]', '[a]'];
84    }
85}
86