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

update to phpunit 8 #274

Merged
merged 2 commits into from
Jun 9, 2019
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ artifacts/
vendor/
composer.lock
phpunit.xml
.phpunit.result.cache
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
"ralouphie/getallheaders": "^2.0.5 || ^3.0.0"
},
"require-dev": {
"phpunit/phpunit": "^7.4",
"http-interop/http-factory-tests": "^0.5",
"ext-zlib": "*"
"phpunit/phpunit": "^8.1",
"http-interop/http-factory-tests": "dev-master"
Copy link
Member Author

Choose a reason for hiding this comment

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

@Nyholm there is no tag and no branch alias for phpunit 8 support in http-interop/http-factory-tests yet

},
"provide": {
"psr/http-factory-implementation": "1.0",
Expand Down
10 changes: 9 additions & 1 deletion src/HttpFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ public function createStream(string $content = ''): StreamInterface
*/
public function createStreamFromFile(string $file, string $mode = 'r'): StreamInterface
{
$resource = \GuzzleHttp\Psr7\try_fopen($file, $mode);
try {
$resource = \GuzzleHttp\Psr7\try_fopen($file, $mode);
} catch (\RuntimeException $e) {
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
throw new \InvalidArgumentException(sprintf('Invalid file opening mode "%s"', $mode), 0, $e);
}

throw $e;
}

return \GuzzleHttp\Psr7\stream_for($resource);
}
Expand Down
4 changes: 2 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,12 @@ function rewind_body(MessageInterface $message)
function try_fopen($filename, $mode)
{
$ex = null;
set_error_handler(function () use ($filename, $mode, &$ex) {
set_error_handler(function (int $errno, string $errstr) use ($filename, $mode, &$ex) {
$ex = new \RuntimeException(sprintf(
'Unable to open %s using mode %s: %s',
$filename,
$mode,
func_get_args()[1]
$errstr
));
});

Expand Down
26 changes: 9 additions & 17 deletions tests/AppendStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@

class AppendStreamTest extends TestCase
{
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Each stream must be readable
*/
public function testValidatesStreamsAreReadable()
{
$a = new AppendStream();
Expand All @@ -24,23 +20,19 @@ public function testValidatesStreamsAreReadable()
$s->expects($this->once())
->method('isReadable')
->will($this->returnValue(false));
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Each stream must be readable');
$a->addStream($s);
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage The AppendStream can only seek with SEEK_SET
*/
public function testValidatesSeekType()
{
$a = new AppendStream();
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('The AppendStream can only seek with SEEK_SET');
$a->seek(100, SEEK_CUR);
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Unable to seek stream 0 of the AppendStream
*/
public function testTriesToRewindOnSeek()
{
$a = new AppendStream();
Expand All @@ -57,6 +49,8 @@ public function testTriesToRewindOnSeek()
->method('rewind')
->will($this->throwException(new \RuntimeException()));
$a->addStream($s);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Unable to seek stream 0 of the AppendStream');
$a->seek(10);
}

Expand Down Expand Up @@ -108,7 +102,7 @@ public function testDetachesEachStream()
$this->assertFalse($a->isWritable());

$this->assertNull($s1->detach());
$this->assertInternalType('resource', $handle, 'resource is not closed when detaching');
$this->assertIsResource($handle, 'resource is not closed when detaching');
fclose($handle);
}

Expand All @@ -132,16 +126,14 @@ public function testClosesEachStream()
$this->assertFalse(is_resource($handle));
}

/**
* @expectedExceptionMessage Cannot write to an AppendStream
* @expectedException \RuntimeException
*/
public function testIsNotWritable()
{
$a = new AppendStream([Psr7\stream_for('foo')]);
$this->assertFalse($a->isWritable());
$this->assertTrue($a->isSeekable());
$this->assertTrue($a->isReadable());
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Cannot write to an AppendStream');
$a->write('foo');
}

Expand Down
6 changes: 2 additions & 4 deletions tests/BufferStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ public function testRemovesReadDataFromBuffer()
$this->assertEquals('', $b->read(10));
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Cannot determine the position of a BufferStream
*/
public function testCanCastToStringOrGetContents()
{
$b = new BufferStream();
Expand All @@ -43,6 +39,8 @@ public function testCanCastToStringOrGetContents()
$this->assertEquals('foo', $b->read(3));
$b->write('bar');
$this->assertEquals('bazbar', (string) $b);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Cannot determine the position of a BufferStream');
$b->tell();
}

Expand Down
15 changes: 5 additions & 10 deletions tests/CachingStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class CachingStreamTest extends TestCase
/** @var Stream */
private $decorated;

protected function setUp()
protected function setUp(): void
{
$this->decorated = Psr7\stream_for('testing');
$this->body = new CachingStream($this->decorated);
}

protected function tearDown()
protected function tearDown(): void
{
$this->decorated->close();
$this->body->close();
Expand Down Expand Up @@ -153,31 +153,27 @@ public function testSkipsOverwrittenBytes()
$this->assertEquals("0001\n", Psr7\readline($body));
// Write over part of the body yet to be read, so skip some bytes
$this->assertEquals(5, $body->write("TEST\n"));
$this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
// Read, which skips bytes, then reads
$this->assertEquals("0003\n", Psr7\readline($body));
$this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
$this->assertEquals("0004\n", Psr7\readline($body));
$this->assertEquals("0005\n", Psr7\readline($body));

// Overwrite part of the cached body (so don't skip any bytes)
$body->seek(5);
$this->assertEquals(5, $body->write("ABCD\n"));
$this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
$this->assertEquals("TEST\n", Psr7\readline($body));
$this->assertEquals("0003\n", Psr7\readline($body));
$this->assertEquals("0004\n", Psr7\readline($body));
$this->assertEquals("0005\n", Psr7\readline($body));
$this->assertEquals("0006\n", Psr7\readline($body));
$this->assertEquals(5, $body->write("1234\n"));
$this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));

// Seek to 0 and ensure the overwritten bit is replaced
$body->seek(0);
$this->assertEquals("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", $body->read(50));

// Ensure that casting it to a string does not include the bit that was overwritten
$this->assertContains("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", (string) $body);
$this->assertStringContainsString("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", (string) $body);
}

public function testClosesBothStreams()
Expand All @@ -189,11 +185,10 @@ public function testClosesBothStreams()
$this->assertFalse(is_resource($s));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testEnsuresValidWhence()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid whence');
$this->body->seek(10, -123456);
}
}
8 changes: 3 additions & 5 deletions tests/FnStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
*/
class FnStreamTest extends TestCase
{
/**
* @expectedException \BadMethodCallException
* @expectedExceptionMessage seek() is not implemented in the FnStream
*/
public function testThrowsWhenNotImplemented()
{
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('seek() is not implemented in the FnStream');
(new FnStream([]))->seek(1);
}

Expand Down Expand Up @@ -75,7 +73,7 @@ public function testDecoratesStream()
$b->seek(0, SEEK_END);
$b->write('bar');
$this->assertEquals('foobar', (string) $b);
$this->assertInternalType('resource', $b->detach());
$this->assertIsResource($b->detach());
$b->close();
}

Expand Down
48 changes: 13 additions & 35 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,11 @@ public function testCalculatesHash()
$this->assertEquals(md5('foobazbar'), Psr7\hash($s, 'md5'));
}

/**
* @expectedException \RuntimeException
*/
public function testCalculatesHashThrowsWhenSeekFails()
{
$s = new NoSeekStream(Psr7\stream_for('foobazbar'));
$s->read(2);
$this->expectException(\RuntimeException::class);
Psr7\hash($s, 'md5');
}

Expand All @@ -183,16 +181,14 @@ public function testCalculatesHashSeeksToOriginalPosition()
public function testOpensFilesSuccessfully()
{
$r = Psr7\try_fopen(__FILE__, 'r');
$this->assertInternalType('resource', $r);
$this->assertIsResource($r);
fclose($r);
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Unable to open /path/to/does/not/exist using mode r
*/
public function testThrowsExceptionNotWarning()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Unable to open /path/to/does/not/exist using mode r');
Psr7\try_fopen('/path/to/does/not/exist', 'r');
}

Expand Down Expand Up @@ -349,12 +345,10 @@ public function testParsesRequestMessagesWithFoldedHeadersOnHttp10()
$this->assertEquals('Bar Bam', $request->getHeaderLine('Foo'));
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid header syntax: Obsolete line folding
*/
public function testRequestParsingFailsWithFoldedHeadersOnHttp11()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid header syntax: Obsolete line folding');
Psr7\parse_response("GET_DATA / HTTP/1.1\r\nFoo: Bar\r\n Biz: Bam\r\n\r\n");
}

Expand All @@ -368,11 +362,9 @@ public function testParsesRequestMessagesWhenHeaderDelimiterIsOnlyALineFeed()
$this->assertEquals('Bam', $request->getHeaderLine('Baz'));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testValidatesRequestMessages()
{
$this->expectException(\InvalidArgumentException::class);
Psr7\parse_request("HTTP/1.1 200 OK\r\n\r\n");
}

Expand Down Expand Up @@ -422,12 +414,9 @@ public function testParsesResponseWithFoldedHeadersOnHttp10()
$this->assertSame('Test', (string)$response->getBody());
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid header syntax: Obsolete line folding
*/
public function testResponseParsingFailsWithFoldedHeadersOnHttp11()
{
$this->expectException(\InvalidArgumentException::class);
Psr7\parse_response("HTTP/1.1 200\r\nFoo: Bar\r\n Biz: Bam\r\nBaz: Qux\r\n\r\nTest");
}

Expand All @@ -443,20 +432,15 @@ public function testParsesResponseWhenHeaderDelimiterIsOnlyALineFeed()
$this->assertSame("Test\n\nOtherTest", (string)$response->getBody());
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid message: Missing header delimiter
*/
public function testResponseParsingFailsWithoutHeaderDelimiter()
{
$this->expectException(\InvalidArgumentException::class);
Psr7\parse_response("HTTP/1.0 200\r\nFoo: Bar\r\n Baz: Bam\r\nBaz: Qux\r\n");
}

/**
* @expectedException \InvalidArgumentException
*/
public function testValidatesResponseMessages()
{
$this->expectException(\InvalidArgumentException::class);
Psr7\parse_response("GET / HTTP/1.1\r\n\r\n");
}

Expand All @@ -482,11 +466,9 @@ public function testCreatesUriForValue()
);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testValidatesUri()
{
$this->expectException(\InvalidArgumentException::class);
Psr7\uri_for([]);
}

Expand Down Expand Up @@ -541,11 +523,9 @@ public function testCreatePassesThrough()
$this->assertSame($s, Psr7\stream_for($s));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testThrowsExceptionForUnknown()
{
$this->expectException(\InvalidArgumentException::class);
Psr7\stream_for(new \stdClass());
}

Expand Down Expand Up @@ -673,9 +653,6 @@ public function testRewindsBody()
$this->assertEquals(0, $body->tell());
}

/**
* @expectedException \RuntimeException
*/
public function testThrowsWhenBodyCannotBeRewound()
{
$body = Psr7\stream_for('abc');
Expand All @@ -686,6 +663,7 @@ public function testThrowsWhenBodyCannotBeRewound()
},
]);
$res = new Psr7\Response(200, [], $body);
$this->expectException(\RuntimeException::class);
Psr7\rewind_body($res);
}

Expand Down
3 changes: 3 additions & 0 deletions tests/InflateStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use GuzzleHttp\Psr7\NoSeekStream;
use PHPUnit\Framework\TestCase;

/**
* @requires extension zlib
*/
class InflateStreamTest extends TestCase
{
public function testInflatesStreams()
Expand Down
Loading