mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-08 20:38:52 +00:00
Merge branch 'develop-olympus' into develop
* develop-olympus: [task/mssql-db-tests] Remove MS SQL helper values from SELECT LIMIT results. [task/mssql-db-tests] Split up database tests into SELECT and write operations [task/mssql-db-tests] PHPUnit output got stuck after unterminated ob_start. [task/mssql-db-tests] sql_query_limit must return all results when total = 0 [task/mssql-db-tests] Add support for odbc & sqlsrv PDO test connections [task/mssql-db-tests] Refactored getConnection into multiple smaller parts. [task/mssql-db-tests] Allow test configuration with environment variables. [task/mssql-db-tests] No longer display an error when skipping db tests. [task/mssql-db-tests] Use a simple getter for test case helpers. Conflicts: tests/template/template.php
This commit is contained in:
commit
f164906d77
9 changed files with 491 additions and 323 deletions
|
@ -349,7 +349,8 @@ class dbal_mssqlnative extends dbal
|
||||||
{
|
{
|
||||||
$this->query_result = false;
|
$this->query_result = false;
|
||||||
|
|
||||||
if ($offset === false || $offset == 0)
|
// total == 0 means all results - not zero results
|
||||||
|
if ($offset == 0 && $total !== 0)
|
||||||
{
|
{
|
||||||
if (strpos($query, "SELECT") === false)
|
if (strpos($query, "SELECT") === false)
|
||||||
{
|
{
|
||||||
|
@ -360,13 +361,21 @@ class dbal_mssqlnative extends dbal
|
||||||
$query = preg_replace('/SELECT(\s*DISTINCT)?/Dsi', 'SELECT$1 TOP '.$total, $query);
|
$query = preg_replace('/SELECT(\s*DISTINCT)?/Dsi', 'SELECT$1 TOP '.$total, $query);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else if ($offset > 0)
|
||||||
{
|
{
|
||||||
$query = preg_replace('/SELECT(\s*DISTINCT)?/Dsi', 'SELECT$1 TOP(10000000) ', $query);
|
$query = preg_replace('/SELECT(\s*DISTINCT)?/Dsi', 'SELECT$1 TOP(10000000) ', $query);
|
||||||
$query = 'SELECT *
|
$query = 'SELECT *
|
||||||
FROM (SELECT sub2.*, ROW_NUMBER() OVER(ORDER BY sub2.line2) AS line3
|
FROM (SELECT sub2.*, ROW_NUMBER() OVER(ORDER BY sub2.line2) AS line3
|
||||||
FROM (SELECT 1 AS line2, sub1.* FROM (' . $query . ') AS sub1) as sub2) AS sub3
|
FROM (SELECT 1 AS line2, sub1.* FROM (' . $query . ') AS sub1) as sub2) AS sub3';
|
||||||
WHERE line3 BETWEEN ' . ($offset+1) . ' AND ' . ($offset + $total);
|
|
||||||
|
if ($total > 0)
|
||||||
|
{
|
||||||
|
$query .= ' WHERE line3 BETWEEN ' . ($offset+1) . ' AND ' . ($offset + $total);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$query .= ' WHERE line3 > ' . $offset;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $this->sql_query($query, $cache_ttl);
|
$result = $this->sql_query($query, $cache_ttl);
|
||||||
|
@ -406,13 +415,18 @@ class dbal_mssqlnative extends dbal
|
||||||
|
|
||||||
$row = @sqlsrv_fetch_array($query_id, SQLSRV_FETCH_ASSOC);
|
$row = @sqlsrv_fetch_array($query_id, SQLSRV_FETCH_ASSOC);
|
||||||
|
|
||||||
// I hope i am able to remove this later... hopefully only a PHP or MSSQL bug
|
|
||||||
if ($row)
|
if ($row)
|
||||||
{
|
{
|
||||||
foreach ($row as $key => $value)
|
foreach ($row as $key => $value)
|
||||||
{
|
{
|
||||||
$row[$key] = ($value === ' ' || $value === NULL) ? '' : $value;
|
$row[$key] = ($value === ' ' || $value === NULL) ? '' : $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// remove helper values from LIMIT queries
|
||||||
|
if (isset($row['line2']))
|
||||||
|
{
|
||||||
|
unset($row['line2'], $row['line3']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return $row;
|
return $row;
|
||||||
}
|
}
|
||||||
|
@ -624,4 +638,4 @@ class dbal_mssqlnative extends dbal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -1,33 +1,57 @@
|
||||||
Running Tests
|
Running Tests
|
||||||
-------------
|
=============
|
||||||
|
|
||||||
Prerequisites
|
Prerequisites
|
||||||
-------------
|
=============
|
||||||
|
|
||||||
PHPUnit
|
PHPUnit
|
||||||
=======
|
-------
|
||||||
|
|
||||||
phpBB unit tests use PHPUnit framework. Version 3.3 or better is required
|
phpBB unit tests use PHPUnit framework. Version 3.3 or better is required
|
||||||
to run the tests. PHPUnit prefers to be installed via PEAR; refer to
|
to run the tests. PHPUnit prefers to be installed via PEAR; refer to
|
||||||
http://www.phpunit.de/ for more information.
|
http://www.phpunit.de/ for more information.
|
||||||
|
|
||||||
PHP extensions
|
PHP extensions
|
||||||
==============
|
--------------
|
||||||
|
|
||||||
Unit tests use several PHP extensions that board code does not use. Currently
|
Unit tests use several PHP extensions that board code does not use. Currently
|
||||||
the following PHP extensions must be installed and enabled to run unit tests:
|
the following PHP extensions must be installed and enabled to run unit tests:
|
||||||
|
|
||||||
- ctype
|
- ctype
|
||||||
|
|
||||||
|
Database Tests
|
||||||
|
--------------
|
||||||
|
By default all tests requiring a database connection will use sqlite. If you
|
||||||
|
do not have sqlite installed the tests will be skipped. If you wish to run the
|
||||||
|
tests on a different database you have to create a test_config.php file within
|
||||||
|
your tests directory following the same format as phpBB's config.php. An example
|
||||||
|
for mysqli can be found below. More information on configuration options can be
|
||||||
|
found on the wiki (see below).
|
||||||
|
|
||||||
|
<?php
|
||||||
|
$dbms = 'mysqli';
|
||||||
|
$dbhost = 'localhost';
|
||||||
|
$dbport = '';
|
||||||
|
$dbname = 'database';
|
||||||
|
$dbuser = 'user';
|
||||||
|
$dbpasswd = 'password';
|
||||||
|
|
||||||
|
Alternatively you can specify parameters in the environment, so e.g. the following
|
||||||
|
will run phpunit with the same parameters as in the shown test_config.php file:
|
||||||
|
|
||||||
|
$ PHPBB_TEST_DBMS='mysqli' PHPBB_TEST_DBHOST='localhost' \
|
||||||
|
PHPBB_TEST_DBNAME='database' PHPBB_TEST_DBUSER='user' \
|
||||||
|
PHPBB_TEST_DBPASSWD='password' phpunit all_tests.php
|
||||||
|
|
||||||
Running
|
Running
|
||||||
-------
|
=======
|
||||||
|
|
||||||
Once the prerequisites are installed, run the tests from tests directory:
|
Once the prerequisites are installed, run the tests from tests directory:
|
||||||
|
|
||||||
$ phpunit all_tests.php
|
$ phpunit all_tests.php
|
||||||
|
|
||||||
More Information
|
More Information
|
||||||
----------------
|
================
|
||||||
|
|
||||||
Further information is available on phpbb wiki:
|
Further information is available on phpbb wiki:
|
||||||
http://wiki.phpbb.com/display/DEV/Unit+Tests
|
http://wiki.phpbb.com/display/DEV/Unit+Tests
|
||||||
|
|
|
@ -15,7 +15,8 @@ if (!defined('PHPUnit_MAIN_METHOD'))
|
||||||
require_once 'test_framework/framework.php';
|
require_once 'test_framework/framework.php';
|
||||||
require_once 'PHPUnit/TextUI/TestRunner.php';
|
require_once 'PHPUnit/TextUI/TestRunner.php';
|
||||||
|
|
||||||
require_once 'dbal/dbal.php';
|
require_once 'dbal/select.php';
|
||||||
|
require_once 'dbal/write.php';
|
||||||
|
|
||||||
class phpbb_dbal_all_tests
|
class phpbb_dbal_all_tests
|
||||||
{
|
{
|
||||||
|
@ -28,7 +29,8 @@ class phpbb_dbal_all_tests
|
||||||
{
|
{
|
||||||
$suite = new PHPUnit_Framework_TestSuite('phpBB Database Abstraction Layer');
|
$suite = new PHPUnit_Framework_TestSuite('phpBB Database Abstraction Layer');
|
||||||
|
|
||||||
$suite->addTestSuite('phpbb_dbal_test');
|
$suite->addTestSuite('phpbb_dbal_select_test');
|
||||||
|
$suite->addTestSuite('phpbb_dbal_write_test');
|
||||||
|
|
||||||
return $suite;
|
return $suite;
|
||||||
}
|
}
|
||||||
|
|
18
tests/dbal/fixtures/config.xml
Normal file
18
tests/dbal/fixtures/config.xml
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<dataset>
|
||||||
|
<table name="phpbb_config">
|
||||||
|
<column>config_name</column>
|
||||||
|
<column>config_value</column>
|
||||||
|
<column>is_dynamic</column>
|
||||||
|
<row>
|
||||||
|
<value>config1</value>
|
||||||
|
<value>foo</value>
|
||||||
|
<value>0</value>
|
||||||
|
</row>
|
||||||
|
<row>
|
||||||
|
<value>config2</value>
|
||||||
|
<value>bar</value>
|
||||||
|
<value>1</value>
|
||||||
|
</row>
|
||||||
|
</table>
|
||||||
|
</dataset>
|
|
@ -10,7 +10,7 @@
|
||||||
require_once 'test_framework/framework.php';
|
require_once 'test_framework/framework.php';
|
||||||
require_once '../phpBB/includes/functions.php';
|
require_once '../phpBB/includes/functions.php';
|
||||||
|
|
||||||
class phpbb_dbal_test extends phpbb_database_test_case
|
class phpbb_dbal_select_test extends phpbb_database_test_case
|
||||||
{
|
{
|
||||||
public function getDataSet()
|
public function getDataSet()
|
||||||
{
|
{
|
||||||
|
@ -318,174 +318,4 @@ class phpbb_dbal_test extends phpbb_database_test_case
|
||||||
|
|
||||||
$db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function build_array_insert_data()
|
|
||||||
{
|
|
||||||
return array(
|
|
||||||
array(array(
|
|
||||||
'config_name' => 'test_version',
|
|
||||||
'config_value' => '0.0.0',
|
|
||||||
'is_dynamic' => 1,
|
|
||||||
)),
|
|
||||||
array(array(
|
|
||||||
'config_name' => 'second config',
|
|
||||||
'config_value' => '10',
|
|
||||||
'is_dynamic' => 0,
|
|
||||||
)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider build_array_insert_data
|
|
||||||
*/
|
|
||||||
public function test_build_array_insert($sql_ary)
|
|
||||||
{
|
|
||||||
$db = $this->new_dbal();
|
|
||||||
|
|
||||||
$sql = 'INSERT INTO phpbb_config ' . $db->sql_build_array('INSERT', $sql_ary);
|
|
||||||
$result = $db->sql_query($sql);
|
|
||||||
|
|
||||||
$sql = "SELECT *
|
|
||||||
FROM phpbb_config
|
|
||||||
WHERE config_name = '" . $sql_ary['config_name'] . "'";
|
|
||||||
$result = $db->sql_query_limit($sql, 1);
|
|
||||||
|
|
||||||
$this->assertEquals($sql_ary, $db->sql_fetchrow($result));
|
|
||||||
|
|
||||||
$db->sql_freeresult($result);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function delete_data()
|
|
||||||
{
|
|
||||||
return array(
|
|
||||||
array(
|
|
||||||
"WHERE config_name = 'test_version'",
|
|
||||||
array(
|
|
||||||
array(
|
|
||||||
'config_name' => 'second config',
|
|
||||||
'config_value' => '10',
|
|
||||||
'is_dynamic' => 0,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'',
|
|
||||||
array(),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider delete_data
|
|
||||||
*/
|
|
||||||
public function test_delete($where, $expected)
|
|
||||||
{
|
|
||||||
$db = $this->new_dbal();
|
|
||||||
|
|
||||||
$sql = 'DELETE FROM phpbb_config
|
|
||||||
' . $where;
|
|
||||||
$result = $db->sql_query($sql);
|
|
||||||
|
|
||||||
$sql = 'SELECT *
|
|
||||||
FROM phpbb_config';
|
|
||||||
$result = $db->sql_query($sql);
|
|
||||||
|
|
||||||
$this->assertEquals($expected, $db->sql_fetchrowset($result));
|
|
||||||
|
|
||||||
$db->sql_freeresult($result);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test_multiple_insert()
|
|
||||||
{
|
|
||||||
$db = $this->new_dbal();
|
|
||||||
|
|
||||||
$batch_ary = array(
|
|
||||||
array(
|
|
||||||
'config_name' => 'batch one',
|
|
||||||
'config_value' => 'b1',
|
|
||||||
'is_dynamic' => 0,
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'config_name' => 'batch two',
|
|
||||||
'config_value' => 'b2',
|
|
||||||
'is_dynamic' => 1,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
$result = $db->sql_multi_insert('phpbb_config', $batch_ary);
|
|
||||||
|
|
||||||
$sql = 'SELECT *
|
|
||||||
FROM phpbb_config
|
|
||||||
ORDER BY config_name ASC';
|
|
||||||
$result = $db->sql_query($sql);
|
|
||||||
|
|
||||||
$this->assertEquals($batch_ary, $db->sql_fetchrowset($result));
|
|
||||||
|
|
||||||
$db->sql_freeresult($result);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function update_data()
|
|
||||||
{
|
|
||||||
return array(
|
|
||||||
array(
|
|
||||||
array(
|
|
||||||
'config_value' => '20',
|
|
||||||
'is_dynamic' => 0,
|
|
||||||
),
|
|
||||||
" WHERE config_name = 'batch one'",
|
|
||||||
array(
|
|
||||||
array(
|
|
||||||
'config_name' => 'batch one',
|
|
||||||
'config_value' => '20',
|
|
||||||
'is_dynamic' => 0,
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'config_name' => 'batch two',
|
|
||||||
'config_value' => 'b2',
|
|
||||||
'is_dynamic' => 1,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
array(
|
|
||||||
'config_value' => '0',
|
|
||||||
'is_dynamic' => 1,
|
|
||||||
),
|
|
||||||
'',
|
|
||||||
array(
|
|
||||||
array(
|
|
||||||
'config_name' => 'batch one',
|
|
||||||
'config_value' => '0',
|
|
||||||
'is_dynamic' => 1,
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'config_name' => 'batch two',
|
|
||||||
'config_value' => '0',
|
|
||||||
'is_dynamic' => 1,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider update_data
|
|
||||||
*/
|
|
||||||
public function test_update($sql_ary, $where, $expected)
|
|
||||||
{
|
|
||||||
$db = $this->new_dbal();
|
|
||||||
|
|
||||||
$sql = 'UPDATE phpbb_config
|
|
||||||
SET ' . $db->sql_build_array('UPDATE', $sql_ary) . $where;
|
|
||||||
$result = $db->sql_query($sql);
|
|
||||||
|
|
||||||
$sql = 'SELECT *
|
|
||||||
FROM phpbb_config
|
|
||||||
ORDER BY config_name ASC';
|
|
||||||
$result = $db->sql_query($sql);
|
|
||||||
|
|
||||||
$this->assertEquals($expected, $db->sql_fetchrowset($result));
|
|
||||||
|
|
||||||
$db->sql_freeresult($result);
|
|
||||||
}
|
|
||||||
}
|
}
|
172
tests/dbal/write.php
Normal file
172
tests/dbal/write.php
Normal file
|
@ -0,0 +1,172 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @package testing
|
||||||
|
* @copyright (c) 2008 phpBB Group
|
||||||
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
require_once 'test_framework/framework.php';
|
||||||
|
require_once '../phpBB/includes/functions.php';
|
||||||
|
|
||||||
|
class phpbb_dbal_write_test extends phpbb_database_test_case
|
||||||
|
{
|
||||||
|
public function getDataSet()
|
||||||
|
{
|
||||||
|
return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function build_array_insert_data()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(array(
|
||||||
|
'config_name' => 'test_version',
|
||||||
|
'config_value' => '0.0.0',
|
||||||
|
'is_dynamic' => 1,
|
||||||
|
)),
|
||||||
|
array(array(
|
||||||
|
'config_name' => 'second config',
|
||||||
|
'config_value' => '10',
|
||||||
|
'is_dynamic' => 0,
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider build_array_insert_data
|
||||||
|
*/
|
||||||
|
public function test_build_array_insert($sql_ary)
|
||||||
|
{
|
||||||
|
$db = $this->new_dbal();
|
||||||
|
|
||||||
|
$sql = 'INSERT INTO phpbb_config ' . $db->sql_build_array('INSERT', $sql_ary);
|
||||||
|
$result = $db->sql_query($sql);
|
||||||
|
|
||||||
|
$sql = "SELECT *
|
||||||
|
FROM phpbb_config
|
||||||
|
WHERE config_name = '" . $sql_ary['config_name'] . "'";
|
||||||
|
$result = $db->sql_query_limit($sql, 1);
|
||||||
|
|
||||||
|
$this->assertEquals($sql_ary, $db->sql_fetchrow($result));
|
||||||
|
|
||||||
|
$db->sql_freeresult($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_delete()
|
||||||
|
{
|
||||||
|
$db = $this->new_dbal();
|
||||||
|
|
||||||
|
$sql = "DELETE FROM phpbb_config
|
||||||
|
WHERE config_name = 'config1'";
|
||||||
|
$result = $db->sql_query($sql);
|
||||||
|
|
||||||
|
$sql = 'SELECT *
|
||||||
|
FROM phpbb_config';
|
||||||
|
$result = $db->sql_query($sql);
|
||||||
|
$rows = $db->sql_fetchrowset($result);
|
||||||
|
|
||||||
|
$this->assertEquals(1, sizeof($rows));
|
||||||
|
$this->assertEquals('config2', $rows[0]['config_name']);
|
||||||
|
|
||||||
|
$db->sql_freeresult($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_multiple_insert()
|
||||||
|
{
|
||||||
|
$db = $this->new_dbal();
|
||||||
|
|
||||||
|
// empty the table
|
||||||
|
$sql = 'DELETE FROM phpbb_config';
|
||||||
|
$db->sql_query($sql);
|
||||||
|
|
||||||
|
$batch_ary = array(
|
||||||
|
array(
|
||||||
|
'config_name' => 'batch one',
|
||||||
|
'config_value' => 'b1',
|
||||||
|
'is_dynamic' => 0,
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'config_name' => 'batch two',
|
||||||
|
'config_value' => 'b2',
|
||||||
|
'is_dynamic' => 1,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
$result = $db->sql_multi_insert('phpbb_config', $batch_ary);
|
||||||
|
|
||||||
|
$sql = 'SELECT *
|
||||||
|
FROM phpbb_config
|
||||||
|
ORDER BY config_name ASC';
|
||||||
|
$result = $db->sql_query($sql);
|
||||||
|
|
||||||
|
$this->assertEquals($batch_ary, $db->sql_fetchrowset($result));
|
||||||
|
|
||||||
|
$db->sql_freeresult($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function update_data()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(
|
||||||
|
array(
|
||||||
|
'config_value' => '23',
|
||||||
|
'is_dynamic' => 0,
|
||||||
|
),
|
||||||
|
" WHERE config_name = 'config1'",
|
||||||
|
array(
|
||||||
|
array(
|
||||||
|
'config_name' => 'config1',
|
||||||
|
'config_value' => '23',
|
||||||
|
'is_dynamic' => 0,
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'config_name' => 'config2',
|
||||||
|
'config_value' => 'bar',
|
||||||
|
'is_dynamic' => 1,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
array(
|
||||||
|
'config_value' => '0',
|
||||||
|
'is_dynamic' => 1,
|
||||||
|
),
|
||||||
|
'',
|
||||||
|
array(
|
||||||
|
array(
|
||||||
|
'config_name' => 'config1',
|
||||||
|
'config_value' => '0',
|
||||||
|
'is_dynamic' => 1,
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'config_name' => 'config2',
|
||||||
|
'config_value' => '0',
|
||||||
|
'is_dynamic' => 1,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider update_data
|
||||||
|
*/
|
||||||
|
public function test_update($sql_ary, $where, $expected)
|
||||||
|
{
|
||||||
|
$db = $this->new_dbal();
|
||||||
|
|
||||||
|
$sql = 'UPDATE phpbb_config
|
||||||
|
SET ' . $db->sql_build_array('UPDATE', $sql_ary) . $where;
|
||||||
|
$result = $db->sql_query($sql);
|
||||||
|
|
||||||
|
$sql = 'SELECT *
|
||||||
|
FROM phpbb_config
|
||||||
|
ORDER BY config_name ASC';
|
||||||
|
$result = $db->sql_query($sql);
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $db->sql_fetchrowset($result));
|
||||||
|
|
||||||
|
$db->sql_freeresult($result);
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,14 +9,18 @@
|
||||||
|
|
||||||
abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_TestCase
|
abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_TestCase
|
||||||
{
|
{
|
||||||
|
private static $already_connected;
|
||||||
|
|
||||||
protected $test_case_helpers;
|
protected $test_case_helpers;
|
||||||
|
|
||||||
public function init_test_case_helpers()
|
public function get_test_case_helpers()
|
||||||
{
|
{
|
||||||
if (!$this->test_case_helpers)
|
if (!$this->test_case_helpers)
|
||||||
{
|
{
|
||||||
$this->test_case_helpers = new phpbb_test_case_helpers($this);
|
$this->test_case_helpers = new phpbb_test_case_helpers($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->test_case_helpers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_dbms_data($dbms)
|
public function get_dbms_data($dbms)
|
||||||
|
@ -50,7 +54,7 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
|
||||||
'mssqlnative' => array(
|
'mssqlnative' => array(
|
||||||
'SCHEMA' => 'mssql',
|
'SCHEMA' => 'mssql',
|
||||||
'DELIM' => 'GO',
|
'DELIM' => 'GO',
|
||||||
'PDO' => 'odbc',
|
'PDO' => 'sqlsrv',
|
||||||
),
|
),
|
||||||
'oracle' => array(
|
'oracle' => array(
|
||||||
'SCHEMA' => 'oracle',
|
'SCHEMA' => 'oracle',
|
||||||
|
@ -79,6 +83,50 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function get_database_config()
|
||||||
|
{
|
||||||
|
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');
|
||||||
|
|
||||||
|
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
|
// NOTE: This function is not the same as split_sql_file from functions_install
|
||||||
public function split_sql_file($sql, $dbms)
|
public function split_sql_file($sql, $dbms)
|
||||||
{
|
{
|
||||||
|
@ -99,7 +147,8 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
|
||||||
|
|
||||||
if ($dbms == 'sqlite')
|
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)
|
foreach ($data as $i => $query)
|
||||||
{
|
{
|
||||||
$data[$i] = preg_replace('/^#.*$/m', "\n", $query);
|
$data[$i] = preg_replace('/^#.*$/m', "\n", $query);
|
||||||
|
@ -109,79 +158,198 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getConnection()
|
/**
|
||||||
|
* Retrieves a list of all tables from the database.
|
||||||
|
*
|
||||||
|
* @param PDO $pdo
|
||||||
|
* @param string $dbms
|
||||||
|
* @return array(string)
|
||||||
|
*/
|
||||||
|
function get_tables($pdo, $dbms)
|
||||||
{
|
{
|
||||||
static $already_connected;
|
switch ($pdo)
|
||||||
|
|
||||||
$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)
|
|
||||||
{
|
{
|
||||||
if ($database_config['dbms'] == 'sqlite')
|
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.
|
||||||
|
*
|
||||||
|
* @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, $use_db)
|
||||||
|
{
|
||||||
|
$dsn = $dbms['PDO'] . ':';
|
||||||
|
|
||||||
|
switch ($dbms['PDO'])
|
||||||
|
{
|
||||||
|
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:
|
||||||
|
$dsn .= 'host=' . $config['dbhost'];
|
||||||
|
|
||||||
|
if ($use_db)
|
||||||
|
{
|
||||||
|
$dsn .= ';dbname=' . $config['dbname'];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$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'])
|
||||||
|
{
|
||||||
|
case 'sqlite':
|
||||||
|
if (file_exists($config['dbhost']))
|
||||||
|
{
|
||||||
|
unlink($config['dbhost']);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
$pdo = $this->new_pdo($config, $dbms, false);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$pdo->exec('DROP DATABASE ' . $config['dbname']);
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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', '>='))
|
||||||
{
|
{
|
||||||
$pdo = new PDO($dbms_data['PDO'] . ':' . $database_config['dbhost']);
|
$dbms['SCHEMA'] .= '_41';
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$pdo = new PDO($dbms_data['PDO'] . ':host=' . $database_config['dbhost'] . ';dbname=' . $database_config['dbname'], $database_config['dbuser'], $database_config['dbpasswd']);
|
$dbms['SCHEMA'] .= '_40';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
$sql = $this->split_sql_file(file_get_contents("../phpBB/install/schemas/{$dbms['SCHEMA']}_schema.sql"), $config['dbms']);
|
||||||
|
|
||||||
|
foreach ($sql as $query)
|
||||||
{
|
{
|
||||||
if ($database_config['dbms'] == 'sqlite')
|
$pdo->exec($query);
|
||||||
{
|
}
|
||||||
// delete existing database
|
}
|
||||||
if (file_exists($database_config['dbhost']))
|
|
||||||
{
|
|
||||||
unlink($database_config['dbhost']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$pdo = new PDO($dbms_data['PDO'] . ':' . $database_config['dbhost']);
|
public function getConnection()
|
||||||
}
|
{
|
||||||
else
|
$config = $this->get_database_config();
|
||||||
{
|
$dbms = $this->get_dbms_data($config['dbms']);
|
||||||
$pdo = new PDO($dbms_data['PDO'] . ':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']);
|
if (!self::$already_connected)
|
||||||
|
{
|
||||||
|
$this->recreate_db($config, $dbms);
|
||||||
|
}
|
||||||
|
|
||||||
$pdo = new PDO($dbms_data['PDO'] . ':host=' . $database_config['dbhost'] . ';dbname=' . $database_config['dbname'], $database_config['dbuser'], $database_config['dbpasswd']);
|
$pdo = $this->new_pdo($config, $dbms, true);
|
||||||
}
|
|
||||||
|
|
||||||
// good for debug
|
if (!self::$already_connected)
|
||||||
// $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
{
|
||||||
|
$this->load_schema($pdo, $config, $dbms);
|
||||||
|
|
||||||
if ($database_config['dbms'] == 'mysql')
|
self::$already_connected = true;
|
||||||
{
|
|
||||||
$sth = $pdo->query('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, $sth);
|
|
||||||
}
|
|
||||||
|
|
||||||
$sql_query = $this->split_sql_file(file_get_contents("../phpBB/install/schemas/{$dbms_data['SCHEMA']}_schema.sql"), $database_config['dbms']);
|
|
||||||
|
|
||||||
foreach ($sql_query as $sql)
|
|
||||||
{
|
|
||||||
$pdo->exec($sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
$already_connected = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->createDefaultDBConnection($pdo, 'testdb');
|
return $this->createDefaultDBConnection($pdo, 'testdb');
|
||||||
|
@ -189,13 +357,20 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
|
||||||
|
|
||||||
public function new_dbal()
|
public function new_dbal()
|
||||||
{
|
{
|
||||||
$this->init_test_case_helpers();
|
global $phpbb_root_path, $phpEx;
|
||||||
return $this->test_case_helpers->new_dbal();
|
|
||||||
|
$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 = '')
|
public function setExpectedTriggerError($errno, $message = '')
|
||||||
{
|
{
|
||||||
$this->init_test_case_helpers();
|
$this->get_test_case_helpers()->setExpectedTriggerError($errno, $message);
|
||||||
$this->test_case_helpers->setExpectedTriggerError($errno, $message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,17 +11,18 @@ class phpbb_test_case extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
protected $test_case_helpers;
|
protected $test_case_helpers;
|
||||||
|
|
||||||
public function init_test_case_helpers()
|
public function get_test_case_helpers()
|
||||||
{
|
{
|
||||||
if (!$this->test_case_helpers)
|
if (!$this->test_case_helpers)
|
||||||
{
|
{
|
||||||
$this->test_case_helpers = new phpbb_test_case_helpers($this);
|
$this->test_case_helpers = new phpbb_test_case_helpers($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->test_case_helpers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setExpectedTriggerError($errno, $message = '')
|
public function setExpectedTriggerError($errno, $message = '')
|
||||||
{
|
{
|
||||||
$this->init_test_case_helpers();
|
$this->get_test_case_helpers()->setExpectedTriggerError($errno, $message);
|
||||||
$this->test_case_helpers->setExpectedTriggerError($errno, $message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,74 +18,6 @@ class phpbb_test_case_helpers
|
||||||
$this->test_case = $test_case;
|
$this->test_case = $test_case;
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
{
|
|
||||||
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:
|
|
||||||
\"<?php
|
|
||||||
\$dbms = 'mysqli';
|
|
||||||
\$dbhost = 'localhost';
|
|
||||||
\$dbport = '';
|
|
||||||
\$dbname = 'database';
|
|
||||||
\$dbuser = 'user';
|
|
||||||
\$dbpasswd = 'password';
|
|
||||||
\"
|
|
||||||
|
|
||||||
NOTE: The database is dropped and recreated with the phpbb-db-schema! Do NOT specify a database with important data.", E_USER_ERROR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 = '')
|
public function setExpectedTriggerError($errno, $message = '')
|
||||||
{
|
{
|
||||||
$exceptionName = '';
|
$exceptionName = '';
|
||||||
|
|
Loading…
Add table
Reference in a new issue