Skip to content

Commit d116a97

Browse files
committed
tests: renamed $context -> $explorer
1 parent 9023cbb commit d116a97

File tree

68 files changed

+511
-511
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+511
-511
lines changed

readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ Nette Database Explorer layer helps you to fetch database data more easily and i
7171

7272
Let's take a look at common use-case. You need to fetch books and their authors. It is common 1:N relationship. The often used implementation fetches data by one SQL query with table joins. The second possibility is to fetch data separately, run one query for getting books and then get an author for each book by another query (e.g. in your foreach cycle). This could be easily optimized to run only two queries, one for books, and another for the needed authors - and this is just the way how Nette Database Explorer does it.
7373

74-
Selecting data starts with the table, just call `$context->table()` on the `Nette\Database\Explorer` object. The easiest way to get it is [described here](https://doc.nette.org/database-core#toc-configuration), but if we use Nette Database Explorer alone, it can be [manually created](https://doc.nette.org/database-explorer#toc-manual-creating-nette-database-context).
74+
Selecting data starts with the table, just call `$explorer->table()` on the `Nette\Database\Explorer` object. The easiest way to get it is [described here](https://doc.nette.org/database-core#toc-configuration), but if we use Nette Database Explorer alone, it can be [manually created](https://doc.nette.org/database-explorer#toc-manual-creating-nette-database-context).
7575

7676

7777
```php
78-
$selection = $context->table('book'); // db table name is "book"
78+
$selection = $explorer->table('book'); // db table name is "book"
7979
```
8080

8181
We can simply iterate over the selection and pass through all the books. The rows are fetched as ActiveRow instances; you can read row data from their properties.
8282

8383
```php
84-
$books = $context->table('book');
84+
$books = $explorer->table('book');
8585
foreach ($books as $book) {
8686
echo $book->title;
8787
echo $book->author_id;
@@ -91,7 +91,7 @@ foreach ($books as $book) {
9191
Getting just one specific row is done by `get()` method, which directly returns an ActiveRow instance.
9292

9393
```php
94-
$book = $context->table('book')->get(2); // returns book with id 2
94+
$book = $explorer->table('book')->get(2); // returns book with id 2
9595
echo $book->title;
9696
echo $book->author_id;
9797
```
@@ -100,7 +100,7 @@ Working with relationships
100100
--------------------------
101101

102102
```php
103-
$books = $context->table('book');
103+
$books = $explorer->table('book');
104104

105105
foreach ($books as $book) {
106106
echo 'title: ' . $book->title;

tests/Database.DI/DatabaseExtension.basic.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ test('', function () {
4040
Assert::type(Nette\Database\Connection::class, $connection);
4141
Assert::same('sqlite::memory:', $connection->getDsn());
4242

43-
$context = $container->getService('database.default.context');
44-
Assert::type(Nette\Database\Explorer::class, $context);
45-
Assert::same($connection, $context->getConnection());
43+
$explorer = $container->getService('database.default.context');
44+
Assert::type(Nette\Database\Explorer::class, $explorer);
45+
Assert::same($connection, $explorer->getConnection());
4646

47-
Assert::type(Nette\Database\Structure::class, $context->getStructure());
48-
Assert::type(Nette\Database\Conventions\DiscoveredConventions::class, $context->getConventions());
47+
Assert::type(Nette\Database\Structure::class, $explorer->getStructure());
48+
Assert::type(Nette\Database\Conventions\DiscoveredConventions::class, $explorer->getConventions());
4949

5050
// aliases
5151
Assert::same($connection, $container->getService('nette.database.default'));
52-
Assert::same($context, $container->getService('nette.database.default.context'));
52+
Assert::same($explorer, $container->getService('nette.database.default.context'));
5353
});

tests/Database.DI/DatabaseExtension.multiple.phpt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ test('', function () {
5050
Assert::same($connection, $container->getByType(Nette\Database\Connection::class));
5151
Assert::same('sqlite::memory:', $connection->getDsn());
5252

53-
$context = $container->getService('database.first.context');
54-
Assert::type(Nette\Database\Explorer::class, $context);
55-
Assert::same($context, $container->getByType(Nette\Database\Explorer::class));
56-
Assert::same($connection, $context->getConnection());
53+
$explorer = $container->getService('database.first.context');
54+
Assert::type(Nette\Database\Explorer::class, $explorer);
55+
Assert::same($explorer, $container->getByType(Nette\Database\Explorer::class));
56+
Assert::same($connection, $explorer->getConnection());
5757

58-
Assert::type(Nette\Database\Structure::class, $context->getStructure());
59-
Assert::same($context->getStructure(), $container->getByType(Nette\Database\IStructure::class));
60-
Assert::type(Nette\Database\Conventions\DiscoveredConventions::class, $context->getConventions());
61-
Assert::same($context->getConventions(), $container->getByType(Nette\Database\IConventions::class));
58+
Assert::type(Nette\Database\Structure::class, $explorer->getStructure());
59+
Assert::same($explorer->getStructure(), $container->getByType(Nette\Database\IStructure::class));
60+
Assert::type(Nette\Database\Conventions\DiscoveredConventions::class, $explorer->getConventions());
61+
Assert::same($explorer->getConventions(), $container->getByType(Nette\Database\IConventions::class));
6262

6363
// aliases
6464
Assert::same($connection, $container->getService('nette.database.first'));
65-
Assert::same($context, $container->getService('nette.database.first.context'));
65+
Assert::same($explorer, $container->getService('nette.database.first.context'));
6666
});

tests/Database/Connection.lazy.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ test('non lazy', function () {
2323

2424
test('lazy', function () {
2525
$connection = new Nette\Database\Connection('dsn', 'user', 'password', ['lazy' => true]);
26-
$context = new Nette\Database\Explorer($connection, new Structure($connection, new DevNullStorage));
27-
Assert::exception(function () use ($context) {
28-
$context->query('SELECT ?', 10);
26+
$explorer = new Nette\Database\Explorer($connection, new Structure($connection, new DevNullStorage));
27+
Assert::exception(function () use ($explorer) {
28+
$explorer->query('SELECT ?', 10);
2929
}, Nette\Database\DriverException::class, '%a%valid data source %a%');
3030
});
3131

tests/Database/Context.transaction.phpt

Lines changed: 0 additions & 44 deletions
This file was deleted.

tests/Database/Context.fetch.phpt renamed to tests/Database/Explorer.fetch.phpt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ require __DIR__ . '/connect.inc.php'; // create $connection
1414
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
1515

1616

17-
test('fetch', function () use ($context) {
18-
$row = $context->fetch('SELECT name, id FROM author WHERE id = ?', 11);
17+
test('fetch', function () use ($explorer) {
18+
$row = $explorer->fetch('SELECT name, id FROM author WHERE id = ?', 11);
1919
Assert::type(Nette\Database\Row::class, $row);
2020
Assert::equal(Nette\Database\Row::from([
2121
'name' => 'Jakub Vrana',
@@ -24,27 +24,27 @@ test('fetch', function () use ($context) {
2424
});
2525

2626

27-
test('fetchField', function () use ($context) {
28-
Assert::same('Jakub Vrana', $context->fetchField('SELECT name FROM author ORDER BY id'));
27+
test('fetchField', function () use ($explorer) {
28+
Assert::same('Jakub Vrana', $explorer->fetchField('SELECT name FROM author ORDER BY id'));
2929
});
3030

3131

32-
test('fetchFields', function () use ($context) {
33-
Assert::same([11, 'Jakub Vrana'], $context->fetchFields('SELECT id, name FROM author ORDER BY id'));
32+
test('fetchFields', function () use ($explorer) {
33+
Assert::same([11, 'Jakub Vrana'], $explorer->fetchFields('SELECT id, name FROM author ORDER BY id'));
3434
});
3535

3636

37-
test('fetchPairs', function () use ($context) {
38-
$pairs = $context->fetchPairs('SELECT name, id FROM author WHERE id > ? ORDER BY id', 11);
37+
test('fetchPairs', function () use ($explorer) {
38+
$pairs = $explorer->fetchPairs('SELECT name, id FROM author WHERE id > ? ORDER BY id', 11);
3939
Assert::same([
4040
'David Grudl' => 12,
4141
'Geek' => 13,
4242
], $pairs);
4343
});
4444

4545

46-
test('fetchAll', function () use ($context) {
47-
$arr = $context->fetchAll('SELECT name, id FROM author WHERE id < ? ORDER BY id', 13);
46+
test('fetchAll', function () use ($explorer) {
47+
$arr = $explorer->fetchAll('SELECT name, id FROM author WHERE id < ? ORDER BY id', 13);
4848
Assert::equal([
4949
Nette\Database\Row::from(['name' => 'Jakub Vrana', 'id' => 11]),
5050
Nette\Database\Row::from(['name' => 'David Grudl', 'id' => 12]),

tests/Database/Context.query.phpt renamed to tests/Database/Explorer.query.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ require __DIR__ . '/connect.inc.php'; // create $connection
1414
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
1515

1616

17-
test('', function () use ($context) {
18-
$res = $context->query('SELECT id FROM author WHERE id = ?', 11);
17+
test('', function () use ($explorer) {
18+
$res = $explorer->query('SELECT id FROM author WHERE id = ?', 11);
1919
Assert::type(Nette\Database\ResultSet::class, $res);
2020
Assert::same('SELECT id FROM author WHERE id = ?', $res->getQueryString());
2121
Assert::same([11], $res->getParameters());
2222
});
2323

2424

25-
test('', function () use ($context) {
26-
$res = $context->query('SELECT id FROM author WHERE id = ? OR id = ?', 11, 12);
25+
test('', function () use ($explorer) {
26+
$res = $explorer->query('SELECT id FROM author WHERE id = ? OR id = ?', 11, 12);
2727
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
2828
Assert::same([11, 12], $res->getParameters());
2929
});
3030

3131

32-
test('', function () use ($context) {
33-
$res = $context->queryArgs('SELECT id FROM author WHERE id = ? OR id = ?', [11, 12]);
32+
test('', function () use ($explorer) {
33+
$res = $explorer->queryArgs('SELECT id FROM author WHERE id = ? OR id = ?', [11, 12]);
3434
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
3535
Assert::same([11, 12], $res->getParameters());
3636
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Database\Connection transaction methods.
5+
* @dataProvider? databases.ini
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
use Tester\Assert;
11+
12+
require __DIR__ . '/connect.inc.php'; // create $connection
13+
14+
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");
15+
16+
17+
test('', function () use ($explorer) {
18+
$explorer->beginTransaction();
19+
$explorer->query('DELETE FROM book');
20+
$explorer->rollBack();
21+
22+
Assert::same(3, $explorer->fetchField('SELECT id FROM book WHERE id = ', 3));
23+
});
24+
25+
26+
test('', function () use ($explorer) {
27+
Assert::exception(function () use ($explorer) {
28+
$explorer->transaction(function () use ($explorer) {
29+
$explorer->query('DELETE FROM book');
30+
throw new Exception('my exception');
31+
});
32+
}, \Throwable::class, 'my exception');
33+
34+
Assert::same(3, $explorer->fetchField('SELECT id FROM book WHERE id = ', 3));
35+
});
36+
37+
38+
test('', function () use ($explorer) {
39+
$explorer->beginTransaction();
40+
$explorer->query('DELETE FROM book');
41+
$explorer->commit();
42+
43+
Assert::null($explorer->fetchField('SELECT id FROM book WHERE id = ', 3));
44+
});

tests/Database/Table/ActiveRow.__toString().phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ require __DIR__ . '/../connect.inc.php'; // create $connection
1414
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverName}-nette_test1.sql");
1515

1616

17-
test('', function () use ($context) {
18-
Assert::same('2', (string) $context->table('book')->get(2));
17+
test('', function () use ($explorer) {
18+
Assert::same('2', (string) $explorer->table('book')->get(2));
1919
});
2020

2121

22-
test('', function () use ($context) {
23-
Assert::same(2, $context->table('book')->get(2)->getPrimary());
22+
test('', function () use ($explorer) {
23+
Assert::same(2, $explorer->table('book')->get(2)->getPrimary());
2424
});

tests/Database/Table/GroupedSelection.insert().phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ require __DIR__ . '/../connect.inc.php'; // create $connection
1414
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverName}-nette_test1.sql");
1515

1616

17-
test('', function () use ($context) {
18-
$book = $context->table('book')->get(1);
17+
test('', function () use ($explorer) {
18+
$book = $explorer->table('book')->get(1);
1919
$book->related('book_tag')->insert(['tag_id' => 23]);
2020

2121
Assert::equal(3, $book->related('book_tag')->count());
@@ -25,8 +25,8 @@ test('', function () use ($context) {
2525
});
2626

2727

28-
test('test counting already fetched rows', function () use ($context) {
29-
$book = $context->table('book')->get(1);
28+
test('test counting already fetched rows', function () use ($explorer) {
29+
$book = $explorer->table('book')->get(1);
3030
iterator_to_array($book->related('book_tag'));
3131
$book->related('book_tag')->insert(['tag_id' => 23]);
3232
Assert::equal(3, $book->related('book_tag')->count());

0 commit comments

Comments
 (0)