expectException(StreamException::class); try { set_error_handler(StreamException::handleError(...)); trigger_error('Oh no!'); } finally { restore_error_handler(); } } public function testOnlyAcceptsStreamResource(): void { $this->expectException(InvalidArgumentException::class); $handle = fopen('php://memory', 'w+'); $this->assertNotFalse($handle); fwrite($handle, 'test'); fclose($handle); /** @psalm-suppress InvalidArgument */ new class($handle) { use StreamTrait; }; } public function testReadRead(): void { $handle = fopen('php://memory', 'w+'); $this->assertNotFalse($handle); fwrite($handle, 'test'); fseek($handle, 0); $r = new class($handle) { use StreamReadTrait; }; $this->assertEquals('test', $r->read(4)); $this->assertEmpty($r->read(4)); $r->close(); $this->expectException(StreamException::class); $r->read(4); } public function testReadGetContents(): void { $handle = fopen('php://memory', 'w+'); $this->assertNotFalse($handle); fwrite($handle, 'test'); fseek($handle, 0); $r = new class($handle) { use StreamReadTrait; }; $this->assertEquals('test', $r->getContents()); $this->assertEmpty($r->getContents()); $r->close(); $this->expectException(StreamException::class); $r->getContents(); } public function testWriteWrite(): void { $handle = fopen('php://memory', 'w'); $this->assertNotFalse($handle); $w = new class($handle) { use StreamWriteTrait; }; $this->assertEquals(4, $w->write('test')); $w->close(); $this->expectException(StreamException::class); $w->write('test'); } public function testWriteFlush(): void { $handle = fopen('php://memory', 'w'); $this->assertNotFalse($handle); $w = new class($handle) { use StreamWriteTrait; }; $w->flush(); $w->close(); $this->expectException(StreamException::class); $w->flush(); } public function testChildStdin(): void { $handle = fopen('php://memory', 'w'); $this->assertNotFalse($handle); $stdin = new ChildStdin($handle); $this->assertEquals( $handle, $stdin->getDescriptiorSpec(0), ); } public function testChildStdout(): void { $handle = fopen('php://memory', 'r'); $this->assertNotFalse($handle); $stdout = new ChildStdout($handle); $this->assertEquals( $handle, $stdout->getDescriptiorSpec(1), ); } public function testChildStderr(): void { $handle = fopen('php://memory', 'r'); $this->assertNotFalse($handle); $stderr = new ChildStderr($handle); $this->assertEquals( $handle, $stderr->getDescriptiorSpec(2), ); } }