Skip to content

Commit 8e9a427

Browse files
committed
tests: added/improved tests
1 parent 451b33e commit 8e9a427

34 files changed

Lines changed: 531 additions & 365 deletions
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* Test: ConnectionPanel
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Bridges\DatabaseTracy\ConnectionPanel;
10+
use Nette\Database\Connection;
11+
use Tester\Assert;
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
test('Tracy Bar', function () {
17+
$connection = new Connection('sqlite::memory:');
18+
$panel = ConnectionPanel::initialize($connection, addBarPanel: true, name: 'foo');
19+
20+
$connection->beginTransaction();
21+
$connection->query('SELECT 1');
22+
$connection->commit();
23+
try {
24+
$connection->query('SELECT');
25+
} catch (Throwable) {
26+
}
27+
28+
Assert::matchFile(__DIR__ . '/tab.html', $panel->getTab());
29+
Assert::matchFile(__DIR__ . '/panel.html', $panel->getPanel());
30+
});
31+
32+
test('Bluescreen Panel', function () {
33+
$connection = new Connection('sqlite::memory:');
34+
try {
35+
$connection->query('SELECT');
36+
} catch (Throwable $e) {
37+
}
38+
39+
Assert::same(
40+
[
41+
'tab' => 'SQL',
42+
'panel' => "<pre class=\"dump\"><strong style=\"color:blue\">SELECT</strong></pre>\n",
43+
],
44+
ConnectionPanel::renderException($e),
45+
);
46+
});
47+
48+
test('deprecated initialization', function () {
49+
$connection = new Connection('sqlite::memory:');
50+
$panel = Nette\Database\Helpers::initializeTracy($connection, addBarPanel: true, name: 'foo');
51+
52+
$connection->beginTransaction();
53+
$connection->query('SELECT 1');
54+
$connection->commit();
55+
try {
56+
$connection->query('SELECT');
57+
} catch (Throwable) {
58+
}
59+
60+
Assert::matchFile(__DIR__ . '/tab.html', $panel->getTab());
61+
Assert::matchFile(__DIR__ . '/panel.html', $panel->getPanel());
62+
});

tests/Database.Tracy/panel.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
%A%
2+
<h1 title="sqlite::memory:">Queries: 4, time: %a% ms, foo</h1>
3+
4+
<div class="tracy-inner">
5+
<div class="tracy-inner-container">
6+
<table class="tracy-sortable">
7+
%A%
8+
<td %a%>%A%</td>
9+
<td class="nette-DbConnectionPanel-sql nette-DbConnectionPanel-sql-"><pre class="dump">::beginTransaction</pre>
10+
<a class="nette-DbConnectionPanel-source" href="%a%" title="%a%Connection.php:%d%" class="tracy-editor">%a%<b>Connection.php</b>:%d%</a> </td>
11+
<td></td>
12+
</tr>
13+
<tr>
14+
<td %a%>%A%</td>
15+
<td class="nette-DbConnectionPanel-sql nette-DbConnectionPanel-sql-select"><pre class="dump"><strong style="color:blue">SELECT</strong> 1</pre>
16+
<table class="tracy-collapsed nette-DbConnectionPanel-explain">%A%</table>
17+
<a class="nette-DbConnectionPanel-source" href="%a%" title="%a%Connection.php:%d%" class="tracy-editor">%a%<b>Connection.php</b>:%d%</a> </td>
18+
<td>0</td>
19+
</tr>
20+
<tr>
21+
<td %a%>%A%</td>
22+
<td class="nette-DbConnectionPanel-sql nette-DbConnectionPanel-sql-"><pre class="dump">::commit</pre>
23+
<a class="nette-DbConnectionPanel-source" href="%a%" title="%a%Connection.php:%d%" class="tracy-editor">%a%<b>Connection.php</b>:%d%</a> </td>
24+
<td></td>
25+
</tr>
26+
<tr>
27+
<td %a%>
28+
<span title="SQLSTATE[HY000]: %a%">ERROR</span>
29+
</td>
30+
<td class="nette-DbConnectionPanel-sql nette-DbConnectionPanel-sql-"><pre class="dump"><strong style="color:blue">SELECT</strong></pre>
31+
<a class="nette-DbConnectionPanel-source" href="%a%" title="%a%SqliteDriver.php:%d%" class="tracy-editor">%a%<b>SqliteDriver.php</b>:%d%</a> </td>
32+
<td></td>
33+
</tr>
34+
</table>
35+
</div>
36+
</div>

tests/Database.Tracy/tab.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<span title="Nette\Database foo">
2+
<svg %A%</svg><span class="tracy-label">%a% ms / 4</span>
3+
</span>

tests/Database/Connection.exceptions.mysql.phpt

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,80 @@ use Tester\Assert;
1212
require __DIR__ . '/../bootstrap.php';
1313

1414
$connection = connectToDB()->getConnection();
15+
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
1516

16-
$options = Tester\Environment::loadData();
17-
$e = Assert::exception(
18-
fn() => new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'),
19-
Nette\Database\ConnectionException::class,
20-
'%a% Access denied for user %a%',
21-
);
17+
test('Exception thrown for invalid database credentials', function () {
18+
$options = Tester\Environment::loadData();
19+
$e = Assert::exception(
20+
fn() => new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'),
21+
Nette\Database\ConnectionException::class,
22+
'%a% Access denied for user %a%',
23+
);
2224

23-
Assert::same(1045, $e->getDriverCode());
24-
Assert::contains($e->getSqlState(), ['HY000', '28000']);
25-
Assert::same($e->getCode(), $e->getSqlState());
25+
Assert::same(1045, $e->getDriverCode());
26+
Assert::contains($e->getSqlState(), ['HY000', '28000']);
27+
Assert::same($e->getCode(), $e->getSqlState());
28+
});
2629

2730

28-
$e = Assert::exception(
29-
fn() => $connection->rollback(),
30-
Nette\Database\DriverException::class,
31-
'There is no active transaction',
32-
0,
33-
);
31+
test('Exception thrown when calling rollback with no active transaction', function () use ($connection) {
32+
$e = Assert::exception(
33+
fn() => $connection->rollback(),
34+
Nette\Database\DriverException::class,
35+
'There is no active transaction',
36+
0,
37+
);
38+
Assert::same(null, $e->getDriverCode());
39+
});
3440

35-
Assert::same(null, $e->getDriverCode());
41+
42+
test('Exception thrown for syntax error in SQL query', function () use ($connection) {
43+
$e = Assert::exception(
44+
fn() => $connection->query('SELECT'),
45+
Nette\Database\DriverException::class,
46+
'%a% Syntax error %a%',
47+
'42000',
48+
);
49+
50+
Assert::same(1064, $e->getDriverCode());
51+
Assert::same($e->getCode(), $e->getSqlState());
52+
});
53+
54+
55+
test('Exception thrown for unique constraint violation', function () use ($connection) {
56+
$e = Assert::exception(
57+
fn() => $connection->query('INSERT INTO author (id, name, web, born) VALUES (11, "", "", NULL)'),
58+
Nette\Database\UniqueConstraintViolationException::class,
59+
'%a% Integrity constraint violation: %a%',
60+
'23000',
61+
);
62+
63+
Assert::same(1062, $e->getDriverCode());
64+
Assert::same($e->getCode(), $e->getSqlState());
65+
});
66+
67+
68+
test('Exception thrown for not null constraint violation', function () use ($connection) {
69+
$e = Assert::exception(
70+
fn() => $connection->query('INSERT INTO author (name, web, born) VALUES (NULL, "", NULL)'),
71+
Nette\Database\NotNullConstraintViolationException::class,
72+
'%a% Integrity constraint violation: %a%',
73+
'23000',
74+
);
75+
76+
Assert::same(1048, $e->getDriverCode());
77+
Assert::same($e->getCode(), $e->getSqlState());
78+
});
79+
80+
81+
test('Exception thrown for foreign key constraint violation', function () use ($connection) {
82+
$e = Assert::exception(
83+
fn() => $connection->query('INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, "")'),
84+
Nette\Database\ForeignKeyConstraintViolationException::class,
85+
'%a% a foreign key constraint fails %a%',
86+
'23000',
87+
);
88+
89+
Assert::same(1452, $e->getDriverCode());
90+
Assert::same($e->getCode(), $e->getSqlState());
91+
});

tests/Database/Connection.exceptions.postgre.phpt

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,82 @@ use Tester\Assert;
1212
require __DIR__ . '/../bootstrap.php';
1313

1414
$connection = connectToDB()->getConnection();
15+
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
1516

16-
$options = Tester\Environment::loadData();
17-
$e = Assert::exception(
18-
fn() => new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'),
19-
Nette\Database\ConnectionException::class,
20-
null,
21-
'08006',
22-
);
2317

24-
Assert::same(7, $e->getDriverCode());
25-
Assert::same($e->getCode(), $e->getSqlState());
18+
test('Exception thrown for invalid database credentials', function () {
19+
$options = Tester\Environment::loadData();
20+
$e = Assert::exception(
21+
fn() => new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'),
22+
Nette\Database\ConnectionException::class,
23+
null,
24+
'08006',
25+
);
2626

27+
Assert::same(7, $e->getDriverCode());
28+
Assert::same($e->getCode(), $e->getSqlState());
29+
});
2730

28-
$e = Assert::exception(
29-
fn() => $connection->rollback(),
30-
Nette\Database\DriverException::class,
31-
'There is no active transaction',
32-
0,
33-
);
3431

35-
Assert::same(null, $e->getDriverCode());
32+
test('Exception thrown when calling rollback with no active transaction', function () use ($connection) {
33+
$e = Assert::exception(
34+
fn() => $connection->rollback(),
35+
Nette\Database\DriverException::class,
36+
'There is no active transaction',
37+
0,
38+
);
39+
40+
Assert::same(null, $e->getDriverCode());
41+
});
42+
43+
44+
test('Exception thrown for syntax error in SQL query', function () use ($connection) {
45+
$e = Assert::exception(
46+
fn() => $connection->query('SELECT INTO'),
47+
Nette\Database\DriverException::class,
48+
'%a% syntax error %A%',
49+
'42601',
50+
);
51+
52+
Assert::same(7, $e->getDriverCode());
53+
Assert::same($e->getCode(), $e->getSqlState());
54+
});
55+
56+
57+
test('Exception thrown for unique constraint violation', function () use ($connection) {
58+
$e = Assert::exception(
59+
fn() => $connection->query("INSERT INTO author (id, name, web, born) VALUES (11, '', '', NULL)"),
60+
Nette\Database\UniqueConstraintViolationException::class,
61+
'%a% Unique violation: %A%',
62+
'23505',
63+
);
64+
65+
Assert::same(7, $e->getDriverCode());
66+
Assert::same($e->getCode(), $e->getSqlState());
67+
});
68+
69+
70+
test('Exception thrown for not null constraint violation', function () use ($connection) {
71+
$e = Assert::exception(
72+
fn() => $connection->query("INSERT INTO author (name, web, born) VALUES (NULL, '', NULL)"),
73+
Nette\Database\NotNullConstraintViolationException::class,
74+
'%a% Not null violation: %A%',
75+
'23502',
76+
);
77+
78+
Assert::same(7, $e->getDriverCode());
79+
Assert::same($e->getCode(), $e->getSqlState());
80+
});
81+
82+
83+
test('Exception thrown for foreign key constraint violation', function () use ($connection) {
84+
$e = Assert::exception(
85+
fn() => $connection->query("INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, '')"),
86+
Nette\Database\ForeignKeyConstraintViolationException::class,
87+
'%a% Foreign key violation: %A%',
88+
'23503',
89+
);
90+
91+
Assert::same(7, $e->getDriverCode());
92+
Assert::same($e->getCode(), $e->getSqlState());
93+
});

tests/Database/Connection.exceptions.sqlite.phpt

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,79 @@ use Tester\Assert;
1212
require __DIR__ . '/../bootstrap.php';
1313

1414
$connection = connectToDB()->getConnection();
15+
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
1516

16-
$e = Assert::exception(
17-
fn() => new Nette\Database\Connection('sqlite:.'),
18-
Nette\Database\ConnectionException::class,
19-
'SQLSTATE[HY000] [14] unable to open database file',
20-
'HY000',
21-
);
2217

23-
Assert::same(14, $e->getDriverCode());
24-
Assert::same($e->getCode(), $e->getSqlState());
18+
test('Exception thrown for unable to open database file', function () {
19+
$e = Assert::exception(
20+
fn() => new Nette\Database\Connection('sqlite:.'),
21+
Nette\Database\ConnectionException::class,
22+
'SQLSTATE[HY000] [14] unable to open database file',
23+
'HY000',
24+
);
2525

26+
Assert::same(14, $e->getDriverCode());
27+
Assert::same($e->getCode(), $e->getSqlState());
28+
});
2629

27-
$e = Assert::exception(
28-
fn() => $connection->rollback(),
29-
Nette\Database\DriverException::class,
30-
'There is no active transaction',
31-
0,
32-
);
3330

34-
Assert::same(null, $e->getDriverCode());
31+
test('Exception thrown when calling rollback with no active transaction', function () use ($connection) {
32+
$e = Assert::exception(
33+
fn() => $connection->rollback(),
34+
Nette\Database\DriverException::class,
35+
'There is no active transaction',
36+
0,
37+
);
38+
39+
Assert::same(null, $e->getDriverCode());
40+
});
41+
42+
43+
test('Exception thrown for error in SQL query', function () use ($connection) {
44+
$e = Assert::exception(
45+
fn() => $connection->query('SELECT'),
46+
Nette\Database\DriverException::class,
47+
'%a% error%a%',
48+
'HY000',
49+
);
50+
51+
Assert::same(1, $e->getDriverCode());
52+
Assert::same($e->getCode(), $e->getSqlState());
53+
});
54+
55+
56+
test('Exception thrown for unique constraint violation', function () use ($connection) {
57+
$e = Assert::exception(
58+
fn() => $connection->query('INSERT INTO author (id, name, web, born) VALUES (11, "", "", NULL)'),
59+
Nette\Database\UniqueConstraintViolationException::class,
60+
'%a% Integrity constraint violation: %a%',
61+
'23000',
62+
);
63+
64+
Assert::same(19, $e->getDriverCode());
65+
Assert::same($e->getCode(), $e->getSqlState());
66+
});
67+
68+
69+
test('Exception thrown for not null constraint violation', function () use ($connection) {
70+
$e = Assert::exception(
71+
fn() => $connection->query('INSERT INTO author (name, web, born) VALUES (NULL, "", NULL)'),
72+
Nette\Database\NotNullConstraintViolationException::class,
73+
'%a% Integrity constraint violation: %a%',
74+
'23000',
75+
);
76+
77+
Assert::same(19, $e->getDriverCode());
78+
Assert::same($e->getCode(), $e->getSqlState());
79+
});
80+
81+
82+
test('Exception thrown for foreign key constraint violation', function () use ($connection) {
83+
$e = Assert::exception(function () use ($connection) {
84+
$connection->query('PRAGMA foreign_keys=true');
85+
$connection->query('INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, "")');
86+
}, Nette\Database\ForeignKeyConstraintViolationException::class, '%a% Integrity constraint violation: %a%', '23000');
87+
88+
Assert::same(19, $e->getDriverCode());
89+
Assert::same($e->getCode(), $e->getSqlState());
90+
});

0 commit comments

Comments
 (0)