From ee846c461c6f0d4ec02dcefa6c82090587bc7012 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Fri, 22 Oct 2010 19:11:18 +0200 Subject: [task/mssql-db-tests] Use a simple getter for test case helpers. Calling initialisation to then use the member directly seems more complicated than just having a method that returns the instance or creates it if necessary. PHPBB3-9868 --- tests/test_framework/phpbb_database_test_case.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'tests/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 index f6bf420ebc..c183c61f91 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -11,12 +11,14 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test { protected $test_case_helpers; - public function init_test_case_helpers() + public function get_test_case_helpers() { if (!$this->test_case_helpers) { $this->test_case_helpers = new phpbb_test_case_helpers($this); } + + return $this->test_case_helpers; } public function get_dbms_data($dbms) @@ -113,8 +115,7 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test { static $already_connected; - $this->init_test_case_helpers(); - $database_config = $this->test_case_helpers->get_database_config(); + $database_config = $this->get_test_case_helpers()->get_database_config(); $dbms_data = $this->get_dbms_data($database_config['dbms']); @@ -189,13 +190,11 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test public function new_dbal() { - $this->init_test_case_helpers(); - return $this->test_case_helpers->new_dbal(); + return $this->get_test_case_helpers()->new_dbal(); } public function setExpectedTriggerError($errno, $message = '') { - $this->init_test_case_helpers(); - $this->test_case_helpers->setExpectedTriggerError($errno, $message); + $this->get_test_case_helpers()->setExpectedTriggerError($errno, $message); } } -- cgit v1.2.1 From 9dbbfea5fd31e40acc6fb4a195795b3e285a5b56 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Fri, 22 Oct 2010 19:27:41 +0200 Subject: [task/mssql-db-tests] No longer display an error when skipping db tests. Tests are run with sqlite by default now anyway, so in the majority of cases the error message explaining how to set up database test running will not be displayed anyway. Database tests are now generally simply skipped if no configuration can be found. The RUNNING_TESTS.txt file explains how to set them up however, and more info is available on the wiki. The get_database_config method was moved from test_case_helpers to database_test_case because it has no general purpose. PHPBB3-9868 --- tests/test_framework/phpbb_database_test_case.php | 48 ++++++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) (limited to 'tests/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 index c183c61f91..33dbce709b 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -81,6 +81,41 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test } } + public function get_database_config() + { + static $show_error = true; + + if (file_exists('test_config.php')) + { + include('test_config.php'); + + return array( + 'dbms' => $dbms, + 'dbhost' => $dbhost, + 'dbport' => $dbport, + 'dbname' => $dbname, + 'dbuser' => $dbuser, + 'dbpasswd' => $dbpasswd, + ); + } + else if (extension_loaded('sqlite') && version_compare(PHPUnit_Runner_Version::id(), '3.4.15', '>=')) + { + // Silently use sqlite + return array( + 'dbms' => 'sqlite', + 'dbhost' => 'phpbb_unit_tests.sqlite2', // filename + 'dbport' => '', + 'dbname' => '', + 'dbuser' => '', + 'dbpasswd' => '', + ); + } + else + { + $this->markTestSkipped('Missing test_config.php: See first error.'); + } + } + // NOTE: This function is not the same as split_sql_file from functions_install public function split_sql_file($sql, $dbms) { @@ -115,7 +150,7 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test { static $already_connected; - $database_config = $this->get_test_case_helpers()->get_database_config(); + $database_config = $this->get_database_config(); $dbms_data = $this->get_dbms_data($database_config['dbms']); @@ -190,7 +225,16 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test public function new_dbal() { - return $this->get_test_case_helpers()->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 = '') -- cgit v1.2.1 From a397f81a2bc03019910c088376cf563d05dadf93 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Fri, 22 Oct 2010 19:50:43 +0200 Subject: [task/mssql-db-tests] Allow test configuration with environment variables. To allow execution of the tests with different configurations without having to use the test_config.php file, environment variables of the form PHPBB_TEST_ can now be used, e.g. PHPBB_TEST_DBMS to set the variables otherwise expected in test_config.php PHPBB3-9868 --- tests/test_framework/phpbb_database_test_case.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'tests/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 index 33dbce709b..8b96298b6a 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -85,7 +85,18 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test { static $show_error = true; - if (file_exists('test_config.php')) + if (isset($_SERVER['PHPBB_TEST_DBMS'])) + { + return array( + 'dbms' => isset($_SERVER['PHPBB_TEST_DBMS']) ? $_SERVER['PHPBB_TEST_DBMS'] : '', + 'dbhost' => isset($_SERVER['PHPBB_TEST_DBHOST']) ? $_SERVER['PHPBB_TEST_DBHOST'] : '', + 'dbport' => isset($_SERVER['PHPBB_TEST_DBPORT']) ? $_SERVER['PHPBB_TEST_DBPORT'] : '', + 'dbname' => isset($_SERVER['PHPBB_TEST_DBNAME']) ? $_SERVER['PHPBB_TEST_DBNAME'] : '', + 'dbuser' => isset($_SERVER['PHPBB_TEST_DBUSER']) ? $_SERVER['PHPBB_TEST_DBUSER'] : '', + 'dbpasswd' => isset($_SERVER['PHPBB_TEST_DBPASSWD']) ? $_SERVER['PHPBB_TEST_DBPASSWD'] : '', + ); + } + else if (file_exists('test_config.php')) { include('test_config.php'); -- cgit v1.2.1 From 832035f744f8fc42f1ac491d6847dca06b9c837b Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Fri, 22 Oct 2010 20:34:52 +0200 Subject: [task/mssql-db-tests] Refactored getConnection into multiple smaller parts. This is a first step to simplify the extraction of database specific code parts into separate classes. PHPBB3-9868 --- tests/test_framework/phpbb_database_test_case.php | 135 ++++++++++++++-------- 1 file changed, 84 insertions(+), 51 deletions(-) (limited to 'tests/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 index 8b96298b6a..49121b8f8d 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -83,8 +83,6 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test public function get_database_config() { - static $show_error = true; - if (isset($_SERVER['PHPBB_TEST_DBMS'])) { return array( @@ -157,76 +155,111 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test return $data; } - public function getConnection() + /** + * Returns a PDO connection for the configured database. + * + * @param array $config The database configuration + * @param array $dbms Information on the used DBMS. + * @param bool $use_db Whether the DSN should be tied to a + * particular database making it impossible + * to delete that database. + * @return PDO The PDO database connection. + */ + public function new_pdo($config, $dbms, $delete_db) { - static $already_connected; + $dsn = $dbms['PDO'] . ':'; - $database_config = $this->get_database_config(); + switch ($config['dbms']) + { + case 'sqlite': + $dsn .= $config['dbhost']; + break; - $dbms_data = $this->get_dbms_data($database_config['dbms']); + default: + $dsn .= 'host=' . $config['dbhost']; - if ($already_connected) - { - if ($database_config['dbms'] == 'sqlite') - { - $pdo = new PDO($dbms_data['PDO'] . ':' . $database_config['dbhost']); - } - else - { - $pdo = new PDO($dbms_data['PDO'] . ':host=' . $database_config['dbhost'] . ';dbname=' . $database_config['dbname'], $database_config['dbuser'], $database_config['dbpasswd']); - } + if ($use_db) + { + $dsn .= ';dbname=' . $config['dbname']; + } + break; } - else + + $pdo = new PDO($dsn, $config['dbuser'], $config['dbpasswd']);; + + // good for debug + // $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + return $pdo; + } + + private function recreate_db($config, $dbms) + { + switch ($config['dbms']) { - if ($database_config['dbms'] == 'sqlite') - { - // delete existing database - if (file_exists($database_config['dbhost'])) + case 'sqlite': + if (file_exists($config['dbhost'])) { - unlink($database_config['dbhost']); + unlink($config['dbhost']); } + break; - $pdo = new PDO($dbms_data['PDO'] . ':' . $database_config['dbhost']); - } - else - { - $pdo = new PDO($dbms_data['PDO'] . ':host=' . $database_config['dbhost'] . ';', $database_config['dbuser'], $database_config['dbpasswd']);try + default: + $pdo = $this->new_pdo($config, $dbms, false); + + try { - $pdo->exec('DROP DATABASE ' . $database_config['dbname']); + $pdo->exec('DROP DATABASE ' . $config['dbname']); } catch (PDOException $e){} // ignore non existent db - $pdo->exec('CREATE DATABASE ' . $database_config['dbname']); + $pdo->exec('CREATE DATABASE ' . $config['dbname']); + break; + } + } - $pdo = new PDO($dbms_data['PDO'] . ':host=' . $database_config['dbhost'] . ';dbname=' . $database_config['dbname'], $database_config['dbuser'], $database_config['dbpasswd']); + private function load_schema($pdo, $config, $dbms) + { + if ($config['dbms'] == 'mysql') + { + $sth = $pdo->query('SELECT VERSION() AS version'); + $row = $sth->fetch(PDO::FETCH_ASSOC); + + if (version_compare($row['version'], '4.1.3', '>=')) + { + $dbms['SCHEMA'] .= '_41'; } + else + { + $dbms['SCHEMA'] .= '_40'; + } + } - // good for debug - // $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $sql = $this->split_sql_file(file_get_contents("../phpBB/install/schemas/{$dbms['SCHEMA']}_schema.sql"), $config['dbms']); - if ($database_config['dbms'] == 'mysql') - { - $sth = $pdo->query('SELECT VERSION() AS version'); - $row = $sth->fetch(PDO::FETCH_ASSOC); + foreach ($sql as $query) + { + $pdo->exec($query); + } + } - if (version_compare($row['version'], '4.1.3', '>=')) - { - $dbms_data['SCHEMA'] .= '_41'; - } - else - { - $dbms_data['SCHEMA'] .= '_40'; - } + public function getConnection() + { + static $already_connected; - unset($row, $sth); - } + $config = $this->get_database_config(); + $dbms = $this->get_dbms_data($config['dbms']); - $sql_query = $this->split_sql_file(file_get_contents("../phpBB/install/schemas/{$dbms_data['SCHEMA']}_schema.sql"), $database_config['dbms']); + if (!$already_connected) + { + $this->recreate_db($config, $dbms); + } - foreach ($sql_query as $sql) - { - $pdo->exec($sql); - } + $pdo = $this->new_pdo($config, $dbms, true); + + if (!$already_connected) + { + $this->load_schema($pdo, $config, $dbms); $already_connected = true; } -- cgit v1.2.1 From 801f66b4a2b6caae7b818a1aecb1c70d6ae155bb Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Fri, 22 Oct 2010 21:00:15 +0200 Subject: [task/mssql-db-tests] Add support for odbc & sqlsrv PDO test connections PHPBB3-9868 --- tests/test_framework/phpbb_database_test_case.php | 100 ++++++++++++++++++++-- 1 file changed, 94 insertions(+), 6 deletions(-) (limited to 'tests/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 index 49121b8f8d..de9a91fa45 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -52,7 +52,7 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test 'mssqlnative' => array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', - 'PDO' => 'odbc', + 'PDO' => 'sqlsrv', ), 'oracle' => array( 'SCHEMA' => 'oracle', @@ -145,7 +145,8 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test if ($dbms == 'sqlite') { - // trim # off query to satisfy sqlite + // remove comment lines starting with # - they are not proper sqlite + // syntax and break sqlite2 foreach ($data as $i => $query) { $data[$i] = preg_replace('/^#.*$/m', "\n", $query); @@ -155,6 +156,66 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test return $data; } + /** + * Retrieves a list of all tables from the database. + * + * @param PDO $pdo + * @param string $dbms + * @return array(string) + */ + function get_tables($pdo, $dbms) + { + switch ($pdo) + { + case 'mysql': + case 'mysql4': + case 'mysqli': + $sql = 'SHOW TABLES'; + break; + + case 'sqlite': + $sql = 'SELECT name + FROM sqlite_master + WHERE type = "table"'; + break; + + case 'mssql': + case 'mssql_odbc': + case 'mssqlnative': + $sql = "SELECT name + FROM sysobjects + WHERE type='U'"; + break; + + case 'postgres': + $sql = 'SELECT relname + FROM pg_stat_user_tables'; + break; + + case 'firebird': + $sql = 'SELECT rdb$relation_name + FROM rdb$relations + WHERE rdb$view_source is null + AND rdb$system_flag = 0'; + break; + + case 'oracle': + $sql = 'SELECT table_name + FROM USER_TABLES'; + break; + } + + $result = $pdo->query($sql); + + $tables = array(); + while ($row = $result->fetch(PDO::FETCH_NUM)) + { + $tables[] = current($row); + } + + return $tables; + } + /** * Returns a PDO connection for the configured database. * @@ -165,14 +226,30 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test * to delete that database. * @return PDO The PDO database connection. */ - public function new_pdo($config, $dbms, $delete_db) + public function new_pdo($config, $dbms, $use_db) { $dsn = $dbms['PDO'] . ':'; - switch ($config['dbms']) + switch ($dbms['PDO']) { - case 'sqlite': + case 'sqlite2': + $dsn .= $config['dbhost']; + break; + + case 'sqlsrv': + // prefix the hostname (or DSN) with Server= so using just (local)\SQLExpress + // works for example, further parameters can still be appended using ;x=y + $dsn .= 'Server='; + // no break -> rest like ODBC + case 'odbc': + // for ODBC assume dbhost is a suitable DSN + // e.g. Driver={SQL Server Native Client 10.0};Server=(local)\SQLExpress; $dsn .= $config['dbhost']; + + if ($use_db) + { + $dsn .= ';Database=' . $config['dbname']; + } break; default: @@ -211,7 +288,18 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test { $pdo->exec('DROP DATABASE ' . $config['dbname']); } - catch (PDOException $e){} // ignore non existent db + catch (PDOException $e) + { + // try to delete all tables if dropping the database was not possible. + foreach ($this->get_tables() as $table) + { + try + { + $pdo->exec('DROP TABLE ' . $table); + } + catch (PDOException $e){} // ignore non-existent tables + } + } $pdo->exec('CREATE DATABASE ' . $config['dbname']); break; -- cgit v1.2.1 From fa8dca2400f1bef5e3faa43e491a5e2c15eafe11 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Mon, 25 Oct 2010 19:20:51 +0200 Subject: [task/mssql-db-tests] Split up database tests into SELECT and write operations SELECT is based on the user table fixture, write (INSERT/UPDATE/DELETE) is tested using the config table fixture. PHPBB3-9868 --- tests/test_framework/phpbb_database_test_case.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'tests/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 index de9a91fa45..a64bae8c57 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -9,6 +9,8 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_TestCase { + private static $already_connected; + protected $test_case_helpers; public function get_test_case_helpers() @@ -333,23 +335,21 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test public function getConnection() { - static $already_connected; - $config = $this->get_database_config(); $dbms = $this->get_dbms_data($config['dbms']); - if (!$already_connected) + if (!self::$already_connected) { $this->recreate_db($config, $dbms); } $pdo = $this->new_pdo($config, $dbms, true); - if (!$already_connected) + if (!self::$already_connected) { $this->load_schema($pdo, $config, $dbms); - $already_connected = true; + self::$already_connected = true; } return $this->createDefaultDBConnection($pdo, 'testdb'); -- cgit v1.2.1