2011-07-15 11:57:53 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package testing
|
|
|
|
* @copyright (c) 2011 phpBB Group
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../phpBB/includes/db/migrator.php';
|
2013-01-08 22:09:14 -06:00
|
|
|
require_once dirname(__FILE__) . '/../../phpBB/includes/db/migration/migration.php';
|
2011-07-15 11:57:53 -04:00
|
|
|
require_once dirname(__FILE__) . '/../../phpBB/includes/db/db_tools.php';
|
|
|
|
|
|
|
|
require_once dirname(__FILE__) . '/migration/dummy.php';
|
|
|
|
require_once dirname(__FILE__) . '/migration/unfulfillable.php';
|
2013-01-09 16:31:56 -06:00
|
|
|
require_once dirname(__FILE__) . '/migration/if.php';
|
2013-01-10 12:49:13 -06:00
|
|
|
require_once dirname(__FILE__) . '/migration/recall.php';
|
2013-01-10 13:52:11 -06:00
|
|
|
require_once dirname(__FILE__) . '/migration/revert.php';
|
|
|
|
require_once dirname(__FILE__) . '/migration/revert_with_dependency.php';
|
2013-01-10 15:09:51 -06:00
|
|
|
require_once dirname(__FILE__) . '/migration/fail.php';
|
2011-07-15 11:57:53 -04:00
|
|
|
|
|
|
|
class phpbb_dbal_migrator_test extends phpbb_database_test_case
|
|
|
|
{
|
|
|
|
protected $db;
|
|
|
|
protected $db_tools;
|
|
|
|
protected $migrator;
|
|
|
|
|
|
|
|
public function getDataSet()
|
|
|
|
{
|
|
|
|
return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/migrator.xml');
|
|
|
|
}
|
|
|
|
|
2011-08-07 19:10:38 -04:00
|
|
|
public function setUp()
|
2011-07-15 11:57:53 -04:00
|
|
|
{
|
2013-01-08 22:09:14 -06:00
|
|
|
parent::setUp();
|
2011-07-15 11:57:53 -04:00
|
|
|
|
|
|
|
$this->db = $this->new_dbal();
|
|
|
|
$this->db_tools = new phpbb_db_tools($this->db);
|
2013-01-08 22:09:14 -06:00
|
|
|
|
|
|
|
$this->config = new phpbb_config_db($this->db, new phpbb_mock_cache, 'phpbb_config');
|
|
|
|
|
|
|
|
$tools = array(
|
|
|
|
new phpbb_db_migration_tool_config($this->config),
|
|
|
|
);
|
|
|
|
$this->migrator = new phpbb_db_migrator($this->config, $this->db, $this->db_tools, 'phpbb_migrations', dirname(__FILE__) . '/../../phpBB/', 'php', 'phpbb_', $tools);
|
2011-07-15 11:57:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_update()
|
|
|
|
{
|
|
|
|
$this->migrator->set_migrations(array('phpbb_dbal_migration_dummy'));
|
|
|
|
|
|
|
|
// schema
|
|
|
|
$this->migrator->update();
|
|
|
|
$this->assertFalse($this->migrator->finished());
|
|
|
|
|
2011-08-07 19:10:38 -04:00
|
|
|
$this->assertSqlResultEquals(
|
|
|
|
array(array('success' => '1')),
|
|
|
|
"SELECT 1 as success
|
|
|
|
FROM phpbb_migrations
|
|
|
|
WHERE migration_name = 'phpbb_dbal_migration_dummy'
|
|
|
|
AND migration_start_time >= " . (time() - 1) . "
|
|
|
|
AND migration_start_time <= " . (time() + 1),
|
|
|
|
'Start time set correctly'
|
|
|
|
);
|
|
|
|
|
2011-07-15 11:57:53 -04:00
|
|
|
// data
|
|
|
|
$this->migrator->update();
|
|
|
|
$this->assertTrue($this->migrator->finished());
|
|
|
|
|
|
|
|
$this->assertSqlResultEquals(
|
|
|
|
array(array('extra_column' => '1')),
|
|
|
|
"SELECT extra_column FROM phpbb_config WHERE config_name = 'foo'",
|
|
|
|
'Dummy migration created extra_column with value 1 in all rows.'
|
|
|
|
);
|
|
|
|
|
2011-08-07 19:10:38 -04:00
|
|
|
$this->assertSqlResultEquals(
|
|
|
|
array(array('success' => '1')),
|
|
|
|
"SELECT 1 as success
|
|
|
|
FROM phpbb_migrations
|
|
|
|
WHERE migration_name = 'phpbb_dbal_migration_dummy'
|
|
|
|
AND migration_start_time <= migration_end_time
|
|
|
|
AND migration_end_time >= " . (time() - 1) . "
|
|
|
|
AND migration_end_time <= " . (time() + 1),
|
|
|
|
'End time set correctly'
|
|
|
|
);
|
2013-01-09 16:31:56 -06:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
$this->db_tools->sql_column_remove('phpbb_config', 'extra_column');
|
2011-07-15 11:57:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_unfulfillable()
|
|
|
|
{
|
|
|
|
$this->migrator->set_migrations(array('phpbb_dbal_migration_unfulfillable', 'phpbb_dbal_migration_dummy'));
|
|
|
|
|
|
|
|
while (!$this->migrator->finished())
|
|
|
|
{
|
|
|
|
$this->migrator->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($this->migrator->finished());
|
|
|
|
|
|
|
|
$this->assertSqlResultEquals(
|
|
|
|
array(array('extra_column' => '1')),
|
|
|
|
"SELECT extra_column FROM phpbb_config WHERE config_name = 'foo'",
|
|
|
|
'Dummy migration was run, even though an unfulfillable migration was found.'
|
|
|
|
);
|
2013-01-10 13:52:11 -06:00
|
|
|
|
|
|
|
$this->db_tools->sql_column_remove('phpbb_config', 'extra_column');
|
2011-07-15 11:57:53 -04:00
|
|
|
}
|
2013-01-09 16:31:56 -06:00
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
2013-01-10 12:49:13 -06:00
|
|
|
|
|
|
|
public function test_recall()
|
|
|
|
{
|
|
|
|
$this->migrator->set_migrations(array('phpbb_dbal_migration_recall'));
|
|
|
|
|
|
|
|
global $migrator_test_call_input;
|
|
|
|
|
|
|
|
// Run the schema first
|
|
|
|
$this->migrator->update();
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
while (!$this->migrator->finished())
|
|
|
|
{
|
|
|
|
$this->migrator->update();
|
|
|
|
|
|
|
|
$this->assertSame($i, $migrator_test_call_input);
|
|
|
|
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSame(10, $migrator_test_call_input);
|
|
|
|
}
|
2013-01-10 13:52:11 -06:00
|
|
|
|
|
|
|
public function test_revert()
|
|
|
|
{
|
|
|
|
// Make sure there are no other migrations in the db, this could cause issues
|
|
|
|
$this->db->sql_query("DELETE FROM phpbb_migrations");
|
|
|
|
$this->migrator->load_migration_state();
|
|
|
|
|
|
|
|
$this->migrator->set_migrations(array('phpbb_dbal_migration_revert', 'phpbb_dbal_migration_revert_with_dependency'));
|
|
|
|
|
2013-01-10 15:09:51 -06:00
|
|
|
$this->assertFalse($this->migrator->migration_state('phpbb_dbal_migration_revert'));
|
|
|
|
$this->assertFalse($this->migrator->migration_state('phpbb_dbal_migration_revert_with_dependency'));
|
2013-01-10 13:52:11 -06:00
|
|
|
|
|
|
|
// Install the migration first
|
|
|
|
while (!$this->migrator->finished())
|
|
|
|
{
|
|
|
|
$this->migrator->update();
|
|
|
|
}
|
|
|
|
|
2013-01-10 15:09:51 -06:00
|
|
|
$this->assertTrue($this->migrator->migration_state('phpbb_dbal_migration_revert') !== false);
|
|
|
|
$this->assertTrue($this->migrator->migration_state('phpbb_dbal_migration_revert_with_dependency') !== false);
|
2013-01-10 13:52:11 -06:00
|
|
|
|
|
|
|
$this->assertSqlResultEquals(
|
|
|
|
array(array('bar_column' => '1')),
|
|
|
|
"SELECT bar_column FROM phpbb_config WHERE config_name = 'foo'",
|
|
|
|
'Installing revert migration failed to create bar_column.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue(isset($this->config['foobartest']));
|
|
|
|
|
2013-01-10 15:09:51 -06:00
|
|
|
while ($this->migrator->migration_state('phpbb_dbal_migration_revert') !== false)
|
2013-01-10 13:52:11 -06:00
|
|
|
{
|
|
|
|
$this->migrator->revert('phpbb_dbal_migration_revert');
|
|
|
|
}
|
|
|
|
|
2013-01-10 15:09:51 -06:00
|
|
|
$this->assertFalse($this->migrator->migration_state('phpbb_dbal_migration_revert'));
|
|
|
|
$this->assertFalse($this->migrator->migration_state('phpbb_dbal_migration_revert_with_dependency'));
|
2013-01-10 13:52:11 -06:00
|
|
|
|
|
|
|
$this->assertFalse(isset($this->config['foobartest']));
|
|
|
|
|
2013-01-10 15:09:51 -06:00
|
|
|
$sql = 'SELECT * FROM phpbb_config';
|
|
|
|
$result = $this->db->sql_query_limit($sql, 1);
|
|
|
|
$row = $this->db->sql_fetchrow($result);
|
|
|
|
$this->db->sql_freeresult($result);
|
|
|
|
|
|
|
|
if (isset($row['bar_column']))
|
|
|
|
{
|
|
|
|
$this->fail('Revert did not remove test_column.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_fail()
|
|
|
|
{
|
|
|
|
$this->migrator->set_migrations(array('phpbb_dbal_migration_fail'));
|
|
|
|
|
|
|
|
$this->assertFalse(isset($this->config['foobar3']));
|
|
|
|
|
2013-01-10 13:52:11 -06:00
|
|
|
try
|
|
|
|
{
|
2013-01-10 15:09:51 -06:00
|
|
|
while (!$this->migrator->finished())
|
|
|
|
{
|
|
|
|
$this->migrator->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (phpbb_db_migration_exception $e) {}
|
|
|
|
|
|
|
|
// Failure should have caused an automatic roll-back, so this should not exist.
|
|
|
|
$this->assertFalse(isset($this->config['foobar3']));
|
|
|
|
|
|
|
|
$sql = 'SELECT * FROM phpbb_config';
|
|
|
|
$result = $this->db->sql_query_limit($sql, 1);
|
|
|
|
$row = $this->db->sql_fetchrow($result);
|
|
|
|
$this->db->sql_freeresult($result);
|
|
|
|
|
|
|
|
if (isset($row['test_column']))
|
|
|
|
{
|
|
|
|
$this->fail('Revert did not remove test_column.');
|
2013-01-10 13:52:11 -06:00
|
|
|
}
|
|
|
|
}
|
2011-07-15 11:57:53 -04:00
|
|
|
}
|