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