File: GenericNameParserTest.php

package info (click to toggle)
php-doctrine-dbal 4.3.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,092 kB
  • sloc: php: 60,293; xml: 618; makefile: 23
file content (142 lines) | stat: -rw-r--r-- 4,712 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Schema\Name\Parser;

use Doctrine\DBAL\Schema\Name\Identifier;
use Doctrine\DBAL\Schema\Name\Parser\Exception;
use Doctrine\DBAL\Schema\Name\Parser\Exception\ExpectedDot;
use Doctrine\DBAL\Schema\Name\Parser\Exception\ExpectedNextIdentifier;
use Doctrine\DBAL\Schema\Name\Parser\Exception\UnableToParseIdentifier;
use Doctrine\DBAL\Schema\Name\Parser\GenericNameParser;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class GenericNameParserTest extends TestCase
{
    private GenericNameParser $parser;

    protected function setUp(): void
    {
        $this->parser = new GenericNameParser();
    }

    /**
     * @param list<Identifier> $expected
     *
     * @throws Exception
     */
    #[DataProvider('validInputProvider')]
    public function testValidInput(string $input, array $expected): void
    {
        $name = $this->parser->parse($input);
        self::assertEquals($expected, $name->getIdentifiers());
    }

    /** @return iterable<array{string, list<Identifier>}> */
    public static function validInputProvider(): iterable
    {
        yield ['table', [Identifier::unquoted('table')]];
        yield ['schema.table', [Identifier::unquoted('schema'), Identifier::unquoted('table')]];

        yield ['"example.com"', [Identifier::quoted('example.com')]];
        yield ['`example.com`', [Identifier::quoted('example.com')]];
        yield ['[example.com]', [Identifier::quoted('example.com')]];

        yield [
            'a."b".c.`d`.e.[f].g',
            [
                Identifier::unquoted('a'),
                Identifier::quoted('b'),
                Identifier::unquoted('c'),
                Identifier::quoted('d'),
                Identifier::unquoted('e'),
                Identifier::quoted('f'),
                Identifier::unquoted('g'),
            ],
        ];

        yield ['"schema"."table"', [Identifier::quoted('schema'), Identifier::quoted('table')]];
        yield ['`schema`.`table`', [Identifier::quoted('schema'), Identifier::quoted('table')]];
        yield ['[schema].[table]', [Identifier::quoted('schema'), Identifier::quoted('table')]];

        yield [
            'schema."example.com"',
            [
                Identifier::unquoted('schema'),
                Identifier::quoted('example.com'),
            ],
        ];

        yield [
            '"a""b".`c``d`.[e]]f]',
            [
                Identifier::quoted('a"b'),
                Identifier::quoted('c`d'),
                Identifier::quoted('e]f'),
            ],
        ];

        yield [
            'schéma."übermäßigkeit".`àçcênt`.[éxtrême].çhâràctér',
            [
                Identifier::unquoted('schéma'),
                Identifier::quoted('übermäßigkeit'),
                Identifier::quoted('àçcênt'),
                Identifier::quoted('éxtrême'),
                Identifier::unquoted('çhâràctér'),
            ],
        ];

        yield [
            '" spaced identifier ".more',
            [
                Identifier::quoted(' spaced identifier '),
                Identifier::unquoted('more'),
            ],
        ];

        yield [
            '0."0".`0`.[0]',
            [
                Identifier::unquoted('0'),
                Identifier::quoted('0'),
                Identifier::quoted('0'),
                Identifier::quoted('0'),
            ],
        ];
    }

    /**
     * @param class-string<Exception> $expectedException
     *
     * @throws Exception
     */
    #[DataProvider('invalidInputProvider')]
    public function testInvalidInput(string $input, string $expectedException): void
    {
        $this->expectException($expectedException);
        $this->parser->parse($input);
    }

    /** @return iterable<array{string, class-string<Exception>}> */
    public static function invalidInputProvider(): iterable
    {
        yield ['', ExpectedNextIdentifier::class];

        yield ['"example.com', UnableToParseIdentifier::class];
        yield ['`example.com', UnableToParseIdentifier::class];
        yield ['[example.com', UnableToParseIdentifier::class];
        yield ['schema."example.com', UnableToParseIdentifier::class];
        yield ['schema.[example.com', UnableToParseIdentifier::class];
        yield ['schema.`example.com', UnableToParseIdentifier::class];

        yield ['schema.', ExpectedNextIdentifier::class];
        yield ['schema..', UnableToParseIdentifier::class];
        yield ['.table', UnableToParseIdentifier::class];

        yield ['schema.table name', ExpectedDot::class];
        yield ['"schema.[example.com]', UnableToParseIdentifier::class];
    }
}