Passed
Branch psalm-3 (1add06)
by Wilmer
13:44 queued 10:02
created

testGetTablesAliasesFromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 18
rs 9.9
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\TestSupport;
6
7
use Yiisoft\Db\Exception\InvalidArgumentException;
8
use Yiisoft\Db\Expression\Expression;
9
use Yiisoft\Db\Query\Query;
10
11
trait GetTablesAliasTestTrait
12
{
13
    abstract protected function createQuery(): Query;
14
15
    public function testGetTableNamesIsFromArrayWithAlias(): void
16
    {
17
        $query = $this->createQuery();
18
19
        $query->from([
20
            'prf' => 'profile',
21
            '{{usr}}' => '{{user}}',
22
            '{{a b}}' => '{{c d}}',
23
            'post AS p',
24
        ]);
25
26
        $tables = $query->getTablesUsedInFrom();
27
28
        $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

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

130
        $this->/** @scrutinizer ignore-call */ 
131
               expectException(InvalidArgumentException::class);
Loading history...
131
        $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

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