Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit f31cff8

Browse files
committed
qa: extract data provider for HTTP/2 tests
Extracts `validHttpVersions()` and `validResponseHttpVersionProvider()` methods to ensure we test various functionalities against each of the three supported HTTP versions. Updates `testResponseFactoryFromStringCreatesValidResponse()` to use the latter, and splits `testResponseCanRenderStatusLine()` into `testResponseCanRenderStatusLineUsingDefaultReasonPhrase()` and `testResponseCanRenderStatusLineUsingCustomReasonPhrase()`, each using `validHttpVersions()` as a data provider.
1 parent bea753d commit f31cff8

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed

test/ResponseTest.php

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,74 @@
1616

1717
class ResponseTest extends TestCase
1818
{
19-
public function testResponseFactoryFromStringCreatesValidResponse()
19+
public function validHttpVersions()
2020
{
21-
$string = 'HTTP/1.0 200 OK' . "\r\n\r\n" . 'Foo Bar';
22-
$response = Response::fromString($string);
23-
$this->assertEquals(200, $response->getStatusCode());
24-
$this->assertEquals('Foo Bar', $response->getContent());
21+
yield 'http/1.0' => ['1.0'];
22+
yield 'http/1.1' => ['1.1'];
23+
yield 'http/2' => ['2'];
24+
}
25+
26+
public function validResponseHttpVersionProvider()
27+
{
28+
$responseTemplate = "HTTP/%s 200 OK\r\n\r\nFoo Bar";
29+
foreach ($this->validHttpVersions() as $testCase => $data) {
30+
$version = array_shift($data);
31+
yield $testCase => [
32+
'response' => sprintf($responseTemplate, $version),
33+
'expectedVersion' => $version,
34+
'expectedStatus' => '200',
35+
'expectedContent' => 'Foo Bar',
36+
];
37+
}
38+
}
2539

26-
$string = 'HTTP/2 200 OK' . "\r\n\r\n" . 'Foo Bar';
40+
/**
41+
* @dataProvider validResponseHttpVersionProvider
42+
* @param string $string Response string
43+
* @param string $expectedVersion
44+
* @param string $expectedStatus
45+
* @param string $expectedContent
46+
*/
47+
public function testResponseFactoryFromStringCreatesValidResponse(
48+
$string,
49+
$expectedVersion,
50+
$expectedStatus,
51+
$expectedContent
52+
) {
2753
$response = Response::fromString($string);
28-
$this->assertEquals('2', $response->getVersion());
54+
$this->assertEquals($expectedVersion, $response->getVersion());
55+
$this->assertEquals($expectedStatus, $response->getStatusCode());
56+
$this->assertEquals($expectedContent, $response->getContent());
2957
}
3058

31-
public function testResponseCanRenderStatusLine()
59+
/**
60+
* @dataProvider validHttpVersions
61+
* @param string $version
62+
*/
63+
public function testResponseCanRenderStatusLineUsingDefaultReasonPhrase($version)
3264
{
65+
$expected = sprintf('HTTP/%s 404 Not Found', $version);
3366
$response = new Response();
34-
$response->setVersion('1.1');
67+
$response->setVersion($version);
3568
$response->setStatusCode(Response::STATUS_CODE_404);
36-
$this->assertEquals('HTTP/1.1 404 Not Found', $response->renderStatusLine());
69+
$this->assertEquals($expected, $response->renderStatusLine());
70+
}
3771

72+
/**
73+
* @dataProvider validHttpVersions
74+
* @param string $version
75+
*/
76+
public function testResponseCanRenderStatusLineUsingCustomReasonPhrase($version)
77+
{
78+
$expected = sprintf('HTTP/%s 404 Foo Bar', $version);
79+
$response = new Response();
80+
$response->setVersion($version);
81+
$response->setStatusCode(Response::STATUS_CODE_404);
3882
$response->setReasonPhrase('Foo Bar');
39-
$this->assertEquals('HTTP/1.1 404 Foo Bar', $response->renderStatusLine());
40-
41-
$response->setVersion('2');
42-
$this->assertEquals('HTTP/2 404 Foo Bar', $response->renderStatusLine());
83+
$this->assertEquals($expected, $response->renderStatusLine());
4384
}
4485

45-
public function testInvalidHTTPVersion()
86+
public function testInvalidHTTP2VersionString()
4687
{
4788
$this->expectException(InvalidArgumentException::class);
4889
$this->expectExceptionMessage('A valid response status line was not found in the provided string');

0 commit comments

Comments
 (0)