Passed
Push — master ( 8807b4...37e483 )
by Wilmer
03:08
created

QueryHelperTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
c 0
b 0
f 0
dl 0
loc 129
rs 10
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A createQueryHelper() 0 3 1
A testFilterCondition() 0 3 1
A testCleanUpTableNamesException() 0 7 1
A filterConditionDataProvider() 0 38 1
A testNormalizeOrderBy() 0 3 1
A testCleanUpTableNames() 0 5 1
A normalizeSelectProvider() 0 11 1
A normalizeOrderByProvider() 0 7 1
A tablesNameDataProvider() 0 10 1
A testNormalizeSelect() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Query\Helper;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Db\Exception\InvalidArgumentException;
9
use Yiisoft\Db\Expression\Expression;
10
use Yiisoft\Db\Query\Helper\QueryHelper;
11
use Yiisoft\Db\Schema\Quoter;
12
13
/**
14
 * @psalm-suppress PropertyNotSetInConstructor
15
 */
16
final class QueryHelperTest extends TestCase
17
{
18
    public function tablesNameDataProvider(): array
19
    {
20
        return [
21
            [['customer'], '', ['{{customer}}' => '{{customer}}']],
22
            [['profile AS "prf"'], '', ['{{prf}}' => '{{profile}}']],
23
            [['mainframe as400'], '', ['{{as400}}' => '{{mainframe}}']],
24
            [
25
                ['x' => new Expression('(SELECT id FROM user)')],
26
                '',
27
                ['{{x}}' => new Expression('(SELECT id FROM user)')],
28
            ],
29
        ];
30
    }
31
32
    /**
33
     * @dataProvider tablesNameDataProvider
34
     */
35
    public function testCleanUpTableNames(array $tables, string $prefixDatabase, array $expected): void
0 ignored issues
show
Unused Code introduced by
The parameter $prefixDatabase is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

35
    public function testCleanUpTableNames(array $tables, /** @scrutinizer ignore-unused */ string $prefixDatabase, array $expected): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        $this->assertEquals(
38
            $expected,
39
            $this->createQueryHelper()->cleanUpTableNames($tables, new Quoter('"', '"'))
40
        );
41
    }
42
43
    public function testCleanUpTableNamesException(): void
44
    {
45
        $this->expectException(InvalidArgumentException::class);
46
        $this->expectExceptionMessage('To use Expression in from() method, pass it in array format with alias.');
47
        $this->createQueryHelper()->cleanUpTableNames(
48
            [new Expression('(SELECT id FROM user)')],
49
            new Quoter('"', '"')
50
        );
51
    }
52
53
    public function filterConditionDataProvider(): array
54
    {
55
        return [
56
            /* like */
57
            [['like', 'name', []], []],
58
            [['not like', 'name', []], []],
59
            [['or like', 'name', []],  []],
60
            [['or not like', 'name', []], []],
61
62
            /* not */
63
            [['not', ''], []],
64
65
            /* and */
66
            [['and', '', ''], []],
67
            [['and', '', 'id=2'], ['and', 'id=2']],
68
            [['and', 'id=1', ''], ['and', 'id=1']],
69
            [['and', 'type=1', ['or', '', 'id=2']], ['and', 'type=1', ['or', 'id=2']]],
70
71
            /* or */
72
            [['or', 'id=1', ''], ['or', 'id=1']],
73
            [['or', 'type=1', ['or', '', 'id=2']], ['or', 'type=1', ['or', 'id=2']]],
74
75
            /* between */
76
            [['between', 'id', 1, null], []],
77
            [['not between', 'id', null, 10], []],
78
79
            /* in */
80
            [['in', 'id', []], []],
81
            [['not in', 'id', []], []],
82
83
            /* simple conditions */
84
            [['=', 'a', ''], []],
85
            [['>', 'a', ''], []],
86
            [['>=', 'a', ''], []],
87
            [['<', 'a', ''], []],
88
            [['<=', 'a', ''], []],
89
            [['<>', 'a', ''], []],
90
            [['!=', 'a', ''], []],
91
        ];
92
    }
93
94
    /**
95
     * @dataProvider filterConditionDataProvider
96
     */
97
    public function testFilterCondition(array|string $condition, array|string $expected): void
98
    {
99
        $this->assertEquals($expected, $this->createQueryHelper()->filterCondition($condition));
100
    }
101
102
    public function normalizeOrderByProvider(): array
103
    {
104
        return [
105
            ['id', ['id' => 4]],
106
            [['id'], ['id']],
107
            ['name ASC, date DESC', ['name' => 4, 'date' => 3]],
108
            [new Expression('SUBSTR(name, 3, 4) DESC, x ASC'), [new Expression('SUBSTR(name, 3, 4) DESC, x ASC')]],
109
        ];
110
    }
111
112
    /**
113
     * @dataProvider normalizeOrderByProvider
114
     */
115
    public function testNormalizeOrderBy(array|string|Expression $columns, array|string $expected): void
116
    {
117
        $this->assertEquals($expected, $this->createQueryHelper()->normalizeOrderBy($columns));
118
    }
119
120
    public function normalizeSelectProvider(): array
121
    {
122
        return [
123
            ['exists', ['exists' => 'exists']],
124
            ['count(*) > 1', ['count(*) > 1']],
125
            ['name, name, name as X, name as X', ['name' => 'name', 'X' => 'name']],
126
            [
127
                ['email', 'address', 'status' => new Expression('1')],
128
                ['email' => 'email', 'address' => 'address', 'status' => new Expression('1')],
129
            ],
130
            [new Expression('1 as Ab'), [new Expression('1 as Ab')]],
131
        ];
132
    }
133
134
    /**
135
     * @dataProvider normalizeSelectProvider
136
     */
137
    public function testNormalizeSelect(array|string|Expression $columns, array|string $expected): void
138
    {
139
        $this->assertEquals($expected, $this->createQueryHelper()->normalizeSelect($columns));
140
    }
141
142
    private function createQueryHelper(): QueryHelper
143
    {
144
        return new QueryHelper();
145
    }
146
}
147