ConsoleUITest.php 2.7 KB

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