1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-24 02:43:09 +01:00
php-dibi/tests/dibi/Connection.connect.phpt

88 lines
2.1 KiB
Plaintext
Raw Normal View History

2015-01-12 05:33:41 +01:00
<?php
/**
* @dataProvider ../databases.ini
*/
2017-06-09 22:20:47 +02:00
declare(strict_types=1);
use Dibi\Connection;
use Tester\Assert;
2015-01-12 05:33:41 +01:00
require __DIR__ . '/bootstrap.php';
2020-08-16 16:10:27 +02:00
test('', function () use ($config) {
$conn = new Connection($config);
2015-01-12 05:33:41 +01:00
Assert::true($conn->isConnected());
$conn->disconnect();
Assert::false($conn->isConnected());
});
2020-08-16 16:10:27 +02:00
test('lazy', function () use ($config) {
$conn = new Connection($config + ['lazy' => true]);
2015-01-12 05:33:41 +01:00
Assert::false($conn->isConnected());
$conn->query('SELECT 1');
Assert::true($conn->isConnected());
});
2020-08-16 16:10:27 +02:00
test('', function () use ($config) {
$conn = new Connection($config);
2015-01-12 05:33:41 +01:00
Assert::true($conn->isConnected());
Assert::null($conn->getConfig('lazy'));
Assert::same($config['driver'], $conn->getConfig('driver'));
Assert::type(Dibi\Driver::class, $conn->getDriver());
2015-01-12 05:33:41 +01:00
});
2020-08-16 16:10:27 +02:00
test('', function () use ($config) {
$conn = new Connection($config);
Assert::true($conn->isConnected());
$conn->disconnect();
Assert::false($conn->isConnected());
$conn->disconnect();
Assert::false($conn->isConnected());
});
2020-08-16 16:10:27 +02:00
test('', function () use ($config) {
2020-02-14 12:40:54 +01:00
$conn = new Connection($config);
Assert::equal('hello', $conn->query('SELECT %s', 'hello')->fetchSingle());
$conn->disconnect();
$conn->connect();
Assert::equal('hello', $conn->query('SELECT %s', 'hello')->fetchSingle());
});
2020-08-16 16:10:27 +02:00
test('', function () use ($config) {
Assert::exception(function () use ($config) {
new Connection($config + ['onConnect' => '']);
}, InvalidArgumentException::class, "Configuration option 'onConnect' must be array.");
$e = Assert::exception(function () use ($config) {
new Connection($config + ['onConnect' => ['STOP']]);
}, Dibi\DriverException::class);
Assert::same('STOP', $e->getSql());
$e = Assert::exception(function () use ($config) {
new Connection($config + ['onConnect' => [['STOP %i', 123]]]);
}, Dibi\DriverException::class);
Assert::same('STOP 123', $e->getSql());
// lazy
$conn = new Connection($config + ['lazy' => true, 'onConnect' => ['STOP']]);
$e = Assert::exception(function () use ($conn) {
$conn->query('SELECT 1');
}, Dibi\DriverException::class);
Assert::same('STOP', $e->getSql());
});