Failed Conditions
Push — master ( 9355a2...379085 )
by Sergei
26s queued 12s
created

tests/Doctrine/Tests/DBAL/Functional/WriteTest.php (1 issue)

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