Passed
Push — master ( f4eeb8...0d22ee )
by Sergei
11:47 queued 01:39
created

testGetTableNamesIsFromObjectgenerateException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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

30
        $this->/** @scrutinizer ignore-call */ 
31
               assertEquals(
Loading history...
31
            [
32 5
                '{{prf}}' => '{{profile}}',
33
                '{{usr}}' => '{{user}}',
34
                '{{a b}}' => '{{c d}}',
35
                '{{p}}' => '{{post}}',
36
            ],
37
            $tables
38
        );
39
    }
40
41 5
    public function testGetTableNamesIsFromArrayWithoutAlias(): void
42
    {
43 5
        $query = $this->createQuery();
44
45 5
        $query->from([
46
            '{{profile}}',
47
            'user',
48
        ]);
49
50 5
        $tables = $query->getTablesUsedInFrom();
51
52 5
        $this->assertEquals(['{{profile}}' => '{{profile}}', '{{user}}' => '{{user}}'], $tables);
53
    }
54
55 5
    public function testGetTableNamesIsFromString(): void
56
    {
57 5
        $query = $this->createQuery();
58
59 5
        $query->from('profile AS \'prf\', user "usr", `order`, "customer", "a b" as "c d"');
60
61 5
        $tables = $query->getTablesUsedInFrom();
62
63 5
        $this->assertEquals(
64
            [
65 5
                '{{prf}}' => '{{profile}}',
66
                '{{usr}}' => '{{user}}',
67
                '{{order}}' => '{{order}}',
68
                '{{customer}}' => '{{customer}}',
69
                '{{c d}}' => '{{a b}}',
70
            ],
71
            $tables
72
        );
73
    }
74
75 5
    public function testGetTableNamesIsFromObjectgenerateException(): void
76
    {
77 5
        $query = $this->createQuery();
78
79 5
        $query->from(new stdClass());
0 ignored issues
show
Bug introduced by
new stdClass() of type stdClass is incompatible with the type Yiisoft\Db\Expression\Ex...nInterface|array|string expected by parameter $tables of Yiisoft\Db\Query\Query::from(). ( Ignorable by Annotation )

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

79
        $query->from(/** @scrutinizer ignore-type */ new stdClass());
Loading history...
80
81 5
        $this->expectException(InvalidConfigException::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

81
        $this->/** @scrutinizer ignore-call */ 
82
               expectException(InvalidConfigException::class);
Loading history...
82
83 5
        $query->getTablesUsedInFrom();
84
    }
85
86 5
    public function testGetTablesAliasesFromString(): void
87
    {
88 5
        $query = $this->createQuery();
89
90 5
        $query->from('profile AS \'prf\', user "usr", service srv, order, [a b] [c d], {{something}} AS myalias');
91
92 5
        $tables = $query->getTablesUsedInFrom();
93
94 5
        $this->assertEquals(
95
            [
96 5
                '{{prf}}' => '{{profile}}',
97
                '{{usr}}' => '{{user}}',
98
                '{{srv}}' => '{{service}}',
99
                '{{order}}' => '{{order}}',
100
                '{{c d}}' => '{{a b}}',
101
                '{{myalias}}' => '{{something}}',
102
            ],
103
            $tables
104
        );
105
    }
106
107
    /**
108
     * {@see https://github.com/yiisoft/yii2/issues/14150}
109
     */
110 5
    public function testGetTableNamesIsFromPrefixedTableName(): void
111
    {
112 5
        $query = $this->createQuery();
113
114 5
        $query->from('{{%order_item}}');
115
116 5
        $tables = $query->getTablesUsedInFrom();
117
118 5
        $this->assertEquals(['{{%order_item}}' => '{{%order_item}}'], $tables);
119
    }
120
121
    /**
122
     * {@see https://github.com/yiisoft/yii2/issues/14211}}
123
     */
124 5
    public function testGetTableNamesIsFromTableNameWithDatabase(): void
125
    {
126 5
        $query = $this->createQuery();
127
128 5
        $query->from('tickets.workflows');
129
130 5
        $tables = $query->getTablesUsedInFrom();
131
132 5
        $this->assertEquals(['{{tickets.workflows}}' => '{{tickets.workflows}}'], $tables);
133
    }
134
135 5
    public function testGetTableNamesIsFromAliasedExpression(): void
136
    {
137 5
        $query = $this->createQuery();
138
139 5
        $expression = new Expression('(SELECT id FROM user)');
140
141 5
        $query->from($expression);
142
143 5
        $this->expectException(InvalidArgumentException::class);
144 5
        $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

144
        $this->/** @scrutinizer ignore-call */ 
145
               expectExceptionMessage('To use Expression in from() method, pass it in array format with alias.');
Loading history...
145
146 5
        $tables = $query->getTablesUsedInFrom();
147
148
        $this->assertEquals(['{{x}}' => $expression], $tables);
149
    }
150
151 5
    public function testGetTableNamesIsFromAliasedArrayWithExpression(): void
152
    {
153 5
        $query = $this->createQuery();
154
155 5
        $query->from(['x' => new Expression('(SELECT id FROM user)')]);
156
157 5
        $tables = $query->getTablesUsedInFrom();
158
159 5
        $this->assertEquals(['{{x}}' => '(SELECT id FROM user)'], $tables);
160
    }
161
162 5
    public function testGetTableNamesIsFromAliasedSubquery(): void
163
    {
164 5
        $query = $this->createQuery();
165
166 5
        $subQuery = $this->createQuery();
167
168 5
        $subQuery->from('user');
169 5
        $query->from(['x' => $subQuery]);
170
171 5
        $expected = ['{{x}}' => $subQuery];
172
173 5
        $tables = $query->getTablesUsedInFrom();
174
175 5
        $this->assertEquals($expected, $tables);
176
    }
177
}
178