Passed
Push — master ( 8f524d...baa0bd )
by William
09:23
created

StatementTest::getAliasesProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 88
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 50
nc 1
nop 0
dl 0
loc 88
rs 9.0909
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Tests\Builder;
6
7
use PhpMyAdmin\SqlParser\Components\Condition;
8
use PhpMyAdmin\SqlParser\Components\Expression;
9
use PhpMyAdmin\SqlParser\Components\Limit;
10
use PhpMyAdmin\SqlParser\Components\OptionsArray;
11
use PhpMyAdmin\SqlParser\Parser;
12
use PhpMyAdmin\SqlParser\Statements\SelectStatement;
13
use PhpMyAdmin\SqlParser\Tests\TestCase;
14
15
class StatementTest extends TestCase
16
{
17
    public function testBuilder(): void
18
    {
19
        $stmt = new SelectStatement();
20
21
        $stmt->options = new OptionsArray(['DISTINCT']);
22
23
        $stmt->expr[] = new Expression('sakila', 'film', 'film_id', 'fid');
24
        $stmt->expr[] = new Expression('COUNT(film_id)');
25
26
        $stmt->from[] = new Expression('', 'film', '');
27
        $stmt->from[] = new Expression('', 'actor', '');
28
29
        $stmt->where[] = new Condition('film_id > 10');
30
        $stmt->where[] = new Condition('OR');
31
        $stmt->where[] = new Condition('actor.age > 25');
32
33
        $stmt->limit = new Limit(1, 10);
34
35
        $this->assertEquals(
36
            'SELECT DISTINCT `sakila`.`film`.`film_id` AS `fid`, COUNT(film_id) ' .
37
            'FROM `film`, `actor` ' .
38
            'WHERE film_id > 10 OR actor.age > 25 ' .
39
            'LIMIT 10, 1',
40
            (string) $stmt
41
        );
42
    }
43
44
    /**
45
     * @psalm-param array<string, array{
46
     *   alias: (string|null),
47
     *   tables: array<string, array{alias: (string|null), columns: array<string, string>}>
48
     * }> $expected
49
     *
50
     * @dataProvider getAliasesProvider
51
     */
52
    public function testGetAliases(string $query, string $db, array $expected): void
53
    {
54
        $parser = new Parser($query);
55
        $this->assertInstanceOf(SelectStatement::class, $parser->statements[0]);
56
        $this->assertEquals($expected, $parser->statements[0]->getAliases($db));
57
    }
58
59
    /**
60
     * @psalm-return list<array{string, string, array<string, array{
61
     *   alias: (string|null),
62
     *   tables: array<string, array{alias: (string|null), columns: array<string, string>}>
63
     * }>}>
64
     */
65
    public static function getAliasesProvider(): array
66
    {
67
        return [
68
            [
69
                'select * from (select 1) tbl',
70
                'mydb',
71
                [],
72
            ],
73
            [
74
                'select i.name as `n`,abcdef gh from qwerty i',
75
                'mydb',
76
                [
77
                    'mydb' => [
78
                        'alias' => null,
79
                        'tables' => [
80
                            'qwerty' => [
81
                                'alias' => 'i',
82
                                'columns' => [
83
                                    'name' => 'n',
84
                                    'abcdef' => 'gh',
85
                                ],
86
                            ],
87
                        ],
88
                    ],
89
                ],
90
            ],
91
            [
92
                'select film_id id,title from film',
93
                'sakila',
94
                [
95
                    'sakila' => [
96
                        'alias' => null,
97
                        'tables' => [
98
                            'film' => [
99
                                'alias' => null,
100
                                'columns' => ['film_id' => 'id'],
101
                            ],
102
                        ],
103
                    ],
104
                ],
105
            ],
106
            [
107
                'select `sakila`.`A`.`actor_id` as aid,`F`.`film_id` `fid`,'
108
                . 'last_update updated from `sakila`.actor A join `film_actor` as '
109
                . '`F` on F.actor_id = A.`actor_id`',
110
                'sakila',
111
                [
112
                    'sakila' => [
113
                        'alias' => null,
114
                        'tables' => [
115
                            'film_actor' => [
116
                                'alias' => 'F',
117
                                'columns' => [
118
                                    'film_id' => 'fid',
119
                                    'last_update' => 'updated',
120
                                ],
121
                            ],
122
                            'actor' => [
123
                                'alias' => 'A',
124
                                'columns' => [
125
                                    'actor_id' => 'aid',
126
                                    'last_update' => 'updated',
127
                                ],
128
                            ],
129
                        ],
130
                    ],
131
                ],
132
            ],
133
            [
134
                'SELECT film_id FROM (SELECT * FROM film) as f;',
135
                'sakila',
136
                [],
137
            ],
138
            [
139
                'SELECT 1',
140
                '',
141
                [],
142
            ],
143
            [
144
                'SELECT * FROM orders AS ord WHERE 1',
145
                'db',
146
                [
147
                    'db' => [
148
                        'alias' => null,
149
                        'tables' => [
150
                            'orders' => [
151
                                'alias' => 'ord',
152
                                'columns' => [],
153
                            ],
154
                        ],
155
                    ],
156
                ],
157
            ],
158
        ];
159
    }
160
}
161