php-censor/tests/B8Framework/DatabaseTest.php

95 lines
2.5 KiB
PHP
Raw Normal View History

2016-04-17 08:34:12 +02:00
<?php
namespace Tests\b8;
use b8\Config;
2016-04-17 08:34:12 +02:00
use b8\Database;
class DatabaseTest extends \PHPUnit\Framework\TestCase
2016-04-17 08:34:12 +02:00
{
protected function setUp()
{
$config = new Config([
'b8' => [
'database' => [
'servers' => [
2017-01-29 03:49:43 +01:00
'read' => [
['host' => 'localhost'],
],
'write' => [
['host' => 'localhost'],
],
],
2017-02-18 04:31:33 +01:00
'type' => DB_TYPE,
'name' => DB_NAME,
'username' => DB_USER,
'password' => DB_PASS,
],
],
]);
}
2016-04-21 19:06:58 +02:00
2017-01-04 09:16:20 +01:00
protected function checkDatabaseConnection()
{
try {
$connection = Database::getConnection('read');
} catch (\Exception $e) {
if ('Could not connect to any read servers.' === $e->getMessage()) {
$this->markTestSkipped('Test skipped because test database doesn`t exist.');
} else {
throw $e;
}
}
2016-04-21 19:06:58 +02:00
}
public function testGetWriteConnection()
{
2017-01-04 09:16:20 +01:00
$this->checkDatabaseConnection();
2016-04-21 19:06:58 +02:00
2017-01-04 09:16:20 +01:00
$connection = Database::getConnection('write');
2018-02-16 10:41:56 +01:00
self::assertInstanceOf('\b8\Database', $connection);
2016-04-21 19:06:58 +02:00
}
public function testGetDetails()
{
2017-01-04 09:16:20 +01:00
$this->checkDatabaseConnection();
2016-04-21 19:06:58 +02:00
$details = Database::getConnection('read')->getDetails();
2018-02-16 10:41:56 +01:00
self::assertTrue(is_array($details));
self::assertTrue(($details['db'] == DB_NAME));
self::assertTrue(($details['user'] == DB_USER));
self::assertTrue(($details['pass'] == DB_PASS));
2016-04-21 19:06:58 +02:00
}
/**
* @expectedException \Exception
*/
public function testConnectionFailure()
{
2017-01-04 09:16:20 +01:00
$this->checkDatabaseConnection();
Database::reset();
$config = new Config([
'b8' => [
'database' => [
'servers' => [
2017-01-29 03:49:43 +01:00
'read' => [
['host' => 'localhost'],
],
'write' => [
['host' => 'localhost'],
],
],
2017-10-19 17:33:00 +02:00
'type' => DB_TYPE,
'name' => 'b8_test_2',
'username' => '',
'password' => '',
],
],
]);
2016-04-21 19:06:58 +02:00
Database::getConnection('read');
}
2017-01-04 09:16:20 +01:00
}