-
-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathTable.join.phpt
More file actions
96 lines (75 loc) · 3.1 KB
/
Table.join.phpt
File metadata and controls
96 lines (75 loc) · 3.1 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* Test: Nette\Database\Table: Join.
* @dataProvider? ../databases.ini
*/
use Nette\Database\ISupplementalDriver;
use Tester\Assert;
require __DIR__ . '/../connect.inc.php'; // create $connection
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverName}-nette_test1.sql");
$driver = $connection->getSupplementalDriver();
test(function () use ($context) {
$apps = [];
foreach ($context->table('book')->order('author.name, title') as $book) { // SELECT `book`.* FROM `book` LEFT JOIN `author` ON `book`.`author_id` = `author`.`id` ORDER BY `author`.`name`, `title`
$apps[$book->title] = $book->author->name; // SELECT * FROM `author` WHERE (`author`.`id` IN (12, 11))
}
Assert::same([
'Dibi' => 'David Grudl',
'Nette' => 'David Grudl',
'1001 tipu a triku pro PHP' => 'Jakub Vrana',
'JUSH' => 'Jakub Vrana',
], $apps);
});
test(function () use ($context, $driver) {
$joinSql = $context->table('book_tag')->where('book_id', 1)->select('tag.*')->getSql();
if ($driver->isSupported(ISupplementalDriver::SUPPORT_SCHEMA)) {
Assert::same(
reformat('SELECT [tag].* FROM [book_tag] LEFT JOIN [public].[tag] [tag] ON [book_tag].[tag_id] = [tag].[id] WHERE ([book_id] = ?)'),
$joinSql
);
} else {
Assert::same(
reformat('SELECT [tag].* FROM [book_tag] LEFT JOIN [tag] ON [book_tag].[tag_id] = [tag].[id] WHERE ([book_id] = ?)'),
$joinSql
);
}
});
test(function () use ($context, $driver) {
$joinSql = $context->table('book_tag')->where('book_id', 1)->select('Tag.id')->getSql();
if ($driver->isSupported(ISupplementalDriver::SUPPORT_SCHEMA)) {
Assert::same(
reformat('SELECT [Tag].[id] FROM [book_tag] LEFT JOIN [public].[tag] [Tag] ON [book_tag].[tag_id] = [Tag].[id] WHERE ([book_id] = ?)'),
$joinSql
);
} else {
Assert::same(
reformat('SELECT [Tag].[id] FROM [book_tag] LEFT JOIN [tag] [Tag] ON [book_tag].[tag_id] = [Tag].[id] WHERE ([book_id] = ?)'),
$joinSql
);
}
});
test(function () use ($context) {
$tags = [];
foreach ($context->table('book_tag')->where('book.author.name', 'Jakub Vrana')->group('book_tag.tag_id')->order('book_tag.tag_id') as $book_tag) { // SELECT `book_tag`.* FROM `book_tag` INNER JOIN `book` ON `book_tag`.`book_id` = `book`.`id` INNER JOIN `author` ON `book`.`author_id` = `author`.`id` WHERE (`author`.`name` = ?) GROUP BY `book_tag`.`tag_id`
$tags[] = $book_tag->tag->name; // SELECT * FROM `tag` WHERE (`tag`.`id` IN (21, 22, 23))
}
Assert::same([
'PHP',
'MySQL',
'JavaScript',
], $tags);
});
test(function () use ($context) {
Assert::same(2, $context->table('author')->where('author_id', 11)->count(':book.id')); // SELECT COUNT(book.id) FROM `author` LEFT JOIN `book` ON `author`.`id` = `book`.`author_id` WHERE (`author_id` = 11)
});
test(function () use ($connection, $structure, $driverName) {
$context = new Nette\Database\Context(
$connection,
$structure,
new Nette\Database\Conventions\DiscoveredConventions($structure)
);
$books = $context->table('book')->select('book.*, author.name, translator.name');
Assert::error(function() use($books) {
iterator_to_array($books);
}, E_USER_NOTICE);
});