Passed
Pull Request — master (#396)
by Wilmer
02:32
created

QueryHelperTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
c 0
b 0
f 0
dl 0
loc 138
rs 10
wmc 11

11 Methods

Rating   Name   Duplication   Size   Complexity  
A createQueryHelper() 0 3 1
A testFilterCondition() 0 3 1
A testCleanUpTableNamesException() 0 9 1
A filterConditionDataProvider() 0 38 1
A testNormalizeOrderBy() 0 3 1
A createConnectionMock() 0 3 1
A testCleanUpTableNames() 0 7 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\Connection\ConnectionInterface;
9
use Yiisoft\Db\Exception\InvalidArgumentException;
10
use Yiisoft\Db\Expression\Expression;
11
use Yiisoft\Db\Query\Helper\QueryHelper;
12
use Yiisoft\Db\Schema\Quoter;
13
14
final class QueryHelperTest extends TestCase
15
{
16
    public function tablesNameDataProvider(): array
17
    {
18
        return [
19
            [['customer'], '', ['{{customer}}' => '{{customer}}']],
20
            [['profile AS "prf"'], '', ['{{prf}}' => '{{profile}}']],
21
            [['mainframe as400'], '', ['{{as400}}' => '{{mainframe}}']],
22
            [
23
                ['x' => new Expression('(SELECT id FROM user)')],
24
                '',
25
                ['{{x}}' => new Expression('(SELECT id FROM user)')],
26
            ],
27
        ];
28
    }
29
30
    /**
31
     * @dataProvider tablesNameDataProvider
32
     */
33
    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

33
    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...
34
    {
35
        $connection = $this->createConnectionMock();
0 ignored issues
show
Unused Code introduced by
The assignment to $connection is dead and can be removed.
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
        $connection = $this->createConnectionMock();
0 ignored issues
show
Unused Code introduced by
The assignment to $connection is dead and can be removed.
Loading history...
46
47
        $this->expectException(InvalidArgumentException::class);
48
        $this->expectExceptionMessage('To use Expression in from() method, pass it in array format with alias.');
49
        $this->createQueryHelper()->cleanUpTableNames(
50
            [new Expression('(SELECT id FROM user)')],
51
            new Quoter('"', '"')
52
        );
53
    }
54
55
    public function filterConditionDataProvider(): array
56
    {
57
        return [
58
            /* like */
59
            [['like', 'name', []], []],
60
            [['not like', 'name', []], []],
61
            [['or like', 'name', []],  []],
62
            [['or not like', 'name', []], []],
63
64
            /* not */
65
            [['not', ''], []],
66
67
            /* and */
68
            [['and', '', ''], []],
69
            [['and', '', 'id=2'], ['and', 'id=2']],
70
            [['and', 'id=1', ''], ['and', 'id=1']],
71
            [['and', 'type=1', ['or', '', 'id=2']], ['and', 'type=1', ['or', 'id=2']]],
72
73
            /* or */
74
            [['or', 'id=1', ''], ['or', 'id=1']],
75
            [['or', 'type=1', ['or', '', 'id=2']], ['or', 'type=1', ['or', 'id=2']]],
76
77
            /* between */
78
            [['between', 'id', 1, null], []],
79
            [['not between', 'id', null, 10], []],
80
81
            /* in */
82
            [['in', 'id', []], []],
83
            [['not in', 'id', []], []],
84
85
            /* simple conditions */
86
            [['=', 'a', ''], []],
87
            [['>', 'a', ''], []],
88
            [['>=', 'a', ''], []],
89
            [['<', 'a', ''], []],
90
            [['<=', 'a', ''], []],
91
            [['<>', 'a', ''], []],
92
            [['!=', 'a', ''], []],
93
        ];
94
    }
95
96
    /**
97
     * @dataProvider filterConditionDataProvider
98
     */
99
    public function testFilterCondition(array|string $condition, array|string $expected): void
100
    {
101
        $this->assertEquals($expected, $this->createQueryHelper()->filterCondition($condition));
102
    }
103
104
    public function normalizeOrderByProvider(): array
105
    {
106
        return [
107
            ['id', ['id' => 4]],
108
            [['id'], ['id']],
109
            ['name ASC, date DESC', ['name' => 4, 'date' => 3]],
110
            [new Expression('SUBSTR(name, 3, 4) DESC, x ASC'), [new Expression('SUBSTR(name, 3, 4) DESC, x ASC')]],
111
        ];
112
    }
113
114
    /**
115
     * @dataProvider normalizeOrderByProvider
116
     */
117
    public function testNormalizeOrderBy(array|string|Expression $columns, array|string $expected): void
118
    {
119
        $this->assertEquals($expected, $this->createQueryHelper()->normalizeOrderBy($columns));
120
    }
121
122
    public function normalizeSelectProvider(): array
123
    {
124
        return [
125
            ['exists', ['exists' => 'exists']],
126
            ['count(*) > 1', ['count(*) > 1']],
127
            ['name, name, name as X, name as X', ['name' => 'name', 'X' => 'name']],
128
            [
129
                ['email', 'address', 'status' => new Expression('1')],
130
                ['email' => 'email', 'address' => 'address', 'status' => new Expression('1')],
131
            ],
132
            [new Expression('1 as Ab'), [new Expression('1 as Ab')]],
133
        ];
134
    }
135
136
    /**
137
     * @dataProvider normalizeSelectProvider
138
     */
139
    public function testNormalizeSelect(array|string|Expression $columns, array|string $expected): void
140
    {
141
        $this->assertEquals($expected, $this->createQueryHelper()->normalizeSelect($columns));
142
    }
143
144
    private function createQueryHelper(): QueryHelper
145
    {
146
        return new QueryHelper();
147
    }
148
149
    private function createConnectionMock(): ConnectionInterface
150
    {
151
        return $this->createMock(ConnectionInterface::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createMock...ectionInterface::class) returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Yiisoft\Db\Connection\ConnectionInterface.
Loading history...
152
    }
153
}
154