2
0

PlatformTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Test\PhpDevCommunity\PaperORM;
  3. use PhpDevCommunity\PaperORM\EntityManager;
  4. use PhpDevCommunity\PaperORM\Mapping\Column\BoolColumn;
  5. use PhpDevCommunity\PaperORM\Mapping\Column\IntColumn;
  6. use PhpDevCommunity\PaperORM\Mapping\Column\PrimaryKeyColumn;
  7. use PhpDevCommunity\PaperORM\Mapping\Column\StringColumn;
  8. use PhpDevCommunity\PaperORM\Metadata\ColumnMetadata;
  9. use PhpDevCommunity\UniTester\TestCase;
  10. class PlatformTest extends TestCase
  11. {
  12. private EntityManager $em;
  13. protected function setUp(): void
  14. {
  15. $this->em = new EntityManager([
  16. 'driver' => 'sqlite',
  17. 'user' => null,
  18. 'password' => null,
  19. 'memory' => true,
  20. ]);
  21. }
  22. protected function tearDown(): void
  23. {
  24. $this->em->getConnection()->close();
  25. }
  26. protected function execute(): void
  27. {
  28. $this->testCreateTables();
  29. $this->testDropTable();
  30. $this->testDropColumn();
  31. $this->testAddColumn();
  32. $this->testRenameColumn();
  33. }
  34. public function testCreateTables()
  35. {
  36. $this->em->getConnection()->close();
  37. $this->em->getConnection()->connect();
  38. $platform = $this->em->createDatabasePlatform();
  39. $platform->createTable('user', [
  40. new PrimaryKeyColumn('id'),
  41. new StringColumn('firstname'),
  42. new StringColumn('lastname'),
  43. new StringColumn('email'),
  44. new StringColumn('password'),
  45. new BoolColumn('is_active'),
  46. ]);
  47. $platform->createTable('post', [
  48. new PrimaryKeyColumn('id'),
  49. new IntColumn('user_id'),
  50. new StringColumn('title'),
  51. new StringColumn('content'),
  52. ], [
  53. 'FOREIGN KEY (user_id) REFERENCES user (id)'
  54. ]);
  55. $this->assertStrictEquals(2, count($platform->listTables()));
  56. $this->assertEquals(['user', 'post'], $platform->listTables());
  57. }
  58. public function testDropTable()
  59. {
  60. $this->em->getConnection()->close();
  61. $this->em->getConnection()->connect();
  62. $platform = $this->em->createDatabasePlatform();
  63. $platform->createTable('user', [
  64. new PrimaryKeyColumn('id'),
  65. new StringColumn('firstname'),
  66. new StringColumn('lastname'),
  67. new StringColumn('email'),
  68. new StringColumn('password'),
  69. new BoolColumn('is_active'),
  70. ]);
  71. $this->assertStrictEquals(1, count($platform->listTables()));
  72. $platform->dropTable('user');
  73. $this->assertStrictEquals(0, count($platform->listTables()));
  74. }
  75. public function testDropColumn()
  76. {
  77. if (\SQLite3::version()['versionString'] < '3.35.0') {
  78. return;
  79. }
  80. $this->em->getConnection()->close();
  81. $this->em->getConnection()->connect();
  82. $platform = $this->em->createDatabasePlatform();
  83. $platform->createTable('user', [
  84. new PrimaryKeyColumn('id'),
  85. new StringColumn('firstname'),
  86. new StringColumn('lastname'),
  87. new StringColumn('email'),
  88. new StringColumn('password'),
  89. new BoolColumn('is_active'),
  90. ]);
  91. $this->assertStrictEquals(6, count($platform->listTableColumns('user')));
  92. $platform->dropColumn('user', new StringColumn('lastname'));
  93. $this->assertStrictEquals(5, count($platform->listTableColumns('user')));
  94. }
  95. public function testAddColumn()
  96. {
  97. $this->em->getConnection()->close();
  98. $this->em->getConnection()->connect();
  99. $platform = $this->em->createDatabasePlatform();
  100. $platform->createTable('user', [
  101. new PrimaryKeyColumn('id'),
  102. new StringColumn('firstname'),
  103. new StringColumn('lastname'),
  104. new StringColumn('email'),
  105. new StringColumn('password'),
  106. new BoolColumn('is_active'),
  107. ]);
  108. $this->assertStrictEquals(6, count($platform->listTableColumns('user')));
  109. $platform->addColumn('user', new StringColumn('username'));
  110. $this->assertStrictEquals(7, count($platform->listTableColumns('user')));
  111. }
  112. public function testRenameColumn()
  113. {
  114. $this->em->getConnection()->close();
  115. $this->em->getConnection()->connect();
  116. $platform = $this->em->createDatabasePlatform();
  117. $platform->createTable('user', [
  118. new PrimaryKeyColumn('id'),
  119. new StringColumn('firstname'),
  120. new StringColumn('lastname'),
  121. new StringColumn('email'),
  122. new StringColumn('password'),
  123. new BoolColumn('is_active'),
  124. ]);
  125. $columnsAsArray = array_map(function (ColumnMetadata $column) {
  126. return $column->toArray();
  127. }, $platform->listTableColumns('user'));
  128. $columns = array_column($columnsAsArray, 'name');
  129. $this->assertTrue(in_array('firstname', $columns));
  130. $platform->renameColumn('user', 'firstname', 'prenom');
  131. $columnsAsArray = array_map(function (ColumnMetadata $column) {
  132. return $column->toArray();
  133. }, $platform->listTableColumns('user'));
  134. $columns = array_column($columnsAsArray, 'name');
  135. $this->assertTrue(!in_array('firstname', $columns));
  136. $this->assertTrue(in_array('prenom', $columns));
  137. }
  138. }