2
0

CommandTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Test\Michel\PaperORMBundle;
  3. use Michel\PaperORMBundle\Command\PaperDatabaseCreateCommand;
  4. use Michel\PaperORMBundle\Command\PaperDatabaseDropCommand;
  5. use Michel\PaperORMBundle\Command\PaperDatabaseSyncCommand;
  6. use Michel\PaperORMBundle\Command\PaperMigrationDiffCommand;
  7. use Michel\PaperORMBundle\Command\PaperMigrationMigrateCommand;
  8. use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
  9. use Symfony\Component\Console\Tester\CommandTester;
  10. use Test\Michel\PaperORMBundle\Fixtures\TestKernel;
  11. use Symfony\Bundle\FrameworkBundle\Console\Application;
  12. class CommandTest extends KernelTestCase
  13. {
  14. protected static function getKernelClass(): string
  15. {
  16. return TestKernel::class;
  17. }
  18. protected function setUp(): void
  19. {
  20. self::bootKernel();
  21. }
  22. protected function tearDown(): void
  23. {
  24. $folder = self::$kernel->getContainer()->getParameter('kernel.project_dir') . '/migrations';
  25. array_map('unlink', glob("$folder/*.*"));
  26. }
  27. public function testCreateDatabaseCommand(): void
  28. {
  29. self::bootKernel();
  30. $application = new Application(self::$kernel);
  31. $command = self::$kernel->getContainer()->get(PaperDatabaseCreateCommand::class);
  32. $application->add($command);
  33. $commandTester = new CommandTester($application->find('paper:database:create'));
  34. $exitCode = $commandTester->execute([
  35. '--if-not-exists' => true,
  36. ]);
  37. $this->assertSame(0, $exitCode, 'Command should return success code 0');
  38. $this->assertStringContainsString(
  39. '[INFO] The SQL database ":memory:" has been successfully created (if it did not already exist).',
  40. $commandTester->getDisplay()
  41. );
  42. }
  43. public function testDropDatabaseCommand(): void
  44. {
  45. self::bootKernel();
  46. $application = new Application(self::$kernel);
  47. $command = self::$kernel->getContainer()->get(PaperDatabaseDropCommand::class);
  48. $application->add($command);
  49. $commandTester = new CommandTester($application->find('paper:database:drop'));
  50. $exitCode = $commandTester->execute([
  51. ]);
  52. $this->assertSame(1, $exitCode, 'Command should return success code 1');
  53. $this->assertStringContainsString(
  54. '[ERROR] You must use the --force option to drop the database.',
  55. $commandTester->getDisplay()
  56. );
  57. $exitCode = $commandTester->execute([
  58. '--force' => true
  59. ]);
  60. $this->assertSame(0, $exitCode, 'Command should return success code 0');
  61. $this->assertStringContainsString(
  62. '[OK] The SQL database has been successfully dropped.',
  63. $commandTester->getDisplay()
  64. );
  65. }
  66. public function testDatabaseSyncCommand(): void
  67. {
  68. self::bootKernel();
  69. $application = new Application(self::$kernel);
  70. $command = self::$kernel->getContainer()->get(PaperDatabaseSyncCommand::class);
  71. $application->add($command);
  72. $commandTester = new CommandTester($application->find('paper:database:sync'));
  73. $exitCode = $commandTester->execute([
  74. ]);
  75. $this->assertSame(0, $exitCode, 'Command should return success code 0');
  76. $this->assertStringContainsString(
  77. 'Normal entities : 0',
  78. $commandTester->getDisplay()
  79. );
  80. }
  81. public function testMigrationDiffCommand(): void
  82. {
  83. self::bootKernel();
  84. $application = new Application(self::$kernel);
  85. $command = self::$kernel->getContainer()->get(PaperMigrationDiffCommand::class);
  86. $application->add($command);
  87. $commandTester = new CommandTester($application->find('paper:migration:diff'));
  88. $exitCode = $commandTester->execute([
  89. ]);
  90. $this->assertSame(0, $exitCode, 'Command should return success code 0');
  91. $this->assertStringContainsString(
  92. '[INFO] No application migration file was generated — schema already in sync.',
  93. $commandTester->getDisplay()
  94. );
  95. }
  96. public function testMigrationMigrateCommand(): void
  97. {
  98. self::bootKernel();
  99. $application = new Application(self::$kernel);
  100. $command = self::$kernel->getContainer()->get(PaperMigrationMigrateCommand::class);
  101. $application->add($command);
  102. $commandTester = new CommandTester($application->find('paper:migration:migrate'));
  103. $exitCode = $commandTester->execute([
  104. ]);
  105. $this->assertSame(0, $exitCode, 'Command should return success code 0');
  106. $this->assertStringContainsString(
  107. '[INFO] No migrations to run. The database is already up to date.',
  108. $commandTester->getDisplay()
  109. );
  110. }
  111. }