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 6, 2024
1 parent 6a917e6 commit 713429f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
8 changes: 8 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,14 @@
<code><![CDATA[RequestInterfaceStaticReturnTypes]]></code>
</UnusedClass>
</file>
<file src="test/StreamTest.php">
<PossiblyUnusedMethod>
<code><![CDATA[invalidResources]]></code>
<code><![CDATA[invalidStringResources]]></code>
<code><![CDATA[provideDataForIsReadable]]></code>
<code><![CDATA[provideDataForIsWritable]]></code>
</PossiblyUnusedMethod>
</file>
<file src="test/TestAsset/CallbacksForCallbackStream.php">
<PossiblyUnusedMethod>
<code><![CDATA[sampleCallback]]></code>
Expand Down
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 or non-existent stream identifier or file path provided: "%s"',
$stream,
),
0,
$error
);
Expand Down
25 changes: 17 additions & 8 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 @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -503,9 +504,7 @@ public static function invalidResources(): array
];
}

/**
* @dataProvider invalidResources
*/
#[DataProvider('invalidResources')]
public function testAttachWithNonStringNonResourceRaisesException(mixed $resource): void
{
$this->expectException(InvalidArgumentException::class);
Expand All @@ -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
Expand Down

0 comments on commit 713429f

Please sign in to comment.