1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-24 10:53:17 +01:00
php-dibi/tests/dibi/Fluent.fetch.phpt

51 lines
1.5 KiB
Plaintext
Raw Normal View History

2015-01-12 05:33:41 +01:00
<?php
/**
* @dataProvider ../databases.ini
*/
use Tester\Assert;
use Dibi\Row;
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");
// fetch a single value
$res = $conn->select('title')->from('products')->orderBy('product_id');
Assert::equal('Chair', $res->fetchSingle());
// fetch complete result set
$res = $conn->select('*')->from('products')->orderBy('product_id');
2015-10-06 01:39:01 +02:00
Assert::equal([
new Row(['product_id' => num(1), 'title' => 'Chair']),
new Row(['product_id' => num(2), 'title' => 'Table']),
new Row(['product_id' => num(3), 'title' => 'Computer']),
2015-10-06 01:39:01 +02:00
], $res->fetchAll());
2015-01-12 05:33:41 +01:00
// more complex association array
if (!in_array($config['system'], ['odbc', 'sqlsrv'])) {
2015-10-06 01:39:01 +02:00
$res = $conn->select(['products.title' => 'title', 'customers.name' => 'name'])->select('orders.amount')->as('amount')
2015-01-12 05:33:41 +01:00
->from('products')
->innerJoin('orders')->using('(product_id)')
->innerJoin('customers')->using('([customer_id])')
->orderBy('order_id');
2015-10-06 01:39:01 +02:00
Assert::equal([
'Arnold Rimmer' => [
'Chair' => new Row(['title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0)]),
'Computer' => new Row(['title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0)]),
2015-10-06 01:39:01 +02:00
],
'Dave Lister' => [
'Table' => new Row(['title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0)]),
2015-10-06 01:39:01 +02:00
],
'Kristine Kochanski' => [
'Computer' => new Row(['title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0)]),
2015-10-06 01:39:01 +02:00
],
], $res->fetchAssoc('name,title'));
2015-01-12 05:33:41 +01:00
}