Passed
Pull Request — master (#372)
by Wilmer
05:11 queued 02:34
created

GetTablesAliasTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 67
c 0
b 0
f 0
dl 0
loc 150
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetTableNamesIsFromPrefixedTableName() 0 9 1
A testGetTableNamesIsFromString() 0 17 1
A testGetTableNamesIsFromAliasedSubquery() 0 14 1
A testGetTableNamesIsFromAliasedArrayWithExpression() 0 9 1
A testGetTableNamesIsFromAliasedExpression() 0 14 1
A testGetTableNamesIsFromArrayWithAlias() 0 21 1
A testGetTablesAliasesFromString() 0 18 1
A testGetTableNamesIsFromArrayWithoutAlias() 0 12 1
A testGetTableNamesIsFromTableNameWithDatabase() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests;
6
7
use Yiisoft\Db\Exception\InvalidArgumentException;
8
use Yiisoft\Db\Expression\Expression;
9
use Yiisoft\Db\Query\Query;
10
11
trait GetTablesAliasTrait
12
{
13
    public function testGetTableNamesIsFromArrayWithAlias(): void
14
    {
15
        $query = new Query($this->getConnection());
0 ignored issues
show
Bug introduced by
It seems like getConnection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

15
        $query = new Query($this->/** @scrutinizer ignore-call */ getConnection());
Loading history...
16
17
        $query->from([
18
            'prf' => 'profile',
19
            '{{usr}}' => '{{user}}',
20
            '{{a b}}' => '{{c d}}',
21
            'post AS p',
22
        ]);
23
24
        $tables = $query->getTablesUsedInFrom();
25
26
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

26
        $this->/** @scrutinizer ignore-call */ 
27
               assertEquals(
Loading history...
27
            [
28
                '{{prf}}' => '{{profile}}',
29
                '{{usr}}' => '{{user}}',
30
                '{{a b}}' => '{{c d}}',
31
                '{{p}}' => '{{post}}',
32
            ],
33
            $tables
34
        );
35
    }
36
37
    public function testGetTableNamesIsFromArrayWithoutAlias(): void
38
    {
39
        $query = new Query($this->getConnection());
40
41
        $query->from([
42
            '{{profile}}',
43
            'user',
44
        ]);
45
46
        $tables = $query->getTablesUsedInFrom();
47
48
        $this->assertEquals(['{{profile}}' => '{{profile}}', '{{user}}' => '{{user}}'], $tables);
49
    }
50
51
    public function testGetTableNamesIsFromString(): void
52
    {
53
        $query = new Query($this->getConnection());
54
55
        $query->from('profile AS \'prf\', user "usr", `order`, "customer", "a b" as "c d"');
56
57
        $tables = $query->getTablesUsedInFrom();
58
59
        $this->assertEquals(
60
            [
61
                '{{prf}}' => '{{profile}}',
62
                '{{usr}}' => '{{user}}',
63
                '{{order}}' => '{{order}}',
64
                '{{customer}}' => '{{customer}}',
65
                '{{c d}}' => '{{a b}}',
66
            ],
67
            $tables
68
        );
69
    }
70
71
    public function testGetTablesAliasesFromString(): void
72
    {
73
        $query = new Query($this->getConnection());
74
75
        $query->from('profile AS \'prf\', user "usr", service srv, order, [a b] [c d], {{something}} AS myalias');
76
77
        $tables = $query->getTablesUsedInFrom();
78
79
        $this->assertEquals(
80
            [
81
                '{{prf}}' => '{{profile}}',
82
                '{{usr}}' => '{{user}}',
83
                '{{srv}}' => '{{service}}',
84
                '{{order}}' => '{{order}}',
85
                '{{c d}}' => '{{a b}}',
86
                '{{myalias}}' => '{{something}}',
87
            ],
88
            $tables
89
        );
90
    }
91
92
    /**
93
     * {@see https://github.com/yiisoft/yii2/issues/14150}
94
     */
95
    public function testGetTableNamesIsFromPrefixedTableName(): void
96
    {
97
        $query = new Query($this->getConnection());
98
99
        $query->from('{{%order_item}}');
100
101
        $tables = $query->getTablesUsedInFrom();
102
103
        $this->assertEquals(['{{%order_item}}' => '{{%order_item}}'], $tables);
104
    }
105
106
    /**
107
     * {@see https://github.com/yiisoft/yii2/issues/14211}}
108
     */
109
    public function testGetTableNamesIsFromTableNameWithDatabase(): void
110
    {
111
        $query = new Query($this->getConnection());
112
113
        $query->from('tickets.workflows');
114
115
        $tables = $query->getTablesUsedInFrom();
116
117
        $this->assertEquals(['{{tickets.workflows}}' => '{{tickets.workflows}}'], $tables);
118
    }
119
120
    public function testGetTableNamesIsFromAliasedExpression(): void
121
    {
122
        $query = new Query($this->getConnection());
123
124
        $expression = new Expression('(SELECT id FROM user)');
125
126
        $query->from($expression);
127
128
        $this->expectException(InvalidArgumentException::class);
0 ignored issues
show
Bug introduced by
It seems like expectException() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

128
        $this->/** @scrutinizer ignore-call */ 
129
               expectException(InvalidArgumentException::class);
Loading history...
129
        $this->expectExceptionMessage('To use Expression in from() method, pass it in array format with alias.');
0 ignored issues
show
Bug introduced by
It seems like expectExceptionMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

129
        $this->/** @scrutinizer ignore-call */ 
130
               expectExceptionMessage('To use Expression in from() method, pass it in array format with alias.');
Loading history...
130
131
        $tables = $query->getTablesUsedInFrom();
132
133
        $this->assertEquals(['{{x}}' => $expression], $tables);
134
    }
135
136
    public function testGetTableNamesIsFromAliasedArrayWithExpression(): void
137
    {
138
        $query = new Query($this->getConnection());
139
140
        $query->from(['x' => new Expression('(SELECT id FROM user)')]);
141
142
        $tables = $query->getTablesUsedInFrom();
143
144
        $this->assertEquals(['{{x}}' => '(SELECT id FROM user)'], $tables);
145
    }
146
147
    public function testGetTableNamesIsFromAliasedSubquery(): void
148
    {
149
        $query = new Query($this->getConnection());
150
151
        $subQuery = new Query($this->getConnection());
152
153
        $subQuery->from('user');
154
        $query->from(['x' => $subQuery]);
155
156
        $expected = ['{{x}}' => $subQuery];
157
158
        $tables = $query->getTablesUsedInFrom();
159
160
        $this->assertEquals($expected, $tables);
161
    }
162
}
163