1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_TestCase
{
static private $already_connected;
private $db_connections;
protected $test_case_helpers;
protected $fixture_xml_data;
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'),
);
$this->db_connections = array();
}
protected function tearDown()
{
parent::tearDown();
// Close all database connections from this test
if (!empty($this->db_connections))
{
foreach ($this->db_connections as $db)
{
$db->sql_close();
}
}
}
protected function setUp()
{
parent::setUp();
// 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);
}
}
/**
* Performs synchronisations for a given table/column set on the database
*
* @param array $table_column_map Information about the tables/columns to synchronise
*
* @return null
*/
protected function database_synchronisation($table_column_map)
{
$config = $this->get_database_config();
$manager = $this->create_connection_manager($config);
$manager->connect();
$manager->database_synchronisation($table_column_map);
}
public function createXMLDataSet($path)
{
$db_config = $this->get_database_config();
// Firebird requires table and column names to be uppercase
if ($db_config['dbms'] == 'phpbb\db\driver\firebird')
{
$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);
$new_fixture = tmpfile();
fwrite($new_fixture, $xml_data);
fseek($new_fixture, 0);
$meta_data = stream_get_meta_data($new_fixture);
$path = $meta_data['uri'];
}
$this->fixture_xml_data = parent::createXMLDataSet($path);
return $this->fixture_xml_data;
}
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_database_config()
{
$config = phpbb_test_case_helpers::get_test_config();
if (!isset($config['dbms']))
{
$this->markTestSkipped('Missing test_config.php: See first error.');
}
return $config;
}
public function getConnection()
{
$config = $this->get_database_config();
$manager = $this->create_connection_manager($config);
if (!self::$already_connected)
{
$manager->recreate_db();
}
$manager->connect();
if (!self::$already_connected)
{
$manager->load_schema($this->new_dbal());
self::$already_connected = true;
}
return $this->createDefaultDBConnection($manager->get_pdo(), 'testdb');
}
public function new_dbal()
{
global $phpbb_root_path, $phpEx;
$config = $this->get_database_config();
$db = new $config['dbms']();
$db->sql_connect($config['dbhost'], $config['dbuser'], $config['dbpasswd'], $config['dbname'], $config['dbport']);
$this->db_connections[] = $db;
return $db;
}
public function assertSqlResultEquals($expected, $sql, $message = '')
{
$db = $this->new_dbal();
$result = $db->sql_query($sql);
$rows = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);
$this->assertEquals($expected, $rows, $message);
}
public function setExpectedTriggerError($errno, $message = '')
{
$this->get_test_case_helpers()->setExpectedTriggerError($errno, $message);
}
protected function create_connection_manager($config)
{
return new phpbb_database_test_connection_manager($config);
}
/**
* Converts a match in the middle of a string to uppercase.
* This is necessary for transforming the fixture information for Firebird tests
*
* @param $matches The array of matches from a regular expression
*
* @return string The string with the specified match converted to uppercase
*/
static public function to_upper($matches)
{
return $matches[1] . strtoupper($matches[2]) . $matches[3];
}
public function assert_array_content_equals($one, $two)
{
// http://stackoverflow.com/questions/3838288/phpunit-assert-two-arrays-are-equal-but-order-of-elements-not-important
// but one array_diff is not enough!
if (sizeof(array_diff($one, $two)) || sizeof(array_diff($two, $one)))
{
// get a nice error message
$this->assertEquals($one, $two);
}
else
{
// increase assertion count
$this->assertTrue(true);
}
}
}
|