|
| 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\Test\Mock; |
| 15 | + |
| 16 | +use CodeIgniter\CLI\CLI; |
| 17 | +use CodeIgniter\Test\CIUnitTestCase; |
| 18 | +use CodeIgniter\Test\StreamFilterTrait; |
| 19 | +use PHPUnit\Framework\Attributes\Group; |
| 20 | + |
| 21 | +/** |
| 22 | + * @internal |
| 23 | + */ |
| 24 | +#[Group('Others')] |
| 25 | +final class MockInputOutputTest extends CIUnitTestCase |
| 26 | +{ |
| 27 | + use StreamFilterTrait; |
| 28 | + |
| 29 | + protected function tearDown(): void |
| 30 | + { |
| 31 | + parent::tearDown(); |
| 32 | + |
| 33 | + CLI::resetInputOutput(); |
| 34 | + } |
| 35 | + |
| 36 | + public function testFwriteThroughMockPreservesEnclosingStreamFilter(): void |
| 37 | + { |
| 38 | + CLI::write('before mock'); |
| 39 | + $this->assertStringContainsString('before mock', $this->getStreamFilterBuffer()); |
| 40 | + |
| 41 | + $io = new MockInputOutput(); |
| 42 | + CLI::setInputOutput($io); |
| 43 | + CLI::write('through mock'); |
| 44 | + CLI::resetInputOutput(); |
| 45 | + |
| 46 | + // The mock captured its own write into its own buffer... |
| 47 | + $this->assertStringContainsString('through mock', $io->getOutput()); |
| 48 | + // ...and left the enclosing StreamFilterTrait buffer untouched. |
| 49 | + $this->assertStringNotContainsString('through mock', $this->getStreamFilterBuffer()); |
| 50 | + |
| 51 | + // The enclosing filter is still attached, so later writes are captured. |
| 52 | + CLI::write('after mock'); |
| 53 | + $this->assertStringContainsString('before mock', $this->getStreamFilterBuffer()); |
| 54 | + $this->assertStringContainsString('after mock', $this->getStreamFilterBuffer()); |
| 55 | + } |
| 56 | + |
| 57 | + public function testInputThroughMockPreservesEnclosingStreamFilter(): void |
| 58 | + { |
| 59 | + $io = new MockInputOutput(); |
| 60 | + $io->setInputs(['y']); |
| 61 | + CLI::setInputOutput($io); |
| 62 | + CLI::prompt('Continue?', ['y', 'n']); |
| 63 | + CLI::resetInputOutput(); |
| 64 | + |
| 65 | + CLI::write('after prompt'); |
| 66 | + $this->assertStringContainsString('after prompt', $this->getStreamFilterBuffer()); |
| 67 | + } |
| 68 | +} |
0 commit comments