|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Command; |
| 4 | + |
| 5 | +use Override; |
| 6 | +use PHPStan\Internal\AgentDetector; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Symfony\Component\Console\Input\StringInput; |
| 9 | +use Symfony\Component\Console\Output\StreamOutput; |
| 10 | +use function fclose; |
| 11 | +use function fopen; |
| 12 | +use function getenv; |
| 13 | +use function putenv; |
| 14 | +use function rewind; |
| 15 | +use function rtrim; |
| 16 | +use function str_replace; |
| 17 | +use function stream_get_contents; |
| 18 | + |
| 19 | +class ErrorsConsoleStyleTest extends TestCase |
| 20 | +{ |
| 21 | + |
| 22 | + /** @var array<string, string|false> */ |
| 23 | + private array $originalEnvVars = []; |
| 24 | + |
| 25 | + #[Override] |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + foreach ([...AgentDetector::ENV_VARS, 'GITHUB_ACTIONS'] as $var) { |
| 29 | + $this->originalEnvVars[$var] = getenv($var); |
| 30 | + putenv($var); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + #[Override] |
| 35 | + protected function tearDown(): void |
| 36 | + { |
| 37 | + foreach ($this->originalEnvVars as $var => $value) { |
| 38 | + putenv($var . ($value !== false ? '=' . $value : '')); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public function testProgressOutputInAgentDoesNotOverwrite(): void |
| 43 | + { |
| 44 | + $agentOutput = $this->renderProgressOutput(true); |
| 45 | + $regularOutput = $this->renderProgressOutput(false); |
| 46 | + |
| 47 | + self::assertSame( |
| 48 | + rtrim(<<<'EOT' |
| 49 | + 0/2 [>---------------------------] 0% |
| 50 | + 2/2 [============================] 100% |
| 51 | + EOT), |
| 52 | + $agentOutput, |
| 53 | + ); |
| 54 | + self::assertSame( |
| 55 | + " 0/2 [>---------------------------] 0%\033[1G\033[2K 2/2 [============================] 100%", |
| 56 | + $regularOutput, |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + private function renderProgressOutput(bool $isAgent): string |
| 61 | + { |
| 62 | + if ($isAgent) { |
| 63 | + putenv('AI_AGENT=1'); |
| 64 | + } else { |
| 65 | + putenv('AI_AGENT'); |
| 66 | + } |
| 67 | + |
| 68 | + $stream = fopen('php://memory', 'w+'); |
| 69 | + self::assertNotFalse($stream); |
| 70 | + |
| 71 | + $output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, true); |
| 72 | + $errorStyle = new ErrorsConsoleStyle(new StringInput(''), $output); |
| 73 | + |
| 74 | + $progressBar = $errorStyle->createProgressBar(2); |
| 75 | + $progressBar->setBarCharacter('='); |
| 76 | + $progressBar->setEmptyBarCharacter('-'); |
| 77 | + $progressBar->setProgressCharacter('>'); |
| 78 | + $progressBar->setProgress(0); |
| 79 | + $progressBar->display(); |
| 80 | + $progressBar->setProgress(2); |
| 81 | + $progressBar->display(); |
| 82 | + |
| 83 | + rewind($stream); |
| 84 | + $contents = stream_get_contents($stream); |
| 85 | + fclose($stream); |
| 86 | + |
| 87 | + self::assertIsString($contents); |
| 88 | + |
| 89 | + return str_replace(["\r\n", "\r"], "\n", $contents); |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments