2010-03-26 16:39:37 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package testing
|
|
|
|
* @copyright (c) 2008 phpBB Group
|
2011-12-31 16:05:02 +00:00
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
2010-03-26 16:39:37 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_TestCase
|
|
|
|
{
|
2011-02-14 00:00:59 +01:00
|
|
|
static private $already_connected;
|
2010-10-25 19:20:51 +02:00
|
|
|
|
2010-03-26 16:39:37 +01:00
|
|
|
protected $test_case_helpers;
|
|
|
|
|
2012-12-01 22:34:03 -06:00
|
|
|
protected $fixture_xml_data;
|
|
|
|
|
2011-01-07 01:07:21 +01:00
|
|
|
public function __construct($name = NULL, array $data = array(), $dataName = '')
|
|
|
|
{
|
|
|
|
parent::__construct($name, $data, $dataName);
|
|
|
|
$this->backupStaticAttributesBlacklist += array(
|
|
|
|
'PHP_CodeCoverage' => array('instance'),
|
|
|
|
'PHP_CodeCoverage_Filter' => array('instance'),
|
|
|
|
'PHP_CodeCoverage_Util' => array('ignoredLines', 'templateMethods'),
|
|
|
|
'PHP_Timer' => array('startTimes',),
|
|
|
|
'PHP_Token_Stream' => array('customTokens'),
|
|
|
|
'PHP_Token_Stream_CachingFactory' => array('cache'),
|
|
|
|
|
|
|
|
'phpbb_database_test_case' => array('already_connected'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-11-18 20:38:58 -06:00
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2012-12-01 22:34:03 -06:00
|
|
|
// Resynchronise tables if a fixture was loaded
|
|
|
|
if (isset($this->fixture_xml_data))
|
|
|
|
{
|
|
|
|
$config = $this->get_database_config();
|
|
|
|
$manager = $this->create_connection_manager($config);
|
|
|
|
$manager->connect();
|
|
|
|
$manager->post_setup_synchronisation($this->fixture_xml_data);
|
|
|
|
}
|
2012-11-18 20:38:58 -06:00
|
|
|
}
|
|
|
|
|
2012-02-28 06:18:24 -06:00
|
|
|
public function createXMLDataSet($path)
|
|
|
|
{
|
|
|
|
$db_config = $this->get_database_config();
|
|
|
|
|
2012-04-02 05:57:48 -05:00
|
|
|
// Firebird requires table and column names to be uppercase
|
2012-02-28 13:47:15 -06:00
|
|
|
if ($db_config['dbms'] == 'firebird')
|
2012-02-28 06:18:24 -06:00
|
|
|
{
|
|
|
|
$xml_data = file_get_contents($path);
|
|
|
|
$xml_data = preg_replace_callback('/(?:(<table name="))([a-z_]+)(?:(">))/', 'phpbb_database_test_case::to_upper', $xml_data);
|
|
|
|
$xml_data = preg_replace_callback('/(?:(<column>))([a-z_]+)(?:(<\/column>))/', 'phpbb_database_test_case::to_upper', $xml_data);
|
|
|
|
|
2012-05-13 16:56:07 -05:00
|
|
|
$new_fixture = tmpfile();
|
|
|
|
fwrite($new_fixture, $xml_data);
|
|
|
|
fseek($new_fixture, 0);
|
2012-02-28 06:18:24 -06:00
|
|
|
|
2012-05-13 16:56:07 -05:00
|
|
|
$meta_data = stream_get_meta_data($new_fixture);
|
2012-02-28 06:18:24 -06:00
|
|
|
$path = $meta_data['uri'];
|
|
|
|
}
|
|
|
|
|
2012-12-01 22:34:03 -06:00
|
|
|
$this->fixture_xml_data = parent::createXMLDataSet($path);
|
|
|
|
|
|
|
|
return $this->fixture_xml_data;
|
2012-02-28 06:18:24 -06:00
|
|
|
}
|
|
|
|
|
2010-10-22 19:11:18 +02:00
|
|
|
public function get_test_case_helpers()
|
2010-03-26 16:39:37 +01:00
|
|
|
{
|
|
|
|
if (!$this->test_case_helpers)
|
|
|
|
{
|
|
|
|
$this->test_case_helpers = new phpbb_test_case_helpers($this);
|
|
|
|
}
|
2010-10-22 19:11:18 +02:00
|
|
|
|
|
|
|
return $this->test_case_helpers;
|
2010-03-26 16:39:37 +01:00
|
|
|
}
|
|
|
|
|
2010-10-22 19:27:41 +02:00
|
|
|
public function get_database_config()
|
|
|
|
{
|
2012-04-20 23:50:49 -05:00
|
|
|
$config = phpbb_test_case_helpers::get_test_config();
|
2010-10-22 19:27:41 +02:00
|
|
|
|
2012-04-20 23:50:49 -05:00
|
|
|
if (!isset($config['dbms']))
|
2010-10-22 19:27:41 +02:00
|
|
|
{
|
|
|
|
$this->markTestSkipped('Missing test_config.php: See first error.');
|
|
|
|
}
|
2012-04-20 23:50:49 -05:00
|
|
|
|
|
|
|
return $config;
|
2010-10-22 19:27:41 +02:00
|
|
|
}
|
|
|
|
|
2010-10-22 20:34:52 +02:00
|
|
|
public function getConnection()
|
|
|
|
{
|
|
|
|
$config = $this->get_database_config();
|
2011-02-14 00:00:59 +01:00
|
|
|
|
|
|
|
$manager = $this->create_connection_manager($config);
|
2010-03-27 10:21:16 +01:00
|
|
|
|
2010-10-25 19:20:51 +02:00
|
|
|
if (!self::$already_connected)
|
2010-10-22 20:34:52 +02:00
|
|
|
{
|
2011-02-14 00:00:59 +01:00
|
|
|
$manager->recreate_db();
|
2010-10-22 20:34:52 +02:00
|
|
|
}
|
2010-03-26 17:37:01 +01:00
|
|
|
|
2011-02-14 00:00:59 +01:00
|
|
|
$manager->connect();
|
2010-10-22 20:34:52 +02:00
|
|
|
|
2010-10-25 19:20:51 +02:00
|
|
|
if (!self::$already_connected)
|
2010-10-22 20:34:52 +02:00
|
|
|
{
|
2011-02-14 00:00:59 +01:00
|
|
|
$manager->load_schema();
|
2010-10-25 19:20:51 +02:00
|
|
|
self::$already_connected = true;
|
2010-03-26 17:37:01 +01:00
|
|
|
}
|
|
|
|
|
2011-02-14 00:00:59 +01:00
|
|
|
return $this->createDefaultDBConnection($manager->get_pdo(), 'testdb');
|
2010-03-26 16:39:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function new_dbal()
|
|
|
|
{
|
2010-10-22 19:27:41 +02:00
|
|
|
global $phpbb_root_path, $phpEx;
|
|
|
|
|
|
|
|
$config = $this->get_database_config();
|
|
|
|
|
2011-01-31 12:58:18 +01:00
|
|
|
require_once dirname(__FILE__) . '/../../phpBB/includes/db/' . $config['dbms'] . '.php';
|
2010-10-22 19:27:41 +02:00
|
|
|
$dbal = 'dbal_' . $config['dbms'];
|
|
|
|
$db = new $dbal();
|
|
|
|
$db->sql_connect($config['dbhost'], $config['dbuser'], $config['dbpasswd'], $config['dbname'], $config['dbport']);
|
|
|
|
|
|
|
|
return $db;
|
2010-03-26 16:39:37 +01:00
|
|
|
}
|
|
|
|
|
2011-01-21 21:31:41 +01:00
|
|
|
public function assertSqlResultEquals($expected, $sql, $message = '')
|
2011-01-04 17:14:36 +01:00
|
|
|
{
|
|
|
|
$db = $this->new_dbal();
|
|
|
|
|
|
|
|
$result = $db->sql_query($sql);
|
|
|
|
$rows = $db->sql_fetchrowset($result);
|
|
|
|
$db->sql_freeresult($result);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $rows, $message);
|
|
|
|
}
|
|
|
|
|
2010-03-26 16:39:37 +01:00
|
|
|
public function setExpectedTriggerError($errno, $message = '')
|
|
|
|
{
|
2010-10-22 19:11:18 +02:00
|
|
|
$this->get_test_case_helpers()->setExpectedTriggerError($errno, $message);
|
2010-03-26 16:39:37 +01:00
|
|
|
}
|
2011-02-14 00:00:59 +01:00
|
|
|
|
|
|
|
protected function create_connection_manager($config)
|
|
|
|
{
|
|
|
|
return new phpbb_database_test_connection_manager($config);
|
|
|
|
}
|
2012-02-28 06:18:24 -06:00
|
|
|
|
2012-04-02 05:57:48 -05:00
|
|
|
/**
|
|
|
|
* Converts a match in the middle of a string to uppercase.
|
2012-05-13 16:56:07 -05:00
|
|
|
* This is necessary for transforming the fixture information for Firebird tests
|
2012-04-02 05:57:48 -05:00
|
|
|
*
|
|
|
|
* @param $matches The array of matches from a regular expression
|
|
|
|
*
|
|
|
|
* @return string The string with the specified match converted to uppercase
|
|
|
|
*/
|
2012-11-06 10:41:06 -05:00
|
|
|
static public function to_upper($matches)
|
2012-02-28 06:18:24 -06:00
|
|
|
{
|
|
|
|
return $matches[1] . strtoupper($matches[2]) . $matches[3];
|
|
|
|
}
|
2010-03-26 16:39:37 +01:00
|
|
|
}
|