Change test setup to be more extensible

This commit is contained in:
Timothy Warren 2015-11-10 16:29:17 -05:00
parent 1e2a7ee566
commit a1c3b9e7db
12 changed files with 172 additions and 132 deletions

View File

@ -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

104
build.xml
View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="animeclient" default="full-build">
<project name="query" default="full-build">
<!-- By default, we assume all tools to be on the $PATH -->
<property name="pdepend" value="pdepend"/>
<property name="phpcpd" value="phpcpd"/>
@ -10,23 +10,23 @@
<property name="phpunit" value="phpunit"/>
<property name="sonar" value="sonar-runner"/>
<!-- Use this when the tools are located as PHARs in ${basedir}/build/tools
<property name="pdepend" value="${basedir}/build/tools/pdepend.phar"/>
<property name="phpcpd" value="${basedir}/build/tools/phpcpd.phar"/>
<property name="phpcs" value="${basedir}/build/tools/phpcs.phar"/>
<property name="phpdox" value="${basedir}/build/tools/phpdox.phar"/>
<property name="phploc" value="${basedir}/build/tools/phploc.phar"/>
<property name="phpmd" value="${basedir}/build/tools/phpmd.phar"/>
<property name="phpunit" value="${basedir}/build/tools/phpunit.phar"/> -->
<!-- Use this when the tools are located as PHARs in build/tools
<property name="pdepend" value="build/tools/pdepend.phar"/>
<property name="phpcpd" value="build/tools/phpcpd.phar"/>
<property name="phpcs" value="build/tools/phpcs.phar"/>
<property name="phpdox" value="build/tools/phpdox.phar"/>
<property name="phploc" value="build/tools/phploc.phar"/>
<property name="phpmd" value="build/tools/phpmd.phar"/>
<property name="phpunit" value="build/tools/phpunit.phar"/> -->
<!-- Use this when the tools are managed by Composer in ${basedir}/vendor/bin
<property name="pdepend" value="${basedir}/vendor/bin/pdepend"/>
<property name="phpcpd" value="${basedir}/vendor/bin/phpcpd"/>
<property name="phpcs" value="${basedir}/vendor/bin/phpcs"/>
<property name="phpdox" value="${basedir}/vendor/bin/phpdox"/>
<property name="phploc" value="${basedir}/vendor/bin/phploc"/>
<property name="phpmd" value="${basedir}/vendor/bin/phpmd"/>
<property name="phpunit" value="${basedir}/vendor/bin/phpunit"/> -->
<!-- Use this when the tools are managed by Composer in vendor/bin
<property name="pdepend" value="vendor/bin/pdepend"/>
<property name="phpcpd" value="vendor/bin/phpcpd"/>
<property name="phpcs" value="vendor/bin/phpcs"/>
<property name="phpdox" value="vendor/bin/phpdox"/>
<property name="phploc" value="vendor/bin/phploc"/>
<property name="phpmd" value="vendor/bin/phpmd"/>
<property name="phpunit" value="vendor/bin/phpunit"/> -->
<target name="full-build"
depends="prepare,static-analysis,phpunit,phpdox,sonar,-check-failure"
@ -61,11 +61,11 @@
<target name="clean"
unless="clean.done"
description="Cleanup build artifacts">
<delete dir="${basedir}/build/api"/>
<delete dir="${basedir}/build/coverage"/>
<delete dir="${basedir}/build/logs"/>
<delete dir="${basedir}/build/pdepend"/>
<delete dir="${basedir}/build/phpdox"/>
<delete dir="build/api"/>
<delete dir="build/coverage"/>
<delete dir="build/logs"/>
<delete dir="build/pdepend"/>
<delete dir="build/phpdox"/>
<property name="clean.done" value="true"/>
</target>
@ -73,11 +73,11 @@
unless="prepare.done"
depends="clean"
description="Prepare for build">
<mkdir dir="${basedir}/build/api"/>
<mkdir dir="${basedir}/build/coverage"/>
<mkdir dir="${basedir}/build/logs"/>
<mkdir dir="${basedir}/build/pdepend"/>
<mkdir dir="${basedir}/build/phpdox"/>
<mkdir dir="build/api"/>
<mkdir dir="build/coverage"/>
<mkdir dir="build/logs"/>
<mkdir dir="build/pdepend"/>
<mkdir dir="build/phpdox"/>
<property name="prepare.done" value="true"/>
</target>
@ -87,14 +87,12 @@
<apply executable="php" taskname="lint">
<arg value="-l" />
<fileset dir="${basedir}/src">
<fileset dir="src">
<include name="**/*.php" />
<modified />
</fileset>
<fileset dir="${basedir}/tests">
<fileset dir="tests">
<include name="**/*.php" />
<modified />
</fileset>
</apply>
@ -106,8 +104,8 @@
description="Measure project size using PHPLOC and print human readable output. Intended for usage on the command line.">
<exec executable="${phploc}" taskname="phploc">
<arg value="--count-tests" />
<arg path="${basedir}/src" />
<arg path="${basedir}/tests" />
<arg path="src" />
<arg path="tests" />
</exec>
<property name="phploc.done" value="true"/>
@ -120,11 +118,11 @@
<exec executable="${phploc}" taskname="phploc">
<arg value="--count-tests" />
<arg value="--log-csv" />
<arg path="${basedir}/build/logs/phploc.csv" />
<arg path="build/logs/phploc.csv" />
<arg value="--log-xml" />
<arg path="${basedir}/build/logs/phploc.xml" />
<arg path="${basedir}/src" />
<arg path="${basedir}/tests" />
<arg path="build/logs/phploc.xml" />
<arg path="src" />
<arg path="tests" />
</exec>
<property name="phploc.done" value="true"/>
@ -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.">
<exec executable="${pdepend}" taskname="pdepend">
<arg value="--jdepend-xml=${basedir}/build/logs/jdepend.xml" />
<arg value="--jdepend-chart=${basedir}/build/pdepend/dependencies.svg" />
<arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramid.svg" />
<arg path="${basedir}/src" />
<arg value="--jdepend-xml=build/logs/jdepend.xml" />
<arg value="--jdepend-chart=build/pdepend/dependencies.svg" />
<arg value="--overview-pyramid=build/pdepend/overview-pyramid.svg" />
<arg path="src" />
</exec>
<property name="pdepend.done" value="true"/>
@ -151,8 +149,8 @@
<arg value="--standard=PSR2" />
<arg value="--extensions=php" />
<arg value="--ignore=autoload.php" />
<arg path="${basedir}/src" />
<arg path="${basedir}/tests" />
<arg path="src" />
<arg path="tests" />
</exec>
<property name="phpcs.done" value="true"/>
@ -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.">
<exec executable="${phpcs}" output="/dev/null" taskname="phpcs">
<arg value="--report=checkstyle" />
<arg value="--report-file=${basedir}/build/logs/checkstyle.xml" />
<arg value="--report-file=build/logs/checkstyle.xml" />
<arg value="--standard=PSR2" />
<arg value="--extensions=php" />
<arg value="--ignore=autoload.php" />
<arg path="${basedir}/src" />
<arg path="${basedir}/tests" />
<arg path="src" />
<arg path="tests" />
</exec>
<property name="phpcs.done" value="true"/>
@ -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.">
<exec executable="${phpcpd}" taskname="phpcpd">
<arg path="${basedir}/src" />
<arg path="src" />
</exec>
<property name="phpcpd.done" value="true"/>
@ -191,8 +189,8 @@
description="Find duplicate code using PHPCPD and log result in XML format. Intended for usage within a continuous integration environment.">
<exec executable="${phpcpd}" taskname="phpcpd">
<arg value="--log-pmd" />
<arg path="${basedir}/build/logs/pmd-cpd.xml" />
<arg path="${basedir}/src" />
<arg path="build/logs/pmd-cpd.xml" />
<arg path="src" />
</exec>
<property name="phpcpd.done" value="true"/>
@ -202,9 +200,9 @@
unless="phpunit.done"
depends="prepare"
description="Run unit tests with PHPUnit">
<exec executable="${phpunit}" resultproperty="result.phpunit" taskname="phpunit">
<exec executable="${phpunit}" taskname="phpunit">
<arg value="--configuration"/>
<arg path="${basedir}/build/phpunit.xml"/>
<arg path="build/phpunit.xml"/>
</exec>
<property name="phpunit.done" value="true"/>
@ -216,7 +214,7 @@
description="Run unit tests with PHPUnit (without generating code coverage reports)">
<exec executable="${phpunit}" failonerror="true" taskname="phpunit">
<arg value="--configuration"/>
<arg path="${basedir}/build/phpunit.xml"/>
<arg path="build/phpunit.xml"/>
<arg value="--no-coverage"/>
</exec>
@ -227,7 +225,7 @@
unless="phpdox.done"
depends="phploc-ci,phpcs-ci,phpunit"
description="Generate project documentation using phpDox">
<exec executable="${phpdox}" dir="${basedir}/build" taskname="phpdox"/>
<exec executable="${phpdox}" dir="build" taskname="phpdox"/>
<property name="phpdox.done" value="true"/>
</target>

View File

@ -97,6 +97,14 @@
</source>
-->
<!-- PHPUnit Coverage XML -->
<source type="phpunit">
<!-- <coverage path="clover.xml" />-->
<!-- @path - the directory where the xml code coverage report can be found -->
<!--<filter directory="${phpDox.project.source}" />-->
<!-- @directory - path of the phpunit config whitelist filter directory -->
</source>
</enrich>
<!-- <build engine="..." enabled="true" output="..." /> -->

View File

@ -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

View File

@ -135,4 +135,4 @@ class QueryParser {
}
// End of query_parser.php
// End of QueryParser.php

View File

@ -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

View File

@ -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

View File

@ -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);
}

View File

@ -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_');
}

View File

@ -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);
}

View File

@ -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_');
}

20
tests/settings.json.dist Normal file
View File

@ -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
}
}