MigratorTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. class Minz_MigratorTest extends TestCase
  4. {
  5. public function testAddMigration() {
  6. $migrator = new Minz_Migrator();
  7. $migrator->addMigration('foo', function () {
  8. return true;
  9. });
  10. $migrations = $migrator->migrations();
  11. $this->assertArrayHasKey('foo', $migrations);
  12. $result = $migrations['foo']();
  13. $this->assertTrue($result);
  14. }
  15. public function testAddMigrationFailsIfUncallableMigration() {
  16. $this->expectException(BadFunctionCallException::class);
  17. $this->expectExceptionMessage('foo migration cannot be called.');
  18. $migrator = new Minz_Migrator();
  19. $migrator->addMigration('foo', null);
  20. }
  21. public function testMigrationsIsSorted() {
  22. $migrator = new Minz_Migrator();
  23. $migrator->addMigration('2_foo', function () {
  24. return true;
  25. });
  26. $migrator->addMigration('10_foo', function () {
  27. return true;
  28. });
  29. $migrator->addMigration('1_foo', function () {
  30. return true;
  31. });
  32. $expected_versions = ['1_foo', '2_foo', '10_foo'];
  33. $migrations = $migrator->migrations();
  34. $this->assertSame($expected_versions, array_keys($migrations));
  35. }
  36. public function testSetAppliedVersions() {
  37. $migrator = new Minz_Migrator();
  38. $migrator->addMigration('foo', function () {
  39. return true;
  40. });
  41. $migrator->setAppliedVersions(['foo']);
  42. $this->assertSame(['foo'], $migrator->appliedVersions());
  43. }
  44. public function testSetAppliedVersionsTrimArgument() {
  45. $migrator = new Minz_Migrator();
  46. $migrator->addMigration('foo', function () {
  47. return true;
  48. });
  49. $migrator->setAppliedVersions(["foo\n"]);
  50. $this->assertSame(['foo'], $migrator->appliedVersions());
  51. }
  52. public function testSetAppliedVersionsFailsIfMigrationDoesNotExist() {
  53. $this->expectException(DomainException::class);
  54. $this->expectExceptionMessage('foo migration does not exist.');
  55. $migrator = new Minz_Migrator();
  56. $migrator->setAppliedVersions(['foo']);
  57. }
  58. public function testVersions() {
  59. $migrator = new Minz_Migrator();
  60. $migrator->addMigration('foo', function () {
  61. return true;
  62. });
  63. $migrator->addMigration('bar', function () {
  64. return true;
  65. });
  66. $versions = $migrator->versions();
  67. $this->assertSame(['bar', 'foo'], $versions);
  68. }
  69. public function testMigrate() {
  70. $migrator = new Minz_Migrator();
  71. $spy = false;
  72. $migrator->addMigration('foo', function () use (&$spy) {
  73. $spy = true;
  74. return true;
  75. });
  76. $this->assertEmpty($migrator->appliedVersions());
  77. $result = $migrator->migrate();
  78. $this->assertTrue($spy);
  79. $this->assertSame(['foo'], $migrator->appliedVersions());
  80. $this->assertSame([
  81. 'foo' => true,
  82. ], $result);
  83. }
  84. public function testMigrateCallsMigrationsInSortedOrder() {
  85. $migrator = new Minz_Migrator();
  86. $spy_foo_1_is_called = false;
  87. $migrator->addMigration('2_foo', function () use (&$spy_foo_1_is_called) {
  88. return $spy_foo_1_is_called;
  89. });
  90. $migrator->addMigration('1_foo', function () use (&$spy_foo_1_is_called) {
  91. $spy_foo_1_is_called = true;
  92. return true;
  93. });
  94. $result = $migrator->migrate();
  95. $this->assertSame(['1_foo', '2_foo'], $migrator->appliedVersions());
  96. $this->assertSame([
  97. '1_foo' => true,
  98. '2_foo' => true,
  99. ], $result);
  100. }
  101. public function testMigrateDoesNotCallAppliedMigrations() {
  102. $migrator = new Minz_Migrator();
  103. $spy = false;
  104. $migrator->addMigration('1_foo', function () use (&$spy) {
  105. $spy = true;
  106. return true;
  107. });
  108. $migrator->setAppliedVersions(['1_foo']);
  109. $result = $migrator->migrate();
  110. $this->assertFalse($spy);
  111. $this->assertSame([], $result);
  112. }
  113. public function testMigrateCallNonAppliedBetweenTwoApplied() {
  114. $migrator = new Minz_Migrator();
  115. $migrator->addMigration('1_foo', function () {
  116. return true;
  117. });
  118. $migrator->addMigration('2_foo', function () {
  119. return true;
  120. });
  121. $migrator->addMigration('3_foo', function () {
  122. return true;
  123. });
  124. $migrator->setAppliedVersions(['1_foo', '3_foo']);
  125. $result = $migrator->migrate();
  126. $this->assertSame(['1_foo', '2_foo', '3_foo'], $migrator->appliedVersions());
  127. $this->assertSame([
  128. '2_foo' => true,
  129. ], $result);
  130. }
  131. public function testMigrateWithMigrationReturningFalseDoesNotApplyVersion() {
  132. $migrator = new Minz_Migrator();
  133. $migrator->addMigration('1_foo', function () {
  134. return true;
  135. });
  136. $migrator->addMigration('2_foo', function () {
  137. return false;
  138. });
  139. $result = $migrator->migrate();
  140. $this->assertSame(['1_foo'], $migrator->appliedVersions());
  141. $this->assertSame([
  142. '1_foo' => true,
  143. '2_foo' => false,
  144. ], $result);
  145. }
  146. public function testMigrateWithMigrationReturningFalseDoesNotExecuteNextMigrations() {
  147. $migrator = new Minz_Migrator();
  148. $migrator->addMigration('1_foo', function () {
  149. return false;
  150. });
  151. $spy = false;
  152. $migrator->addMigration('2_foo', function () use (&$spy) {
  153. $spy = true;
  154. return true;
  155. });
  156. $result = $migrator->migrate();
  157. $this->assertEmpty($migrator->appliedVersions());
  158. $this->assertFalse($spy);
  159. $this->assertSame([
  160. '1_foo' => false,
  161. ], $result);
  162. }
  163. public function testMigrateWithFailingMigration() {
  164. $migrator = new Minz_Migrator();
  165. $migrator->addMigration('foo', function () {
  166. throw new \Exception('Oops, it failed.');
  167. });
  168. $result = $migrator->migrate();
  169. $this->assertEmpty($migrator->appliedVersions());
  170. $this->assertSame([
  171. 'foo' => 'Oops, it failed.',
  172. ], $result);
  173. }
  174. public function testUpToDate() {
  175. $migrator = new Minz_Migrator();
  176. $migrator->addMigration('foo', function () {
  177. return true;
  178. });
  179. $migrator->setAppliedVersions(['foo']);
  180. $upToDate = $migrator->upToDate();
  181. $this->assertTrue($upToDate);
  182. }
  183. public function testUpToDateIfRemainingMigration() {
  184. $migrator = new Minz_Migrator();
  185. $migrator->addMigration('1_foo', function () {
  186. return true;
  187. });
  188. $migrator->addMigration('2_foo', function () {
  189. return true;
  190. });
  191. $migrator->setAppliedVersions(['2_foo']);
  192. $upToDate = $migrator->upToDate();
  193. $this->assertFalse($upToDate);
  194. }
  195. public function testUpToDateIfNoMigrations() {
  196. $migrator = new Minz_Migrator();
  197. $upToDate = $migrator->upToDate();
  198. $this->assertTrue($upToDate);
  199. }
  200. public function testConstructorLoadsDirectory() {
  201. $migrations_path = TESTS_PATH . '/fixtures/migrations/';
  202. $migrator = new Minz_Migrator($migrations_path);
  203. $expected_versions = ['2019_12_22_FooBar', '2019_12_23_Baz'];
  204. $migrations = $migrator->migrations();
  205. $this->assertSame($expected_versions, array_keys($migrations));
  206. }
  207. public function testExecute() {
  208. $migrations_path = TESTS_PATH . '/fixtures/migrations/';
  209. $applied_migrations_path = tempnam('/tmp', 'applied_migrations.txt');
  210. $result = Minz_Migrator::execute($migrations_path, $applied_migrations_path);
  211. $this->assertTrue($result);
  212. $versions = file_get_contents($applied_migrations_path);
  213. $this->assertSame("2019_12_22_FooBar\n2019_12_23_Baz", $versions);
  214. }
  215. public function testExecuteWithAlreadyAppliedMigration() {
  216. $migrations_path = TESTS_PATH . '/fixtures/migrations/';
  217. $applied_migrations_path = tempnam('/tmp', 'applied_migrations.txt');
  218. file_put_contents($applied_migrations_path, '2019_12_22_FooBar');
  219. $result = Minz_Migrator::execute($migrations_path, $applied_migrations_path);
  220. $this->assertTrue($result);
  221. $versions = file_get_contents($applied_migrations_path);
  222. $this->assertSame("2019_12_22_FooBar\n2019_12_23_Baz", $versions);
  223. }
  224. public function testExecuteWithAppliedMigrationInDifferentOrder() {
  225. $migrations_path = TESTS_PATH . '/fixtures/migrations/';
  226. $applied_migrations_path = tempnam('/tmp', 'applied_migrations.txt');
  227. file_put_contents($applied_migrations_path, "2019_12_23_Baz\n2019_12_22_FooBar");
  228. $result = Minz_Migrator::execute($migrations_path, $applied_migrations_path);
  229. $this->assertTrue($result);
  230. $versions = file_get_contents($applied_migrations_path);
  231. // if the order changes, it probably means the first versions comparaison
  232. // test doesn't work anymore
  233. $this->assertSame("2019_12_23_Baz\n2019_12_22_FooBar", $versions);
  234. }
  235. public function testExecuteFailsIfVersionPathDoesNotExist() {
  236. $migrations_path = TESTS_PATH . '/fixtures/migrations/';
  237. $applied_migrations_path = tempnam('/tmp', 'applied_migrations.txt');
  238. $expected_result = "Cannot open the {$applied_migrations_path} file";
  239. unlink($applied_migrations_path);
  240. $result = Minz_Migrator::execute($migrations_path, $applied_migrations_path);
  241. $this->assertSame($expected_result, $result);
  242. }
  243. public function testExecuteFailsIfAMigrationIsFailing() {
  244. $migrations_path = TESTS_PATH . '/fixtures/migrations_with_failing/';
  245. $applied_migrations_path = tempnam('/tmp', 'applied_migrations.txt');
  246. $expected_result = 'A migration failed to be applied, please see previous logs';
  247. $result = Minz_Migrator::execute($migrations_path, $applied_migrations_path);
  248. $this->assertSame($expected_result, $result);
  249. $versions = file_get_contents($applied_migrations_path);
  250. $this->assertSame('2020_01_11_FooBar', $versions);
  251. }
  252. }