From 713429fa3e70ca2a236a20231b0d0b883a45b11e Mon Sep 17 00:00:00 2001 From: George Steel Date: Thu, 5 Sep 2024 15:58:14 +0100 Subject: [PATCH] Change exception message and tests to more accurately reflect error conditions Signed-off-by: George Steel --- psalm-baseline.xml | 8 ++++++++ src/Stream.php | 5 ++++- test/StreamTest.php | 25 +++++++++++++++++-------- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 47cebd2e..a88cc5d8 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -379,6 +379,14 @@ + + + + + + + + diff --git a/src/Stream.php b/src/Stream.php index c449c592..f5cecbb5 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -329,7 +329,10 @@ private function setStream($stream, string $mode = 'r'): void if (! is_resource($resource)) { throw new Exception\RuntimeException( - sprintf('Invalid stream reference provided: %s', (string) $error?->getMessage()), + sprintf( + 'Empty or non-existent stream identifier or file path provided: "%s"', + $stream, + ), 0, $error ); diff --git a/test/StreamTest.php b/test/StreamTest.php index 498bdff2..1f8b372b 100644 --- a/test/StreamTest.php +++ b/test/StreamTest.php @@ -9,6 +9,7 @@ use InvalidArgumentException; use Laminas\Diactoros\Exception\InvalidArgumentException as DiactorosInvalidArgumentException; use Laminas\Diactoros\Stream; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use ReflectionProperty; use RuntimeException; @@ -349,9 +350,9 @@ private function findNonExistentTempName(): string } /** - * @dataProvider provideDataForIsWritable * @param non-empty-string $mode */ + #[DataProvider('provideDataForIsWritable')] public function testIsWritableReturnsCorrectFlagForMode(string $mode, bool $fileShouldExist, bool $flag): void { if ($fileShouldExist) { @@ -395,9 +396,9 @@ public static function provideDataForIsReadable(): array } /** - * @dataProvider provideDataForIsReadable * @param non-empty-string $mode */ + #[DataProvider('provideDataForIsReadable')] public function testIsReadableReturnsCorrectFlagForMode(string $mode, bool $fileShouldExist, bool $flag): void { if ($fileShouldExist) { @@ -503,9 +504,7 @@ public static function invalidResources(): array ]; } - /** - * @dataProvider invalidResources - */ + #[DataProvider('invalidResources')] public function testAttachWithNonStringNonResourceRaisesException(mixed $resource): void { $this->expectException(InvalidArgumentException::class); @@ -515,12 +514,22 @@ public function testAttachWithNonStringNonResourceRaisesException(mixed $resourc $this->stream->attach($resource); } - public function testAttachWithInvalidStringResourceRaisesException(): void + public static function invalidStringResources(): array + { + return [ + 'Empty String' => [''], + 'File path does not exist' => ['/tmp/not-a-valid-file-path'], + 'Invalid stream' => ['php://mammary'], + ]; + } + + #[DataProvider('invalidStringResources')] + public function testAttachWithInvalidStringResourceRaisesException(string $stream): void { $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Invalid stream'); + $this->expectExceptionMessage('Empty on non-existent stream identifier or file path provided'); - $this->stream->attach('foo-bar-baz'); + $this->stream->attach($stream); } public function testAttachWithResourceAttachesResource(): void