Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

Doctrine/Tests/DBAL/Functional/ExceptionTest.php (1 issue)

1
<?php
2
namespace Doctrine\Tests\DBAL\Functional;
3
4
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
5
use Doctrine\DBAL\Exception;
6
use Doctrine\DBAL\Schema\Table;
7
8
class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
9
{
10
    protected function setUp()
11
    {
12
        parent::setUp();
13
14
        if ( !($this->_conn->getDriver() instanceof ExceptionConverterDriver)) {
15
            $this->markTestSkipped('Driver does not support special exception handling.');
16
        }
17
    }
18
19
    public function testPrimaryConstraintViolationException()
20
    {
21
        $table = new \Doctrine\DBAL\Schema\Table("duplicatekey_table");
22
        $table->addColumn('id', 'integer', array());
23
        $table->setPrimaryKey(array('id'));
24
25
        $this->_conn->getSchemaManager()->createTable($table);
26
27
        $this->_conn->insert("duplicatekey_table", array('id' => 1));
28
29
        $this->expectException(Exception\UniqueConstraintViolationException::class);
30
        $this->_conn->insert("duplicatekey_table", array('id' => 1));
31
    }
32
33
    public function testTableNotFoundException()
34
    {
35
        $sql = "SELECT * FROM unknown_table";
36
37
        $this->expectException(Exception\TableNotFoundException::class);
38
        $this->_conn->executeQuery($sql);
39
    }
40
41
    public function testTableExistsException()
42
    {
43
        $schemaManager = $this->_conn->getSchemaManager();
44
        $table = new \Doctrine\DBAL\Schema\Table("alreadyexist_table");
45
        $table->addColumn('id', 'integer', array());
46
        $table->setPrimaryKey(array('id'));
47
48
        $this->expectException(Exception\TableExistsException::class);
49
        $schemaManager->createTable($table);
50
        $schemaManager->createTable($table);
51
    }
52
53 View Code Duplication
    public function testForeignKeyConstraintViolationExceptionOnInsert()
54
    {
55
        if ( ! $this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
56
            $this->markTestSkipped("Only fails on platforms with foreign key constraints.");
57
        }
58
59
        $this->setUpForeignKeyConstraintViolationExceptionTest();
60
61
        try {
62
            $this->_conn->insert("constraint_error_table", array('id' => 1));
63
            $this->_conn->insert("owning_table", array('id' => 1, 'constraint_id' => 1));
64
        } catch (\Exception $exception) {
65
            $this->tearDownForeignKeyConstraintViolationExceptionTest();
66
67
            throw $exception;
68
        }
69
70
        $this->expectException(Exception\ForeignKeyConstraintViolationException::class);
71
72
        try {
73
            $this->_conn->insert('owning_table', array('id' => 2, 'constraint_id' => 2));
74
        } catch (Exception\ForeignKeyConstraintViolationException $exception) {
75
            $this->tearDownForeignKeyConstraintViolationExceptionTest();
76
77
            throw $exception;
78
        } catch (\Exception $exception) {
79
            $this->tearDownForeignKeyConstraintViolationExceptionTest();
80
81
            throw $exception;
82
        }
83
84
        $this->tearDownForeignKeyConstraintViolationExceptionTest();
85
    }
86
87 View Code Duplication
    public function testForeignKeyConstraintViolationExceptionOnUpdate()
88
    {
89
        if ( ! $this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
90
            $this->markTestSkipped("Only fails on platforms with foreign key constraints.");
91
        }
92
93
        $this->setUpForeignKeyConstraintViolationExceptionTest();
94
95
        try {
96
            $this->_conn->insert("constraint_error_table", array('id' => 1));
97
            $this->_conn->insert("owning_table", array('id' => 1, 'constraint_id' => 1));
98
        } catch (\Exception $exception) {
99
            $this->tearDownForeignKeyConstraintViolationExceptionTest();
100
101
            throw $exception;
102
        }
103
104
        $this->expectException(Exception\ForeignKeyConstraintViolationException::class);
105
106
        try {
107
            $this->_conn->update('constraint_error_table', array('id' => 2), array('id' => 1));
108
        } catch (Exception\ForeignKeyConstraintViolationException $exception) {
109
            $this->tearDownForeignKeyConstraintViolationExceptionTest();
110
111
            throw $exception;
112
        } catch (\Exception $exception) {
113
            $this->tearDownForeignKeyConstraintViolationExceptionTest();
114
115
            throw $exception;
116
        }
117
118
        $this->tearDownForeignKeyConstraintViolationExceptionTest();
119
    }
120
121 View Code Duplication
    public function testForeignKeyConstraintViolationExceptionOnDelete()
122
    {
123
        if ( ! $this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
124
            $this->markTestSkipped("Only fails on platforms with foreign key constraints.");
125
        }
126
127
        $this->setUpForeignKeyConstraintViolationExceptionTest();
128
129
        try {
130
            $this->_conn->insert("constraint_error_table", array('id' => 1));
131
            $this->_conn->insert("owning_table", array('id' => 1, 'constraint_id' => 1));
132
        } catch (\Exception $exception) {
133
            $this->tearDownForeignKeyConstraintViolationExceptionTest();
134
135
            throw $exception;
136
        }
137
138
        $this->expectException(Exception\ForeignKeyConstraintViolationException::class);
139
140
        try {
141
            $this->_conn->delete('constraint_error_table', array('id' => 1));
142
        } catch (Exception\ForeignKeyConstraintViolationException $exception) {
143
            $this->tearDownForeignKeyConstraintViolationExceptionTest();
144
145
            throw $exception;
146
        } catch (\Exception $exception) {
147
            $this->tearDownForeignKeyConstraintViolationExceptionTest();
148
149
            throw $exception;
150
        }
151
152
        $this->tearDownForeignKeyConstraintViolationExceptionTest();
153
    }
154
155
    public function testForeignKeyConstraintViolationExceptionOnTruncate()
156
    {
157
        $platform = $this->_conn->getDatabasePlatform();
158
159
        if (!$platform->supportsForeignKeyConstraints()) {
160
            $this->markTestSkipped("Only fails on platforms with foreign key constraints.");
161
        }
162
163
        $this->setUpForeignKeyConstraintViolationExceptionTest();
164
165
        try {
166
            $this->_conn->insert("constraint_error_table", array('id' => 1));
167
            $this->_conn->insert("owning_table", array('id' => 1, 'constraint_id' => 1));
168
        } catch (\Exception $exception) {
169
            $this->tearDownForeignKeyConstraintViolationExceptionTest();
170
171
            throw $exception;
172
        }
173
174
        $this->expectException(Exception\ForeignKeyConstraintViolationException::class);
175
176
        try {
177
            $this->_conn->executeUpdate($platform->getTruncateTableSQL('constraint_error_table'));
178
        } catch (Exception\ForeignKeyConstraintViolationException $exception) {
179
            $this->tearDownForeignKeyConstraintViolationExceptionTest();
180
181
            throw $exception;
182
        } catch (\Exception $exception) {
183
            $this->tearDownForeignKeyConstraintViolationExceptionTest();
184
185
            throw $exception;
186
        }
187
188
        $this->tearDownForeignKeyConstraintViolationExceptionTest();
189
    }
190
191
    public function testNotNullConstraintViolationException()
192
    {
193
        $schema = new \Doctrine\DBAL\Schema\Schema();
194
195
        $table = $schema->createTable("notnull_table");
196
        $table->addColumn('id', 'integer', array());
197
        $table->addColumn('value', 'integer', array('notnull' => true));
198
        $table->setPrimaryKey(array('id'));
199
200
        foreach ($schema->toSql($this->_conn->getDatabasePlatform()) as $sql) {
201
            $this->_conn->exec($sql);
202
        }
203
204
        $this->expectException(Exception\NotNullConstraintViolationException::class);
205
        $this->_conn->insert("notnull_table", array('id' => 1, 'value' => null));
206
    }
207
208 View Code Duplication
    public function testInvalidFieldNameException()
209
    {
210
        $schema = new \Doctrine\DBAL\Schema\Schema();
211
212
        $table = $schema->createTable("bad_fieldname_table");
213
        $table->addColumn('id', 'integer', array());
214
215
        foreach ($schema->toSql($this->_conn->getDatabasePlatform()) as $sql) {
216
            $this->_conn->exec($sql);
217
        }
218
219
        $this->expectException(Exception\InvalidFieldNameException::class);
220
        $this->_conn->insert("bad_fieldname_table", array('name' => 5));
221
    }
222
223
    public function testNonUniqueFieldNameException()
224
    {
225
        $schema = new \Doctrine\DBAL\Schema\Schema();
226
227
        $table = $schema->createTable("ambiguous_list_table");
228
        $table->addColumn('id', 'integer');
229
230
        $table2 = $schema->createTable("ambiguous_list_table_2");
231
        $table2->addColumn('id', 'integer');
232
233
        foreach ($schema->toSql($this->_conn->getDatabasePlatform()) as $sql) {
234
            $this->_conn->exec($sql);
235
        }
236
237
        $sql = 'SELECT id FROM ambiguous_list_table, ambiguous_list_table_2';
238
        $this->expectException(Exception\NonUniqueFieldNameException::class);
239
        $this->_conn->executeQuery($sql);
240
    }
241
242
    public function testUniqueConstraintViolationException()
243
    {
244
        $schema = new \Doctrine\DBAL\Schema\Schema();
245
246
        $table = $schema->createTable("unique_field_table");
247
        $table->addColumn('id', 'integer');
248
        $table->addUniqueIndex(array('id'));
249
250
        foreach ($schema->toSql($this->_conn->getDatabasePlatform()) as $sql) {
251
            $this->_conn->exec($sql);
252
        }
253
254
        $this->_conn->insert("unique_field_table", array('id' => 5));
255
        $this->expectException(Exception\UniqueConstraintViolationException::class);
256
        $this->_conn->insert("unique_field_table", array('id' => 5));
257
    }
258
259 View Code Duplication
    public function testSyntaxErrorException()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
260
    {
261
        $table = new \Doctrine\DBAL\Schema\Table("syntax_error_table");
262
        $table->addColumn('id', 'integer', array());
263
        $table->setPrimaryKey(array('id'));
264
265
        $this->_conn->getSchemaManager()->createTable($table);
266
267
        $sql = 'SELECT id FRO syntax_error_table';
268
        $this->expectException(Exception\SyntaxErrorException::class);
269
        $this->_conn->executeQuery($sql);
270
    }
271
272
    /**
273
     * @dataProvider getSqLiteOpenConnection
274
     */
275
    public function testConnectionExceptionSqLite($mode, $exceptionClass)
276
    {
277
        if ($this->_conn->getDatabasePlatform()->getName() != 'sqlite') {
278
            $this->markTestSkipped("Only fails this way on sqlite");
279
        }
280
281
        $filename = sprintf('%s/%s', sys_get_temp_dir(), 'doctrine_failed_connection_'.$mode.'.db');
282
283
        if (file_exists($filename)) {
284
            chmod($filename, 0200); // make the file writable again, so it can be removed on Windows
285
            unlink($filename);
286
        }
287
288
        touch($filename);
289
        chmod($filename, $mode);
290
291
        $params = array(
292
            'driver' => 'pdo_sqlite',
293
            'path'   => $filename,
294
        );
295
        $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
296
297
        $schema = new \Doctrine\DBAL\Schema\Schema();
298
        $table = $schema->createTable("no_connection");
299
        $table->addColumn('id', 'integer');
300
301
        $this->expectException($exceptionClass);
302
        foreach ($schema->toSql($conn->getDatabasePlatform()) as $sql) {
303
            $conn->exec($sql);
304
        }
305
    }
306
307
    public function getSqLiteOpenConnection()
308
    {
309
        return array(
310
            // mode 0 is considered read-only on Windows
311
            array(0000, defined('PHP_WINDOWS_VERSION_BUILD') ? Exception\ReadOnlyException::class : Exception\ConnectionException::class),
312
            array(0444, Exception\ReadOnlyException::class),
313
        );
314
    }
315
316
    /**
317
     * @dataProvider getConnectionParams
318
     */
319
    public function testConnectionException($params)
320
    {
321
        if ($this->_conn->getDatabasePlatform()->getName() == 'sqlite') {
322
            $this->markTestSkipped("Only skipped if platform is not sqlite");
323
        }
324
325
        if ($this->_conn->getDatabasePlatform()->getName() == 'drizzle') {
326
            $this->markTestSkipped("Drizzle does not always support authentication");
327
        }
328
329
        if ($this->_conn->getDatabasePlatform()->getName() == 'postgresql' && isset($params['password'])) {
330
            $this->markTestSkipped("Does not work on Travis");
331
        }
332
333
        $defaultParams = $this->_conn->getParams();
334
        $params = array_merge($defaultParams, $params);
335
336
        $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
337
338
        $schema = new \Doctrine\DBAL\Schema\Schema();
339
        $table = $schema->createTable("no_connection");
340
        $table->addColumn('id', 'integer');
341
342
        $this->expectException(Exception\ConnectionException::class);
343
344
        foreach ($schema->toSql($conn->getDatabasePlatform()) as $sql) {
345
            $conn->exec($sql);
346
        }
347
    }
348
349
    public function getConnectionParams()
350
    {
351
        return array(
352
            array(array('user' => 'not_existing')),
353
            array(array('password' => 'really_not')),
354
            array(array('host' => 'localnope')),
355
        );
356
    }
357
358
    private function setUpForeignKeyConstraintViolationExceptionTest()
359
    {
360
        $schemaManager = $this->_conn->getSchemaManager();
361
362
        $table = new Table("constraint_error_table");
363
        $table->addColumn('id', 'integer', array());
364
        $table->setPrimaryKey(array('id'));
365
366
        $owningTable = new Table("owning_table");
367
        $owningTable->addColumn('id', 'integer', array());
368
        $owningTable->addColumn('constraint_id', 'integer', array());
369
        $owningTable->setPrimaryKey(array('id'));
370
        $owningTable->addForeignKeyConstraint($table, array('constraint_id'), array('id'));
371
372
        $schemaManager->createTable($table);
373
        $schemaManager->createTable($owningTable);
374
    }
375
376
    private function tearDownForeignKeyConstraintViolationExceptionTest()
377
    {
378
        $schemaManager = $this->_conn->getSchemaManager();
379
380
        $schemaManager->dropTable('owning_table');
381
        $schemaManager->dropTable('constraint_error_table');
382
    }
383
}
384