Skip to content

Commit

Permalink
Change exception message and tests to more accurately reflect error c…
Browse files Browse the repository at this point in the history
…onditions

Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Sep 5, 2024
1 parent 6a917e6 commit a30d4a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 on non-existent stream identifier or file path provided: %s',
(string) $error?->getMessage(),
),
0,
$error
);
Expand Down
17 changes: 14 additions & 3 deletions test/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -515,12 +516,22 @@ public function testAttachWithNonStringNonResourceRaisesException(mixed $resourc
$this->stream->attach($resource);
}

public function testAttachWithInvalidStringResourceRaisesException(): void
public static function invalidStringResources(): array

Check failure on line 519 in test/StreamTest.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyUnusedMethod

test/StreamTest.php:519:28: PossiblyUnusedMethod: Cannot find any calls to method LaminasTest\Diactoros\StreamTest::invalidStringResources (see https://psalm.dev/087)
{
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
Expand Down

0 comments on commit a30d4a6

Please sign in to comment.