1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-23 18:33:45 +01:00
php-dibi/tests/dibi/DataSource.phpt

162 lines
3.3 KiB
Plaintext
Raw Normal View History

2015-01-12 05:33:41 +01:00
<?php
2017-06-09 22:20:47 +02:00
declare(strict_types=1);
use Dibi\Row;
use Tester\Assert;
2015-01-12 05:33:41 +01:00
require __DIR__ . '/bootstrap.php';
$conn = new Dibi\Connection($config);
2015-01-12 05:33:41 +01:00
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
$ds = $conn->dataSource('SELECT * FROM products');
Assert::match(
2015-06-19 03:11:36 +02:00
reformat('
2015-01-12 05:33:41 +01:00
SELECT *
2015-06-19 03:11:36 +02:00
FROM (SELECT * FROM products) t'),
2015-01-12 05:33:41 +01:00
(string) $ds
);
Assert::same(3, $ds->count());
Assert::same(3, $ds->getTotalCount());
Assert::same(
reformat('SELECT COUNT(*) FROM (SELECT * FROM products) t'),
dibi::$sql
);
$ds->select('title');
$ds->orderBy('title', dibi::DESC);
$ds->where('title like "%a%"');
Assert::match(
reformat("
SELECT [title]
FROM (SELECT * FROM products) t
WHERE (title like '%a%')
ORDER BY [title] DESC
"),
(string) $ds
);
$ds->select('product_id');
$ds->orderBy('product_id', dibi::ASC);
$ds->where('product_id = %i', 1);
Assert::match(
reformat("
SELECT [title], [product_id]
FROM (SELECT * FROM products) t
WHERE (title like '%a%') AND (product_id = 1)
ORDER BY [title] DESC, [product_id] ASC
"),
(string) $ds
);
2015-10-06 01:39:01 +02:00
$ds->select(['product_id']);
$ds->orderBy(['product_id' => dibi::ASC]);
$ds->where(['product_id = 1']);
2015-01-12 05:33:41 +01:00
Assert::match(
reformat("
SELECT [product_id]
FROM (SELECT * FROM products) t
WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1)
ORDER BY [product_id] ASC
"),
(string) $ds
);
Assert::same(1, $ds->count());
Assert::same(3, $ds->getTotalCount());
Assert::match(reformat("SELECT COUNT(*) FROM (
SELECT [product_id]
FROM (SELECT * FROM products) t
WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1)
ORDER BY [product_id] ASC
) t"), dibi::$sql);
Assert::same(1, $ds->toDataSource()->count());
2015-10-06 01:39:01 +02:00
Assert::equal([
new Row([
2015-01-12 05:33:41 +01:00
'product_id' => 1,
2015-10-06 01:39:01 +02:00
]),
], iterator_to_array($ds));
2015-01-12 05:33:41 +01:00
Assert::match(
reformat("
SELECT [product_id]
FROM (SELECT * FROM products) t
WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1)
ORDER BY [product_id] ASC
"),
dibi::$sql
);
$fluent = $ds->toFluent();
Assert::same(1, $fluent->count());
Assert::match(
reformat("SELECT * FROM (
SELECT [product_id]
FROM (SELECT * FROM products) t
WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1)
ORDER BY [product_id] ASC
) t"),
(string) $fluent
);
$ds = $conn->select('title')->from('products')->toDataSource();
Assert::match(
2015-06-19 03:11:36 +02:00
reformat('
2015-01-12 05:33:41 +01:00
SELECT *
2015-06-19 03:11:36 +02:00
FROM (SELECT [title] FROM [products]) t'),
2015-01-12 05:33:41 +01:00
(string) $ds
);
Assert::equal(new Row([
2015-01-12 05:33:41 +01:00
'product_id' => 1,
'title' => 'Chair',
2015-10-06 01:39:01 +02:00
]), $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetch());
2015-01-12 05:33:41 +01:00
Assert::same(1, $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchSingle());
Assert::same(
2015-10-06 01:39:01 +02:00
[1 => 'Chair', 'Table', 'Computer'],
2015-01-12 05:33:41 +01:00
$conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchPairs()
);
2015-10-06 01:39:01 +02:00
Assert::equal([
1 => new Row([
2015-01-12 05:33:41 +01:00
'product_id' => 1,
'title' => 'Chair',
2015-10-06 01:39:01 +02:00
]),
new Row([
2015-01-12 05:33:41 +01:00
'product_id' => 2,
'title' => 'Table',
2015-10-06 01:39:01 +02:00
]),
new Row([
2015-01-12 05:33:41 +01:00
'product_id' => 3,
'title' => 'Computer',
2015-10-06 01:39:01 +02:00
]),
], $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchAssoc('product_id'));
2015-01-12 05:33:41 +01:00
$ds = new Dibi\DataSource('products', $conn);
2015-01-12 05:33:41 +01:00
Assert::match(
2015-06-19 03:11:36 +02:00
reformat('
2015-01-12 05:33:41 +01:00
SELECT *
2015-06-19 03:11:36 +02:00
FROM [products]'),
2015-01-12 05:33:41 +01:00
(string) $ds
);
Assert::same(3, $ds->count());
Assert::same(3, $ds->getTotalCount());
Assert::same(reformat('SELECT COUNT(*) FROM [products]'), dibi::$sql);