1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Functional; |
4
|
|
|
use Doctrine\DBAL\Types\Type; |
5
|
|
|
use PDO; |
6
|
|
|
|
7
|
|
|
class WriteTest extends \Doctrine\Tests\DbalFunctionalTestCase |
8
|
|
|
{ |
9
|
|
|
protected function setUp() |
10
|
|
|
{ |
11
|
|
|
parent::setUp(); |
12
|
|
|
|
13
|
|
|
$this->createTable('write_table'); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
private function createTable($tableName) |
17
|
|
|
{ |
18
|
|
|
$table = new \Doctrine\DBAL\Schema\Table($tableName); |
19
|
|
|
$table->addColumn('id', 'integer', array('autoincrement' => true)); |
20
|
|
|
$table->addColumn('test_int', 'integer'); |
21
|
|
|
$table->addColumn('test_string', 'string', array('notnull' => false)); |
22
|
|
|
$table->setPrimaryKey(array('id')); |
23
|
|
|
|
24
|
|
|
$this->_conn->getSchemaManager()->createTable($table); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function tearDown() |
28
|
|
|
{ |
29
|
|
|
$this->_conn->getSchemaManager()->dropTable('write_table'); |
30
|
|
|
|
31
|
|
|
parent::tearDown(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @group DBAL-80 |
36
|
|
|
*/ |
37
|
|
|
public function testExecuteUpdateFirstTypeIsNull() |
38
|
|
|
{ |
39
|
|
|
$sql = "INSERT INTO write_table (test_string, test_int) VALUES (?, ?)"; |
40
|
|
|
$this->_conn->executeUpdate($sql, array("text", 1111), array(null, PDO::PARAM_INT)); |
41
|
|
|
|
42
|
|
|
$sql = "SELECT * FROM write_table WHERE test_string = ? AND test_int = ?"; |
43
|
|
|
$this->assertTrue((bool)$this->_conn->fetchColumn($sql, array("text", 1111))); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
public function testExecuteUpdate() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$sql = "INSERT INTO write_table (test_int) VALUES ( " . $this->_conn->quote(1) . ")"; |
49
|
|
|
$affected = $this->_conn->executeUpdate($sql); |
50
|
|
|
|
51
|
|
|
$this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testExecuteUpdateWithTypes() |
55
|
|
|
{ |
56
|
|
|
$sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; |
57
|
|
|
$affected = $this->_conn->executeUpdate($sql, array(1, 'foo'), array(\PDO::PARAM_INT, \PDO::PARAM_STR)); |
58
|
|
|
|
59
|
|
|
$this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testPrepareRowCountReturnsAffectedRows() |
63
|
|
|
{ |
64
|
|
|
$sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; |
65
|
|
|
$stmt = $this->_conn->prepare($sql); |
66
|
|
|
|
67
|
|
|
$stmt->bindValue(1, 1); |
68
|
|
|
$stmt->bindValue(2, "foo"); |
69
|
|
|
$stmt->execute(); |
70
|
|
|
|
71
|
|
|
$this->assertEquals(1, $stmt->rowCount()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testPrepareWithPdoTypes() |
75
|
|
|
{ |
76
|
|
|
$sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; |
77
|
|
|
$stmt = $this->_conn->prepare($sql); |
78
|
|
|
|
79
|
|
|
$stmt->bindValue(1, 1, \PDO::PARAM_INT); |
80
|
|
|
$stmt->bindValue(2, "foo", \PDO::PARAM_STR); |
81
|
|
|
$stmt->execute(); |
82
|
|
|
|
83
|
|
|
$this->assertEquals(1, $stmt->rowCount()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
public function testPrepareWithDbalTypes() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; |
89
|
|
|
$stmt = $this->_conn->prepare($sql); |
90
|
|
|
|
91
|
|
|
$stmt->bindValue(1, 1, Type::getType('integer')); |
92
|
|
|
$stmt->bindValue(2, "foo", Type::getType('string')); |
93
|
|
|
$stmt->execute(); |
94
|
|
|
|
95
|
|
|
$this->assertEquals(1, $stmt->rowCount()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
View Code Duplication |
public function testPrepareWithDbalTypeNames() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; |
101
|
|
|
$stmt = $this->_conn->prepare($sql); |
102
|
|
|
|
103
|
|
|
$stmt->bindValue(1, 1, 'integer'); |
104
|
|
|
$stmt->bindValue(2, "foo", 'string'); |
105
|
|
|
$stmt->execute(); |
106
|
|
|
|
107
|
|
|
$this->assertEquals(1, $stmt->rowCount()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function insertRows() |
111
|
|
|
{ |
112
|
|
|
$this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 1, 'test_string' => 'foo'))); |
113
|
|
|
$this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar'))); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testInsert() |
117
|
|
|
{ |
118
|
|
|
$this->insertRows(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testDelete() |
122
|
|
|
{ |
123
|
|
|
$this->insertRows(); |
124
|
|
|
|
125
|
|
|
$this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 2))); |
126
|
|
|
$this->assertEquals(1, count($this->_conn->fetchAll('SELECT * FROM write_table'))); |
127
|
|
|
|
128
|
|
|
$this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 1))); |
129
|
|
|
$this->assertEquals(0, count($this->_conn->fetchAll('SELECT * FROM write_table'))); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testUpdate() |
133
|
|
|
{ |
134
|
|
|
$this->insertRows(); |
135
|
|
|
|
136
|
|
|
$this->assertEquals(1, $this->_conn->update('write_table', array('test_string' => 'bar'), array('test_string' => 'foo'))); |
137
|
|
|
$this->assertEquals(2, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar'))); |
138
|
|
|
$this->assertEquals(0, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar'))); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @group DBAL-445 |
143
|
|
|
*/ |
144
|
|
|
public function testInsertWithKeyValueTypes() |
145
|
|
|
{ |
146
|
|
|
$testString = new \DateTime('2013-04-14 10:10:10'); |
147
|
|
|
|
148
|
|
|
$this->_conn->insert( |
149
|
|
|
'write_table', |
150
|
|
|
array('test_int' => '30', 'test_string' => $testString), |
151
|
|
|
array('test_string' => 'datetime', 'test_int' => 'integer') |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
$data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30'); |
155
|
|
|
|
156
|
|
|
$this->assertEquals($testString->format($this->_conn->getDatabasePlatform()->getDateTimeFormatString()), $data); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @group DBAL-445 |
161
|
|
|
*/ |
162
|
|
|
public function testUpdateWithKeyValueTypes() |
163
|
|
|
{ |
164
|
|
|
$testString = new \DateTime('2013-04-14 10:10:10'); |
165
|
|
|
|
166
|
|
|
$this->_conn->insert( |
167
|
|
|
'write_table', |
168
|
|
|
array('test_int' => '30', 'test_string' => $testString), |
169
|
|
|
array('test_string' => 'datetime', 'test_int' => 'integer') |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
$testString = new \DateTime('2013-04-15 10:10:10'); |
173
|
|
|
|
174
|
|
|
$this->_conn->update( |
175
|
|
|
'write_table', |
176
|
|
|
array('test_string' => $testString), |
177
|
|
|
array('test_int' => '30'), |
178
|
|
|
array('test_string' => 'datetime', 'test_int' => 'integer') |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
$data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30'); |
182
|
|
|
|
183
|
|
|
$this->assertEquals($testString->format($this->_conn->getDatabasePlatform()->getDateTimeFormatString()), $data); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @group DBAL-445 |
188
|
|
|
*/ |
189
|
|
|
public function testDeleteWithKeyValueTypes() |
190
|
|
|
{ |
191
|
|
|
$val = new \DateTime('2013-04-14 10:10:10'); |
192
|
|
|
$this->_conn->insert( |
193
|
|
|
'write_table', |
194
|
|
|
array('test_int' => '30', 'test_string' => $val), |
195
|
|
|
array('test_string' => 'datetime', 'test_int' => 'integer') |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
$this->_conn->delete('write_table', array('test_int' => 30, 'test_string' => $val), array('test_string' => 'datetime', 'test_int' => 'integer')); |
199
|
|
|
|
200
|
|
|
$data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30'); |
201
|
|
|
|
202
|
|
|
$this->assertFalse($data); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
View Code Duplication |
public function testEmptyIdentityInsert() |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
$table = new \Doctrine\DBAL\Schema\Table('test_empty_identity'); |
208
|
|
|
$table->addColumn('id', 'integer', array('autoincrement' => true)); |
209
|
|
|
$table->setPrimaryKey(array('id')); |
210
|
|
|
|
211
|
|
|
$this->_conn->getSchemaManager()->createTable($table); |
212
|
|
|
|
213
|
|
|
$sql = $this->_conn->getDatabasePlatform()->getEmptyIdentityInsertSQL('test_empty_identity', 'id'); |
214
|
|
|
|
215
|
|
|
$this->assertSame(1, $this->_conn->exec($sql)); |
216
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @group DBAL-2688 |
221
|
|
|
*/ |
222
|
|
|
public function testUpdateWhereIsNull() |
223
|
|
|
{ |
224
|
|
|
$this->_conn->insert( |
225
|
|
|
'write_table', |
226
|
|
|
['test_int' => '30', 'test_string' => null], |
227
|
|
|
['test_string' => 'string', 'test_int' => 'integer'] |
228
|
|
|
); |
229
|
|
|
|
230
|
|
|
$data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30'); |
231
|
|
|
|
232
|
|
|
$this->assertCount(1, $data); |
233
|
|
|
|
234
|
|
|
$this->_conn->update('write_table', ['test_int' => 10], ['test_string' => null], ['test_string' => 'string', 'test_int' => 'integer']); |
235
|
|
|
|
236
|
|
|
$data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30'); |
237
|
|
|
|
238
|
|
|
$this->assertCount(0, $data); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function testDeleteWhereIsNull() |
242
|
|
|
{ |
243
|
|
|
$this->_conn->insert( |
244
|
|
|
'write_table', |
245
|
|
|
['test_int' => '30', 'test_string' => null], |
246
|
|
|
['test_string' => 'string', 'test_int' => 'integer'] |
247
|
|
|
); |
248
|
|
|
|
249
|
|
|
$data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30'); |
250
|
|
|
|
251
|
|
|
$this->assertCount(1, $data); |
252
|
|
|
|
253
|
|
|
$this->_conn->delete('write_table', ['test_string' => null], ['test_string' => 'string']); |
254
|
|
|
|
255
|
|
|
$data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30'); |
256
|
|
|
|
257
|
|
|
$this->assertCount(0, $data); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.