2
0

DatabaseSyncCommandTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Test\Michel\PaperORM;
  3. use Michel\Console\CommandParser;
  4. use Michel\Console\CommandRunner;
  5. use Michel\Console\Output;
  6. use Michel\PaperORM\Collector\EntityDirCollector;
  7. use Michel\PaperORM\Command\DatabaseSyncCommand;
  8. use Michel\PaperORM\Command\ShowTablesCommand;
  9. use Michel\PaperORM\EntityManager;
  10. use Michel\PaperORM\Mapping\Column\BoolColumn;
  11. use Michel\PaperORM\Mapping\Column\IntColumn;
  12. use Michel\PaperORM\Mapping\Column\JoinColumn;
  13. use Michel\PaperORM\Mapping\Column\PrimaryKeyColumn;
  14. use Michel\PaperORM\Mapping\Column\StringColumn;
  15. use Michel\PaperORM\Migration\PaperMigration;
  16. use Michel\PaperORM\PaperConfiguration;
  17. use Michel\UniTester\TestCase;
  18. use Test\Michel\PaperORM\Entity\UserTest;
  19. use Test\Michel\PaperORM\Helper\DataBaseHelperTest;
  20. class DatabaseSyncCommandTest extends TestCase
  21. {
  22. protected function setUp(): void
  23. {
  24. }
  25. protected function tearDown(): void
  26. {
  27. }
  28. protected function execute(): void
  29. {
  30. foreach (DataBaseHelperTest::drivers() as $params) {
  31. $em = EntityManager::createFromConfig(PaperConfiguration::fromArray($params));
  32. $this->executeTest($em);
  33. $em->getConnection()->close();
  34. }
  35. }
  36. private function executeTest(EntityManager $em)
  37. {
  38. $platform = $em->getPlatform();
  39. $platform->createDatabaseIfNotExists();
  40. $platform->dropDatabase();
  41. $platform->createDatabaseIfNotExists();
  42. $paperMigration = PaperMigration::create($em, 'mig_versions', __DIR__ . '/migrations');
  43. $runner = new CommandRunner([
  44. new DatabaseSyncCommand($paperMigration, EntityDirCollector::bootstrap([__DIR__ . '/Entity']), 'test'),
  45. ]);
  46. $out = [];
  47. $code = $runner->run(new CommandParser(['', 'paper:database:sync', '--no-execute']), new Output(function ($message) use(&$out) {
  48. $out[] = $message;
  49. }));
  50. $this->assertEquals(0, $code);
  51. $this->assertStringContains( implode(' ', $out), "[INFO] Preview mode only — SQL statements were displayed but NOT executed.");
  52. $out = [];
  53. $code = $runner->run(new CommandParser(['', 'paper:database:sync']), new Output(function ($message) use(&$out) {
  54. $out[] = $message;
  55. }));
  56. $this->assertEquals(0, $code);
  57. $this->assertStringContains( implode(' ', $out), "✔ Executed:");
  58. $out = [];
  59. $code = $runner->run(new CommandParser(['', 'paper:database:sync']), new Output(function ($message) use(&$out) {
  60. $out[] = $message;
  61. }));
  62. $this->assertEquals(0, $code);
  63. $this->assertStringContains( implode(' ', $out), "No differences detected — all entities are already in sync with the database schema.");
  64. $platform->dropDatabase();
  65. }
  66. }