StreamOutputTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Output;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Output\Output;
  13. use Symfony\Component\Console\Output\StreamOutput;
  14. class StreamOutputTest extends TestCase
  15. {
  16. protected $stream;
  17. protected function setUp(): void
  18. {
  19. $this->stream = fopen('php://memory', 'a', false);
  20. }
  21. protected function tearDown(): void
  22. {
  23. $this->stream = null;
  24. }
  25. public function testConstructor()
  26. {
  27. $output = new StreamOutput($this->stream, Output::VERBOSITY_QUIET, true);
  28. $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '__construct() takes the verbosity as its first argument');
  29. $this->assertTrue($output->isDecorated(), '__construct() takes the decorated flag as its second argument');
  30. }
  31. public function testStreamIsRequired()
  32. {
  33. $this->expectException(\InvalidArgumentException::class);
  34. $this->expectExceptionMessage('The StreamOutput class needs a stream as its first argument.');
  35. new StreamOutput('foo');
  36. }
  37. public function testGetStream()
  38. {
  39. $output = new StreamOutput($this->stream);
  40. $this->assertEquals($this->stream, $output->getStream(), '->getStream() returns the current stream');
  41. }
  42. public function testDoWrite()
  43. {
  44. $output = new StreamOutput($this->stream);
  45. $output->writeln('foo');
  46. rewind($output->getStream());
  47. $this->assertEquals('foo'.\PHP_EOL, stream_get_contents($output->getStream()), '->doWrite() writes to the stream');
  48. }
  49. public function testDoWriteOnFailure()
  50. {
  51. $resource = fopen(__DIR__.'/../Fixtures/stream_output_file.txt', 'r', false);
  52. $output = new StreamOutput($resource);
  53. rewind($output->getStream());
  54. $this->assertEquals('', stream_get_contents($output->getStream()));
  55. }
  56. }