ConsoleUITest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Tests\Unit\Service\Cron\UI;
  3. use App\Service\Cron\UI\ConsoleUI;
  4. use PHPUnit\Framework\MockObject\MockObject;
  5. use PHPUnit\Framework\TestCase;
  6. use Symfony\Component\Console\Helper\ProgressBar;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. class TestableConsoleUI extends ConsoleUI
  9. {
  10. public function setProgressBar(ProgressBar $progressBar): void
  11. {
  12. $this->progressBar = $progressBar;
  13. }
  14. }
  15. class ConsoleUITest extends TestCase
  16. {
  17. private mixed $output;
  18. public function setUp(): void
  19. {
  20. \DG\BypassFinals::enable();
  21. $this->output = $this->getMockBuilder(OutputInterface::class)->disableOriginalConstructor()->getMock();
  22. }
  23. /**
  24. * @param list<string> $methodNames
  25. */
  26. private function getConsoleUiMockFor(array $methodNames): \App\Tests\Service\Cron\UI\TestableConsoleUI|MockObject
  27. {
  28. return $this->getMockBuilder(TestableConsoleUI::class)
  29. ->setConstructorArgs([$this->output])
  30. ->setMethodsExcept($methodNames)
  31. ->getMock();
  32. }
  33. public function testPrint(): void
  34. {
  35. $consoleUI = $this->getConsoleUiMockFor(['print']);
  36. $this->output->expects(self::once())->method('writeln')->with('foo');
  37. $consoleUI->print('foo');
  38. }
  39. public function testProgress(): void
  40. {
  41. /** @var ProgressBar|MockObject $progressBar */
  42. $progressBar = $this->getMockBuilder(ProgressBar::class)->disableOriginalConstructor()->getMock();
  43. $consoleUI = $this->getMockBuilder(TestableConsoleUI::class)
  44. ->setConstructorArgs([$this->output])
  45. ->setMethodsExcept(['progress', 'setProgressBar'])
  46. ->getMock();
  47. $consoleUI->setProgressBar($progressBar);
  48. $progressBar->expects(self::once())
  49. ->method('setProgress')
  50. ->with(21);
  51. $progressBar->expects(self::once())
  52. ->method('setMaxSteps')
  53. ->with(100);
  54. $consoleUI->progress(21, 100);
  55. }
  56. public function testProgressEnd(): void
  57. {
  58. /** @var ProgressBar|MockObject $progressBar */
  59. $progressBar = $this->getMockBuilder(ProgressBar::class)->disableOriginalConstructor()->getMock();
  60. $consoleUI = $this->getConsoleUiMockFor(['progress', 'setProgressBar']);
  61. $consoleUI->setProgressBar($progressBar);
  62. $progressBar->expects(self::once())
  63. ->method('setProgress')
  64. ->with(100);
  65. $progressBar->expects(self::once())
  66. ->method('setMaxSteps')
  67. ->with(100);
  68. $consoleUI->expects(self::once())->method('print')->with('');
  69. $consoleUI->progress(100, 100);
  70. }
  71. }