|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\CLI; |
| 15 | + |
| 16 | +use CodeIgniter\Test\CIUnitTestCase; |
| 17 | +use CodeIgniter\Test\StreamFilterTrait; |
| 18 | +use PHPUnit\Framework\Attributes\Group; |
| 19 | + |
| 20 | +/** |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +#[Group('Others')] |
| 24 | +final class NullInputOutputTest extends CIUnitTestCase |
| 25 | +{ |
| 26 | + use StreamFilterTrait; |
| 27 | + |
| 28 | + public function testFwriteDiscardsOutput(): void |
| 29 | + { |
| 30 | + $io = new NullInputOutput(); |
| 31 | + $io->fwrite(STDOUT, 'should not appear'); |
| 32 | + $io->fwrite(STDERR, 'should not appear either'); |
| 33 | + |
| 34 | + $this->assertSame('', $this->getStreamFilterBuffer()); |
| 35 | + } |
| 36 | + |
| 37 | + public function testInputReturnsEmptyStringWithoutEchoingPrefix(): void |
| 38 | + { |
| 39 | + $io = new NullInputOutput(); |
| 40 | + |
| 41 | + $this->assertSame('', $io->input()); |
| 42 | + $this->assertSame('', $io->input('any prefix > ')); |
| 43 | + $this->assertSame('', $this->getStreamFilterBuffer()); |
| 44 | + } |
| 45 | + |
| 46 | + public function testCanBeSwappedIntoCliToSilenceWrites(): void |
| 47 | + { |
| 48 | + $prior = CLI::getInputOutput(); |
| 49 | + CLI::setInputOutput(new NullInputOutput()); |
| 50 | + |
| 51 | + try { |
| 52 | + CLI::write('this should be discarded'); |
| 53 | + CLI::error('this too'); |
| 54 | + $this->assertSame('', $this->getStreamFilterBuffer()); |
| 55 | + } finally { |
| 56 | + if ($prior instanceof InputOutput) { |
| 57 | + CLI::setInputOutput($prior); |
| 58 | + } else { |
| 59 | + CLI::resetInputOutput(); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments