From a1c3b9e7db3f6bba83b85240604151a9985546bb Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Tue, 10 Nov 2015 16:29:17 -0500 Subject: [PATCH] Change test setup to be more extensible --- autoload.php | 21 +++-- build.xml | 104 ++++++++++++------------- build/phpdox.xml | 8 ++ src/Query/Drivers/Sqlite/Util.php | 4 +- src/Query/QueryParser.php | 2 +- tests/bootstrap.php | 60 +++++++++----- tests/core/connection_manager_test.php | 9 +-- tests/databases/mysql/MySQLQBTest.php | 20 ++--- tests/databases/mysql/MySQLTest.php | 14 ++-- tests/databases/pgsql/PgSQLQBTest.php | 24 +++--- tests/databases/pgsql/PgSQLTest.php | 18 ++--- tests/settings.json.dist | 20 +++++ 12 files changed, 172 insertions(+), 132 deletions(-) create mode 100644 tests/settings.json.dist diff --git a/autoload.php b/autoload.php index 52e0f05..405933b 100644 --- a/autoload.php +++ b/autoload.php @@ -6,7 +6,7 @@ * * @package Query * @author Timothy J. Warren - * @copyright Copyright (c) 2012 - 2014 + * @copyright Copyright (c) 2012 - 2015 * @link https://github.com/aviat4ion/Query * @license http://philsturgeon.co.uk/code/dbad-license */ @@ -25,25 +25,32 @@ namespace Query; * Reference to root path * @subpackage Core */ -if ( ! defined('QBASE_PATH')) define('QBASE_PATH', dirname(__FILE__).'/src/'); - +if ( ! defined('QBASE_PATH')) +{ + define('QBASE_PATH', __DIR__.'/src/'); +} /** * Path to driver classes * @subpackage Core */ -if ( ! defined('QDRIVER_PATH')) define('QDRIVER_PATH', QBASE_PATH.'drivers/'); +if ( ! defined('QDRIVER_PATH')) +{ + define('QDRIVER_PATH', QBASE_PATH.'drivers/'); +} // Require some common functions require(QBASE_PATH.'common.php'); // Load Query Classes -spl_autoload_register(function ($class) -{ +spl_autoload_register(function ($class) { // Load by namespace $path = str_replace('\\', DIRECTORY_SEPARATOR, $class); $file = QBASE_PATH . "{$path}.php"; - if (file_exists($file)) require_once($file); + if (file_exists($file)) + { + require_once($file); + } }); // End of autoload.php \ No newline at end of file diff --git a/build.xml b/build.xml index ac8a30a..427fde8 100644 --- a/build.xml +++ b/build.xml @@ -1,5 +1,5 @@ - + @@ -10,23 +10,23 @@ - + - + - - - - - + + + + + @@ -73,11 +73,11 @@ unless="prepare.done" depends="clean" description="Prepare for build"> - - - - - + + + + + @@ -87,14 +87,12 @@ - + - - + - @@ -106,8 +104,8 @@ description="Measure project size using PHPLOC and print human readable output. Intended for usage on the command line."> - - + + @@ -120,11 +118,11 @@ - + - - - + + + @@ -135,10 +133,10 @@ depends="prepare" description="Calculate software metrics using PHP_Depend and log result in XML format. Intended for usage within a continuous integration environment."> - - - - + + + + @@ -151,8 +149,8 @@ - - + + @@ -164,12 +162,12 @@ description="Find coding standard violations using PHP_CodeSniffer and log result in XML format. Intended for usage within a continuous integration environment."> - + - - + + @@ -179,7 +177,7 @@ unless="phpcpd.done" description="Find duplicate code using PHPCPD and print human readable output. Intended for usage on the command line before committing."> - + @@ -191,8 +189,8 @@ description="Find duplicate code using PHPCPD and log result in XML format. Intended for usage within a continuous integration environment."> - - + + @@ -202,9 +200,9 @@ unless="phpunit.done" depends="prepare" description="Run unit tests with PHPUnit"> - + - + @@ -216,7 +214,7 @@ description="Run unit tests with PHPUnit (without generating code coverage reports)"> - + @@ -227,7 +225,7 @@ unless="phpdox.done" depends="phploc-ci,phpcs-ci,phpunit" description="Generate project documentation using phpDox"> - + diff --git a/build/phpdox.xml b/build/phpdox.xml index a1592f7..a5f4e9c 100644 --- a/build/phpdox.xml +++ b/build/phpdox.xml @@ -97,6 +97,14 @@ --> + + + + + + + + diff --git a/src/Query/Drivers/Sqlite/Util.php b/src/Query/Drivers/Sqlite/Util.php index 088309a..a8e3994 100644 --- a/src/Query/Drivers/Sqlite/Util.php +++ b/src/Query/Drivers/Sqlite/Util.php @@ -114,9 +114,7 @@ class Util extends \Query\AbstractUtil { $sql_array[] = $r['sql']; } - $sql_structure = implode(";\n", $sql_array) . ";"; - - return $sql_structure; + return implode(";\n", $sql_array) . ";"; } } // End of sqlite_util.php \ No newline at end of file diff --git a/src/Query/QueryParser.php b/src/Query/QueryParser.php index 89478e8..8f0c072 100644 --- a/src/Query/QueryParser.php +++ b/src/Query/QueryParser.php @@ -135,4 +135,4 @@ class QueryParser { } -// End of query_parser.php \ No newline at end of file +// End of QueryParser.php \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f42f994..2927f3e 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -30,6 +30,34 @@ if ( ! defined('IS_QUERCUS')) } } +/** + * Unit test bootstrap - Using phpunit + */ +define('QTEST_DIR', realpath(__DIR__)); +define('QBASE_DIR', realpath(QTEST_DIR.'/../') . '/'); +define('QDS', DIRECTORY_SEPARATOR); + +// Include db classes +require_once(QBASE_DIR . 'autoload.php'); + +function get_json_config() +{ + $files = array( + __DIR__ . '/settings.json', + __DIR__ . '/settings.json.dist' + ); + + foreach($files as $file) + { + if (is_file($file)) + { + return json_decode(file_get_contents($file)); + } + } + + return FALSE; +} + /** * Base class for TestCases */ @@ -70,39 +98,29 @@ class Query_TestCase extends PHPUnit_Framework_TestCase { { if (is_object($first)) { - $res = ($first === $second); - } - else - { + $res = ($first === $second); + } + else + { $temp = $first; - $first = uniqid("test"); - $is_ref = ($first === $second); - $first = $temp; - $res = $is_ref; - } - $this->assertTrue($res, $message); + $first = uniqid("test"); + $is_ref = ($first === $second); + $first = $temp; + $res = $is_ref; + } + $this->assertTrue($res, $message); } } // -------------------------------------------------------------------------- - -/** - * Unit test bootstrap - Using phpunit - */ -define('QTEST_DIR', realpath(dirname(__FILE__))); -define('QBASE_DIR', realpath(QTEST_DIR.'/../') . '/'); -define('QDS', DIRECTORY_SEPARATOR); - $path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'; @unlink($path); -// Include db classes -require_once(QBASE_DIR . 'autoload.php'); - // Require base testing classes //require_once(QTEST_DIR . '/core/core_test.php'); require_once(QTEST_DIR . '/core/base_db_test.php'); //require_once(QTEST_DIR . '/core/query_parser_test.php'); require_once(QTEST_DIR . '/core/base_query_builder_test.php'); + // End of bootstrap.php \ No newline at end of file diff --git a/tests/core/connection_manager_test.php b/tests/core/connection_manager_test.php index c803ef1..2115922 100644 --- a/tests/core/connection_manager_test.php +++ b/tests/core/connection_manager_test.php @@ -2,7 +2,7 @@ class Connection_Manager_Test extends Query_TestCase { - static $instance = NULL; + protected static $instance = NULL; public static function setUpBeforeClass() { @@ -98,12 +98,5 @@ class Connection_Manager_Test extends Query_TestCase { $this->assertEqual($conn, self::$instance->get_connection('conn_manager')); } - - // -------------------------------------------------------------------------- - - public function testCreateDsn() - { - - } } // End of connection_manager_test.php \ No newline at end of file diff --git a/tests/databases/mysql/MySQLQBTest.php b/tests/databases/mysql/MySQLQBTest.php index 50770ea..85e384e 100644 --- a/tests/databases/mysql/MySQLQBTest.php +++ b/tests/databases/mysql/MySQLQBTest.php @@ -20,16 +20,8 @@ class MySQLQBTest extends QBTest { public static function setUpBeforeClass() { - // Attempt to connect, if there is a test config file - if (is_file(QTEST_DIR . "/settings.json")) - { - $params = json_decode(file_get_contents(QTEST_DIR . "/settings.json")); - $params = $params->mysql; - $params->type = "MySQL"; - $params->options = array(); - $params->options[PDO::ATTR_PERSISTENT] = TRUE; - } - elseif (($var = getenv('CI'))) // Travis CI Connection Info + $params = get_json_config(); + if (($var = getenv('CI'))) // Travis CI Connection Info { $params = array( 'host' => '127.0.0.1', @@ -41,6 +33,14 @@ class MySQLQBTest extends QBTest { 'type' => 'mysql' ); } + // Attempt to connect, if there is a test config file + else if ($params !== FALSE) + { + $params = $params->mysql; + $params->type = "MySQL"; + $params->options = array(); + $params->options[PDO::ATTR_PERSISTENT] = TRUE; + } self::$db = Query($params); } diff --git a/tests/databases/mysql/MySQLTest.php b/tests/databases/mysql/MySQLTest.php index c7d232f..a2a008d 100644 --- a/tests/databases/mysql/MySQLTest.php +++ b/tests/databases/mysql/MySQLTest.php @@ -23,20 +23,20 @@ class MySQLTest extends DBTest { public static function setUpBeforeClass() { - // Attempt to connect, if there is a test config file - if (is_file(QTEST_DIR . "/settings.json")) + $params = get_json_config(); + if (($var = getenv('CI'))) + { + self::$db = new \Query\Drivers\Mysql\Driver('host=127.0.0.1;port=3306;dbname=test', 'root'); + } + // Attempt to connect, if there is a test config file + else if ($params !== FALSE) { - $params = json_decode(file_get_contents(QTEST_DIR . "/settings.json")); $params = $params->mysql; self::$db = new \Query\Drivers\Mysql\Driver("mysql:host={$params->host};dbname={$params->database}", $params->user, $params->pass, array( PDO::ATTR_PERSISTENT => TRUE )); } - elseif (($var = getenv('CI'))) - { - self::$db = new \Query\Drivers\Mysql\Driver('host=127.0.0.1;port=3306;dbname=test', 'root'); - } self::$db->set_table_prefix('create_'); } diff --git a/tests/databases/pgsql/PgSQLQBTest.php b/tests/databases/pgsql/PgSQLQBTest.php index 57b9775..f714d06 100644 --- a/tests/databases/pgsql/PgSQLQBTest.php +++ b/tests/databases/pgsql/PgSQLQBTest.php @@ -19,18 +19,8 @@ class PgSQLQBTest extends QBTest { public static function setUpBeforeClass() { - // Attempt to connect, if there is a test config file - if (is_file(QTEST_DIR . "/settings.json")) - { - $params = json_decode(file_get_contents(QTEST_DIR . "/settings.json")); - $params = $params->pgsql; - $params->type = "pgsql"; - $params->port = 5432; - $params->prefix = 'create_'; - $params->options = array(); - $params->options[\PDO::ATTR_PERSISTENT] = TRUE; - } - elseif (getenv('CI')) // Travis CI Connection Info + $params = get_json_config(); + if (getenv('CI')) // Travis CI Connection Info { $params = array( 'host' => '127.0.0.1', @@ -42,6 +32,16 @@ class PgSQLQBTest extends QBTest { 'prefix' => 'create_' ); } + // Attempt to connect, if there is a test config file + else if ($params !== FALSE) + { + $params = $params->pgsql; + $params->type = "pgsql"; + //$params->port = 5432; + //$params->prefix = 'create_'; + $params->options = array(); + $params->options[\PDO::ATTR_PERSISTENT] = TRUE; + } self::$db = Query($params); } diff --git a/tests/databases/pgsql/PgSQLTest.php b/tests/databases/pgsql/PgSQLTest.php index d3b3752..3893e0a 100644 --- a/tests/databases/pgsql/PgSQLTest.php +++ b/tests/databases/pgsql/PgSQLTest.php @@ -29,25 +29,23 @@ class PgTest extends DBTest { { $this->markTestSkipped("Postgres extension for PDO not loaded"); } - } public static function setUpBeforeClass() { $class = "Query\\Drivers\\Pgsql\\Driver"; - // Attempt to connect, if there is a test config file - if (is_file(QTEST_DIR . "/settings.json")) - { - $params = json_decode(file_get_contents(QTEST_DIR . "/settings.json")); - $params = $params->pgsql; - - self::$db = new $class("pgsql:dbname={$params->database};port=5432", $params->user, $params->pass); - } - elseif (($var = getenv('CI'))) + $params = get_json_config(); + if (($var = getenv('CI'))) { self::$db = new $class('host=127.0.0.1;port=5432;dbname=test', 'postgres'); } + // Attempt to connect, if there is a test config file + else if ($params !== FALSE) + { + $params = $params->pgsql; + self::$db = new $class("pgsql:dbname={$params->database};port=5432", $params->user, $params->pass); + } self::$db->set_table_prefix('create_'); } diff --git a/tests/settings.json.dist b/tests/settings.json.dist new file mode 100644 index 0000000..f82bcd3 --- /dev/null +++ b/tests/settings.json.dist @@ -0,0 +1,20 @@ +{ + "mysql": { + "host":"localhost", + "database":"test", + "user":"test", + "name":"my", + "alias":"my", + "prefix":"create_", + "pass":"" + }, + "pgsql": { + "host":"localhost", + "database":"test", + "name":"pg", + "user":"test", + "pass":"test", + "prefix":"create_", + "port":5432 + } +} \ No newline at end of file