Skip to content
Snippets Groups Projects
Commit cae50c0e authored by Taddeus Kroes's avatar Taddeus Kroes
Browse files

Converted unit tests to PHPUnit.

parent 593e7f77
No related branches found
No related tags found
No related merge requests found
docs docs
test/coverage
*.swp
*.~
<?php <?php
include_once 'config.php';
__p::load_plugin('array'); __p::load_plugin('array');
class pQueryArrayTest extends UnitTestCase { class pQueryArrayTest extends PHPUnit_Framework_TestCase {
var $variable; var $variable;
var $arr; var $arr;
function __construct() {
parent::__construct('pQuery array plugin');
}
function setUp() { function setUp() {
$this->variable = array('test'); $this->variable = array('test');
$this->arr = _arr($this->variable); $this->arr = _arr($this->variable);
} }
function test_constructor() { function test_constructor() {
$this->assertIsA($this->arr, 'pQueryArray', 'constructor does not return pQueryArray object.'); $this->assertTrue($this->arr instanceof pQueryArray, 'constructor does not return pQueryArray object.');
$this->assertEqual($this->arr->variable, $this->variable, 'variable is not set correctly.'); $this->assertEquals($this->arr->variable, $this->variable, 'variable is not set correctly.');
} }
function test_get_simple() { function test_get_simple() {
$this->assertEqual($this->arr->get(0), $this->variable[0]); $this->assertEquals($this->arr->get(0), $this->variable[0]);
} }
function test_get_non_existent() { function test_get_non_existent() {
...@@ -40,17 +37,17 @@ class pQueryArrayTest extends UnitTestCase { ...@@ -40,17 +37,17 @@ class pQueryArrayTest extends UnitTestCase {
$orginal = range(1, 4); $orginal = range(1, 4);
$reverse = range(4, 1, -1); $reverse = range(4, 1, -1);
$arr = _arr($orginal); $arr = _arr($orginal);
$this->assertEqual($arr->reverse()->variable, $reverse, 'reverse is not really reverse...'); $this->assertEquals($arr->reverse()->variable, $reverse, 'reverse is not really reverse...');
} }
function test_call_count() { function test_call_count() {
$this->assertEqual($this->arr->count(), count($this->variable)); $this->assertEquals($this->arr->count(), count($this->variable));
} }
function test_call_sort() { function test_call_sort() {
$arr = range(1, 8); $arr = range(1, 8);
shuffle($arr); shuffle($arr);
$this->assertEqual(_arr($arr)->sort()->variable, range(1, 8)); $this->assertEquals(_arr($arr)->sort()->variable, range(1, 8));
} }
} }
......
<?php
include_once 'config.php';
class pQueryTest extends PHPUnit_Framework_TestCase {
}
?>
\ No newline at end of file
...@@ -16,4 +16,6 @@ define('PQUERY_DEBUG', true); ...@@ -16,4 +16,6 @@ define('PQUERY_DEBUG', true);
*/ */
define('PQUERY_ROOT', 'D:/xampp/htdocs/pquery/'); define('PQUERY_ROOT', 'D:/xampp/htdocs/pquery/');
include_once PQUERY_ROOT.'pquery.php';
?> ?>
\ No newline at end of file
<?php
include_once '../../simpletest/autorun.php';
include_once 'config.php';
include_once '../pquery.php';
function is_test_file($filename) {
return preg_match('/^test_\w+\.php$/', $filename);
}
foreach( array_filter(scandir('.'), 'is_test_file') as $file )
include_once $file;
?>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnError="true">
<filter>
<whitelist>
<directory>..</directory>
<exclude>
<directory>../test</directory>
<directory>../test-old</directory>
</exclude>
</whitelist>
</filter>
<testsuites>
<testsuite name="pQuery test suite">
<directory suffix=".php">.</directory>
<exclude>
<file>config.php</file>
</exclude>
</testsuite>
</testsuites>
<logging>
<log type="coverage-html" target="coverage" charset="UTF-8" highlight="true" />
</logging>
</phpunit>
\ No newline at end of file
<?php <?php
include_once 'config.php';
__p::load_plugin('sql'); __p::load_plugin('sql');
class pQuerySqlTest extends UnitTestCase { class pQuerySqlTest extends PHPUnit_Framework_TestCase {
function __construct() { function setUp() {
parent::__construct('pQuery MySQL plugin'); __sql::set_login_data('localhost', 'root', '', 'pquery_test');
} }
function tearDown() { function tearDown() {
...@@ -13,177 +14,174 @@ class pQuerySqlTest extends UnitTestCase { ...@@ -13,177 +14,174 @@ class pQuerySqlTest extends UnitTestCase {
} }
function test_set_login_data() { function test_set_login_data() {
$this->tearDown();
__sql::set_login_data('a', 'b', 'c', 'd'); __sql::set_login_data('a', 'b', 'c', 'd');
$this->assertEqual(__sql::$login_data['host'], 'a'); $this->assertEquals(__sql::$login_data['host'], 'a');
$this->assertEqual(__sql::$login_data['username'], 'b'); $this->assertEquals(__sql::$login_data['username'], 'b');
$this->assertEqual(__sql::$login_data['password'], 'c'); $this->assertEquals(__sql::$login_data['password'], 'c');
$this->assertEqual(__sql::$login_data['dbname'], 'd'); $this->assertEquals(__sql::$login_data['dbname'], 'd');
} }
/**
* @expectedException pQueryException
*/
function test_no_login_data() { function test_no_login_data() {
$this->expectException('pQueryException'); $this->tearDown();
__sql::assert_login_data_exist(); __sql::assert_login_data_exist();
} }
function test_query_getter() { function test_query_getter() {
$sql = _sql('foobar'); $sql = _sql('foobar');
$this->assertEqual($sql->variable, 'foobar'); $this->assertEquals($sql->variable, 'foobar');
$this->assertEqual($sql->query, 'foobar'); $this->assertEquals($sql->query, 'foobar');
} }
function test_variable_query() { function test_variable_query() {
self::set_login_data();
$sql = _sql("select id from foo where bar = '[bar]'") $sql = _sql("select id from foo where bar = '[bar]'")
->set(array('bar' => 'test1')); ->set(array('bar' => 'test1'));
$this->assertEqual($sql->query, "select id from foo where bar = 'test1'"); $this->assertEquals($sql->query, "select id from foo where bar = 'test1'");
} }
function test_unescaped_query() { function test_unescaped_query() {
$sql = _sql("select id from foo where bar = '[bar]'") $sql = _sql("select id from foo where bar = '[bar]'")
->set_unescaped(array('bar' => "select id from foo where bar = 'test1'")); ->set_unescaped(array('bar' => "select id from foo where bar = 'test1'"));
$this->assertEqual($sql->query, "select id from foo where bar = 'select id from foo where bar = 'test1''"); $this->assertEquals($sql->query, "select id from foo where bar = 'select id from foo where bar = 'test1''");
} }
function test_escaped_query() { function test_escaped_query() {
self::set_login_data();
$sql = _sql("select id from foo where bar = '[bar]'") $sql = _sql("select id from foo where bar = '[bar]'")
->set(array('bar' => "select id from foo where bar = 'test1'")); ->set(array('bar' => "select id from foo where bar = 'test1'"));
$this->assertNotEqual($sql->query, "select id from foo where bar = 'select id from foo where bar = 'test1''"); $this->assertNotEquals($sql->query, "select id from foo where bar = 'select id from foo where bar = 'test1''");
} }
function test_constructor_simple() { function test_constructor_simple() {
self::set_login_data();
$sql = _sql("select id from foo where bar = '[0]'", 'test1'); $sql = _sql("select id from foo where bar = '[0]'", 'test1');
$this->assertEqual($sql->query, "select id from foo where bar = 'test1'"); $this->assertEquals($sql->query, "select id from foo where bar = 'test1'");
} }
function test_constructor_advanced() { function test_constructor_advanced() {
self::set_login_data();
$sql = _sql("[0] [bar] [foo] [2]", '1', array('bar' => '2', 'foo' => '3'), '4'); $sql = _sql("[0] [bar] [foo] [2]", '1', array('bar' => '2', 'foo' => '3'), '4');
$this->assertEqual($sql->query, "1 2 3 4"); $this->assertEquals($sql->query, "1 2 3 4");
} }
function test_num_rows() { function test_num_rows() {
self::set_login_data();
$sql = _sql("select bar from foo where id in (1, 2)"); $sql = _sql("select bar from foo where id in (1, 2)");
$this->assertEqual($sql->num_rows(), 2); $this->assertEquals($sql->num_rows(), 2);
} }
function test_escape_column_simple() { function test_escape_column_simple() {
$this->assertEqual(__sql::escape_column('foo'), '`foo`'); $this->assertEquals(__sql::escape_column('foo'), '`foo`');
} }
function test_escape_column_escaped() { function test_escape_column_escaped() {
$this->assertEqual(__sql::escape_column('`foo`'), '`foo`'); $this->assertEquals(__sql::escape_column('`foo`'), '`foo`');
} }
function test_escape_column_table() { function test_escape_column_table() {
$this->assertEqual(__sql::escape_column('foo.bar'), '`foo`.`bar`'); $this->assertEquals(__sql::escape_column('foo.bar'), '`foo`.`bar`');
} }
function test_escape_column_aggregate() { function test_escape_column_aggregate() {
$this->assertEqual(__sql::escape_column('count(foo)'), 'COUNT(`foo`)'); $this->assertEquals(__sql::escape_column('count(foo)'), 'COUNT(`foo`)');
} }
function test_escape_column_aggregate_escaped() { function test_escape_column_aggregate_escaped() {
$this->assertEqual(__sql::escape_column('count(`foo`)'), 'COUNT(`foo`)'); $this->assertEquals(__sql::escape_column('count(`foo`)'), 'COUNT(`foo`)');
} }
function test_parse_columns_star() { function test_parse_columns_star() {
$sql = __sql::select('foo', '*', '', false); $sql = __sql::select('foo', '*', '', false);
$this->assertEqual($sql->query, "SELECT * FROM `foo` WHERE 1;"); $this->assertEquals($sql->query, "SELECT * FROM `foo` WHERE 1;");
} }
function test_parse_columns_simple() { function test_parse_columns_simple() {
$sql = __sql::select('foo', array('id', 'bar'), '', false); $sql = __sql::select('foo', array('id', 'bar'), '', false);
$this->assertEqual($sql->query, "SELECT `id`, `bar` FROM `foo` WHERE 1;"); $this->assertEquals($sql->query, "SELECT `id`, `bar` FROM `foo` WHERE 1;");
} }
function test_parse_columns_as() { function test_parse_columns_as() {
$sql = __sql::select('foo', array('id' => 'foo_id'), '', false); $sql = __sql::select('foo', array('id' => 'foo_id'), '', false);
$this->assertEqual($sql->query, "SELECT `id` AS `foo_id` FROM `foo` WHERE 1;"); $this->assertEquals($sql->query, "SELECT `id` AS `foo_id` FROM `foo` WHERE 1;");
} }
function test_parse_constraints_empty() { function test_parse_constraints_empty() {
$this->assertIdentical(__sql::parse_constraints(null, false), "1"); $this->assertSame(__sql::parse_constraints(null, false), "1");
} }
function test_parse_constraints_string() { function test_parse_constraints_string() {
$constraints = "foo LIKE '%bar%'"; $constraints = "foo LIKE '%bar%'";
$this->assertEqual(__sql::parse_constraints($constraints, false), $constraints); $this->assertEquals(__sql::parse_constraints($constraints, false), $constraints);
} }
function test_parse_constraints_simple() { function test_parse_constraints_simple() {
$this->assertEqual(__sql::parse_constraints( $this->assertEquals(__sql::parse_constraints(
array('id' => 1, 'bar' => 'test1'), false), array('id' => 1, 'bar' => 'test1'), false),
"`id` = '1' AND `bar` = 'test1'"); "`id` = '1' AND `bar` = 'test1'");
} }
function test_parse_constraints_value_list() { function test_parse_constraints_value_list() {
$this->assertEqual(__sql::parse_constraints( $this->assertEquals(__sql::parse_constraints(
array('id' => range(1, 3)), false), array('id' => range(1, 3)), false),
"`id` IN ('1', '2', '3')"); "`id` IN ('1', '2', '3')");
} }
function test_select_query() { function test_select_query() {
$sql = __sql::select('foo', '*', array('bar' => 'test1'), false); $sql = __sql::select('foo', '*', array('bar' => 'test1'), false);
$this->assertEqual($sql->query, "SELECT * FROM `foo` WHERE `bar` = 'test1';"); $this->assertEquals($sql->query, "SELECT * FROM `foo` WHERE `bar` = 'test1';");
} }
function test_update_query() { function test_update_query() {
$sql = __sql::update('foo', array('bar' => 'test4'), array('id' => 1), false); $sql = __sql::update('foo', array('bar' => 'test4'), array('id' => 1), false);
$this->assertEqual($sql->query, "UPDATE `foo` SET `bar` = 'test4' WHERE `id` = '1';"); $this->assertEquals($sql->query, "UPDATE `foo` SET `bar` = 'test4' WHERE `id` = '1';");
} }
function test_insert_query() { function test_insert_query() {
$sql = __sql::insert_row('foo', array('bar' => 'test3'), false); $sql = __sql::insert_row('foo', array('bar' => 'test3'), false);
$this->assertEqual($sql->query, "INSERT INTO `foo`(`bar`) VALUES('test3');"); $this->assertEquals($sql->query, "INSERT INTO `foo`(`bar`) VALUES('test3');");
} }
function test_delete_query() { function test_delete_query() {
$sql = __sql::delete('foo', array('bar' => 'test3'), false); $sql = __sql::delete('foo', array('bar' => 'test3'), false);
$this->assertEqual($sql->query, "DELETE FROM `foo` WHERE `bar` = 'test3';"); $this->assertEquals($sql->query, "DELETE FROM `foo` WHERE `bar` = 'test3';");
} }
/**
* @depends test_select_query
*/
function test_select() { function test_select() {
self::set_login_data();
$sql = _sql("select bar from foo where id = 1"); $sql = _sql("select bar from foo where id = 1");
$result = $sql->fetch('object'); $result = $sql->fetch('object');
$this->assertEqual($result->bar, 'test1'); $this->assertEquals('test1', $result->bar);
$this->assertIdentical($sql->fetch(), false); $this->assertSame($sql->fetch(), false);
} }
/**
* @depends test_update_query
*/
function test_update() { function test_update() {
self::set_login_data(); $update = __sql::update('foo', array('bar' => 'test1'),
$update = __sql::update('foo', array('bar' => 'test4'),
array('id' => 1), false)->execute(); array('id' => 1), false)->execute();
// Do not continue unless the value has been updated // Do not continue unless the value has been updated
if( !$this->assertIdentical($update->result, true) ) $this->assertSame($update->result, true);
return false;
// Chachge the updated record back to its original state
$update = __sql::update('foo', array('bar' => 'test1'),
array('id' => 1), false)->execute();
$this->assertIdentical($update->result, true);
} }
function test_insert_delete() { /**
self::set_login_data(); * @depends test_insert_query
*/
function test_insert() {
$insert = __sql::insert_row('foo', array('bar' => 'test3'))->execute(); $insert = __sql::insert_row('foo', array('bar' => 'test3'))->execute();
$this->assertSame($insert->result, true);
// Do not continue unless the value has been inserted
if( !$this->assertIdentical($insert->result, true) )
return false;
// Delete the record that was just inserted
$delete = __sql::delete('foo', array('bar' => 'test3'))->execute();
$this->assertIdentical($delete->result, true);
} }
static function set_login_data() { /**
__sql::set_login_data('localhost', 'root', '', 'pquery_test'); * @depends test_delete_query
* @depends test_insert
*/
function test_delete() {
$delete = __sql::delete('foo', array('bar' => 'test3'))->execute();
$this->assertSame($delete->result, true);
} }
} }
......
<?php <?php
include_once 'config.php';
__p::load_plugin('template'); __p::load_plugin('template');
class pQueryTemplateTest extends UnitTestCase { class pQueryTemplateTest extends PHPUnit_Framework_TestCase {
const TEMPLATES_FOLDER = 'templates/'; const TEMPLATES_FOLDER = 'templates/';
var $templates_folder; var $templates_folder;
var $file; var $file;
var $tpl; var $tpl;
function __construct() {
parent::__construct('pQuery template plugin');
}
function setUp() { function setUp() {
// Set root to tests/templates // Set root to tests/templates
$this->templates_folder = PQUERY_ROOT.'test/'.self::TEMPLATES_FOLDER; $this->templates_folder = PQUERY_ROOT.'test/'.self::TEMPLATES_FOLDER;
...@@ -35,8 +32,10 @@ class pQueryTemplateTest extends UnitTestCase { ...@@ -35,8 +32,10 @@ class pQueryTemplateTest extends UnitTestCase {
$this->assertTrue(in_array($folder, __tpl::$include_path), 'folder was not added to include path'); $this->assertTrue(in_array($folder, __tpl::$include_path), 'folder was not added to include path');
} }
/**
* @expectedException pQueryException
*/
function test_add_root_failure() { function test_add_root_failure() {
$this->expectException('pQueryException');
__tpl::add_root('non_existing_folder'); __tpl::add_root('non_existing_folder');
} }
...@@ -44,27 +43,29 @@ class pQueryTemplateTest extends UnitTestCase { ...@@ -44,27 +43,29 @@ class pQueryTemplateTest extends UnitTestCase {
$folder = PQUERY_ROOT.'test/'; $folder = PQUERY_ROOT.'test/';
$folder_relative = 'test/'; $folder_relative = 'test/';
__tpl::set_root($folder_relative); __tpl::set_root($folder_relative);
$this->assertEqual(array($folder), __tpl::$include_path, 'folder was not set as only include path'); $this->assertEquals(array($folder), __tpl::$include_path, 'folder was not set as only include path');
} }
function test_set_root_absolute() { function test_set_root_absolute() {
$folder = PQUERY_ROOT.'test/'; $folder = PQUERY_ROOT.'test/';
__tpl::set_root($folder, false); __tpl::set_root($folder, false);
$this->assertEqual(array($folder), __tpl::$include_path, 'folder was not set as only include path'); $this->assertEquals(array($folder), __tpl::$include_path, 'folder was not set as only include path');
} }
function test_constructor() { function test_constructor() {
$this->assertIsA($this->tpl, 'pQueryTemplate', 'constructor does not return pQueryTemplate object'); $this->assertTrue($this->tpl instanceof pQueryTemplate, 'constructor does not return pQueryTemplate object');
} }
function test_open_template_file() { function test_open_template_file() {
$path = $this->templates_folder.$this->file; $path = $this->templates_folder.$this->file;
$content = file_get_contents($path); $content = file_get_contents($path);
$this->assertEqual($this->tpl->content, $content, 'template content is not set correctly'); $this->assertEquals($this->tpl->content, $content, 'template content is not set correctly');
} }
/**
* @expectedException pQueryException
*/
function test_non_existent_file() { function test_non_existent_file() {
$this->expectException('pQueryException');
_tpl('non_existent_file.tpl'); _tpl('non_existent_file.tpl');
} }
...@@ -84,7 +85,7 @@ class pQueryTemplateTest extends UnitTestCase { ...@@ -84,7 +85,7 @@ class pQueryTemplateTest extends UnitTestCase {
// Expected content is defined in a text file // Expected content is defined in a text file
$expected_content = file_get_contents($this->templates_folder.'expect_parse.html'); $expected_content = file_get_contents($this->templates_folder.'expect_parse.html');
$this->assertEqual($this->tpl->parse(), $expected_content); $this->assertEquals($this->tpl->parse(), $expected_content);
} }
} }
......
<?php
class pQueryTest extends UnitTestCase {
function __construct() {
parent::__construct('pQuery base');
}
function test_() {
}
}
?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment