/plugins/migrations/tests/cases/libs/migration_version.test.php

https://github.com/vistik/dashboard · PHP · 277 lines · 165 code · 43 blank · 69 comment · 3 complexity · b87e8d4ed3538f7c225aff9d84ac9270 MD5 · raw file

  1. <?php
  2. App::import('Lib', 'Migrations.MigrationVersion');
  3. Mock::generatePartial(
  4. 'MigrationVersion', 'TestMigrationVersionMockMigrationVersion',
  5. array('getMapping', 'getMigration')
  6. );
  7. class MigrationVersionTest extends CakeTestCase {
  8. /**
  9. * Fixtures property
  10. *
  11. * @var array
  12. */
  13. var $fixtures = array('plugin.migrations.schema_migrations');
  14. /**
  15. * MigrationVersion instance
  16. *
  17. * @var MigrationVersion
  18. */
  19. var $Version;
  20. /**
  21. * start test
  22. *
  23. * @return void
  24. **/
  25. function startTest() {
  26. $this->Version =& new MigrationVersion(array(
  27. 'connection' => 'test_suite'
  28. ));
  29. $plugins = $this->plugins = App::path('plugins');
  30. $plugins[] = dirname(dirname(dirname(__FILE__))) . DS . 'test_app' . DS . 'plugins' . DS;
  31. App::build(array('plugins' => $plugins), true);
  32. }
  33. /**
  34. * endTest method
  35. *
  36. * @return void
  37. **/
  38. function endTest() {
  39. App::build(array('plugins' => $this->plugins), true);
  40. unset($this->Version, $this->plugins);
  41. }
  42. /**
  43. * Test __construct method with no existing migrations table
  44. *
  45. * @return void
  46. */
  47. function testInitialTableCreation() {
  48. $db =& ConnectionManager::getDataSource('test_suite');
  49. $Schema =& new CakeSchema(array('connection' => 'test_suite'));
  50. $Schema->tables = array('schema_migrations' => array());
  51. $db->execute($db->dropSchema($Schema));
  52. $this->assertFalse(in_array($db->fullTableName('schema_migrations', false), $db->listSources()));
  53. $this->Version =& new MigrationVersion(array(
  54. 'connection' => 'test_suite'
  55. ));
  56. $this->assertTrue(in_array($db->fullTableName('schema_migrations', false), $db->listSources()));
  57. }
  58. /**
  59. * testGetMapping method
  60. *
  61. * @return void
  62. */
  63. function testGetMapping() {
  64. $result = $this->Version->getMapping('inexistent_plugin');
  65. $this->assertFalse($result);
  66. $result = $this->Version->getMapping('test_migration_plugin');
  67. $expected = array(
  68. 1 => array(
  69. 'version' => 1,
  70. 'name' => '001_schema_dump',
  71. 'class' => 'M4af6d40056b04408808500cb58157726',
  72. 'type' => 'test_migration_plugin',
  73. 'migrated' => null
  74. )
  75. );
  76. $this->assertEqual($result, $expected);
  77. $result = $this->Version->getMapping('migrations');
  78. $expected = array(
  79. 1 => array(
  80. 'version' => 1,
  81. 'name' => '001_init_migrations',
  82. 'class' => 'M4af6e0f0a1284147a0b100ca58157726',
  83. 'type' => 'migrations',
  84. 'migrated' => '2009-11-10 00:55:34'
  85. )
  86. );
  87. $this->assertEqual($result, $expected);
  88. }
  89. /**
  90. * testGetMigration method
  91. *
  92. * @return void
  93. */
  94. function testGetMigration() {
  95. $result = $this->Version->getMigration('inexistent_migration', 'InexistentMigration', 'test_migration_plugin');
  96. $this->assertFalse($result);
  97. $result = $this->Version->getMigration('001_schema_dump', 'M4af6d40056b04408808500cb58157726', 'test_migration_plugin');
  98. $this->assertIsA($result, 'M4af6d40056b04408808500cb58157726');
  99. $this->assertEqual($result->description, 'Version 001 (schema dump) of TestMigrationPlugin');
  100. // Calling twice to check if it will not try to redeclare the class
  101. $result = $this->Version->getMigration('001_schema_dump', 'M4af6d40056b04408808500cb58157726', 'test_migration_plugin');
  102. $this->assertIsA($result, 'M4af6d40056b04408808500cb58157726');
  103. $this->assertEqual($result->description, 'Version 001 (schema dump) of TestMigrationPlugin');
  104. }
  105. /**
  106. * testSetGetVersion method
  107. *
  108. * @return void
  109. */
  110. function testSetGetVersion() {
  111. $result = $this->Version->getVersion('inexistent_plugin');
  112. $expected = 0;
  113. $this->assertEqual($result, $expected);
  114. $this->assertTrue($this->Version->setVersion(1, 'inexistent_plugin'));
  115. $result = $this->Version->getVersion('inexistent_plugin');
  116. $expected = 1;
  117. $this->assertEqual($result, $expected);
  118. $this->assertTrue($this->Version->setVersion(2, 'inexistent_plugin'));
  119. $result = $this->Version->getVersion('inexistent_plugin');
  120. $expected = 2;
  121. $this->assertEqual($result, $expected);
  122. $this->assertTrue($this->Version->setVersion(2, 'inexistent_plugin', false));
  123. $result = $this->Version->getVersion('inexistent_plugin');
  124. $expected = 1;
  125. $this->assertEqual($result, $expected);
  126. }
  127. /**
  128. * testRun method
  129. *
  130. * @return void
  131. */
  132. function testRun() {
  133. $back = $this->Version;
  134. $options = array('connection' => 'test_suite');
  135. $Version =& new TestMigrationVersionMockMigrationVersion($options);
  136. $this->Version = $Version;
  137. $this->Version->setReturnValue('getMigration', new CakeMigration($options));
  138. $this->Version->Version =& ClassRegistry::init(array(
  139. 'class' => 'schema_migrations', 'ds' => 'test_suite'));
  140. // Variable used on setReturValueAt method
  141. $mappingCount = 0;
  142. // direction => up
  143. $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping());
  144. $this->assertEqual($Version->getVersion('mocks'), 0);
  145. $this->assertTrue($Version->run(array('direction' => 'up', 'type' => 'mocks')));
  146. $this->assertEqual($this->__migrated(), array(1));
  147. $this->assertEqual($Version->getVersion('mocks'), 1);
  148. // direction => down
  149. $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 1));
  150. $this->assertEqual($Version->getVersion('mocks'), 1);
  151. $this->assertTrue($Version->run(array('direction' => 'down', 'type' => 'mocks')));
  152. $this->assertEqual($this->__migrated(), array());
  153. $this->assertEqual($Version->getVersion('mocks'), 0);
  154. // Set 1, 2, 3 versions applied
  155. $this->Version->setVersion(1, 'mocks');
  156. $this->Version->setVersion(2, 'mocks');
  157. $this->Version->setVersion(3, 'mocks');
  158. // direction => up
  159. $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 3));
  160. $this->assertEqual($Version->getVersion('mocks'), 3);
  161. $this->assertTrue($Version->run(array('direction' => 'up', 'type' => 'mocks')));
  162. $this->assertEqual($this->__migrated(), range(1, 4));
  163. $this->assertEqual($Version->getVersion('mocks'), 4);
  164. // direction => down
  165. $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 4));
  166. $this->assertEqual($Version->getVersion('mocks'), 4);
  167. $this->assertTrue($Version->run(array('direction' => 'down', 'type' => 'mocks')));
  168. $this->assertEqual($this->__migrated(), range(1, 3));
  169. $this->assertEqual($Version->getVersion('mocks'), 3);
  170. // version => 7
  171. $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 3));
  172. $this->assertEqual($Version->getVersion('mocks'), 3);
  173. $this->assertTrue($Version->run(array('version' => 7, 'type' => 'mocks')));
  174. $this->assertEqual($this->__migrated(), range(1, 7));
  175. $this->assertEqual($Version->getVersion('mocks'), 7);
  176. // version => 3
  177. $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 7));
  178. $this->assertEqual($Version->getVersion('mocks'), 7);
  179. $this->assertTrue($Version->run(array('version' => 3, 'type' => 'mocks')));
  180. $this->assertEqual($this->__migrated(), range(1, 3));
  181. $this->assertEqual($Version->getVersion('mocks'), 3);
  182. // version => 10 (top version)
  183. $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 3));
  184. $this->assertEqual($Version->getVersion('mocks'), 3);
  185. $this->assertTrue($Version->run(array('version' => 10, 'type' => 'mocks')));
  186. $this->assertEqual($this->__migrated(), range(1, 10));
  187. $this->assertEqual($Version->getVersion('mocks'), 10);
  188. // version => 0 (run down all migrations)
  189. $Version->setReturnValueAt($mappingCount++, 'getMapping', $this->__mapping(1, 10));
  190. $this->assertEqual($Version->getVersion('mocks'), 10);
  191. $this->assertTrue($Version->run(array('version' => 0, 'type' => 'mocks')));
  192. $this->assertEqual($this->__migrated(), array());
  193. $this->assertEqual($Version->getVersion('mocks'), 0);
  194. // Changing values back
  195. $this->Version = $back;
  196. unset($back);
  197. }
  198. /**
  199. * __mapping method
  200. *
  201. * @param int $start
  202. * @param int $end
  203. * @return array
  204. */
  205. function __mapping($start = 0, $end = 0) {
  206. $mapping = array();
  207. for ($i = 1; $i <= 10; $i++) {
  208. $mapping[$i] = array(
  209. 'version' => $i, 'name' => '001_schema_dump',
  210. 'class' => 'M4af9d151e1484b74ad9d007058157726',
  211. 'type' => 'mocks', 'migrated' => null
  212. );
  213. if ($i >= $start && $i <= $end) {
  214. $mapping[$i]['migrated'] = date('r');
  215. }
  216. }
  217. return $mapping;
  218. }
  219. /**
  220. * __migrated method
  221. *
  222. * @return array
  223. */
  224. function __migrated() {
  225. $alias = $this->Version->Version->alias;
  226. $migrated = $this->Version->Version->find('all', array(
  227. 'fields' => array('version'),
  228. 'conditions' => array($alias . '.type' => 'mocks')
  229. ));
  230. $migrated = Set::extract('/' . $alias . '/version', $migrated);
  231. sort($migrated);
  232. return $migrated;
  233. }
  234. }