MigratorTest.php 8.9 KB

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