GroupKeywordTest::makeComponentsFrom()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Tests\Components;
6
7
use Generator;
8
use PhpMyAdmin\SqlParser\Components\Expression;
9
use PhpMyAdmin\SqlParser\Components\GroupKeyword;
10
use PhpMyAdmin\SqlParser\Parsers\GroupKeywords;
11
use PhpMyAdmin\SqlParser\Tests\TestCase;
12
use PHPUnit\Framework\Attributes\DataProvider;
13
14
use function array_map;
15
use function is_array;
16
17
class GroupKeywordTest extends TestCase
18
{
19
    /** @return Generator<string, array{GroupKeyword|array<GroupKeyword>, string}> */
20
    public static function provideExpressions(): Generator
21
    {
22
        yield 'With no expression at all' => [[], ''];
23
24
        yield 'With single simple expression' => [
25
            self::makeComponentFrom('a'),
26
            'a',
27
        ];
28
29
        yield 'With multiple simple expressions' => [
30
            self::makeComponentsFrom('a', 'b', 'c'),
31
            'a, b, c',
32
        ];
33
34
        yield 'With single untrimmed expression' => [
35
            self::makeComponentFrom('  o  '),
36
            'o',
37
        ];
38
39
        yield 'With single untrimmed expression having several kinds of whitespaces' => [
40
            self::makeComponentFrom(" \n\r foo \t\v\x00  "),
41
            'foo',
42
        ];
43
44
        yield 'With multiple untrimmed expressions' => [
45
            self::makeComponentsFrom('  x', ' y ', 'z  '),
46
            'x, y, z',
47
        ];
48
49
        yield 'With multiple untrimmed expression having several kinds of whitespaces' => [
50
            self::makeComponentsFrom(" \n\r\t\v\x00foo", " \n\r\tbar\v\x00", "baz \n\r\t\v\x00"),
51
            'foo, bar, baz',
52
        ];
53
    }
54
55
    /** @param GroupKeyword|array<GroupKeyword> $component */
56
    #[DataProvider('provideExpressions')]
57
    public function testBuild(GroupKeyword|array $component, string $expected): void
58
    {
59
        if (is_array($component)) {
0 ignored issues
show
introduced by
The condition is_array($component) is always true.
Loading history...
60
            $this->assertSame($expected, GroupKeywords::buildAll($component));
61
        } else {
62
            $this->assertSame($expected, $component->build());
63
        }
64
    }
65
66
    private static function makeComponentFrom(string $string): GroupKeyword
67
    {
68
        return new GroupKeyword(new Expression($string));
69
    }
70
71
    /** @return array<GroupKeyword> */
72
    private static function makeComponentsFrom(string ...$string): array
73
    {
74
        return array_map(self::makeComponentFrom(...), $string);
75
    }
76
}
77