From 8d0d23f5f421b7a921f1ac097c432fcf7e372a0f Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Wed, 28 Feb 2018 18:37:22 +0700 Subject: [PATCH] Merged PostgreSQL and MySQL tests inti one PHPUnit XML config. --- .php-censor.yml | 2 +- .travis.yml | 10 +- phpunit.pgsql.xml | 62 ------------ phpunit.mysql.xml => phpunit.xml | 10 +- src/B8Framework/Database.php | 2 +- ...DatabaseTest.php => DatabaseMysqlTest.php} | 20 ++-- tests/B8Framework/DatabasePostgresqlTest.php | 96 +++++++++++++++++++ .../Plugin/Util/PhpUnitResultTest.php | 2 +- tests/bootstrap.php | 11 ++- 9 files changed, 126 insertions(+), 89 deletions(-) delete mode 100644 phpunit.pgsql.xml rename phpunit.mysql.xml => phpunit.xml (88%) rename tests/B8Framework/{DatabaseTest.php => DatabaseMysqlTest.php} (80%) create mode 100755 tests/B8Framework/DatabasePostgresqlTest.php diff --git a/.php-censor.yml b/.php-censor.yml index c927f4de..a08ed6fd 100644 --- a/.php-censor.yml +++ b/.php-censor.yml @@ -11,7 +11,7 @@ setup: test: php_unit: config: - - phpunit.pgsql.xml + - phpunit.xml php_mess_detector: allow_failures: true diff --git a/.travis.yml b/.travis.yml index 80bf4f4b..34fc0f3d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,20 +15,16 @@ php: matrix: fast_finish: true -env: - - TYPE=mysql - - TYPE=pgsql - install: - composer selfupdate - composer install before_script: - - if [[ "$TYPE" == "mysql" ]]; then mysql -e "create database IF NOT EXISTS b8_test;" -uroot; fi - - if [[ "$TYPE" == "pgsql" ]]; then psql -c 'create database b8_test;' -U postgres; fi + - mysql -e "create database IF NOT EXISTS b8_test;" -uroot + - psql -c 'create database b8_test;' -U postgres script: - - vendor/bin/phpunit --configuration phpunit.$TYPE.xml --coverage-clover=coverage.xml + - vendor/bin/phpunit --configuration phpunit.xml --coverage-clover=coverage.xml after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/phpunit.pgsql.xml b/phpunit.pgsql.xml deleted file mode 100644 index 3eab7c62..00000000 --- a/phpunit.pgsql.xml +++ /dev/null @@ -1,62 +0,0 @@ - - - - - - - - - - - - ./tests/B8Framework - - - - ./tests/PHPCensor/Helper - - - ./tests/PHPCensor/Controller - - - ./tests/PHPCensor/Logging - - - ./tests/PHPCensor/Model - - - ./tests/PHPCensor/Plugin - - - ./tests/PHPCensor/Service - - - ./tests/PHPCensor/Command - - - ./tests/PHPCensor/ProcessControl - - - ./tests/PHPCensor/Security - - - - - ./src - - ./src/PHPCensor/Migrations - ./src/PHPCensor/Languages - - - - diff --git a/phpunit.mysql.xml b/phpunit.xml similarity index 88% rename from phpunit.mysql.xml rename to phpunit.xml index 74bdde60..a77aa947 100644 --- a/phpunit.mysql.xml +++ b/phpunit.xml @@ -12,10 +12,12 @@ bootstrap="./tests/bootstrap.php" > - - - - + + + + + + diff --git a/src/B8Framework/Database.php b/src/B8Framework/Database.php index 15cda019..eef6c2b9 100644 --- a/src/B8Framework/Database.php +++ b/src/B8Framework/Database.php @@ -103,7 +103,7 @@ class Database extends \PDO } // No connection? Oh dear. - if (!$connection && $type == 'read') { + if (!$connection && $type === 'read') { throw new \Exception('Could not connect to any ' . $type . ' servers.'); } diff --git a/tests/B8Framework/DatabaseTest.php b/tests/B8Framework/DatabaseMysqlTest.php similarity index 80% rename from tests/B8Framework/DatabaseTest.php rename to tests/B8Framework/DatabaseMysqlTest.php index 7a353e8d..be649d9f 100755 --- a/tests/B8Framework/DatabaseTest.php +++ b/tests/B8Framework/DatabaseMysqlTest.php @@ -4,8 +4,9 @@ namespace Tests\b8; use b8\Config; use b8\Database; +use PHPUnit\Framework\TestCase; -class DatabaseTest extends \PHPUnit\Framework\TestCase +class DatabaseMysqlTest extends TestCase { protected function setUp() { @@ -20,13 +21,14 @@ class DatabaseTest extends \PHPUnit\Framework\TestCase ['host' => 'localhost'], ], ], - 'type' => DB_TYPE, - 'name' => DB_NAME, - 'username' => DB_USER, - 'password' => DB_PASS, + 'type' => 'mysql', + 'name' => MYSQL_DBNAME, + 'username' => MYSQL_USER, + 'password' => MYSQL_PASSWORD, ], ], ]); + Database::reset(); } protected function checkDatabaseConnection() @@ -56,9 +58,9 @@ class DatabaseTest extends \PHPUnit\Framework\TestCase $details = Database::getConnection('read')->getDetails(); self::assertTrue(is_array($details)); - self::assertTrue(($details['db'] == DB_NAME)); - self::assertTrue(($details['user'] == DB_USER)); - self::assertTrue(($details['pass'] == DB_PASS)); + self::assertTrue(($details['db'] === MYSQL_DBNAME)); + self::assertTrue(($details['user'] === MYSQL_USER)); + self::assertTrue(($details['pass'] === MYSQL_PASSWORD)); } /** @@ -81,7 +83,7 @@ class DatabaseTest extends \PHPUnit\Framework\TestCase ['host' => 'localhost'], ], ], - 'type' => DB_TYPE, + 'type' => 'mysql', 'name' => 'b8_test_2', 'username' => '', 'password' => '', diff --git a/tests/B8Framework/DatabasePostgresqlTest.php b/tests/B8Framework/DatabasePostgresqlTest.php new file mode 100755 index 00000000..5e7c1fde --- /dev/null +++ b/tests/B8Framework/DatabasePostgresqlTest.php @@ -0,0 +1,96 @@ + [ + 'database' => [ + 'servers' => [ + 'read' => [ + ['host' => 'localhost'], + ], + 'write' => [ + ['host' => 'localhost'], + ], + ], + 'type' => 'pgsql', + 'name' => POSTGRESQL_DBNAME, + 'username' => POSTGRESQL_USER, + 'password' => POSTGRESQL_PASSWORD, + ], + ], + ]); + Database::reset(); + } + + 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; + } + } + } + + public function testGetWriteConnection() + { + $this->checkDatabaseConnection(); + + $connection = Database::getConnection('write'); + self::assertInstanceOf('\b8\Database', $connection); + } + + public function testGetDetails() + { + $this->checkDatabaseConnection(); + + $details = Database::getConnection('read')->getDetails(); + self::assertTrue(is_array($details)); + self::assertTrue(($details['db'] === POSTGRESQL_DBNAME)); + self::assertTrue(($details['user'] === POSTGRESQL_USER)); + self::assertTrue(($details['pass'] === POSTGRESQL_PASSWORD)); + } + + /** + * @expectedException \Exception + */ + public function testConnectionFailure() + { + $this->checkDatabaseConnection(); + + Database::reset(); + + $config = new Config([ + 'b8' => [ + 'database' => [ + 'servers' => [ + 'read' => [ + ['host' => 'localhost'], + ], + 'write' => [ + ['host' => 'localhost'], + ], + ], + 'type' => 'pgsql', + 'name' => 'b8_test_2', + 'username' => '', + 'password' => '', + ], + ], + ]); + + Database::getConnection('read'); + } +} diff --git a/tests/PHPCensor/Plugin/Util/PhpUnitResultTest.php b/tests/PHPCensor/Plugin/Util/PhpUnitResultTest.php index 58d119b8..9eeaa50a 100644 --- a/tests/PHPCensor/Plugin/Util/PhpUnitResultTest.php +++ b/tests/PHPCensor/Plugin/Util/PhpUnitResultTest.php @@ -83,7 +83,7 @@ class PhpUnitResultTest extends \PHPUnit\Framework\TestCase public static function getTestData() { return [ - 'json' => [PhpUnitResultJson::class, 'tests/PHPCensor/Plugin/SampleFiles/phpunit_money.txt'], + 'json' => [PhpUnitResultJson::class, 'tests/PHPCensor/Plugin/SampleFiles/phpunit_money.txt'], 'junit' => [PhpUnitResultJunit::class, 'tests/PHPCensor/Plugin/SampleFiles/phpunit_money_junit.xml'], ]; } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f308bdc6..85a5c392 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -45,7 +45,10 @@ if (!defined('APP_URL') && !empty($config)) { \PHPCensor\Helper\Lang::init($config, 'en'); -define('DB_NAME', getenv('DB_NAME')); -define('DB_USER', getenv('DB_USER')); -define('DB_TYPE', getenv('DB_TYPE')); -define('DB_PASS', getenv('DB_PASS')); +define('MYSQL_DBNAME', getenv('MYSQL_DBNAME')); +define('MYSQL_USER', getenv('MYSQL_USER')); +define('MYSQL_PASSWORD', getenv('MYSQL_PASSWORD')); + +define('POSTGRESQL_DBNAME', getenv('POSTGRESQL_DBNAME')); +define('POSTGRESQL_USER', getenv('POSTGRESQL_USER')); +define('POSTGRESQL_PASSWORD', getenv('POSTGRESQL_PASSWORD'));