Failed Conditions
Pull Request — develop (#3368)
by Benjamin
13:33
created

WriteTest::testGetSequenceNumber()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 24
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional;
4
5
use DateTime;
6
use Doctrine\DBAL\Driver\DriverException;
7
use Doctrine\DBAL\ParameterType;
8
use Doctrine\DBAL\Schema\AbstractSchemaManager;
9
use Doctrine\DBAL\Schema\Sequence;
10
use Doctrine\DBAL\Schema\Table;
11
use Doctrine\DBAL\Types\Type;
12
use Doctrine\Tests\DbalFunctionalTestCase;
13
use Doctrine\Tests\TestUtil;
14
use Throwable;
15
use function array_filter;
16
use function strtolower;
17
18
class WriteTest extends DbalFunctionalTestCase
19
{
20
    protected function setUp()
21
    {
22
        parent::setUp();
23
24
        try {
25
            /** @var AbstractSchemaManager $sm */
26
            $table = new Table('write_table');
27
            $table->addColumn('id', 'integer', ['autoincrement' => true]);
28
            $table->addColumn('test_int', 'integer');
29
            $table->addColumn('test_string', 'string', ['notnull' => false]);
30
            $table->setPrimaryKey(['id']);
31
32
            $this->connection->getSchemaManager()->createTable($table);
33
        } catch (Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
34
        }
35
        $this->connection->executeUpdate('DELETE FROM write_table');
36
    }
37
38
    /**
39
     * @group DBAL-80
40
     */
41
    public function testExecuteUpdateFirstTypeIsNull()
42
    {
43
        $sql = 'INSERT INTO write_table (test_string, test_int) VALUES (?, ?)';
44
        $this->connection->executeUpdate($sql, ['text', 1111], [null, ParameterType::INTEGER]);
45
46
        $sql = 'SELECT * FROM write_table WHERE test_string = ? AND test_int = ?';
47
        self::assertTrue((bool) $this->connection->fetchColumn($sql, ['text', 1111]));
48
    }
49
50
    public function testExecuteUpdate()
51
    {
52
        $sql      = 'INSERT INTO write_table (test_int) VALUES ( ' . $this->connection->quote(1) . ')';
53
        $affected = $this->connection->executeUpdate($sql);
54
55
        self::assertEquals(1, $affected, 'executeUpdate() should return the number of affected rows!');
56
    }
57
58
    public function testExecuteUpdateWithTypes()
59
    {
60
        $sql      = 'INSERT INTO write_table (test_int, test_string) VALUES (?, ?)';
61
        $affected = $this->connection->executeUpdate(
62
            $sql,
63
            [1, 'foo'],
64
            [ParameterType::INTEGER, ParameterType::STRING]
65
        );
66
67
        self::assertEquals(1, $affected, 'executeUpdate() should return the number of affected rows!');
68
    }
69
70
    public function testPrepareRowCountReturnsAffectedRows()
71
    {
72
        $sql  = 'INSERT INTO write_table (test_int, test_string) VALUES (?, ?)';
73
        $stmt = $this->connection->prepare($sql);
74
75
        $stmt->bindValue(1, 1);
76
        $stmt->bindValue(2, 'foo');
77
        $stmt->execute();
78
79
        self::assertEquals(1, $stmt->rowCount());
80
    }
81
82
    public function testPrepareWithPdoTypes()
83
    {
84
        $sql  = 'INSERT INTO write_table (test_int, test_string) VALUES (?, ?)';
85
        $stmt = $this->connection->prepare($sql);
86
87
        $stmt->bindValue(1, 1, ParameterType::INTEGER);
88
        $stmt->bindValue(2, 'foo', ParameterType::STRING);
89
        $stmt->execute();
90
91
        self::assertEquals(1, $stmt->rowCount());
92
    }
93
94
    public function testPrepareWithDbalTypes()
95
    {
96
        $sql  = 'INSERT INTO write_table (test_int, test_string) VALUES (?, ?)';
97
        $stmt = $this->connection->prepare($sql);
98
99
        $stmt->bindValue(1, 1, Type::getType('integer'));
100
        $stmt->bindValue(2, 'foo', Type::getType('string'));
101
        $stmt->execute();
102
103
        self::assertEquals(1, $stmt->rowCount());
104
    }
105
106
    public function testPrepareWithDbalTypeNames()
107
    {
108
        $sql  = 'INSERT INTO write_table (test_int, test_string) VALUES (?, ?)';
109
        $stmt = $this->connection->prepare($sql);
110
111
        $stmt->bindValue(1, 1, 'integer');
112
        $stmt->bindValue(2, 'foo', 'string');
113
        $stmt->execute();
114
115
        self::assertEquals(1, $stmt->rowCount());
116
    }
117
118
    public function insertRows()
119
    {
120
        self::assertEquals(1, $this->connection->insert('write_table', ['test_int' => 1, 'test_string' => 'foo']));
121
        self::assertEquals(1, $this->connection->insert('write_table', ['test_int' => 2, 'test_string' => 'bar']));
122
    }
123
124
    public function testInsert()
125
    {
126
        $this->insertRows();
127
    }
128
129
    public function testDelete()
130
    {
131
        $this->insertRows();
132
133
        self::assertEquals(1, $this->connection->delete('write_table', ['test_int' => 2]));
134
        self::assertCount(1, $this->connection->fetchAll('SELECT * FROM write_table'));
135
136
        self::assertEquals(1, $this->connection->delete('write_table', ['test_int' => 1]));
137
        self::assertCount(0, $this->connection->fetchAll('SELECT * FROM write_table'));
138
    }
139
140
    public function testUpdate()
141
    {
142
        $this->insertRows();
143
144
        self::assertEquals(1, $this->connection->update('write_table', ['test_string' => 'bar'], ['test_string' => 'foo']));
145
        self::assertEquals(2, $this->connection->update('write_table', ['test_string' => 'baz'], ['test_string' => 'bar']));
146
        self::assertEquals(0, $this->connection->update('write_table', ['test_string' => 'baz'], ['test_string' => 'bar']));
147
    }
148
149
    public function testLastInsertId()
150
    {
151
        if (! $this->connection->getDatabasePlatform()->supportsIdentityColumns()) {
152
            $this->markTestSkipped('This test targets platforms that support identity columns.');
153
        }
154
155
        self::assertEquals(1, $this->connection->insert('write_table', ['test_int' => 2, 'test_string' => 'bar']));
156
157
        $num = $this->connection->lastInsertId();
158
        self::assertGreaterThan(0, $num, 'lastInsertId() should return a positive number.');
159
    }
160
161
    public function testLastInsertIdNotSupported()
162
    {
163
        if ($this->connection->getDatabasePlatform()->supportsIdentityColumns()) {
164
            $this->markTestSkipped('This test targets platforms that don\'t support identity columns.');
165
        }
166
167
        self::assertEquals(1, $this->connection->insert('write_table', ['test_int' => 2, 'test_string' => 'bar']));
168
169
        self::expectException(DriverException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

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

169
        self::/** @scrutinizer ignore-call */ 
170
              expectException(DriverException::class);
Loading history...
170
        $this->connection->lastInsertId();
171
    }
172
173
    public function testLastInsertIdNewConnection()
174
    {
175
        $connection = TestUtil::getConnection();
176
177
        $this->expectException(DriverException::class);
178
        $connection->lastInsertId();
179
    }
180
181
    public function testGetSequenceNumber()
182
    {
183
        if (! $this->connection->getDatabasePlatform()->supportsSequences()) {
184
            $this->markTestSkipped('This test targets platforms that support sequences.');
185
        }
186
187
        $sequence = new Sequence('write_table_id_seq');
188
        try {
189
            $this->connection->getSchemaManager()->createSequence($sequence);
190
        } catch (Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
191
        }
192
193
        $sequences = $this->connection->getSchemaManager()->listSequences();
194
        self::assertCount(1, array_filter($sequences, static function ($sequence) {
195
            return strtolower($sequence->getName()) === 'write_table_id_seq';
196
        }));
197
198
        $stmt            = $this->connection->query($this->connection->getDatabasePlatform()->getSequenceNextValSQL('write_table_id_seq'));
199
        $nextSequenceVal = $stmt->fetchColumn();
200
201
        $sequenceNumber = $this->connection->getSequenceNumber('write_table_id_seq');
202
203
        self::assertGreaterThan(0, $sequenceNumber);
204
        self::assertEquals($nextSequenceVal, $sequenceNumber);
205
    }
206
207
    public function testGetSequenceNumberNotSupportedOrSequenceDoesNotExist()
208
    {
209
        $this->expectException(DriverException::class);
210
        $this->connection->getSequenceNumber('unknown-sequence');
211
    }
212
213
    /**
214
     * @group DBAL-445
215
     */
216
    public function testInsertWithKeyValueTypes()
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
        $data = $this->connection->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');
227
228
        self::assertEquals($testString->format($this->connection->getDatabasePlatform()->getDateTimeFormatString()), $data);
229
    }
230
231
    /**
232
     * @group DBAL-445
233
     */
234
    public function testUpdateWithKeyValueTypes()
235
    {
236
        $testString = new DateTime('2013-04-14 10:10:10');
237
238
        $this->connection->insert(
239
            'write_table',
240
            ['test_int' => '30', 'test_string' => $testString],
241
            ['test_string' => 'datetime', 'test_int' => 'integer']
242
        );
243
244
        $testString = new DateTime('2013-04-15 10:10:10');
245
246
        $this->connection->update(
247
            'write_table',
248
            ['test_string' => $testString],
249
            ['test_int' => '30'],
250
            ['test_string' => 'datetime', 'test_int' => 'integer']
251
        );
252
253
        $data = $this->connection->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');
254
255
        self::assertEquals($testString->format($this->connection->getDatabasePlatform()->getDateTimeFormatString()), $data);
256
    }
257
258
    /**
259
     * @group DBAL-445
260
     */
261
    public function testDeleteWithKeyValueTypes()
262
    {
263
        $val = new DateTime('2013-04-14 10:10:10');
264
        $this->connection->insert(
265
            'write_table',
266
            ['test_int' => '30', 'test_string' => $val],
267
            ['test_string' => 'datetime', 'test_int' => 'integer']
268
        );
269
270
        $this->connection->delete('write_table', ['test_int' => 30, 'test_string' => $val], ['test_string' => 'datetime', 'test_int' => 'integer']);
271
272
        $data = $this->connection->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');
273
274
        self::assertFalse($data);
275
    }
276
277
    public function testEmptyIdentityInsert()
278
    {
279
        $platform = $this->connection->getDatabasePlatform();
280
281
        if (! ($platform->supportsIdentityColumns() || $platform->usesSequenceEmulatedIdentityColumns())) {
282
            $this->markTestSkipped(
283
                'Test only works on platforms with identity columns or sequence emulated identity columns.'
284
            );
285
        }
286
287
        $table = new Table('test_empty_identity');
288
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
289
        $table->setPrimaryKey(['id']);
290
291
        try {
292
            $this->connection->getSchemaManager()->dropTable($table->getQuotedName($platform));
293
        } catch (Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
294
        }
295
296
        foreach ($platform->getCreateTableSQL($table) as $sql) {
297
            $this->connection->exec($sql);
298
        }
299
300
        $seqName = $platform->usesSequenceEmulatedIdentityColumns()
301
            ? $platform->getIdentitySequenceName('test_empty_identity', 'id')
302
            : null;
303
304
        $sql = $platform->getEmptyIdentityInsertSQL('test_empty_identity', 'id');
305
306
        $this->connection->exec($sql);
307
308
        $firstId = $seqName !== null
309
            ? $this->connection->getSequenceNumber($seqName)
310
            : $this->connection->lastInsertId();
311
312
        $this->connection->exec($sql);
313
314
        $secondId = $seqName !== null
315
            ? $this->connection->getSequenceNumber($seqName)
316
            : $this->connection->lastInsertId();
317
318
        self::assertGreaterThan($firstId, $secondId);
319
    }
320
321
    /**
322
     * @group DBAL-2688
323
     */
324
    public function testUpdateWhereIsNull()
325
    {
326
        $this->connection->insert(
327
            'write_table',
328
            ['test_int' => '30', 'test_string' => null],
329
            ['test_string' => 'string', 'test_int' => 'integer']
330
        );
331
332
        $data = $this->connection->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
333
334
        self::assertCount(1, $data);
335
336
        $this->connection->update('write_table', ['test_int' => 10], ['test_string' => null], ['test_string' => 'string', 'test_int' => 'integer']);
337
338
        $data = $this->connection->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
339
340
        self::assertCount(0, $data);
341
    }
342
343
    public function testDeleteWhereIsNull()
344
    {
345
        $this->connection->insert(
346
            'write_table',
347
            ['test_int' => '30', 'test_string' => null],
348
            ['test_string' => 'string', 'test_int' => 'integer']
349
        );
350
351
        $data = $this->connection->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
352
353
        self::assertCount(1, $data);
354
355
        $this->connection->delete('write_table', ['test_string' => null], ['test_string' => 'string']);
356
357
        $data = $this->connection->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
358
359
        self::assertCount(0, $data);
360
    }
361
}
362