mirror of
https://github.com/phpbb/phpbb.git
synced 2025-07-25 19:38:53 +00:00
[feature/migrations] Fix if method (and create a test for it)
PHPBB3-9737
This commit is contained in:
parent
f56e400cd3
commit
445667a62e
4 changed files with 94 additions and 24 deletions
|
@ -364,6 +364,12 @@ class phpbb_db_migrator
|
|||
protected function run_step($step, $last_result = false)
|
||||
{
|
||||
$callable_and_parameters = $this->get_callable_from_step($step, $last_result);
|
||||
|
||||
if ($callable_and_parameters === false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$callable = $callable_and_parameters[0];
|
||||
$parameters = $callable_and_parameters[1];
|
||||
|
||||
|
@ -377,7 +383,7 @@ class phpbb_db_migrator
|
|||
* @param mixed $last_result Result to pass to the callable (only for 'custom' method)
|
||||
* @return array Array with parameters for call_user_func_array(), 0 is the callable, 1 is parameters
|
||||
*/
|
||||
public function get_callable_from_step($step, $last_result = false)
|
||||
protected function get_callable_from_step($step, $last_result = false)
|
||||
{
|
||||
$type = $step[0];
|
||||
$parameters = $step[1];
|
||||
|
@ -406,6 +412,12 @@ class phpbb_db_migrator
|
|||
}
|
||||
|
||||
$condition = $parameters[0];
|
||||
|
||||
if (!$condition)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$step = $parameters[1];
|
||||
|
||||
$callable_and_parameters = $this->get_callable_from_step($step);
|
||||
|
@ -413,10 +425,8 @@ class phpbb_db_migrator
|
|||
$sub_parameters = $callable_and_parameters[1];
|
||||
|
||||
return array(
|
||||
function ($condition) use ($callable, $sub_parameters) {
|
||||
return call_user_func_array($callable, $sub_parameters);
|
||||
},
|
||||
array($condition),
|
||||
$callable,
|
||||
$sub_parameters,
|
||||
);
|
||||
break;
|
||||
case 'custom':
|
||||
|
|
|
@ -19,21 +19,9 @@ class phpbb_dbal_migration_dummy extends phpbb_db_migration
|
|||
return array(
|
||||
'add_columns' => array(
|
||||
'phpbb_config' => array(
|
||||
'extra_column' => array('UINT', 0),
|
||||
'extra_column' => array('UINT', 1),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function update_data()
|
||||
{
|
||||
return array(
|
||||
array('if', array(true, array('custom', array(array($this, 'set_extra_column'))))),
|
||||
);
|
||||
}
|
||||
|
||||
public function set_extra_column()
|
||||
{
|
||||
$this->sql_query('UPDATE phpbb_config SET extra_column = 1');
|
||||
}
|
||||
}
|
||||
|
|
49
tests/dbal/migration/if.php
Normal file
49
tests/dbal/migration/if.php
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
/**
|
||||
*
|
||||
* @package testing
|
||||
* @copyright (c) 2011 phpBB Group
|
||||
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
||||
*
|
||||
*/
|
||||
|
||||
class phpbb_dbal_migration_if extends phpbb_db_migration
|
||||
{
|
||||
function depends_on()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
function update_schema()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
function update_data()
|
||||
{
|
||||
return array(
|
||||
array('if', array(
|
||||
true,
|
||||
array('custom', array(array(&$this, 'test_true'))),
|
||||
)),
|
||||
array('if', array(
|
||||
false,
|
||||
array('custom', array(array(&$this, 'test_false'))),
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
function test_true()
|
||||
{
|
||||
global $migrator_test_if_true_failed;
|
||||
|
||||
$migrator_test_if_true_failed = false;
|
||||
}
|
||||
|
||||
function test_false()
|
||||
{
|
||||
global $migrator_test_if_false_failed;
|
||||
|
||||
$migrator_test_if_false_failed = true;
|
||||
}
|
||||
}
|
|
@ -14,6 +14,7 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/db/db_tools.php';
|
|||
|
||||
require_once dirname(__FILE__) . '/migration/dummy.php';
|
||||
require_once dirname(__FILE__) . '/migration/unfulfillable.php';
|
||||
require_once dirname(__FILE__) . '/migration/if.php';
|
||||
|
||||
class phpbb_dbal_migrator_test extends phpbb_database_test_case
|
||||
{
|
||||
|
@ -41,12 +42,6 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case
|
|||
$this->migrator = new phpbb_db_migrator($this->config, $this->db, $this->db_tools, 'phpbb_migrations', dirname(__FILE__) . '/../../phpBB/', 'php', 'phpbb_', $tools);
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
// cleanup
|
||||
$this->db_tools->sql_column_remove('phpbb_config', 'extra_column');
|
||||
}
|
||||
|
||||
public function test_update()
|
||||
{
|
||||
$this->migrator->set_migrations(array('phpbb_dbal_migration_dummy'));
|
||||
|
@ -85,6 +80,9 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case
|
|||
AND migration_end_time <= " . (time() + 1),
|
||||
'End time set correctly'
|
||||
);
|
||||
|
||||
// cleanup
|
||||
$this->db_tools->sql_column_remove('phpbb_config', 'extra_column');
|
||||
}
|
||||
|
||||
public function test_unfulfillable()
|
||||
|
@ -104,4 +102,29 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case
|
|||
'Dummy migration was run, even though an unfulfillable migration was found.'
|
||||
);
|
||||
}
|
||||
|
||||
public function test_if()
|
||||
{
|
||||
$this->migrator->set_migrations(array('phpbb_dbal_migration_if'));
|
||||
|
||||
// Don't like this, but I'm not sure there is any other way to do this
|
||||
global $migrator_test_if_true_failed, $migrator_test_if_false_failed;
|
||||
$migrator_test_if_true_failed = true;
|
||||
$migrator_test_if_false_failed = false;
|
||||
|
||||
while (!$this->migrator->finished())
|
||||
{
|
||||
$this->migrator->update();
|
||||
}
|
||||
|
||||
if ($migrator_test_if_true_failed)
|
||||
{
|
||||
$this->fail('True test failed');
|
||||
}
|
||||
|
||||
if ($migrator_test_if_false_failed)
|
||||
{
|
||||
$this->fail('False test failed');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue