Passed
Pull Request — master (#397)
by Wilmer
41:34 queued 21:59
created

CommonCommandPDOTest::testBindValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 0
dl 0
loc 32
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Common;
6
7
use PDO;
8
use PHPUnit\Framework\TestCase;
9
use Yiisoft\Db\Command\Param;
10
use Yiisoft\Db\Command\ParamInterface;
11
12
abstract class CommonCommandPDOTest extends TestCase
13
{
14
    /**
15
     * @dataProvider \Yiisoft\Db\Tests\Provider\CommandPDOProvider::bindParam()
16
     */
17
    public function testBindParam(
18
        string $field,
19
        string $name,
20
        mixed $value,
21
        int $dataType,
22
        int|null $length,
23
        mixed $driverOptions,
24
        array $expected,
25
    ): void {
26
        $db = $this->getConnection('customer');
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Yiisoft\Db\Tests\Common\CommonCommandPDOTest. ( Ignorable by Annotation )

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

26
        /** @scrutinizer ignore-call */ 
27
        $db = $this->getConnection('customer');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
27
28
        $sql = "SELECT * FROM customer WHERE $field = $name";
29
        $command = $db->createCommand();
30
        $command->setSql($sql);
31
        $command->bindParam($name, $value, $dataType, $length, $driverOptions);
32
33
        $this->assertSame($sql, $command->getSql());
34
        $this->assertSame($expected, $command->queryOne());
35
    }
36
37
    /**
38
     * Test whether param binding works in other places than WHERE.
39
     *
40
     * @dataProvider \Yiisoft\Db\Tests\Provider\CommandPDOProvider::bindParamsNonWhere()
41
     */
42
    public function testBindParamsNonWhere(string $sql): void
43
    {
44
        $db = $this->getConnection('customer');
45
46
        $db->createCommand()->insert(
47
            'customer',
48
            [
49
                'name' => 'testParams',
50
                'email' => '[email protected]',
51
                'address' => '1',
52
            ]
53
        )->execute();
54
        $params = [':email' => '[email protected]', ':len' => 5];
55
        $command = $db->createCommand($sql, $params);
56
57
        $this->assertSame('Params', $command->queryScalar());
58
    }
59
60
    public function testBindParamValue(): void
61
    {
62
        $db = $this->getConnection('customer');
63
64
        $command = $db->createCommand();
65
66
        // bindParam
67
        $command = $command->setSql(
68
            <<<SQL
69
            INSERT INTO customer(email, name, address) VALUES (:email, :name, :address)
70
            SQL
71
        );
72
        $email = '[email protected]';
73
        $name = 'user4';
74
        $address = 'address4';
75
        $command->bindParam(':email', $email);
76
        $command->bindParam(':name', $name);
77
        $command->bindParam(':address', $address);
78
        $command->execute();
79
        $command = $command->setSql(
80
            <<<SQL
81
            SELECT name FROM customer WHERE email=:email
82
            SQL,
83
        );
84
        $command->bindParam(':email', $email);
85
86
        $this->assertSame($name, $command->queryScalar());
87
88
        // bindValue
89
        $command = $command->setSql(
90
            <<<SQL
91
            INSERT INTO customer(email, name, address) VALUES (:email, 'user5', 'address5')
92
            SQL
93
        );
94
        $command->bindValue(':email', '[email protected]');
95
        $command->execute();
96
        $command = $command->setSql(
97
            <<<SQL
98
            SELECT email FROM customer WHERE name=:name
99
            SQL
100
        );
101
102
        $command->bindValue(':name', 'user5');
103
104
        $this->assertSame('[email protected]', $command->queryScalar());
105
    }
106
107
    public function testBindValues(): void
108
    {
109
        $db = $this->getConnection();
110
111
        $command = $db->createCommand();
112
113
        $values = ['int' => 1, 'string' => 'str'];
114
        $command->bindValues($values);
115
        $bindedValues = $command->getParams(false);
116
117
        $this->assertIsArray($bindedValues);
118
        $this->assertContainsOnlyInstancesOf(ParamInterface::class, $bindedValues);
119
        $this->assertCount(2, $bindedValues);
120
121
        $param = new Param('str', 99);
122
        $command->bindValues(['param' => $param]);
123
        $bindedValues = $command->getParams(false);
124
125
        $this->assertIsArray($bindedValues);
126
        $this->assertContainsOnlyInstancesOf(ParamInterface::class, $bindedValues);
127
        $this->assertCount(3, $bindedValues);
128
        $this->assertSame($param, $bindedValues['param']);
129
        $this->assertNotEquals($param, $bindedValues['int']);
130
131
        /* Replace test */
132
        $command->bindValues(['int' => $param]);
133
        $bindedValues = $command->getParams(false);
134
135
        $this->assertIsArray($bindedValues);
136
        $this->assertContainsOnlyInstancesOf(ParamInterface::class, $bindedValues);
137
        $this->assertCount(3, $bindedValues);
138
        $this->assertSame($param, $bindedValues['int']);
139
    }
140
141
    public function testColumnCase(): void
142
    {
143
        $db = $this->getConnection('order');
144
145
        $this->assertSame(PDO::CASE_NATURAL, $db->getActivePDO()->getAttribute(PDO::ATTR_CASE));
146
147
        $command = $db->createCommand();
148
        $sql = <<<SQL
149
        SELECT [[customer_id]], [[total]] FROM {{order}}
150
        SQL;
151
        $rows = $command->setSql($sql)->queryAll();
152
153
        $this->assertTrue(isset($rows[0]));
154
        $this->assertTrue(isset($rows[0]['customer_id']));
155
        $this->assertTrue(isset($rows[0]['total']));
156
157
        $db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
158
        $rows = $command->setSql($sql)->queryAll();
159
160
        $this->assertTrue(isset($rows[0]));
161
        $this->assertTrue(isset($rows[0]['customer_id']));
162
        $this->assertTrue(isset($rows[0]['total']));
163
164
        $db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);
165
        $rows = $command->setSql($sql)->queryAll();
166
167
        $this->assertTrue(isset($rows[0]));
168
        $this->assertTrue(isset($rows[0]['CUSTOMER_ID']));
169
        $this->assertTrue(isset($rows[0]['TOTAL']));
170
    }
171
}
172