Passed
Push — master ( b0ca68...10680f )
by Wilmer
27:02 queued 23:37
created

AbstractQueryGetTableAliasTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 69
c 1
b 0
f 0
dl 0
loc 161
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testNamesIsFromAliasedArrayWithExpression() 0 10 1
A testNamesIsFromPrefixedTableName() 0 9 1
A testAliasesFromString() 0 18 1
A testNamesIsFromAliasedExpression() 0 14 1
A testNamesIsFromTableNameWithDatabase() 0 9 1
A testNamesIsFromArrayWithAlias() 0 12 1
A testNamesIsFromArrayWithoutAlias() 0 9 1
A testNamesIsFromString() 0 17 1
A testGetTableNamesIsFromAliasedSubquery() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Db\Exception\InvalidArgumentException;
9
use Yiisoft\Db\Expression\Expression;
10
use Yiisoft\Db\Query\Query;
11
use Yiisoft\Db\Tests\Support\TestTrait;
12
13
abstract class AbstractQueryGetTableAliasTest extends TestCase
14
{
15
    use TestTrait;
16
17
    /**
18
     * @throws InvalidArgumentException
19
     */
20
    public function testAliasesFromString(): void
21
    {
22
        $db = $this->getConnection();
23
24
        $query = new Query($db);
25
        $query->from('profile AS \'prf\', user "usr", service srv, order, [a b] [c d], {{something}} AS myalias');
26
        $tables = $query->getTablesUsedInFrom();
27
28
        $this->assertSame(
29
            [
30
                '{{prf}}' => '{{profile}}',
31
                '{{usr}}' => '{{user}}',
32
                '{{srv}}' => '{{service}}',
33
                '{{order}}' => '{{order}}',
34
                '{{c d}}' => '{{a b}}',
35
                '{{myalias}}' => '{{something}}',
36
            ],
37
            $tables,
38
        );
39
    }
40
41
    /**
42
     * @throws InvalidArgumentException
43
     */
44
    public function testGetTableNamesIsFromAliasedSubquery(): void
45
    {
46
        $db = $this->getConnection();
47
48
        $query = new Query($db);
49
        $subQuery = new Query($db);
50
        $subQuery->from('user');
51
        $query->from(['x' => $subQuery]);
52
        $expected = ['{{x}}' => $subQuery];
53
        $tables = $query->getTablesUsedInFrom();
54
55
        $this->assertSame($expected, $tables);
56
    }
57
58
    /**
59
     * @throws InvalidArgumentException
60
     */
61
    public function testNamesIsFromAliasedArrayWithExpression(): void
62
    {
63
        $db = $this->getConnection();
64
65
        $query = new Query($db);
66
        $expression = new Expression('(SELECT id FROM user)');
67
        $query->from(['x' => $expression]);
68
        $tables = $query->getTablesUsedInFrom();
69
70
        $this->assertSame(['{{x}}' => $expression], $tables);
71
    }
72
73
    public function testNamesIsFromAliasedExpression(): void
74
    {
75
        $db = $this->getConnection();
76
77
        $query = new Query($db);
78
        $expression = new Expression('(SELECT id FROM user)');
79
        $query->from($expression);
80
81
        $this->expectException(InvalidArgumentException::class);
82
        $this->expectExceptionMessage('To use Expression in from() method, pass it in array format with alias.');
83
84
        $tables = $query->getTablesUsedInFrom();
85
86
        $this->assertSame(['{{x}}' => $expression], $tables);
87
    }
88
89
    /**
90
     * @throws InvalidArgumentException
91
     */
92
    public function testNamesIsFromArrayWithAlias(): void
93
    {
94
        $db = $this->getConnection();
95
96
        $query = new Query($db);
97
        $query->from(['prf' => 'profile', '{{usr}}' => '{{user}}', '{{a b}}' => '{{c d}}', 'post AS p']);
98
99
        $tables = $query->getTablesUsedInFrom();
100
101
        $this->assertSame(
102
            ['{{prf}}' => '{{profile}}', '{{usr}}' => '{{user}}', '{{a b}}' => '{{c d}}', '{{p}}' => '{{post}}'],
103
            $tables,
104
        );
105
    }
106
107
    /**
108
     * @throws InvalidArgumentException
109
     */
110
    public function testNamesIsFromArrayWithoutAlias(): void
111
    {
112
        $db = $this->getConnection();
113
114
        $query = new Query($db);
115
        $query->from(['{{profile}}', 'user']);
116
        $tables = $query->getTablesUsedInFrom();
117
118
        $this->assertSame(['{{profile}}' => '{{profile}}', '{{user}}' => '{{user}}'], $tables);
119
    }
120
121
    /**
122
     * {@link https://github.com/yiisoft/yii2/issues/14150}
123
     *
124
     * @throws InvalidArgumentException
125
     */
126
    public function testNamesIsFromPrefixedTableName(): void
127
    {
128
        $db = $this->getConnection();
129
130
        $query = new Query($db);
131
        $query->from('{{%order_item}}');
132
        $tables = $query->getTablesUsedInFrom();
133
134
        $this->assertSame(['{{%order_item}}' => '{{%order_item}}'], $tables);
135
    }
136
137
    /**
138
     * @throws InvalidArgumentException
139
     */
140
    public function testNamesIsFromString(): void
141
    {
142
        $db = $this->getConnection();
143
144
        $query = new Query($db);
145
        $query->from('profile AS \'prf\', user "usr", `order`, "customer", "a b" as "c d"');
146
        $tables = $query->getTablesUsedInFrom();
147
148
        $this->assertSame(
149
            [
150
                '{{prf}}' => '{{profile}}',
151
                '{{usr}}' => '{{user}}',
152
                '{{order}}' => '{{order}}',
153
                '{{customer}}' => '{{customer}}',
154
                '{{c d}}' => '{{a b}}',
155
            ],
156
            $tables,
157
        );
158
    }
159
160
    /**
161
     * {@link https://github.com/yiisoft/yii2/issues/14211}}
162
     *
163
     * @throws InvalidArgumentException
164
     */
165
    public function testNamesIsFromTableNameWithDatabase(): void
166
    {
167
        $db = $this->getConnection();
168
169
        $query = new Query($db);
170
        $query->from('tickets.workflows');
171
        $tables = $query->getTablesUsedInFrom();
172
173
        $this->assertSame(['{{tickets.workflows}}' => '{{tickets.workflows}}'], $tables);
174
    }
175
}
176