From bcaf65d7cd951b9a453cac413c420cb418b42f8d Mon Sep 17 00:00:00 2001 From: Patrick Webster Date: Sat, 3 Sep 2011 17:26:36 -0500 Subject: [ticket/10349] Unit tests: Remove comments while loading schema files Perform the same operations that the installer does when preparing the schema files. These functions come straight from /includes/functions_install.php and /includes/functions_admin.php. PHPBB3-10349 --- .../phpbb_database_test_connection_manager.php | 59 +++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) (limited to 'tests/test_framework') diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php index a7559e2183..78be831ecb 100644 --- a/tests/test_framework/phpbb_database_test_connection_manager.php +++ b/tests/test_framework/phpbb_database_test_connection_manager.php @@ -234,7 +234,12 @@ class phpbb_database_test_connection_manager } $filename = $directory . $schema . '_schema.sql'; - $sql = $this->split_sql(file_get_contents($filename)); + $remove_remarks = $this->dbms['COMMENTS']; + + $queries = file_get_contents($filename); + $this->$remove_remarks($queries); + + $sql = $this->split_sql($queries); foreach ($sql as $query) { @@ -279,6 +284,49 @@ class phpbb_database_test_connection_manager return $data; } + /** + * remove_remarks will strip the sql comment lines out of an uploaded sql file + */ + protected function remove_remarks(&$sql) + { + $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql)); + } + + /** + * remove_comments will strip the sql comment lines out of an uploaded sql file + * specifically for mssql and postgres type files in the install.... + */ + protected function remove_comments(&$output) + { + $lines = explode("\n", $output); + $output = ''; + + // try to keep mem. use down + $linecount = sizeof($lines); + + $in_comment = false; + for ($i = 0; $i < $linecount; $i++) + { + if (trim($lines[$i]) == '/*') + { + $in_comment = true; + } + + if (!$in_comment) + { + $output .= $lines[$i] . "\n"; + } + + if (trim($lines[$i]) == '*/') + { + $in_comment = false; + } + } + + unset($lines); + return $output; + } + /** * Map a phpBB dbms driver name to dbms data array */ @@ -289,46 +337,55 @@ class phpbb_database_test_connection_manager 'SCHEMA' => 'firebird', 'DELIM' => ';;', 'PDO' => 'firebird', + 'COMMENTS' => 'remove_remarks', ), 'mysqli' => array( 'SCHEMA' => 'mysql_41', 'DELIM' => ';', 'PDO' => 'mysql', + 'COMMENTS' => 'remove_remarks', ), 'mysql' => array( 'SCHEMA' => 'mysql', 'DELIM' => ';', 'PDO' => 'mysql', + 'COMMENTS' => 'remove_remarks', ), 'mssql' => array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', 'PDO' => 'odbc', + 'COMMENTS' => 'remove_comments', ), 'mssql_odbc'=> array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', 'PDO' => 'odbc', + 'COMMENTS' => 'remove_comments', ), 'mssqlnative' => array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', 'PDO' => 'sqlsrv', + 'COMMENTS' => 'remove_comments', ), 'oracle' => array( 'SCHEMA' => 'oracle', 'DELIM' => '/', 'PDO' => 'oci', + 'COMMENTS' => 'remove_comments', ), 'postgres' => array( 'SCHEMA' => 'postgres', 'DELIM' => ';', 'PDO' => 'pgsql', + 'COMMENTS' => 'remove_comments', ), 'sqlite' => array( 'SCHEMA' => 'sqlite', 'DELIM' => ';', 'PDO' => 'sqlite2', + 'COMMENTS' => 'remove_remarks', ), ); -- cgit v1.2.1 From 4bc11db0ffc3b387e4ad38b0d9ceccfa58f2738d Mon Sep 17 00:00:00 2001 From: Patrick Webster Date: Tue, 20 Sep 2011 23:23:03 -0500 Subject: [ticket/10349] Unit tests: Consolidate schema comment removal functions PHPBB3-10349 --- .../phpbb_database_test_connection_manager.php | 69 ++++------------------ 1 file changed, 11 insertions(+), 58 deletions(-) (limited to 'tests/test_framework') diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php index 78be831ecb..547361b41d 100644 --- a/tests/test_framework/phpbb_database_test_connection_manager.php +++ b/tests/test_framework/phpbb_database_test_connection_manager.php @@ -234,12 +234,11 @@ class phpbb_database_test_connection_manager } $filename = $directory . $schema . '_schema.sql'; - $remove_remarks = $this->dbms['COMMENTS']; $queries = file_get_contents($filename); - $this->$remove_remarks($queries); + $sql = $this->remove_comments($queries); - $sql = $this->split_sql($queries); + $sql = $this->split_sql($sql); foreach ($sql as $query) { @@ -271,60 +270,23 @@ class phpbb_database_test_connection_manager unset($data[key($data)]); } - if ($this->config['dbms'] == '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); - } - } - return $data; } /** - * remove_remarks will strip the sql comment lines out of an uploaded sql file - */ - protected function remove_remarks(&$sql) - { - $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql)); - } - - /** - * remove_comments will strip the sql comment lines out of an uploaded sql file - * specifically for mssql and postgres type files in the install.... + * Removes comments from schema files + * + * Note: This performs the functions of remove_remarks() and remove_comments() used during installation */ - protected function remove_comments(&$output) + protected function remove_comments($sql) { - $lines = explode("\n", $output); - $output = ''; - - // try to keep mem. use down - $linecount = sizeof($lines); + // Remove /* */ comments (http://ostermiller.org/findcomment.html) + $sql = preg_replace('#/\*(.|[\r\n])*?\*/#', "\n", $sql); - $in_comment = false; - for ($i = 0; $i < $linecount; $i++) - { - if (trim($lines[$i]) == '/*') - { - $in_comment = true; - } - - if (!$in_comment) - { - $output .= $lines[$i] . "\n"; - } - - if (trim($lines[$i]) == '*/') - { - $in_comment = false; - } - } + // Remove # style comments + $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql)); - unset($lines); - return $output; + return $sql; } /** @@ -337,55 +299,46 @@ class phpbb_database_test_connection_manager 'SCHEMA' => 'firebird', 'DELIM' => ';;', 'PDO' => 'firebird', - 'COMMENTS' => 'remove_remarks', ), 'mysqli' => array( 'SCHEMA' => 'mysql_41', 'DELIM' => ';', 'PDO' => 'mysql', - 'COMMENTS' => 'remove_remarks', ), 'mysql' => array( 'SCHEMA' => 'mysql', 'DELIM' => ';', 'PDO' => 'mysql', - 'COMMENTS' => 'remove_remarks', ), 'mssql' => array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', 'PDO' => 'odbc', - 'COMMENTS' => 'remove_comments', ), 'mssql_odbc'=> array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', 'PDO' => 'odbc', - 'COMMENTS' => 'remove_comments', ), 'mssqlnative' => array( 'SCHEMA' => 'mssql', 'DELIM' => 'GO', 'PDO' => 'sqlsrv', - 'COMMENTS' => 'remove_comments', ), 'oracle' => array( 'SCHEMA' => 'oracle', 'DELIM' => '/', 'PDO' => 'oci', - 'COMMENTS' => 'remove_comments', ), 'postgres' => array( 'SCHEMA' => 'postgres', 'DELIM' => ';', 'PDO' => 'pgsql', - 'COMMENTS' => 'remove_comments', ), 'sqlite' => array( 'SCHEMA' => 'sqlite', 'DELIM' => ';', 'PDO' => 'sqlite2', - 'COMMENTS' => 'remove_remarks', ), ); -- cgit v1.2.1 From b4dcd4193e7ea52753eb3957bb247b768dc48d12 Mon Sep 17 00:00:00 2001 From: Patrick Webster Date: Wed, 28 Sep 2011 11:27:39 -0500 Subject: [ticket/10349] Update function comment PHPBB3-10349 --- tests/test_framework/phpbb_database_test_connection_manager.php | 2 -- 1 file changed, 2 deletions(-) (limited to 'tests/test_framework') diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php index 547361b41d..5feab16496 100644 --- a/tests/test_framework/phpbb_database_test_connection_manager.php +++ b/tests/test_framework/phpbb_database_test_connection_manager.php @@ -275,8 +275,6 @@ class phpbb_database_test_connection_manager /** * Removes comments from schema files - * - * Note: This performs the functions of remove_remarks() and remove_comments() used during installation */ protected function remove_comments($sql) { -- cgit v1.2.1 From baac9e5d150dab88ed6f1eca8ca61bc4468ab01c Mon Sep 17 00:00:00 2001 From: Patrick Webster Date: Sat, 29 Oct 2011 17:10:10 -0500 Subject: [ticket/10349] Removed duplicated functions from schema loading in tests PHPBB3-10349 --- .../phpbb_database_test_connection_manager.php | 47 ++-------------------- 1 file changed, 4 insertions(+), 43 deletions(-) (limited to 'tests/test_framework') diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php index 5feab16496..770cc84d7e 100644 --- a/tests/test_framework/phpbb_database_test_connection_manager.php +++ b/tests/test_framework/phpbb_database_test_connection_manager.php @@ -7,6 +7,8 @@ * */ +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_install.php'; + class phpbb_database_test_connection_manager { private $config; @@ -236,9 +238,9 @@ class phpbb_database_test_connection_manager $filename = $directory . $schema . '_schema.sql'; $queries = file_get_contents($filename); - $sql = $this->remove_comments($queries); + $sql = remove_comments($queries); - $sql = $this->split_sql($sql); + $sql = split_sql_file($sql, $this->dbms['DELIM']); foreach ($sql as $query) { @@ -246,47 +248,6 @@ class phpbb_database_test_connection_manager } } - /** - * Split contents of an SQL file into an array of SQL statements - * - * Note: This method is not the same as split_sql_file from functions_install. - * - * @param string $sql Raw contents of an SQL file - * - * @return Array of runnable SQL statements - */ - protected function split_sql($sql) - { - $sql = str_replace("\r" , '', $sql); - $data = preg_split('/' . preg_quote($this->dbms['DELIM'], '/') . '$/m', $sql); - - $data = array_map('trim', $data); - - // The empty case - $end_data = end($data); - - if (empty($end_data)) - { - unset($data[key($data)]); - } - - return $data; - } - - /** - * Removes comments from schema files - */ - protected function remove_comments($sql) - { - // Remove /* */ comments (http://ostermiller.org/findcomment.html) - $sql = preg_replace('#/\*(.|[\r\n])*?\*/#', "\n", $sql); - - // Remove # style comments - $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql)); - - return $sql; - } - /** * Map a phpBB dbms driver name to dbms data array */ -- cgit v1.2.1