2010-12-28 17:40:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for the migration model
|
|
|
|
*
|
|
|
|
* @group minion
|
2011-02-17 01:04:19 +02:00
|
|
|
* @group minion.tasks
|
|
|
|
* @group minion.tasks.migrations
|
|
|
|
*/
|
2010-12-28 17:40:54 +02:00
|
|
|
class Minion_Migration_ModelTest extends Kohana_Unittest_Database_TestCase
|
|
|
|
{
|
2011-02-17 01:04:19 +02:00
|
|
|
/**
|
|
|
|
* Runs before the test class as a whole is ran
|
|
|
|
* Creates the test table
|
|
|
|
*/
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
$sql = file_get_contents(Kohana::find_file('', 'minion_schema', 'sql'));
|
|
|
|
|
|
|
|
$sql = str_replace('`minion_migrations`', '`test_minion_migrations`', $sql);
|
|
|
|
|
|
|
|
Database::instance()->query(NULL, 'DROP TABLE IF EXISTS `test_minion_migrations`');
|
|
|
|
Database::instance()->query(NULL, $sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the test tables after the tests have finished
|
|
|
|
*/
|
|
|
|
public static function tearDownAfterClass()
|
|
|
|
{
|
|
|
|
Database::instance()->query(NULL, 'DROP TABLE `test_minion_migrations`');
|
|
|
|
}
|
|
|
|
|
2010-12-28 17:40:54 +02:00
|
|
|
/**
|
|
|
|
* Gets the dataset that should be used to populate db
|
|
|
|
*
|
|
|
|
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
|
|
|
|
*/
|
|
|
|
public function getDataSet()
|
|
|
|
{
|
|
|
|
return $this->createFlatXMLDataSet(
|
2011-02-17 22:43:04 +02:00
|
|
|
Kohana::find_file('tests/datasets', 'minion/migration/model', 'xml')
|
2010-12-28 17:40:54 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an instance of the migration model, pre-loaded with a connection to
|
|
|
|
* the test database
|
|
|
|
*
|
|
|
|
* @return Model_Minion_Migration
|
|
|
|
*/
|
|
|
|
public function getModel()
|
|
|
|
{
|
2011-01-10 17:40:22 +02:00
|
|
|
$model = new Model_Minion_Migration($this->getKohanaConnection());
|
|
|
|
|
|
|
|
return $model->table('test_minion_migrations');
|
2010-12-28 17:40:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that the model can fetch all rows from the database
|
|
|
|
*
|
|
|
|
* @test
|
|
|
|
* @covers Model_Minion_Migration::fetch_all
|
|
|
|
*/
|
|
|
|
public function test_fetch_all()
|
|
|
|
{
|
|
|
|
$migrations = $this->getModel()->fetch_all();
|
|
|
|
|
2011-02-18 04:07:41 +02:00
|
|
|
$this->assertSame(10, count($migrations));
|
2010-12-28 17:40:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the model accurately fetches the latest versions from the database
|
|
|
|
*
|
|
|
|
* @test
|
|
|
|
* @covers Model_Minion_Migration::fetch_current_versions
|
|
|
|
*/
|
|
|
|
public function test_fetch_current_versions()
|
|
|
|
{
|
|
|
|
$versions = $this->getModel()
|
2011-02-17 22:43:04 +02:00
|
|
|
->fetch_current_versions('group', 'timestamp');
|
2010-12-28 17:40:54 +02:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
array (
|
2010-12-30 17:04:09 +02:00
|
|
|
'app' => '20101216080000',
|
|
|
|
'dblogger' => '20101225000000',
|
2011-02-18 04:07:41 +02:00
|
|
|
'done' => '20101546112100',
|
2010-12-28 17:40:54 +02:00
|
|
|
),
|
|
|
|
$versions
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-02-17 23:16:21 +02:00
|
|
|
/**
|
|
|
|
* Provides test data for test_fetch_required_migrations
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provider_fetch_required_migrations()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
// Test going up in a specific group
|
|
|
|
array(
|
2011-02-18 01:53:15 +02:00
|
|
|
array(
|
|
|
|
array(
|
2011-02-18 04:07:41 +02:00
|
|
|
array(
|
|
|
|
'timestamp' => '20101215165000',
|
|
|
|
'description' => 'add-name-column-to-members',
|
|
|
|
'group' => 'app',
|
|
|
|
'applied' => '0',
|
|
|
|
'id' => 'app:20101215165000',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101216000000',
|
|
|
|
'description' => 'add-index-on-name',
|
|
|
|
'group' => 'app',
|
|
|
|
'applied' => '0',
|
|
|
|
'id' => 'app:20101216000000',
|
|
|
|
),
|
2011-02-17 23:16:21 +02:00
|
|
|
),
|
2011-02-18 04:07:41 +02:00
|
|
|
TRUE
|
2011-02-17 23:16:21 +02:00
|
|
|
),
|
|
|
|
'app',
|
|
|
|
TRUE,
|
|
|
|
),
|
|
|
|
// Testing going down with a specific group
|
|
|
|
array(
|
2011-02-18 01:53:15 +02:00
|
|
|
array(
|
|
|
|
array(
|
2011-02-18 04:07:41 +02:00
|
|
|
array(
|
|
|
|
'timestamp' => '20101216080000',
|
|
|
|
'description' => 'remove-password-salt-column',
|
|
|
|
'group' => 'app',
|
|
|
|
'applied' => '1',
|
|
|
|
'id' => 'app:20101216080000',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101215164400',
|
|
|
|
'description' => 'create-tables',
|
|
|
|
'group' => 'app',
|
|
|
|
'applied' => '1',
|
|
|
|
'id' => 'app:20101215164400',
|
|
|
|
),
|
2011-02-17 23:16:21 +02:00
|
|
|
),
|
2011-02-18 04:07:41 +02:00
|
|
|
FALSE
|
2011-02-17 23:16:21 +02:00
|
|
|
),
|
|
|
|
'app',
|
|
|
|
FALSE
|
|
|
|
),
|
|
|
|
// Testing going up across all groups
|
|
|
|
array(
|
2011-02-18 01:53:15 +02:00
|
|
|
array(
|
|
|
|
array(
|
2011-02-18 04:07:41 +02:00
|
|
|
array(
|
|
|
|
'timestamp' => '20101215165000',
|
|
|
|
'description' => 'add-name-column-to-members',
|
|
|
|
'group' => 'app',
|
|
|
|
'applied' => '0',
|
|
|
|
'id' => 'app:20101215165000',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101216000000',
|
|
|
|
'description' => 'add-index-on-name',
|
|
|
|
'group' => 'app',
|
|
|
|
'applied' => '0',
|
|
|
|
'id' => 'app:20101216000000',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101226112100',
|
|
|
|
'description' => 'add-pk',
|
|
|
|
'group' => 'dblogger',
|
|
|
|
'applied' => '0',
|
|
|
|
'id' => 'dblogger:20101226112100',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101526112100',
|
|
|
|
'description' => 'add-col',
|
|
|
|
'group' => 'minion',
|
|
|
|
'applied' => '0',
|
|
|
|
'id' => 'minion:20101526112100',
|
|
|
|
),
|
2011-02-17 23:16:21 +02:00
|
|
|
),
|
2011-02-18 04:07:41 +02:00
|
|
|
TRUE
|
2011-02-17 23:16:21 +02:00
|
|
|
),
|
|
|
|
NULL,
|
|
|
|
TRUE
|
|
|
|
),
|
|
|
|
// Testing going down across all groups
|
|
|
|
array(
|
2011-02-18 01:53:15 +02:00
|
|
|
array(
|
|
|
|
array(
|
2011-02-18 04:07:41 +02:00
|
|
|
array(
|
|
|
|
'timestamp' => '20101546112100',
|
|
|
|
'description' => 'add-bbb',
|
|
|
|
'group' => 'done',
|
|
|
|
'applied' => '1',
|
|
|
|
'id' => 'done:20101546112100'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101536112100',
|
|
|
|
'description' => 'add-aaa',
|
|
|
|
'group' => 'done',
|
|
|
|
'applied' => '1',
|
|
|
|
'id'=> 'done:20101536112100'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101225000000',
|
|
|
|
'description' => 'remove-unique-index',
|
|
|
|
'group' => 'dblogger',
|
|
|
|
'applied' => '1',
|
|
|
|
'id' => 'dblogger:20101225000000',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101216080000',
|
|
|
|
'description' => 'remove-password-salt-column',
|
|
|
|
'group' => 'app',
|
|
|
|
'applied' => '1',
|
|
|
|
'id' => 'app:20101216080000',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101215164500',
|
|
|
|
'description' => 'create-table',
|
|
|
|
'group' => 'dblogger',
|
|
|
|
'applied' => '1',
|
|
|
|
'id' => 'dblogger:20101215164500',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101215164400',
|
|
|
|
'description' => 'create-tables',
|
|
|
|
'group' => 'app',
|
|
|
|
'applied' => '1',
|
|
|
|
'id' => 'app:20101215164400',
|
|
|
|
),
|
2011-02-17 23:16:21 +02:00
|
|
|
),
|
2011-02-18 04:07:41 +02:00
|
|
|
FALSE,
|
2011-02-17 23:16:21 +02:00
|
|
|
),
|
|
|
|
NULL,
|
|
|
|
FALSE
|
2011-02-18 01:53:15 +02:00
|
|
|
),
|
2011-02-18 04:07:41 +02:00
|
|
|
// Test migrating down to a specific version
|
2011-02-18 01:53:15 +02:00
|
|
|
array(
|
|
|
|
array(
|
|
|
|
array(
|
2011-02-18 04:07:41 +02:00
|
|
|
array(
|
|
|
|
'timestamp' => '20101216080000',
|
|
|
|
'description' => 'remove-password-salt-column',
|
|
|
|
'group' => 'app',
|
|
|
|
'applied' => '1',
|
|
|
|
'id' => 'app:20101216080000',
|
|
|
|
),
|
2011-02-18 01:53:15 +02:00
|
|
|
),
|
2011-02-18 04:07:41 +02:00
|
|
|
FALSE
|
|
|
|
),
|
|
|
|
'app',
|
|
|
|
20101215164400
|
|
|
|
),
|
|
|
|
// Test migrating up from nothing to everything
|
|
|
|
array(
|
|
|
|
array(
|
2011-02-18 01:53:15 +02:00
|
|
|
array(
|
2011-02-18 04:07:41 +02:00
|
|
|
array(
|
|
|
|
'timestamp' => '20101526112100',
|
|
|
|
'description' => 'add-col',
|
|
|
|
'group' => 'minion',
|
|
|
|
'applied' => '0',
|
|
|
|
'id' => 'minion:20101526112100',
|
|
|
|
),
|
2011-02-18 01:53:15 +02:00
|
|
|
),
|
2011-02-18 04:07:41 +02:00
|
|
|
TRUE
|
2011-02-18 01:53:15 +02:00
|
|
|
),
|
2011-02-18 04:07:41 +02:00
|
|
|
'minion',
|
|
|
|
'+100'
|
2011-02-18 01:53:15 +02:00
|
|
|
),
|
2011-02-18 04:07:41 +02:00
|
|
|
array(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101546112100',
|
|
|
|
'description' => 'add-bbb',
|
|
|
|
'group' => 'done',
|
|
|
|
'applied' => '1',
|
|
|
|
'id' => 'done:20101546112100'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
FALSE
|
|
|
|
),
|
|
|
|
'done',
|
|
|
|
'-1'
|
|
|
|
)
|
2011-02-17 23:16:21 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that migrations are fetched in the right order depending on what the target is
|
|
|
|
*
|
|
|
|
* @test
|
|
|
|
* @dataProvider provider_fetch_required_migrations
|
|
|
|
* @covers Model_Minion_Migration::fetch_required_migrations
|
|
|
|
* @param array An expected resultset
|
|
|
|
* @param string|array A group, or groups to get migrations for
|
|
|
|
* @param bool|string Target to migrate to
|
|
|
|
*/
|
|
|
|
public function test_fetch_required_migrations($expected, $group, $target)
|
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
$expected,
|
2011-02-18 17:47:46 +02:00
|
|
|
$this->getModel()->fetch_required_migrations( (array) $group, $target)
|
2011-02-17 23:16:21 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-12-30 17:04:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides test data for test_get_migration
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provider_get_migration()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101215164400',
|
|
|
|
'description' => 'create-tables',
|
2011-02-17 22:43:04 +02:00
|
|
|
'group' => 'app',
|
2010-12-30 17:04:09 +02:00
|
|
|
'applied' => '1',
|
|
|
|
'id' => 'app:20101215164400'
|
|
|
|
),
|
|
|
|
'app',
|
|
|
|
'20101215164400',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that Model_Minion_Migration::get_migration can get a migration from
|
|
|
|
* the database
|
|
|
|
*
|
|
|
|
* @test
|
|
|
|
* @covers Model_Minion_Migration::get_migration
|
|
|
|
* @dataProvider provider_get_migration
|
|
|
|
* @param array Expected migration
|
2011-02-17 22:43:04 +02:00
|
|
|
* @param string The migration's group
|
2010-12-30 17:04:09 +02:00
|
|
|
* @param string The migration's timestamp
|
|
|
|
*/
|
2011-02-17 22:43:04 +02:00
|
|
|
public function test_get_migration($expected, $group, $timestamp)
|
2010-12-30 17:04:09 +02:00
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
$expected,
|
2011-02-17 22:43:04 +02:00
|
|
|
$this->getModel()->get_migration($group, $timestamp)
|
2010-12-30 17:04:09 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides test data for test_get_migration_throws_exception_on_invalid_input
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provider_get_migration_throws_exception_on_invalid_input()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(NULL, NULL),
|
|
|
|
array('app', NULL),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If invalid input is passed to get_migration then it should throw an
|
|
|
|
* exception
|
|
|
|
*
|
|
|
|
* @test
|
|
|
|
* @covers Model_Minion_Migration::get_migration
|
|
|
|
* @dataProvider provider_get_migration_throws_exception_on_invalid_input
|
|
|
|
* @expectedException Kohana_Exception
|
|
|
|
*/
|
2011-02-17 22:43:04 +02:00
|
|
|
public function test_get_migration_throws_exception_on_invalid_input($group, $timestamp)
|
2010-12-30 17:04:09 +02:00
|
|
|
{
|
2011-02-17 22:43:04 +02:00
|
|
|
$this->getModel()->get_migration($group, $timestamp);
|
2010-12-30 17:04:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides test data for test_mark_migration
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provider_mark_migration()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101215165000',
|
|
|
|
'description' => 'add-name-column-to-members',
|
2011-02-17 22:43:04 +02:00
|
|
|
'group' => 'app',
|
2010-12-30 17:04:09 +02:00
|
|
|
'applied' => '1',
|
|
|
|
'id' => 'app:20101215165000',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101215165000',
|
2011-02-17 22:43:04 +02:00
|
|
|
'group' => 'app',
|
2010-12-30 17:04:09 +02:00
|
|
|
'description' => 'add-name-column-to-members',
|
|
|
|
),
|
|
|
|
TRUE
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101215165000',
|
|
|
|
'description' => 'add-name-column-to-members',
|
2011-02-17 22:43:04 +02:00
|
|
|
'group' => 'app',
|
2010-12-30 17:04:09 +02:00
|
|
|
'applied' => '0',
|
|
|
|
'id' => 'app:20101215165000',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => '20101215165000',
|
2011-02-17 22:43:04 +02:00
|
|
|
'group' => 'app',
|
2010-12-30 17:04:09 +02:00
|
|
|
'description' => 'add-name-column-to-members',
|
|
|
|
),
|
|
|
|
FALSE
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that Model_Minion_Migration::mark_migration() changes the applied
|
|
|
|
* status of a migration
|
|
|
|
*
|
|
|
|
* @test
|
|
|
|
* @covers Model_Minion_Migration::mark_migration
|
|
|
|
* @dataProvider provider_mark_migration
|
|
|
|
* @param array What the DB record should look like after migration is marked
|
|
|
|
* @param array The migration to update
|
|
|
|
* @param bool Whether the migration should be applied
|
|
|
|
*/
|
|
|
|
public function test_mark_migration($expected, $migration, $applied)
|
|
|
|
{
|
|
|
|
$model = $this->getModel();
|
|
|
|
|
|
|
|
$model->mark_migration($migration, $applied);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expected,
|
2011-02-17 22:43:04 +02:00
|
|
|
$model->get_migration($migration['group'], $migration['timestamp'])
|
2010-12-30 17:04:09 +02:00
|
|
|
);
|
|
|
|
}
|
2011-02-18 01:53:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides test data for test_resolve_target()
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function provider_resolve_target()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(
|
2011-02-18 04:07:41 +02:00
|
|
|
array('20101216080000', FALSE),
|
2011-02-18 01:53:15 +02:00
|
|
|
'app',
|
|
|
|
'-1'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
array(NULL, FALSE),
|
|
|
|
'dblogger',
|
|
|
|
'-10'
|
|
|
|
),
|
|
|
|
array(
|
2011-02-18 04:07:41 +02:00
|
|
|
array('20101226112100', TRUE),
|
2011-02-18 01:53:15 +02:00
|
|
|
'dblogger',
|
|
|
|
'+1',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
array(NULL, TRUE),
|
|
|
|
'app',
|
|
|
|
'+10'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
array(NULL, TRUE),
|
|
|
|
'dblogger',
|
|
|
|
'+100'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that we can resolve a target version for a group.
|
|
|
|
*
|
|
|
|
* Target version can be relative (+migrations_up / -migrations_down) or absolute (i.e. timestamp)
|
|
|
|
*
|
|
|
|
* @test
|
|
|
|
* @dataProvider provider_resolve_target
|
|
|
|
* @covers Model_Minion_Migration::resolve_target
|
|
|
|
* @param array Expected output
|
|
|
|
* @param string Group name
|
|
|
|
* @param string Target version
|
|
|
|
*/
|
|
|
|
public function test_resolve_target($expected, $group, $target)
|
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
$expected,
|
2011-02-18 17:47:46 +02:00
|
|
|
$this->getModel()->resolve_target( (array) $group, $target)
|
2011-02-18 01:53:15 +02:00
|
|
|
);
|
|
|
|
}
|
2010-12-28 17:40:54 +02:00
|
|
|
}
|