ProcessRunnerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkioTest\Shlink\CLI\Util;
  4. use PHPUnit\Framework\TestCase;
  5. use Prophecy\Argument;
  6. use Prophecy\PhpUnit\ProphecyTrait;
  7. use Prophecy\Prophecy\ObjectProphecy;
  8. use Shlinkio\Shlink\CLI\Util\ProcessRunner;
  9. use Symfony\Component\Console\Helper\DebugFormatterHelper;
  10. use Symfony\Component\Console\Helper\HelperSet;
  11. use Symfony\Component\Console\Helper\ProcessHelper;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Symfony\Component\Process\Process;
  14. class ProcessRunnerTest extends TestCase
  15. {
  16. use ProphecyTrait;
  17. private ProcessRunner $runner;
  18. private ObjectProphecy $helper;
  19. private ObjectProphecy $formatter;
  20. private ObjectProphecy $process;
  21. private ObjectProphecy $output;
  22. protected function setUp(): void
  23. {
  24. $this->helper = $this->prophesize(ProcessHelper::class);
  25. $this->formatter = $this->prophesize(DebugFormatterHelper::class);
  26. $helperSet = $this->prophesize(HelperSet::class);
  27. $helperSet->get('debug_formatter')->willReturn($this->formatter->reveal());
  28. $this->helper->getHelperSet()->willReturn($helperSet->reveal());
  29. $this->process = $this->prophesize(Process::class);
  30. $this->runner = new ProcessRunner($this->helper->reveal(), fn () => $this->process->reveal());
  31. $this->output = $this->prophesize(OutputInterface::class);
  32. }
  33. /** @test */
  34. public function noMessagesAreWrittenWhenOutputIsNotVerbose(): void
  35. {
  36. $isVeryVerbose = $this->output->isVeryVerbose()->willReturn(false);
  37. $isDebug = $this->output->isDebug()->willReturn(false);
  38. $mustRun = $this->process->mustRun(Argument::cetera())->willReturn($this->process->reveal());
  39. $this->runner->run($this->output->reveal(), []);
  40. $isVeryVerbose->shouldHaveBeenCalledTimes(2);
  41. $isDebug->shouldHaveBeenCalledOnce();
  42. $mustRun->shouldHaveBeenCalledOnce();
  43. $this->process->isSuccessful()->shouldNotHaveBeenCalled();
  44. $this->process->getCommandLine()->shouldNotHaveBeenCalled();
  45. $this->output->write(Argument::cetera())->shouldNotHaveBeenCalled();
  46. $this->helper->wrapCallback(Argument::cetera())->shouldNotHaveBeenCalled();
  47. $this->formatter->start(Argument::cetera())->shouldNotHaveBeenCalled();
  48. $this->formatter->stop(Argument::cetera())->shouldNotHaveBeenCalled();
  49. }
  50. /** @test */
  51. public function someMessagesAreWrittenWhenOutputIsVerbose(): void
  52. {
  53. $isVeryVerbose = $this->output->isVeryVerbose()->willReturn(true);
  54. $isDebug = $this->output->isDebug()->willReturn(false);
  55. $mustRun = $this->process->mustRun(Argument::cetera())->willReturn($this->process->reveal());
  56. $isSuccessful = $this->process->isSuccessful()->willReturn(true);
  57. $getCommandLine = $this->process->getCommandLine()->willReturn('true');
  58. $start = $this->formatter->start(Argument::cetera())->willReturn('');
  59. $stop = $this->formatter->stop(Argument::cetera())->willReturn('');
  60. $this->runner->run($this->output->reveal(), []);
  61. $isVeryVerbose->shouldHaveBeenCalledTimes(2);
  62. $isDebug->shouldHaveBeenCalledOnce();
  63. $mustRun->shouldHaveBeenCalledOnce();
  64. $this->output->write(Argument::cetera())->shouldHaveBeenCalledTimes(2);
  65. $this->helper->wrapCallback(Argument::cetera())->shouldNotHaveBeenCalled();
  66. $isSuccessful->shouldHaveBeenCalledTimes(2);
  67. $getCommandLine->shouldHaveBeenCalledOnce();
  68. $start->shouldHaveBeenCalledOnce();
  69. $stop->shouldHaveBeenCalledOnce();
  70. }
  71. /** @test */
  72. public function wrapsCallbackWhenOutputIsDebug(): void
  73. {
  74. $isVeryVerbose = $this->output->isVeryVerbose()->willReturn(false);
  75. $isDebug = $this->output->isDebug()->willReturn(true);
  76. $mustRun = $this->process->mustRun(Argument::cetera())->willReturn($this->process->reveal());
  77. $wrapCallback = $this->helper->wrapCallback(Argument::cetera())->willReturn(function (): void {
  78. });
  79. $this->runner->run($this->output->reveal(), []);
  80. $isVeryVerbose->shouldHaveBeenCalledTimes(2);
  81. $isDebug->shouldHaveBeenCalledOnce();
  82. $mustRun->shouldHaveBeenCalledOnce();
  83. $wrapCallback->shouldHaveBeenCalledOnce();
  84. $this->process->isSuccessful()->shouldNotHaveBeenCalled();
  85. $this->process->getCommandLine()->shouldNotHaveBeenCalled();
  86. $this->output->write(Argument::cetera())->shouldNotHaveBeenCalled();
  87. $this->formatter->start(Argument::cetera())->shouldNotHaveBeenCalled();
  88. $this->formatter->stop(Argument::cetera())->shouldNotHaveBeenCalled();
  89. }
  90. }