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 * @package Query
* @author Timothy J. Warren * @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014 * @copyright Copyright (c) 2012 - 2015
* @link https://github.com/aviat4ion/Query * @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license * @license http://philsturgeon.co.uk/code/dbad-license
*/ */
@ -25,25 +25,32 @@ namespace Query;
* Reference to root path * Reference to root path
* @subpackage Core * @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 * Path to driver classes
* @subpackage Core * @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 some common functions
require(QBASE_PATH.'common.php'); require(QBASE_PATH.'common.php');
// Load Query Classes // Load Query Classes
spl_autoload_register(function ($class) spl_autoload_register(function ($class) {
{
// Load by namespace // Load by namespace
$path = str_replace('\\', DIRECTORY_SEPARATOR, $class); $path = str_replace('\\', DIRECTORY_SEPARATOR, $class);
$file = QBASE_PATH . "{$path}.php"; $file = QBASE_PATH . "{$path}.php";
if (file_exists($file)) require_once($file); if (file_exists($file))
{
require_once($file);
}
}); });
// End of autoload.php // End of autoload.php

104
build.xml
View File

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

View File

@ -97,6 +97,14 @@
</source> </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> </enrich>
<!-- <build engine="..." enabled="true" output="..." /> --> <!-- <build engine="..." enabled="true" output="..." /> -->

View File

@ -114,9 +114,7 @@ class Util extends \Query\AbstractUtil {
$sql_array[] = $r['sql']; $sql_array[] = $r['sql'];
} }
$sql_structure = implode(";\n", $sql_array) . ";"; return implode(";\n", $sql_array) . ";";
return $sql_structure;
} }
} }
// End of sqlite_util.php // 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 * Base class for TestCases
*/ */
@ -70,39 +98,29 @@ class Query_TestCase extends PHPUnit_Framework_TestCase {
{ {
if (is_object($first)) if (is_object($first))
{ {
$res = ($first === $second); $res = ($first === $second);
} }
else else
{ {
$temp = $first; $temp = $first;
$first = uniqid("test"); $first = uniqid("test");
$is_ref = ($first === $second); $is_ref = ($first === $second);
$first = $temp; $first = $temp;
$res = $is_ref; $res = $is_ref;
} }
$this->assertTrue($res, $message); $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'; $path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db';
@unlink($path); @unlink($path);
// Include db classes
require_once(QBASE_DIR . 'autoload.php');
// Require base testing classes // Require base testing classes
//require_once(QTEST_DIR . '/core/core_test.php'); //require_once(QTEST_DIR . '/core/core_test.php');
require_once(QTEST_DIR . '/core/base_db_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/query_parser_test.php');
require_once(QTEST_DIR . '/core/base_query_builder_test.php'); require_once(QTEST_DIR . '/core/base_query_builder_test.php');
// End of bootstrap.php // End of bootstrap.php

View File

@ -2,7 +2,7 @@
class Connection_Manager_Test extends Query_TestCase { class Connection_Manager_Test extends Query_TestCase {
static $instance = NULL; protected static $instance = NULL;
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
@ -98,12 +98,5 @@ class Connection_Manager_Test extends Query_TestCase {
$this->assertEqual($conn, self::$instance->get_connection('conn_manager')); $this->assertEqual($conn, self::$instance->get_connection('conn_manager'));
} }
// --------------------------------------------------------------------------
public function testCreateDsn()
{
}
} }
// End of connection_manager_test.php // End of connection_manager_test.php

View File

@ -20,16 +20,8 @@ class MySQLQBTest extends QBTest {
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
// Attempt to connect, if there is a test config file $params = get_json_config();
if (is_file(QTEST_DIR . "/settings.json")) if (($var = getenv('CI'))) // Travis CI Connection Info
{
$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 = array( $params = array(
'host' => '127.0.0.1', 'host' => '127.0.0.1',
@ -41,6 +33,14 @@ class MySQLQBTest extends QBTest {
'type' => 'mysql' '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); self::$db = Query($params);
} }

View File

@ -23,20 +23,20 @@ class MySQLTest extends DBTest {
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
// Attempt to connect, if there is a test config file $params = get_json_config();
if (is_file(QTEST_DIR . "/settings.json")) 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; $params = $params->mysql;
self::$db = new \Query\Drivers\Mysql\Driver("mysql:host={$params->host};dbname={$params->database}", $params->user, $params->pass, array( self::$db = new \Query\Drivers\Mysql\Driver("mysql:host={$params->host};dbname={$params->database}", $params->user, $params->pass, array(
PDO::ATTR_PERSISTENT => TRUE 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_'); self::$db->set_table_prefix('create_');
} }

View File

@ -19,18 +19,8 @@ class PgSQLQBTest extends QBTest {
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
// Attempt to connect, if there is a test config file $params = get_json_config();
if (is_file(QTEST_DIR . "/settings.json")) if (getenv('CI')) // Travis CI Connection Info
{
$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 = array( $params = array(
'host' => '127.0.0.1', 'host' => '127.0.0.1',
@ -42,6 +32,16 @@ class PgSQLQBTest extends QBTest {
'prefix' => 'create_' '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); self::$db = Query($params);
} }

View File

@ -29,25 +29,23 @@ class PgTest extends DBTest {
{ {
$this->markTestSkipped("Postgres extension for PDO not loaded"); $this->markTestSkipped("Postgres extension for PDO not loaded");
} }
} }
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
$class = "Query\\Drivers\\Pgsql\\Driver"; $class = "Query\\Drivers\\Pgsql\\Driver";
// Attempt to connect, if there is a test config file $params = get_json_config();
if (is_file(QTEST_DIR . "/settings.json")) if (($var = getenv('CI')))
{
$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')))
{ {
self::$db = new $class('host=127.0.0.1;port=5432;dbname=test', 'postgres'); 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_'); 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
}
}