Completed
Pull Request — master (#3304)
by Šimon
61:54
created

WriteTest::testEmptyInsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional;
4
5
use Doctrine\DBAL\ParameterType;
6
use Doctrine\DBAL\Types\Type;
7
use function array_filter;
8
use function strtolower;
9
10
class WriteTest extends \Doctrine\Tests\DbalFunctionalTestCase
11
{
12
    protected function setUp()
13
    {
14
        parent::setUp();
15
16
        try {
17
            /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
18
            $table = new \Doctrine\DBAL\Schema\Table("write_table");
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
        } catch(\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
26
27
        }
28
        $this->_conn->executeUpdate('DELETE FROM write_table');
29
    }
30
31
    /**
32
     * @group DBAL-80
33
     */
34
    public function testExecuteUpdateFirstTypeIsNull()
35
    {
36
        $sql = "INSERT INTO write_table (test_string, test_int) VALUES (?, ?)";
37
        $this->_conn->executeUpdate($sql, ['text', 1111], [null, ParameterType::INTEGER]);
38
39
        $sql = "SELECT * FROM write_table WHERE test_string = ? AND test_int = ?";
40
        self::assertTrue((bool) $this->_conn->fetchColumn($sql, ['text', 1111]));
41
    }
42
43
    public function testExecuteUpdate()
44
    {
45
        $sql = "INSERT INTO write_table (test_int) VALUES ( " . $this->_conn->quote(1) . ")";
46
        $affected = $this->_conn->executeUpdate($sql);
47
48
        self::assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
49
    }
50
51
    public function testExecuteUpdateWithTypes()
52
    {
53
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
54
        $affected = $this->_conn->executeUpdate(
55
            $sql,
56
            [1, 'foo'],
57
            [ParameterType::INTEGER, ParameterType::STRING]
58
        );
59
60
        self::assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
61
    }
62
63
    public function testPrepareRowCountReturnsAffectedRows()
64
    {
65
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
66
        $stmt = $this->_conn->prepare($sql);
67
68
        $stmt->bindValue(1, 1);
69
        $stmt->bindValue(2, "foo");
70
        $stmt->execute();
71
72
        self::assertEquals(1, $stmt->rowCount());
73
    }
74
75
    public function testPrepareWithPdoTypes()
76
    {
77
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
78
        $stmt = $this->_conn->prepare($sql);
79
80
        $stmt->bindValue(1, 1, ParameterType::INTEGER);
81
        $stmt->bindValue(2, 'foo', ParameterType::STRING);
82
        $stmt->execute();
83
84
        self::assertEquals(1, $stmt->rowCount());
85
    }
86
87
    public function testPrepareWithDbalTypes()
88
    {
89
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
90
        $stmt = $this->_conn->prepare($sql);
91
92
        $stmt->bindValue(1, 1, Type::getType('integer'));
93
        $stmt->bindValue(2, "foo", Type::getType('string'));
94
        $stmt->execute();
95
96
        self::assertEquals(1, $stmt->rowCount());
97
    }
98
99
    public function testPrepareWithDbalTypeNames()
100
    {
101
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
102
        $stmt = $this->_conn->prepare($sql);
103
104
        $stmt->bindValue(1, 1, 'integer');
105
        $stmt->bindValue(2, "foo", 'string');
106
        $stmt->execute();
107
108
        self::assertEquals(1, $stmt->rowCount());
109
    }
110
111
    public function insertRows()
112
    {
113
        self::assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 1, 'test_string' => 'foo')));
114
        self::assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar')));
115
    }
116
117
    public function testInsert()
118
    {
119
        $this->insertRows();
120
    }
121
122
    public function testEmptyInsert() : void
123
    {
124
        self::assertEquals(0, $this->_conn->insert('write_table', []));
125
    }
126
127
    public function testDelete()
128
    {
129
        $this->insertRows();
130
131
        self::assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 2)));
132
        self::assertCount(1, $this->_conn->fetchAll('SELECT * FROM write_table'));
133
134
        self::assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 1)));
135
        self::assertCount(0, $this->_conn->fetchAll('SELECT * FROM write_table'));
136
    }
137
138
    public function testUpdate()
139
    {
140
        $this->insertRows();
141
142
        self::assertEquals(1, $this->_conn->update('write_table', array('test_string' => 'bar'), array('test_string' => 'foo')));
143
        self::assertEquals(2, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
144
        self::assertEquals(0, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
145
    }
146
147
    public function testLastInsertId()
148
    {
149
        if ( ! $this->_conn->getDatabasePlatform()->prefersIdentityColumns()) {
150
            $this->markTestSkipped('Test only works on platforms with identity columns.');
151
        }
152
153
        self::assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar')));
154
        $num = $this->_conn->lastInsertId();
155
156
        self::assertNotNull($num, "LastInsertId() should not be null.");
157
        self::assertGreaterThan(0, $num, "LastInsertId() should be non-negative number.");
158
    }
159
160
    public function testLastInsertIdSequence()
161
    {
162
        if ( ! $this->_conn->getDatabasePlatform()->supportsSequences()) {
163
            $this->markTestSkipped('Test only works on platforms with sequences.');
164
        }
165
166
        $sequence = new \Doctrine\DBAL\Schema\Sequence('write_table_id_seq');
167
        try {
168
            $this->_conn->getSchemaManager()->createSequence($sequence);
169
        } catch(\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
170
        }
171
172
        $sequences = $this->_conn->getSchemaManager()->listSequences();
173
        self::assertCount(1, array_filter($sequences, function($sequence) {
174
            return strtolower($sequence->getName()) === 'write_table_id_seq';
175
        }));
176
177
        $stmt = $this->_conn->query($this->_conn->getDatabasePlatform()->getSequenceNextValSQL('write_table_id_seq'));
178
        $nextSequenceVal = $stmt->fetchColumn();
179
180
        $lastInsertId = $this->_conn->lastInsertId('write_table_id_seq');
181
182
        self::assertGreaterThan(0, $lastInsertId);
183
        self::assertEquals($nextSequenceVal, $lastInsertId);
184
    }
185
186
    public function testLastInsertIdNoSequenceGiven()
187
    {
188
        if ( ! $this->_conn->getDatabasePlatform()->supportsSequences() || $this->_conn->getDatabasePlatform()->supportsIdentityColumns()) {
189
            $this->markTestSkipped("Test only works consistently on platforms that support sequences and don't support identity columns.");
190
        }
191
192
        self::assertFalse($this->_conn->lastInsertId( null ));
193
194
    }
195
196
    /**
197
     * @group DBAL-445
198
     */
199
    public function testInsertWithKeyValueTypes()
200
    {
201
        $testString = new \DateTime('2013-04-14 10:10:10');
202
203
        $this->_conn->insert(
204
            'write_table',
205
            array('test_int' => '30', 'test_string' => $testString),
206
            array('test_string' => 'datetime', 'test_int' => 'integer')
207
        );
208
209
        $data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');
210
211
        self::assertEquals($testString->format($this->_conn->getDatabasePlatform()->getDateTimeFormatString()), $data);
212
    }
213
214
    /**
215
     * @group DBAL-445
216
     */
217
    public function testUpdateWithKeyValueTypes()
218
    {
219
        $testString = new \DateTime('2013-04-14 10:10:10');
220
221
        $this->_conn->insert(
222
            'write_table',
223
            array('test_int' => '30', 'test_string' => $testString),
224
            array('test_string' => 'datetime', 'test_int' => 'integer')
225
        );
226
227
        $testString = new \DateTime('2013-04-15 10:10:10');
228
229
        $this->_conn->update(
230
            'write_table',
231
            array('test_string' => $testString),
232
            array('test_int' => '30'),
233
            array('test_string' => 'datetime', 'test_int' => 'integer')
234
        );
235
236
        $data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');
237
238
        self::assertEquals($testString->format($this->_conn->getDatabasePlatform()->getDateTimeFormatString()), $data);
239
    }
240
241
    /**
242
     * @group DBAL-445
243
     */
244
    public function testDeleteWithKeyValueTypes()
245
    {
246
        $val = new \DateTime('2013-04-14 10:10:10');
247
        $this->_conn->insert(
248
            'write_table',
249
            array('test_int' => '30', 'test_string' => $val),
250
            array('test_string' => 'datetime', 'test_int' => 'integer')
251
        );
252
253
        $this->_conn->delete('write_table', array('test_int' => 30, 'test_string' => $val), array('test_string' => 'datetime', 'test_int' => 'integer'));
254
255
        $data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');
256
257
        self::assertFalse($data);
258
    }
259
260
    public function testEmptyIdentityInsert()
261
    {
262
        $platform = $this->_conn->getDatabasePlatform();
263
264
        if ( ! ($platform->supportsIdentityColumns() || $platform->usesSequenceEmulatedIdentityColumns()) ) {
265
            $this->markTestSkipped(
266
                'Test only works on platforms with identity columns or sequence emulated identity columns.'
267
            );
268
        }
269
270
        $table = new \Doctrine\DBAL\Schema\Table('test_empty_identity');
271
        $table->addColumn('id', 'integer', array('autoincrement' => true));
272
        $table->setPrimaryKey(array('id'));
273
274
        try {
275
            $this->_conn->getSchemaManager()->dropTable($table->getQuotedName($platform));
276
        } catch(\Exception $e) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
277
278
        foreach ($platform->getCreateTableSQL($table) as $sql) {
279
            $this->_conn->exec($sql);
280
        }
281
282
        $seqName = $platform->usesSequenceEmulatedIdentityColumns()
283
            ? $platform->getIdentitySequenceName('test_empty_identity', 'id')
284
            : null;
285
286
        $sql = $platform->getEmptyIdentityInsertSQL('test_empty_identity', 'id');
287
288
        $this->_conn->exec($sql);
289
290
        $firstId = $this->_conn->lastInsertId($seqName);
291
292
        $this->_conn->exec($sql);
293
294
        $secondId = $this->_conn->lastInsertId($seqName);
295
296
        self::assertGreaterThan($firstId, $secondId);
297
298
    }
299
300
    /**
301
     * @group DBAL-2688
302
     */
303
    public function testUpdateWhereIsNull()
304
    {
305
        $this->_conn->insert(
306
            'write_table',
307
            ['test_int' => '30', 'test_string' => null],
308
            ['test_string' => 'string', 'test_int' => 'integer']
309
        );
310
311
        $data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
312
313
        self::assertCount(1, $data);
314
315
        $this->_conn->update('write_table', ['test_int' => 10], ['test_string' => null], ['test_string' => 'string', 'test_int' => 'integer']);
316
317
        $data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
318
319
        self::assertCount(0, $data);
320
    }
321
322
    public function testDeleteWhereIsNull()
323
    {
324
        $this->_conn->insert(
325
            'write_table',
326
            ['test_int' => '30', 'test_string' => null],
327
            ['test_string' => 'string', 'test_int' => 'integer']
328
        );
329
330
        $data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
331
332
        self::assertCount(1, $data);
333
334
        $this->_conn->delete('write_table', ['test_string' => null], ['test_string' => 'string']);
335
336
        $data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
337
338
        self::assertCount(0, $data);
339
    }
340
}
341