Query/tests/ConnectionManagerTest.php

118 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2016-10-12 22:12:25 -04:00
<?php declare(strict_types=1);
/**
* Query
*
* SQL Query Builder / Database Abstraction Layer
*
2022-09-29 11:33:08 -04:00
* PHP version 8.1
2016-10-12 22:12:25 -04:00
*
2018-01-19 16:50:34 -05:00
* @package Query
2023-01-20 11:30:51 -05:00
* @author Timothy J. Warren <tim@timshome.page>
* @copyright 2012 - 2023 Timothy J. Warren
2018-01-19 16:50:34 -05:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2019-12-11 16:49:42 -05:00
* @link https://git.timshomepage.net/aviat/Query
2023-03-17 16:34:21 -04:00
* @version 4.1.0
2016-10-12 22:12:25 -04:00
*/
2023-03-17 15:30:36 -04:00
namespace Query\Tests;
use DomainException;
use Query\{ConnectionManager, QueryBuilderInterface};
class ConnectionManagerTest extends BaseTestCase
2023-03-17 15:30:36 -04:00
{
protected static $instance;
2019-12-10 12:17:40 -05:00
public static function setUpBeforeClass(): void
{
ConnectionManager::getInstance();
self::$instance = ConnectionManager::getInstance();
}
2019-12-10 12:17:40 -05:00
public static function tearDownAfterClass(): void
2018-01-26 08:39:30 -05:00
{
self::$instance = NULL;
}
2019-12-10 12:17:40 -05:00
public function testNoClone(): void
{
2016-10-12 20:32:23 -04:00
$this->expectException('DomainException');
$this->expectExceptionMessage("Can't clone singleton");
$clone = clone self::$instance;
$this->assertNull($clone);
}
2019-12-10 12:17:40 -05:00
public function testNoSerialize(): void
{
$this->expectException(DomainException::class);
$this->expectExceptionMessage('No serializing of singleton');
serialize(self::$instance);
$this->expectException(DomainException::class);
$this->expectExceptionMessage('No serializing of singleton');
self::$instance->__sleep();
}
2019-12-10 12:17:40 -05:00
public function testNoUnserialize(): void
{
$this->expectException(DomainException::class);
$this->expectExceptionMessage("Can't unserialize singleton");
self::$instance->__wakeup();
}
2019-12-10 12:17:40 -05:00
public function testParseParams(): void
{
2018-01-26 08:39:30 -05:00
$params = new class {
public $type = 'sqlite';
public $file = ':memory:';
public $options = [
2023-03-17 15:30:36 -04:00
'foo' => 'bar',
2018-01-26 08:39:30 -05:00
];
};
2018-01-26 08:39:30 -05:00
$expected = [
':memory:',
'Sqlite',
$params,
2023-03-17 15:30:36 -04:00
['foo' => 'bar'],
2018-01-26 08:39:30 -05:00
];
$this->assertEquals($expected, self::$instance->parseParams($params));
}
2019-12-10 12:17:40 -05:00
public function testConnect(): void
{
2018-01-26 08:39:30 -05:00
$params = new class {
public $type = 'sqlite';
public $file = ':memory:';
public $prefix = 'create_';
public $options = [
2023-03-17 15:30:36 -04:00
'foo' => 'bar',
2018-01-26 08:39:30 -05:00
];
};
$conn = self::$instance->connect($params);
// Check that the connection just made is returned from the get_connection method
$this->assertEquals($conn, self::$instance->getConnection());
}
2019-12-10 12:17:40 -05:00
public function testGetConnection(): void
{
2018-01-26 08:39:30 -05:00
$params = (object) [
'type' => 'sqlite',
'file' => ':memory:',
'prefix' => 'create_',
'alias' => 'conn_manager',
2018-01-26 08:39:30 -05:00
'options' => [
2023-03-17 15:30:36 -04:00
'foo' => 'bar',
],
2018-01-26 08:39:30 -05:00
];
$conn = self::$instance->connect($params);
$this->assertEquals($conn, self::$instance->getConnection('conn_manager'));
}
}
// End of connection_manager_test.php