MigrationTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Test\PhpDevCommunity\PaperORM;
  3. use PhpDevCommunity\PaperORM\EntityManager;
  4. use PhpDevCommunity\PaperORM\Mapper\ColumnMapper;
  5. use PhpDevCommunity\PaperORM\Mapping\Column\IntColumn;
  6. use PhpDevCommunity\PaperORM\Mapping\Column\StringColumn;
  7. use PhpDevCommunity\PaperORM\Migration\PaperMigration;
  8. use PhpDevCommunity\UniTester\TestCase;
  9. use Test\PhpDevCommunity\PaperORM\Entity\PostTest;
  10. use Test\PhpDevCommunity\PaperORM\Entity\UserTest;
  11. class MigrationTest extends TestCase
  12. {
  13. private EntityManager $em;
  14. private string $migrationDir;
  15. private PaperMigration $paperMigration;
  16. protected function setUp(): void
  17. {
  18. $this->em = new EntityManager([
  19. 'driver' => 'sqlite',
  20. 'user' => null,
  21. 'password' => null,
  22. 'memory' => true,
  23. ]);
  24. $this->migrationDir = __DIR__ . '/migrations';
  25. $this->paperMigration = PaperMigration::create($this->em, 'mig_versions', $this->migrationDir);
  26. }
  27. protected function tearDown(): void
  28. {
  29. $this->em->getConnection()->close();
  30. $folder = $this->migrationDir;
  31. array_map('unlink', glob("$folder/*.*"));
  32. }
  33. protected function execute(): void
  34. {
  35. $this->em->getConnection()->close();
  36. $this->testDiff();
  37. $this->testExecute();
  38. $this->testColumnModification();
  39. $this->testFailedMigration();
  40. }
  41. private function testDiff() : void
  42. {
  43. $this->em->getConnection()->close();
  44. $migrationFile = $this->paperMigration->diffEntities([
  45. UserTest::class,
  46. PostTest::class
  47. ]);
  48. $this->assertStringContains(file_get_contents($migrationFile), '-- UP MIGRATION --');
  49. $this->assertStringContains(file_get_contents($migrationFile), 'CREATE TABLE user (id INTEGER PRIMARY KEY NOT NULL,firstname VARCHAR(255) NOT NULL,lastname VARCHAR(255) NOT NULL,email VARCHAR(255) NOT NULL,password VARCHAR(255) NOT NULL,is_active BOOLEAN NOT NULL,created_at DATETIME NOT NULL,last_post_id INTEGER NOT NULL,FOREIGN KEY (last_post_id) REFERENCES post (id));');
  50. $this->assertStringContains(file_get_contents($migrationFile), 'CREATE INDEX IX_2D053F64 ON user (last_post_id);');
  51. $this->assertStringContains(file_get_contents($migrationFile), 'CREATE TABLE post (id INTEGER PRIMARY KEY NOT NULL,title VARCHAR(255) NOT NULL,content VARCHAR(255) NOT NULL,created_at DATETIME NOT NULL,user_id INTEGER NOT NULL,FOREIGN KEY (user_id) REFERENCES user (id));');
  52. $this->assertStringContains(file_get_contents($migrationFile), 'CREATE INDEX IX_A76ED395 ON post (user_id);');
  53. $this->assertStringContains(file_get_contents($migrationFile), '-- DOWN MIGRATION --');
  54. $this->assertStringContains(file_get_contents($migrationFile), 'DROP INDEX IX_2D053F64;');
  55. $this->assertStringContains(file_get_contents($migrationFile), 'DROP TABLE user;');
  56. $this->assertStringContains(file_get_contents($migrationFile), 'DROP INDEX IX_A76ED395;');
  57. $this->assertStringContains(file_get_contents($migrationFile), 'DROP TABLE post;');
  58. }
  59. private function testExecute(): void
  60. {
  61. $this->paperMigration->migrate();
  62. $successList = $this->paperMigration->getSuccessList();
  63. $this->assertTrue(count($successList) === 1);
  64. $migrationFile = $this->paperMigration->diffEntities([UserTest::class]);
  65. $this->assertNull($migrationFile);
  66. }
  67. private function testColumnModification(): void
  68. {
  69. $userColumns = ColumnMapper::getColumns(UserTest::class);
  70. $userColumns[3] = new StringColumn('email', 'email', 255, true, null, true);
  71. $userColumns[] = new IntColumn('childs', 'childs', false, 0);
  72. $migrationFile = $this->paperMigration->diff([
  73. 'user' => [
  74. 'columns' => $userColumns,
  75. 'indexes' => []
  76. ]
  77. ]);
  78. $this->paperMigration->migrate();
  79. $successList = $this->paperMigration->getSuccessList();
  80. $this->assertTrue(count($successList) === 1);
  81. $this->assertEquals(pathinfo($migrationFile, PATHINFO_FILENAME), $successList[0]);
  82. $this->assertStringContains(file_get_contents( $migrationFile ), 'ALTER TABLE user ADD childs INTEGER NOT NULL DEFAULT 0;');
  83. $this->assertStringContains(file_get_contents( $migrationFile ), 'CREATE UNIQUE INDEX IX_E7927C74 ON user (email);');
  84. $this->assertStringContains(file_get_contents( $migrationFile ), 'DROP INDEX IX_E7927C74;');
  85. }
  86. private function testFailedMigration(): void
  87. {
  88. $userColumns = ColumnMapper::getColumns(UserTest::class);
  89. $userColumns[3] = new StringColumn('email', 'email', 100, true, null, true);
  90. $this->paperMigration->diff([
  91. 'user' => [
  92. 'columns' => $userColumns,
  93. 'indexes' => []
  94. ]
  95. ]);
  96. $this->expectException( \RuntimeException::class, function () {
  97. $this->paperMigration->migrate();
  98. });
  99. $successList = $this->paperMigration->getSuccessList();
  100. $this->assertTrue(count($successList) === 0);
  101. }
  102. }