From 94bc65e2038407b8b6d2b23c195b232e07208d22 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 26 Mar 2010 16:39:37 +0100 Subject: [PATCH 1/9] [feature/dbal-tests] Added database test & refactored test framework There is now a phpbb_database_test_case which can be used as a base class for tests that require database access. You have to set up a test_config.php file in your tests/ directory containing host, user, pass etc. Extra test functionality has been moved to phpbb_test_case_helpers to provide the same functionality in database tests and regular tests without duplicating the code. This is achieved through delegation of method calls. --- tests/test_framework/framework.php | 3 + .../phpbb_database_test_case.php | 42 ++++++++++ tests/test_framework/phpbb_test_case.php | 32 +++----- .../phpbb_test_case_helpers.php | 82 +++++++++++++++++++ 4 files changed, 138 insertions(+), 21 deletions(-) create mode 100644 tests/test_framework/phpbb_database_test_case.php create mode 100644 tests/test_framework/phpbb_test_case_helpers.php diff --git a/tests/test_framework/framework.php b/tests/test_framework/framework.php index 5913d20ca0..abdcd1ad79 100644 --- a/tests/test_framework/framework.php +++ b/tests/test_framework/framework.php @@ -33,4 +33,7 @@ if (version_compare(PHPUnit_Runner_Version::id(), '3.3.0', '<')) } require_once 'PHPUnit/Framework.php'; +require_once 'PHPUnit/Extensions/Database/TestCase.php'; +require_once 'test_framework/phpbb_test_case_helpers.php'; require_once 'test_framework/phpbb_test_case.php'; +require_once 'test_framework/phpbb_database_test_case.php'; diff --git a/tests/test_framework/phpbb_database_test_case.php b/tests/test_framework/phpbb_database_test_case.php new file mode 100644 index 0000000000..f0aa54ec8d --- /dev/null +++ b/tests/test_framework/phpbb_database_test_case.php @@ -0,0 +1,42 @@ +test_case_helpers) + { + $this->test_case_helpers = new phpbb_test_case_helpers($this); + } + } + + public function getConnection() + { + $this->init_test_case_helpers(); + $database_config = $this->test_case_helpers->get_database_config(); + + $pdo = new PDO('mysql:host=' . $database_config['dbhost'] . ';dbname=' . $database_config['dbname'], $database_config['dbuser'], $database_config['dbpasswd']); + return $this->createDefaultDBConnection($pdo, 'testdb'); + } + + public function new_dbal() + { + $this->init_test_case_helpers(); + return $this->test_case_helpers->new_dbal(); + } + + public function setExpectedTriggerError($errno, $message = '') + { + $this->init_test_case_helpers(); + $this->test_case_helpers->setExpectedTriggerError($errno, $message); + } +} diff --git a/tests/test_framework/phpbb_test_case.php b/tests/test_framework/phpbb_test_case.php index 3cf2a9d442..af867b29ff 100644 --- a/tests/test_framework/phpbb_test_case.php +++ b/tests/test_framework/phpbb_test_case.php @@ -9,29 +9,19 @@ class phpbb_test_case extends PHPUnit_Framework_TestCase { - protected $expectedTriggerError = false; + protected $test_case_helpers; + + public function init_test_case_helpers() + { + if (!$this->test_case_helpers) + { + $this->test_case_helpers = new phpbb_test_case_helpers($this); + } + } public function setExpectedTriggerError($errno, $message = '') { - $exceptionName = ''; - switch ($errno) - { - case E_NOTICE: - case E_STRICT: - PHPUnit_Framework_Error_Notice::$enabled = true; - $exceptionName = 'PHPUnit_Framework_Error_Notice'; - break; - - case E_WARNING: - PHPUnit_Framework_Error_Warning::$enabled = true; - $exceptionName = 'PHPUnit_Framework_Error_Warning'; - break; - - default: - $exceptionName = 'PHPUnit_Framework_Error'; - break; - } - $this->expectedTriggerError = true; - $this->setExpectedException($exceptionName, (string) $message, $errno); + $this->init_test_case_helpers(); + $this->test_case_helpers->setExpectedTriggerError($errno, $message); } } diff --git a/tests/test_framework/phpbb_test_case_helpers.php b/tests/test_framework/phpbb_test_case_helpers.php new file mode 100644 index 0000000000..27a730e2dd --- /dev/null +++ b/tests/test_framework/phpbb_test_case_helpers.php @@ -0,0 +1,82 @@ +test_case = $test_case; + } + + public function get_database_config() + { + if (!file_exists('test_config.php')) + { + trigger_error("You have to create a test_config.php like this: + $dbms, + 'dbhost' => $dbhost, + 'dbport' => $dbport, + 'dbname' => $dbname, + 'dbuser' => $dbuser, + 'dbpasswd' => $dbpasswd, + ); + } + + public function new_dbal() + { + global $phpbb_root_path, $phpEx; + $config = $this->get_database_config(); + + require_once '../phpBB/includes/db/' . $config['dbms'] . '.php'; + $dbal = 'dbal_' . $config['dbms']; + $db = new $dbal(); + $db->sql_connect($config['dbhost'], $config['dbuser'], $config['dbpasswd'], $config['dbname'], $config['dbport']); + + return $db; + } + + public function setExpectedTriggerError($errno, $message = '') + { + $exceptionName = ''; + switch ($errno) + { + case E_NOTICE: + case E_STRICT: + PHPUnit_Framework_Error_Notice::$enabled = true; + $exceptionName = 'PHPUnit_Framework_Error_Notice'; + break; + + case E_WARNING: + PHPUnit_Framework_Error_Warning::$enabled = true; + $exceptionName = 'PHPUnit_Framework_Error_Warning'; + break; + + default: + $exceptionName = 'PHPUnit_Framework_Error'; + break; + } + $this->expectedTriggerError = true; + $this->test_case->setExpectedException($exceptionName, (string) $message, $errno); + } +} From af654814f63e05be5236075f06943062be007072 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 26 Mar 2010 16:41:19 +0100 Subject: [PATCH 2/9] [feature/dbal-tests] Added tests for dbal fetchrow and fetchfield. --- tests/all_tests.php | 2 ++ tests/dbal/all_tests.php | 40 ++++++++++++++++++++++++++++ tests/dbal/dbal.php | 43 +++++++++++++++++++++++++++++++ tests/dbal/fixtures/two_users.xml | 15 +++++++++++ 4 files changed, 100 insertions(+) create mode 100644 tests/dbal/all_tests.php create mode 100644 tests/dbal/dbal.php create mode 100644 tests/dbal/fixtures/two_users.xml diff --git a/tests/all_tests.php b/tests/all_tests.php index 1ed6126e80..e693427809 100644 --- a/tests/all_tests.php +++ b/tests/all_tests.php @@ -22,6 +22,7 @@ require_once 'request/all_tests.php'; require_once 'security/all_tests.php'; require_once 'template/all_tests.php'; require_once 'text_processing/all_tests.php'; +require_once 'dbal/all_tests.php'; // exclude the test directory from code coverage reports PHPUnit_Util_Filter::addDirectoryToFilter('./'); @@ -42,6 +43,7 @@ class phpbb_all_tests $suite->addTest(phpbb_security_all_tests::suite()); $suite->addTest(phpbb_template_all_tests::suite()); $suite->addTest(phpbb_text_processing_all_tests::suite()); + $suite->addTest(phpbb_dbal_all_tests::suite()); return $suite; } diff --git a/tests/dbal/all_tests.php b/tests/dbal/all_tests.php new file mode 100644 index 0000000000..7aee0f6b16 --- /dev/null +++ b/tests/dbal/all_tests.php @@ -0,0 +1,40 @@ +addTestSuite('phpbb_dbal_test'); + + return $suite; + } +} + +if (PHPUnit_MAIN_METHOD == 'phpbb_dbal_all_tests::main') +{ + phpbb_dbal_all_tests::main(); +} diff --git a/tests/dbal/dbal.php b/tests/dbal/dbal.php new file mode 100644 index 0000000000..a77279105f --- /dev/null +++ b/tests/dbal/dbal.php @@ -0,0 +1,43 @@ +createXMLDataSet(dirname(__FILE__).'/fixtures/two_users.xml'); + } + + public function test_select_row() + { + $db = $this->new_dbal(); + + $result = $db->sql_query('SELECT username_clean + FROM phpbb_users + WHERE user_id = 2'); + $row = $db->sql_fetchrow($result); + + $this->assertEquals(array('username_clean' => 'foobar'), $row); + } + + public function test_select_field() + { + $db = $this->new_dbal(); + + $result = $db->sql_query('SELECT username_clean + FROM phpbb_users + WHERE user_id = 2'); + + $this->assertEquals('foobar', $db->sql_fetchfield('username_clean')); + } +} + diff --git a/tests/dbal/fixtures/two_users.xml b/tests/dbal/fixtures/two_users.xml new file mode 100644 index 0000000000..255f061bd4 --- /dev/null +++ b/tests/dbal/fixtures/two_users.xml @@ -0,0 +1,15 @@ + + + + user_id + username_clean + + 1 + barfoo + + + 2 + foobar + +
+
From a7581085e002035ab5c516fe91fe4ece57087267 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 26 Mar 2010 17:37:01 +0100 Subject: [PATCH 3/9] [feature/dbal-tests] Load phpbb-schema after creating the connection to the database --- .../phpbb_database_test_case.php | 49 ++++++++++++++++++- .../phpbb_test_case_helpers.php | 7 ++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/tests/test_framework/phpbb_database_test_case.php b/tests/test_framework/phpbb_database_test_case.php index f0aa54ec8d..4f017f08d0 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -19,12 +19,59 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test } } + function split_sql_file($sql, $delimiter) + { + $sql = str_replace("\r" , '', $sql); + $data = preg_split('/' . preg_quote($delimiter, '/') . '$/m', $sql); + + $data = array_map('trim', $data); + + // The empty case + $end_data = end($data); + + if (empty($end_data)) + { + unset($data[key($data)]); + } + + return $data; + } + public function getConnection() { + static $already_connected; + $this->init_test_case_helpers(); $database_config = $this->test_case_helpers->get_database_config(); - $pdo = new PDO('mysql:host=' . $database_config['dbhost'] . ';dbname=' . $database_config['dbname'], $database_config['dbuser'], $database_config['dbpasswd']); + if ($already_connected) + { + $pdo = new PDO('mysql:host=' . $database_config['dbhost'] . ';dbname=' . $database_config['dbname'], $database_config['dbuser'], $database_config['dbpasswd']); + } + else + { + $pdo = new PDO('mysql:host=' . $database_config['dbhost'] . ';', $database_config['dbuser'], $database_config['dbpasswd']); + + try + { + $pdo->exec('DROP DATABASE ' . $database_config['dbname']); + } + catch (PDOException $e){} // ignore non existent db + + $pdo->exec('CREATE DATABASE ' . $database_config['dbname']); + + $pdo = new PDO('mysql:host=' . $database_config['dbhost'] . ';dbname=' . $database_config['dbname'], $database_config['dbuser'], $database_config['dbpasswd']); + + $sql_query = $this->split_sql_file(file_get_contents('../phpBB/install/schemas/mysql_41_schema.sql'), ';'); + + foreach ($sql_query as $sql) + { + $pdo->exec($sql); + } + + $already_connected = true; + } + return $this->createDefaultDBConnection($pdo, 'testdb'); } diff --git a/tests/test_framework/phpbb_test_case_helpers.php b/tests/test_framework/phpbb_test_case_helpers.php index 27a730e2dd..f9ab750218 100644 --- a/tests/test_framework/phpbb_test_case_helpers.php +++ b/tests/test_framework/phpbb_test_case_helpers.php @@ -23,13 +23,16 @@ class phpbb_test_case_helpers if (!file_exists('test_config.php')) { trigger_error("You have to create a test_config.php like this: - Date: Fri, 26 Mar 2010 21:02:56 +0100 Subject: [PATCH 4/9] [feature/dbal-tests] Tests for $db->sql_query_limit() --- tests/dbal/dbal.php | 44 ++++++++++++++++++- .../{two_users.xml => three_users.xml} | 4 ++ 2 files changed, 47 insertions(+), 1 deletion(-) rename tests/dbal/fixtures/{two_users.xml => three_users.xml} (77%) diff --git a/tests/dbal/dbal.php b/tests/dbal/dbal.php index a77279105f..72e399adbe 100644 --- a/tests/dbal/dbal.php +++ b/tests/dbal/dbal.php @@ -14,7 +14,7 @@ class phpbb_dbal_test extends phpbb_database_test_case { public function getDataSet() { - return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/two_users.xml'); + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/three_users.xml'); } public function test_select_row() @@ -39,5 +39,47 @@ class phpbb_dbal_test extends phpbb_database_test_case $this->assertEquals('foobar', $db->sql_fetchfield('username_clean')); } + + public static function query_limit_data() + { + return array( + array(0, 0, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + array(0, 1, array(array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + array(1, 0, array(array('username_clean' => 'barfoo'))), + array(1, 1, array(array('username_clean' => 'foobar'))), + array(1, 2, array(array('username_clean' => 'bertie'))), + array(2, 0, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array(2, 2, array(array('username_clean' => 'bertie'))), + array(2, 5, array()), + array(10, 1, array(array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + array(10, 5, array()), + ); + } + + /** + * @dataProvider query_limit_data + */ + public function test_query_limit($total, $offset, $expected) + { + $db = $this->new_dbal(); + + $result = $db->sql_query_limit('SELECT username_clean + FROM phpbb_users + ORDER BY user_id', $total, $offset); + + $ary = array(); + while ($row = $db->sql_fetchrow($result)) + { + $ary[] = $row; + } + $db->sql_freeresult($result); + + $this->assertEquals($expected, $ary); + } } diff --git a/tests/dbal/fixtures/two_users.xml b/tests/dbal/fixtures/three_users.xml similarity index 77% rename from tests/dbal/fixtures/two_users.xml rename to tests/dbal/fixtures/three_users.xml index 255f061bd4..fff26d2b3f 100644 --- a/tests/dbal/fixtures/two_users.xml +++ b/tests/dbal/fixtures/three_users.xml @@ -11,5 +11,9 @@ 2 foobar + + 3 + bertie + From 147d6fd590b0ff0bbed153fd33dcead494f822d0 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 26 Mar 2010 22:33:54 +0100 Subject: [PATCH 5/9] [feature/dbal-tests] Make some tests for return_on_error on SELECT-queries --- tests/dbal/dbal.php | 226 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 213 insertions(+), 13 deletions(-) diff --git a/tests/dbal/dbal.php b/tests/dbal/dbal.php index 72e399adbe..0239f91a2d 100644 --- a/tests/dbal/dbal.php +++ b/tests/dbal/dbal.php @@ -17,27 +17,116 @@ class phpbb_dbal_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/three_users.xml'); } - public function test_select_row() + public static function return_on_error_select_data() { - $db = $this->new_dbal(); - - $result = $db->sql_query('SELECT username_clean - FROM phpbb_users - WHERE user_id = 2'); - $row = $db->sql_fetchrow($result); - - $this->assertEquals(array('username_clean' => 'foobar'), $row); + return array( + array('phpbb_users', "username_clean = 'bertie'", array(array('username_clean' => 'bertie'))), + array('phpbb_users', "username_clean = 'phpBB'", array()), + array('phpbb_users', 'username_clean syntax_error', false), + array('phpbb_users', 'column_not_exists = 2', false), + array('table_not_exists', 'column_not_exists = 2', false), + ); } - public function test_select_field() + /** + * @dataProvider return_on_error_select_data + */ + public function test_return_on_error_select($table, $where, $expected) + { + $db = $this->new_dbal(); + + $db->sql_return_on_error(true); + + $result = $db->sql_query('SELECT username_clean + FROM ' . $table . ' + WHERE ' . $where . ' + ORDER BY user_id ASC'); + + $db->sql_return_on_error(false); + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + } + + public static function fetchrow_data() + { + return array( + array('', array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + array('user_id = 2', array(array('username_clean' => 'foobar'))), + array("username_clean = 'bertie'", array(array('username_clean' => 'bertie'))), + array("username_clean = 'phpBB'", array()), + ); + } + + /** + * @dataProvider fetchrow_data + */ + public function test_fetchrow($where, $expected) { $db = $this->new_dbal(); $result = $db->sql_query('SELECT username_clean FROM phpbb_users - WHERE user_id = 2'); + ' . (($where) ? ' WHERE ' . $where : '') . ' + ORDER BY user_id ASC'); - $this->assertEquals('foobar', $db->sql_fetchfield('username_clean')); + $ary = array(); + while ($row = $db->sql_fetchrow($result)) + { + $ary[] = $row; + } + $db->sql_freeresult($result); + + $this->assertEquals($expected, $ary); + } + + /** + * @dataProvider fetchrow_data + */ + public function test_fetchrowset($where, $expected) + { + $db = $this->new_dbal(); + + $result = $db->sql_query('SELECT username_clean + FROM phpbb_users + ' . (($where) ? ' WHERE ' . $where : '') . ' + ORDER BY user_id ASC'); + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + + $db->sql_freeresult($result); + } + + public static function fetchfield_data() + { + return array( + array('', array('barfoo', 'foobar', 'bertie')), + array('user_id = 2', array('foobar')), + array("username_clean = 'bertie'", array('bertie')), + ); + } + + /** + * @dataProvider fetchfield_data + */ + public function test_fetchfield($where, $expected) + { + $db = $this->new_dbal(); + + $result = $db->sql_query('SELECT username_clean + FROM phpbb_users + ' . (($where) ? ' WHERE ' . $where : '') . ' + ORDER BY user_id ASC'); + + $ary = array(); + while ($row = $db->sql_fetchfield('username_clean')) + { + $ary[] = $row; + } + $db->sql_freeresult($result); + + $this->assertEquals($expected, $ary); } public static function query_limit_data() @@ -70,7 +159,7 @@ class phpbb_dbal_test extends phpbb_database_test_case $result = $db->sql_query_limit('SELECT username_clean FROM phpbb_users - ORDER BY user_id', $total, $offset); + ORDER BY user_id ASC', $total, $offset); $ary = array(); while ($row = $db->sql_fetchrow($result)) @@ -81,5 +170,116 @@ class phpbb_dbal_test extends phpbb_database_test_case $this->assertEquals($expected, $ary); } + + public static function like_expression_data() + { + // * = any_char; # = one_char + return array( + array('barfoo', array(array('username_clean' => 'barfoo'))), + array('bar', array()), + array('bar*', array(array('username_clean' => 'barfoo'))), + array('*bar*', array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array('*b*', array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + array('b*r', array()), + array('b*e', array(array('username_clean' => 'bertie'))), + array('#b*e', array()), + array('b####e', array(array('username_clean' => 'bertie'))), + ); + } + + /** + * @dataProvider like_expression_data + */ + public function test_like_expression($like_expression, $expected) + { + $db = $this->new_dbal(); + + $like_expression = str_replace('*', $db->any_char, $like_expression); + $like_expression = str_replace('#', $db->one_char, $like_expression); + $where = ($like_expression) ? 'username_clean ' . $db->sql_like_expression($like_expression) : ''; + + $result = $db->sql_query('SELECT username_clean + FROM phpbb_users + ' . (($where) ? ' WHERE ' . $where : '') . ' + ORDER BY user_id ASC'); + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + + $db->sql_freeresult($result); + } + + public static function in_set_data() + { + return array( + array('user_id', 3, false, false, array(array('username_clean' => 'bertie'))), + array('user_id', 3, false, true, array(array('username_clean' => 'bertie'))), + array('user_id', 3, true, false, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array('user_id', 3, true, true, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array('user_id', '3', false, false, array(array('username_clean' => 'bertie'))), + array('user_id', '3', false, true, array(array('username_clean' => 'bertie'))), + array('user_id', '3', true, false, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array('user_id', '3', true, true, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array('user_id', array(3), false, false, array(array('username_clean' => 'bertie'))), + array('user_id', array(3), false, true, array(array('username_clean' => 'bertie'))), + array('user_id', array(3), true, false, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array('user_id', array(3), true, true, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'))), + array('user_id', array(1, 3), false, false, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'bertie'))), + array('user_id', array(1, 3), false, true, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'bertie'))), + array('user_id', array(1, 3), true, false, array(array('username_clean' => 'foobar'))), + array('user_id', array(1, 3), true, true, array(array('username_clean' => 'foobar'))), + array('user_id', '', false, false, array()), + array('user_id', '', false, true, array()), + array('user_id', '', true, false, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + array('user_id', '', true, true, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + array('user_id', array(), false, false, false, true), + array('user_id', array(), false, true, array()), + array('user_id', array(), true, false, false, true), + array('user_id', array(), true, true, array(array('username_clean' => 'barfoo'), + array('username_clean' => 'foobar'), + array('username_clean' => 'bertie'))), + ); + } + + /** + * @dataProvider in_set_data + */ + public function test_in_set($field, $array, $negate, $allow_empty_set, $expected, $catch_error = false) + { + $db = $this->new_dbal(); + + if ($catch_error) + { + $db->sql_return_on_error(true); + } + + $result = $db->sql_query('SELECT username_clean + FROM phpbb_users + WHERE ' . $db->sql_in_set($field, $array, $negate, $allow_empty_set) . ' + ORDER BY user_id ASC'); + + if ($catch_error) + { + $db->sql_return_on_error(falsee); + } + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + + $db->sql_freeresult($result); + } } From 53d316dc9ea34b1228591aa9cee766b2ec8abdc7 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 27 Mar 2010 10:21:16 +0100 Subject: [PATCH 6/9] [feature/dbal-tests] Make some tests for build_array_data on SELECT --- tests/dbal/dbal.php | 49 ++++++++++++- .../phpbb_database_test_case.php | 69 ++++++++++++++++++- 2 files changed, 114 insertions(+), 4 deletions(-) diff --git a/tests/dbal/dbal.php b/tests/dbal/dbal.php index 0239f91a2d..f90b5efeb5 100644 --- a/tests/dbal/dbal.php +++ b/tests/dbal/dbal.php @@ -246,12 +246,14 @@ class phpbb_dbal_test extends phpbb_database_test_case array('user_id', '', true, true, array(array('username_clean' => 'barfoo'), array('username_clean' => 'foobar'), array('username_clean' => 'bertie'))), - array('user_id', array(), false, false, false, true), array('user_id', array(), false, true, array()), - array('user_id', array(), true, false, false, true), array('user_id', array(), true, true, array(array('username_clean' => 'barfoo'), array('username_clean' => 'foobar'), array('username_clean' => 'bertie'))), + + // These here would throw errors and therefor $result should be false. + array('user_id', array(), false, false, false, true), + array('user_id', array(), true, false, false, true), ); } @@ -274,7 +276,48 @@ class phpbb_dbal_test extends phpbb_database_test_case if ($catch_error) { - $db->sql_return_on_error(falsee); + $db->sql_return_on_error(false); + } + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + + $db->sql_freeresult($result); + } + + public static function build_array_data() + { + return array( + array(array('username_clean' => 'barfoo'), array(array('username_clean' => 'barfoo'))), + array(array('username_clean' => 'barfoo', 'user_id' => 1), array(array('username_clean' => 'barfoo'))), + array(array('username_clean' => 'barfoo', 'user_id' => 2), array()), + + // These here would throw errors and therefor $result should be false. + array(array(), false, true), + array('no_array', false, true), + array(0, false, true), + ); + } + + /** + * @dataProvider build_array_data + */ + public function test_build_array($assoc_ary, $expected, $catch_error = false) + { + $db = $this->new_dbal(); + + if ($catch_error) + { + $db->sql_return_on_error(true); + } + + $result = $db->sql_query('SELECT username_clean + FROM phpbb_users + WHERE ' . $db->sql_build_array('SELECT', $assoc_ary) . ' + ORDER BY user_id ASC'); + + if ($catch_error) + { + $db->sql_return_on_error(false); } $this->assertEquals($expected, $db->sql_fetchrowset($result)); diff --git a/tests/test_framework/phpbb_database_test_case.php b/tests/test_framework/phpbb_database_test_case.php index 4f017f08d0..547ee6404e 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -19,6 +19,57 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test } } + function get_dbms_data($dbms) + { + $available_dbms = array( + 'firebird' => array( + 'SCHEMA' => 'firebird', + 'DELIM' => ';;', + ), + 'mysqli' => array( + 'SCHEMA' => 'mysql_41', + 'DELIM' => ';', + ), + 'mysql' => array( + 'SCHEMA' => 'mysql', + 'DELIM' => ';', + ), + 'mssql' => array( + 'SCHEMA' => 'mssql', + 'DELIM' => 'GO', + ), + 'mssql_odbc'=> array( + 'SCHEMA' => 'mssql', + 'DELIM' => 'GO', + ), + 'mssqlnative' => array( + 'SCHEMA' => 'mssql', + 'DELIM' => 'GO', + ), + 'oracle' => array( + 'SCHEMA' => 'oracle', + 'DELIM' => '/', + ), + 'postgres' => array( + 'SCHEMA' => 'postgres', + 'DELIM' => ';', + ), + 'sqlite' => array( + 'SCHEMA' => 'sqlite', + 'DELIM' => ';', + ), + ); + + if (isset($available_dbms[$dbms])) + { + return $available_dbms[$dbms]; + } + else + { + trigger_error('Database unsupported', E_USER_ERROR); + } + } + function split_sql_file($sql, $delimiter) { $sql = str_replace("\r" , '', $sql); @@ -62,7 +113,23 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test $pdo = new PDO('mysql:host=' . $database_config['dbhost'] . ';dbname=' . $database_config['dbname'], $database_config['dbuser'], $database_config['dbpasswd']); - $sql_query = $this->split_sql_file(file_get_contents('../phpBB/install/schemas/mysql_41_schema.sql'), ';'); + $dbms_data = $this->get_dbms_data($database_config['dbms']); + if ($database_config['dbms'] == 'mysql') + { + $pdo->exec('SELECT VERSION() AS version'); + $row = $sth->fetch(PDO::FETCH_ASSOC); + if (version_compare($row['version'], '4.1.3', '>=')) + { + $dbms_data['SCHEMA'] .= '_41'; + } + else + { + $dbms_data['SCHEMA'] .= '_40'; + } + unset($row); + } + + $sql_query = $this->split_sql_file(file_get_contents("../phpBB/install/schemas/{$dbms_data['SCHEMA']}_schema.sql"), $dbms_data['DELIM']); foreach ($sql_query as $sql) { From 23beaceadd38f694ca2eea3110a20db48328e6ce Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 1 Apr 2010 18:59:26 +0200 Subject: [PATCH 7/9] [feature/dbal-tests] Fix whitespace and line endings. --- tests/dbal/fixtures/three_users.xml | 38 +++++++++---------- .../phpbb_database_test_case.php | 4 +- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/tests/dbal/fixtures/three_users.xml b/tests/dbal/fixtures/three_users.xml index fff26d2b3f..a6789f4a01 100644 --- a/tests/dbal/fixtures/three_users.xml +++ b/tests/dbal/fixtures/three_users.xml @@ -1,19 +1,19 @@ - - - - user_id - username_clean - - 1 - barfoo - - - 2 - foobar - - - 3 - bertie - -
-
+ + + + user_id + username_clean + + 1 + barfoo + + + 2 + foobar + + + 3 + bertie + +
+
diff --git a/tests/test_framework/phpbb_database_test_case.php b/tests/test_framework/phpbb_database_test_case.php index 547ee6404e..84635fc24d 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -45,7 +45,7 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test 'mssqlnative' => array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', - ), + ), 'oracle' => array( 'SCHEMA' => 'oracle', 'DELIM' => '/', @@ -108,7 +108,7 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test $pdo->exec('DROP DATABASE ' . $database_config['dbname']); } catch (PDOException $e){} // ignore non existent db - + $pdo->exec('CREATE DATABASE ' . $database_config['dbname']); $pdo = new PDO('mysql:host=' . $database_config['dbhost'] . ';dbname=' . $database_config['dbname'], $database_config['dbuser'], $database_config['dbpasswd']); From 53ab8886b1a3a98291627e2baab94ec86c7df685 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 1 Apr 2010 19:08:11 +0200 Subject: [PATCH 8/9] [feature/dbal-tests] Make the PDO prefix depend on the dbms. The database base test will need a few more changes to run on all the databases we support. But those really need to be made on a system where they run and can be tested. Patches welcome! --- .../test_framework/phpbb_database_test_case.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/test_framework/phpbb_database_test_case.php b/tests/test_framework/phpbb_database_test_case.php index 84635fc24d..a80f03b749 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -25,38 +25,47 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test 'firebird' => array( 'SCHEMA' => 'firebird', 'DELIM' => ';;', + 'PDO' => 'firebird', ), 'mysqli' => array( 'SCHEMA' => 'mysql_41', 'DELIM' => ';', + 'PDO' => 'mysql', ), 'mysql' => array( 'SCHEMA' => 'mysql', 'DELIM' => ';', + 'PDO' => 'mysql', ), 'mssql' => array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', + 'PDO' => 'odbc', ), 'mssql_odbc'=> array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', + 'PDO' => 'odbc', ), 'mssqlnative' => array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', + 'PDO' => 'odbc', ), 'oracle' => array( 'SCHEMA' => 'oracle', 'DELIM' => '/', + 'PDO' => 'oci', ), 'postgres' => array( 'SCHEMA' => 'postgres', 'DELIM' => ';', + 'PDO' => 'pgsql', ), 'sqlite' => array( 'SCHEMA' => 'sqlite', 'DELIM' => ';', + 'PDO' => 'sqlite', ), ); @@ -95,13 +104,15 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test $this->init_test_case_helpers(); $database_config = $this->test_case_helpers->get_database_config(); + $dbms_data = $this->get_dbms_data($database_config['dbms']); + if ($already_connected) { - $pdo = new PDO('mysql:host=' . $database_config['dbhost'] . ';dbname=' . $database_config['dbname'], $database_config['dbuser'], $database_config['dbpasswd']); + $pdo = new PDO($dbms_data['PDO'] . ':host=' . $database_config['dbhost'] . ';dbname=' . $database_config['dbname'], $database_config['dbuser'], $database_config['dbpasswd']); } else { - $pdo = new PDO('mysql:host=' . $database_config['dbhost'] . ';', $database_config['dbuser'], $database_config['dbpasswd']); + $pdo = new PDO($dbms_data['PDO'] . ':host=' . $database_config['dbhost'] . ';', $database_config['dbuser'], $database_config['dbpasswd']); try { @@ -113,7 +124,6 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test $pdo = new PDO('mysql:host=' . $database_config['dbhost'] . ';dbname=' . $database_config['dbname'], $database_config['dbuser'], $database_config['dbpasswd']); - $dbms_data = $this->get_dbms_data($database_config['dbms']); if ($database_config['dbms'] == 'mysql') { $pdo->exec('SELECT VERSION() AS version'); From 2bbfa9c29f4ce33d25e58c550540e236a4ee3c1a Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 1 Apr 2010 19:15:34 +0200 Subject: [PATCH 9/9] [feature/dbal-tests] Only output the missing config error message once. The error message was also not properly escaping the variables, thus producing an incorrect example configuration file. --- .../phpbb_test_case_helpers.php | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/tests/test_framework/phpbb_test_case_helpers.php b/tests/test_framework/phpbb_test_case_helpers.php index f9ab750218..0c5932e1ad 100644 --- a/tests/test_framework/phpbb_test_case_helpers.php +++ b/tests/test_framework/phpbb_test_case_helpers.php @@ -20,16 +20,28 @@ class phpbb_test_case_helpers public function get_database_config() { + static $show_error = true; + if (!file_exists('test_config.php')) { + if ($show_error) + { + $show_error = false; + } + else + { + $this->test_case->markTestSkipped('Missing test_config.php: See first error.'); + return; + } + trigger_error("You have to create a test_config.php like this: \"