diff --git a/test/StreamTest.php b/test/StreamTest.php index 10824637..0bd55558 100644 --- a/test/StreamTest.php +++ b/test/StreamTest.php @@ -5,9 +5,11 @@ namespace LaminasTest\Diactoros; use CurlHandle; +use Exception; use GdImage; use InvalidArgumentException; use Laminas\Diactoros\Exception\InvalidArgumentException as DiactorosInvalidArgumentException; +use Laminas\Diactoros\Exception\RuntimeException as DiactorosRuntimeException; use Laminas\Diactoros\Stream; use PHPUnit\Framework\TestCase; use ReflectionProperty; @@ -27,6 +29,8 @@ use function fwrite; use function imagecreate; use function is_resource; +use function restore_error_handler; +use function set_error_handler; use function shmop_open; use function stream_get_meta_data; use function sys_get_temp_dir; @@ -35,6 +39,7 @@ use function unlink; use const DIRECTORY_SEPARATOR; +use const E_WARNING; final class StreamTest extends TestCase { @@ -68,6 +73,28 @@ public function testCanInstantiateWithStreamResource(): void $this->assertInstanceOf(Stream::class, $stream); } + public function testThatInstantiatingWithArbitraryStringCausesException(): void + { + $this->expectException(DiactorosRuntimeException::class); + $this->expectExceptionMessage('Invalid stream reference provided'); + new Stream('foo'); + } + + public function testInvalidStringArgumentWithCustomErrorHandler(): void + { + set_error_handler(function (): never { + throw new Exception('Bad News'); + }, E_WARNING); + + try { + new Stream('foo'); + } catch (DiactorosRuntimeException $error) { + self::assertStringContainsString('Invalid stream reference provided', $error->getMessage()); + } finally { + restore_error_handler(); + } + } + public function testCannotInstantiateWithGDResource(): void { $resource = imagecreate(1, 1);