MigrateDatabaseCommandTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkioTest\Shlink\CLI\Command\Db;
  4. use PHPUnit\Framework\TestCase;
  5. use Prophecy\Argument;
  6. use Prophecy\PhpUnit\ProphecyTrait;
  7. use Prophecy\Prophecy\ObjectProphecy;
  8. use Shlinkio\Shlink\CLI\Command\Db\MigrateDatabaseCommand;
  9. use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
  10. use Symfony\Component\Console\Application;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. use Symfony\Component\Console\Tester\CommandTester;
  13. use Symfony\Component\Lock\LockFactory;
  14. use Symfony\Component\Lock\LockInterface;
  15. use Symfony\Component\Process\PhpExecutableFinder;
  16. class MigrateDatabaseCommandTest extends TestCase
  17. {
  18. use ProphecyTrait;
  19. private CommandTester $commandTester;
  20. private ObjectProphecy $processHelper;
  21. public function setUp(): void
  22. {
  23. $locker = $this->prophesize(LockFactory::class);
  24. $lock = $this->prophesize(LockInterface::class);
  25. $lock->acquire(Argument::any())->willReturn(true);
  26. $lock->release()->will(function (): void {
  27. });
  28. $locker->createLock(Argument::cetera())->willReturn($lock->reveal());
  29. $phpExecutableFinder = $this->prophesize(PhpExecutableFinder::class);
  30. $phpExecutableFinder->find(false)->willReturn('/usr/local/bin/php');
  31. $this->processHelper = $this->prophesize(ProcessRunnerInterface::class);
  32. $command = new MigrateDatabaseCommand(
  33. $locker->reveal(),
  34. $this->processHelper->reveal(),
  35. $phpExecutableFinder->reveal(),
  36. );
  37. $app = new Application();
  38. $app->add($command);
  39. $this->commandTester = new CommandTester($command);
  40. }
  41. /** @test */
  42. public function migrationsCommandIsRunWithProperVerbosity(): void
  43. {
  44. $runCommand = $this->processHelper->run(Argument::type(OutputInterface::class), [
  45. '/usr/local/bin/php',
  46. MigrateDatabaseCommand::DOCTRINE_MIGRATIONS_SCRIPT,
  47. MigrateDatabaseCommand::DOCTRINE_MIGRATE_COMMAND,
  48. '--no-interaction',
  49. ]);
  50. $this->commandTester->execute([]);
  51. $output = $this->commandTester->getDisplay();
  52. self::assertStringContainsString('Migrating database...', $output);
  53. self::assertStringContainsString('Database properly migrated!', $output);
  54. $runCommand->shouldHaveBeenCalledOnce();
  55. }
  56. }