Passed
Pull Request — master (#397)
by Wilmer
02:53
created

AbstractCommandTest::testCreateView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 21
rs 9.7666
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Db\Command\CommandInterface;
9
use Yiisoft\Db\Command\Param;
10
use Yiisoft\Db\Command\ParamInterface;
11
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
12
use Yiisoft\Db\Exception\Exception;
13
use Yiisoft\Db\Exception\InvalidConfigException;
14
use Yiisoft\Db\Exception\NotSupportedException;
15
use Yiisoft\Db\Schema\SchemaBuilderTrait;
16
use Yiisoft\Db\Tests\Support\Assert;
17
use Yiisoft\Db\Tests\Support\DbHelper;
18
use Yiisoft\Db\Tests\Support\TestTrait;
19
20
abstract class AbstractCommandTest extends TestCase
21
{
22
    use SchemaBuilderTrait;
23
    use TestTrait;
24
25
    protected ConnectionPDOInterface $db;
26
    protected string $upsertTestCharCast = '';
27
28
    public function testAutoQuoting(): void
29
    {
30
        $db = $this->getConnection();
31
32
        $sql = <<<SQL
33
        SELECT [[id]], [[t.name]] FROM {{customer}} t
34
        SQL;
35
        $command = $db->createCommand($sql);
36
37
        $this->assertSame(
38
            DbHelper::replaceQuotes(
39
                <<<SQL
40
                SELECT [[id]], [[t]].[[name]] FROM [[customer]] t
41
                SQL,
42
                $db->getName(),
43
            ),
44
            $command->getSql(),
45
        );
46
    }
47
48
    public function testConstruct(): void
49
    {
50
        $db = $this->getConnection();
51
52
        $command = $db->createCommand();
53
54
        $this->assertEmpty($command->getSql());
55
56
        $sql = <<<SQL
57
        SELECT * FROM customer WHERE name=:name
58
        SQL;
59
        $command = $db->createCommand($sql, [':name' => 'John']);
60
61
        $this->assertSame($sql, $command->getSql());
62
        $this->assertSame([':name' => 'John'], $command->getParams());
63
    }
64
65
    public function testGetParams(): void
66
    {
67
        $db = $this->getConnection();
68
69
        $command = $db->createCommand();
70
        $values = ['int' => 1, 'string' => 'str'];
71
        $command->bindValues($values);
72
        $bindedValues = $command->getParams(false);
73
74
        $this->assertIsArray($bindedValues);
75
        $this->assertContainsOnlyInstancesOf(ParamInterface::class, $bindedValues);
76
        $this->assertCount(2, $bindedValues);
77
78
        $param = new Param('str', 99);
79
        $command->bindValues(['param' => $param]);
80
        $bindedValues = $command->getParams(false);
81
82
        $this->assertIsArray($bindedValues);
83
        $this->assertContainsOnlyInstancesOf(ParamInterface::class, $bindedValues);
84
        $this->assertCount(3, $bindedValues);
85
        $this->assertEquals($param, $bindedValues['param']);
86
        $this->assertNotEquals($param, $bindedValues['int']);
87
88
        /* Replace test */
89
        $command->bindValues(['int' => $param]);
90
        $bindedValues = $command->getParams(false);
91
92
        $this->assertIsArray($bindedValues);
93
        $this->assertContainsOnlyInstancesOf(ParamInterface::class, $bindedValues);
94
        $this->assertCount(3, $bindedValues);
95
        $this->assertEquals($param, $bindedValues['int']);
96
    }
97
98
    /**
99
     * Test command getRawSql.
100
     *
101
     * @dataProvider \Yiisoft\Db\Tests\Provider\CommandProvider::rawSql()
102
     *
103
     * @throws Exception
104
     * @throws InvalidConfigException
105
     * @throws NotSupportedException
106
     *
107
     * {@see https://github.com/yiisoft/yii2/issues/8592}
108
     */
109
    public function testGetRawSql(string $sql, array $params, string $expectedRawSql): void
110
    {
111
        $db = $this->getConnection();
112
113
        $command = $db->createCommand($sql, $params);
114
115
        $this->assertSame($expectedRawSql, $command->getRawSql());
116
    }
117
118
    public function testGetSetSql(): void
119
    {
120
        $db = $this->getConnection();
121
122
        $sql = <<<SQL
123
        SELECT * FROM customer
124
        SQL;
125
        $command = $db->createCommand($sql);
126
        $this->assertSame($sql, $command->getSql());
127
128
        $sql2 = <<<SQL
129
        SELECT * FROM order
130
        SQL;
131
        $command->setSql($sql2);
132
        $this->assertSame($sql2, $command->getSql());
133
    }
134
135
    public function testNoCache(): void
136
    {
137
        $db = $this->getConnection();
138
139
        $command = $db->createCommand()->noCache();
140
141
        $this->assertSame(-1, Assert::getInaccessibleProperty($command, 'queryCacheDuration'));
142
        $this->assertInstanceOf(CommandInterface::class, $command);
143
    }
144
145
    public function testPrepareCancel(): void
146
    {
147
        $db = $this->getConnection(true);
148
149
        $command = $db->createCommand();
150
        $command->setSql(
151
            <<<SQL
152
            SELECT * FROM {{customer}}
153
            SQL
154
        );
155
156
        $this->assertNull($command->getPdoStatement());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $command->getPdoStatement() targeting Yiisoft\Db\Driver\PDO\Co...dPDO::getPdoStatement() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
157
158
        $command->prepare();
159
160
        $this->assertNotNull($command->getPdoStatement());
161
162
        $command->cancel();
163
164
        $this->assertNull($command->getPdoStatement());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $command->getPdoStatement() targeting Yiisoft\Db\Driver\PDO\Co...dPDO::getPdoStatement() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
165
    }
166
167
    public function testSetRawSql(): void
168
    {
169
        $db = $this->getConnection();
170
171
        $command = $db->createCommand();
172
        $command->setRawSql(
173
            <<<SQL
174
            SELECT 123
175
            SQL
176
        );
177
178
        $this->assertSame('SELECT 123', $command->getRawSql());
179
    }
180
181
    public function testSetSql(): void
182
    {
183
        $db = $this->getConnection();
184
185
        $command = $db->createCommand();
186
        $command->setSql(
187
            <<<SQL
188
            SELECT 123
189
            SQL
190
        );
191
192
        $this->assertSame('SELECT 123', $command->getSql());
193
    }
194
}
195