/
exceptions.phpt
77 lines (58 loc) · 1.94 KB
/
exceptions.phpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php declare(strict_types = 1);
/**
* @testCase
* @dataProvider? ../../databases.ini
*/
namespace NextrasTests\Dbal;
use Nextras\Dbal\Drivers\Exception\ConnectionException;
use Nextras\Dbal\Drivers\Exception\ForeignKeyConstraintViolationException;
use Nextras\Dbal\Drivers\Exception\NotNullConstraintViolationException;
use Nextras\Dbal\Drivers\Exception\QueryException;
use Nextras\Dbal\Drivers\Exception\UniqueConstraintViolationException;
use Tester\Assert;
require_once __DIR__ . '/../../bootstrap.php';
class ExceptionsTest extends IntegrationTestCase
{
public function testConnection()
{
Assert::exception(function () {
$connection = $this->createConnection(['database' => 'unknown']);
$connection->connect();
}, ConnectionException::class);
Assert::exception(function () {
$connection = $this->createConnection(['username' => 'unknown']);
$connection->connect();
}, ConnectionException::class);
}
public function testForeignKeyException()
{
Assert::exception(function () {
$this->initData($this->connection);
$this->connection->query('UPDATE books SET author_id = 999');
}, ForeignKeyConstraintViolationException::class);
}
public function testUniqueException()
{
Assert::exception(function () {
$this->initData($this->connection);
$this->connection->query('INSERT INTO publishers %values', ['name' => 'Nextras publisher']);
}, UniqueConstraintViolationException::class);
}
public function testNotNullException()
{
Assert::exception(function () {
$this->initData($this->connection);
$this->connection->query('UPDATE books SET title = NULL');
}, NotNullConstraintViolationException::class);
}
public function testQueryException()
{
/** @var QueryException $e */
$e = Assert::exception(function () {
$this->connection->query('SELECT FROM FROM foo');
}, QueryException::class);
Assert::same('SELECT FROM FROM foo', $e->getSqlQuery());
}
}
$test = new ExceptionsTest();
$test->run();