|
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
|
|
|
public function testAliasesFromString(): void |
|
18
|
|
|
{ |
|
19
|
|
|
$db = $this->getConnection(); |
|
20
|
|
|
|
|
21
|
|
|
$query = new Query($db); |
|
22
|
|
|
$query->from('profile AS \'prf\', user "usr", service srv, order, [a b] [c d], {{something}} AS myalias'); |
|
23
|
|
|
$tables = $query->getTablesUsedInFrom(); |
|
24
|
|
|
|
|
25
|
|
|
$this->assertSame( |
|
26
|
|
|
[ |
|
27
|
|
|
'{{prf}}' => '{{profile}}', |
|
28
|
|
|
'{{usr}}' => '{{user}}', |
|
29
|
|
|
'{{srv}}' => '{{service}}', |
|
30
|
|
|
'{{order}}' => '{{order}}', |
|
31
|
|
|
'{{c d}}' => '{{a b}}', |
|
32
|
|
|
'{{myalias}}' => '{{something}}', |
|
33
|
|
|
], |
|
34
|
|
|
$tables, |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testGetTableNamesIsFromAliasedSubquery(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$db = $this->getConnection(); |
|
41
|
|
|
|
|
42
|
|
|
$query = new Query($db); |
|
43
|
|
|
$subQuery = new Query($db); |
|
44
|
|
|
$subQuery->from('user'); |
|
45
|
|
|
$query->from(['x' => $subQuery]); |
|
46
|
|
|
$expected = ['{{x}}' => $subQuery]; |
|
47
|
|
|
$tables = $query->getTablesUsedInFrom(); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertSame($expected, $tables); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testNamesIsFromAliasedArrayWithExpression(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$db = $this->getConnection(); |
|
55
|
|
|
|
|
56
|
|
|
$query = new Query($db); |
|
57
|
|
|
$expression = new Expression('(SELECT id FROM user)'); |
|
58
|
|
|
$query->from(['x' => $expression]); |
|
59
|
|
|
$tables = $query->getTablesUsedInFrom(); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertSame(['{{x}}' => $expression], $tables); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testNamesIsFromAliasedExpression(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$db = $this->getConnection(); |
|
67
|
|
|
|
|
68
|
|
|
$query = new Query($db); |
|
69
|
|
|
$expression = new Expression('(SELECT id FROM user)'); |
|
70
|
|
|
$query->from($expression); |
|
71
|
|
|
|
|
72
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
73
|
|
|
$this->expectExceptionMessage('To use Expression in from() method, pass it in array format with alias.'); |
|
74
|
|
|
|
|
75
|
|
|
$tables = $query->getTablesUsedInFrom(); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertSame(['{{x}}' => $expression], $tables); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testNamesIsFromArrayWithAlias(): void |
|
81
|
|
|
{ |
|
82
|
|
|
$db = $this->getConnection(); |
|
83
|
|
|
|
|
84
|
|
|
$query = new Query($db); |
|
85
|
|
|
$query->from(['prf' => 'profile', '{{usr}}' => '{{user}}', '{{a b}}' => '{{c d}}', 'post AS p']); |
|
86
|
|
|
|
|
87
|
|
|
$tables = $query->getTablesUsedInFrom(); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertSame( |
|
90
|
|
|
['{{prf}}' => '{{profile}}', '{{usr}}' => '{{user}}', '{{a b}}' => '{{c d}}', '{{p}}' => '{{post}}'], |
|
91
|
|
|
$tables, |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testNamesIsFromArrayWithoutAlias(): void |
|
96
|
|
|
{ |
|
97
|
|
|
$db = $this->getConnection(); |
|
98
|
|
|
|
|
99
|
|
|
$query = new Query($db); |
|
100
|
|
|
$query->from(['{{profile}}', 'user']); |
|
101
|
|
|
$tables = $query->getTablesUsedInFrom(); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertSame(['{{profile}}' => '{{profile}}', '{{user}}' => '{{user}}'], $tables); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/14150} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function testNamesIsFromPrefixedTableName(): void |
|
110
|
|
|
{ |
|
111
|
|
|
$db = $this->getConnection(); |
|
112
|
|
|
|
|
113
|
|
|
$query = new Query($db); |
|
114
|
|
|
$query->from('{{%order_item}}'); |
|
115
|
|
|
$tables = $query->getTablesUsedInFrom(); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertSame(['{{%order_item}}' => '{{%order_item}}'], $tables); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testNamesIsFromString(): void |
|
121
|
|
|
{ |
|
122
|
|
|
$db = $this->getConnection(); |
|
123
|
|
|
|
|
124
|
|
|
$query = new Query($db); |
|
125
|
|
|
$query->from('profile AS \'prf\', user "usr", `order`, "customer", "a b" as "c d"'); |
|
126
|
|
|
$tables = $query->getTablesUsedInFrom(); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertSame( |
|
129
|
|
|
[ |
|
130
|
|
|
'{{prf}}' => '{{profile}}', |
|
131
|
|
|
'{{usr}}' => '{{user}}', |
|
132
|
|
|
'{{order}}' => '{{order}}', |
|
133
|
|
|
'{{customer}}' => '{{customer}}', |
|
134
|
|
|
'{{c d}}' => '{{a b}}', |
|
135
|
|
|
], |
|
136
|
|
|
$tables, |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@see https://github.com/yiisoft/yii2/issues/14211}} |
|
142
|
|
|
*/ |
|
143
|
|
|
public function testNamesIsFromTableNameWithDatabase(): void |
|
144
|
|
|
{ |
|
145
|
|
|
$db = $this->getConnection(); |
|
146
|
|
|
|
|
147
|
|
|
$query = new Query($db); |
|
148
|
|
|
$query->from('tickets.workflows'); |
|
149
|
|
|
$tables = $query->getTablesUsedInFrom(); |
|
150
|
|
|
|
|
151
|
|
|
$this->assertSame(['{{tickets.workflows}}' => '{{tickets.workflows}}'], $tables); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|