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

Commit a1b521b

Browse files
committed
Private methods
- Updated `fromGlobals()` to pass `$server` instead of `$_SERVER` to `marshalProtocolVersion()`. - Made `marshalProtocolVersion()` private (for consistency with other methods, and to reduce the public API) - Updated tests to use `ReflectionMethod` in order to directly test the new method. - Fixed an issue discovered in one test; `marshalProtocolVersion()` needs to allow omission of the `HTTP/` prefix.
1 parent 271ae54 commit a1b521b

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/ServerRequestFactory.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function fromGlobals(
7171
);
7272

7373
return $request
74-
->withProtocolVersion(static::marshalProtocolVersion($_SERVER))
74+
->withProtocolVersion(static::marshalProtocolVersion($server))
7575
->withCookieParams($cookies ?: $_COOKIE)
7676
->withQueryParams($query ?: $_GET)
7777
->withParsedBody($body ?: $_POST);
@@ -464,14 +464,17 @@ private static function normalizeNestedFileSpec(array $files = [])
464464
* @param array $server
465465
* @return string
466466
*/
467-
public static function marshalProtocolVersion(array $server)
467+
private static function marshalProtocolVersion(array $server)
468468
{
469-
if (!isset($server['SERVER_PROTOCOL'])) {
469+
if (! isset($server['SERVER_PROTOCOL'])) {
470470
return '1.1';
471471
}
472472

473-
if (!preg_match('#^HTTP/(?P<version>[1-9]\d*\.\d)$#', $server['SERVER_PROTOCOL'], $matches)) {
474-
throw new UnexpectedValueException('Unrecognized protocol version.');
473+
if (! preg_match('#^(HTTP/)?(?P<version>[1-9]\d*\.\d)$#', $server['SERVER_PROTOCOL'], $matches)) {
474+
throw new UnexpectedValueException(sprintf(
475+
'Unrecognized protocol version (%s)',
476+
$server['SERVER_PROTOCOL']
477+
));
475478
}
476479

477480
return $matches['version'];

test/ServerRequestFactoryTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace ZendTest\Diactoros;
1111

1212
use PHPUnit_Framework_TestCase as TestCase;
13+
use ReflectionMethod;
1314
use ReflectionProperty;
1415
use UnexpectedValueException;
1516
use Zend\Diactoros\ServerRequest;
@@ -439,19 +440,25 @@ public function testNormalizeFilesReturnsOnlyActualFilesWhenOriginalFilesContain
439440

440441
public function testMarshalProtocolVersionReturnsHttpVersion()
441442
{
442-
$version = ServerRequestFactory::marshalProtocolVersion(['SERVER_PROTOCOL' => 'HTTP/1.0']);
443+
$method = new ReflectionMethod(ServerRequestFactory::class, 'marshalProtocolVersion');
444+
$method->setAccessible(true);
445+
$version = $method->invoke(null, ['SERVER_PROTOCOL' => 'HTTP/1.0']);
443446
$this->assertEquals('1.0', $version);
444447
}
445448

446449
public function testMarshalProtocolVersionRisesExceptionIfVersionIsNotRecognized()
447450
{
451+
$method = new ReflectionMethod(ServerRequestFactory::class, 'marshalProtocolVersion');
452+
$method->setAccessible(true);
448453
$this->setExpectedException('UnexpectedValueException');
449-
ServerRequestFactory::marshalProtocolVersion(['SERVER_PROTOCOL' => 'dadsa/1.0']);
454+
$method->invoke(null, ['SERVER_PROTOCOL' => 'dadsa/1.0']);
450455
}
451456

452457
public function testMarshalProtocolReturnsDefaultValueIfHeaderIsNotPresent()
453458
{
454-
$version = ServerRequestFactory::marshalProtocolVersion([]);
459+
$method = new ReflectionMethod(ServerRequestFactory::class, 'marshalProtocolVersion');
460+
$method->setAccessible(true);
461+
$version = $method->invoke(null, []);
455462
$this->assertEquals('1.1', $version);
456463
}
457464
}

0 commit comments

Comments
 (0)