Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover invalid arguments to Stream constructor #178

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions test/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -35,6 +39,7 @@
use function unlink;

use const DIRECTORY_SEPARATOR;
use const E_WARNING;

final class StreamTest extends TestCase
{
Expand Down Expand Up @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we expect to see "Bad news" here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably previous exception? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'Bad News' is swallowed by src/Stream try/catch - it should be there in $error->previous

} finally {
restore_error_handler();
}
}

public function testCannotInstantiateWithGDResource(): void
{
$resource = imagecreate(1, 1);
Expand Down