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

Commit 60fd6ac

Browse files
committed
Merge branch 'hotfix/124'
Close #124
2 parents 68fab74 + de8c400 commit 60fd6ac

File tree

5 files changed

+53
-17
lines changed

5 files changed

+53
-17
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ All notable changes to this project will be documented in this file, in reverse
66

77
### Added
88

9-
- Nothing.
9+
- [#124](https://github.com/zendframework/zend-diactoros/pull/124) adds four
10+
more optional arguments to the `ServerRequest` constructor:
11+
- `array $cookies`
12+
- `array $queryParams`
13+
- `null|array|object $parsedBody`
14+
- `string $protocolVersion`
15+
`ServerRequestFactory` was updated to pass values for each of these parameters
16+
when creating an instance, instead of using the related `with*()` methods on
17+
an instance.
1018

1119
### Deprecated
1220

src/ServerRequest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class ServerRequest implements ServerRequestInterface
6969
* @param null|string $method HTTP method for the request, if any.
7070
* @param string|resource|StreamInterface $body Message body, if any.
7171
* @param array $headers Headers for the message, if any.
72+
* @param array $cookies Cookies for the message, if any.
73+
* @param array $queryParams Query params for the message, if any.
74+
* @param null|array|object $parsedBody The deserialized body parameters, if any.
75+
* @param string HTTP protocol version.
7276
* @throws InvalidArgumentException for any invalid value.
7377
*/
7478
public function __construct(
@@ -77,14 +81,22 @@ public function __construct(
7781
$uri = null,
7882
$method = null,
7983
$body = 'php://input',
80-
array $headers = []
84+
array $headers = [],
85+
array $cookies = [],
86+
array $queryParams = [],
87+
$parsedBody = null,
88+
$protocol = '1.1'
8189
) {
8290
$this->validateUploadedFiles($uploadedFiles);
8391

8492
$body = $this->getStream($body);
8593
$this->initialize($uri, $method, $body, $headers);
8694
$this->serverParams = $serverParams;
8795
$this->uploadedFiles = $uploadedFiles;
96+
$this->cookieParams = $cookies;
97+
$this->queryParams = $queryParams;
98+
$this->parsedBody = $parsedBody;
99+
$this->protocol = $protocol;
88100
}
89101

90102
/**

src/ServerRequestFactory.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,20 @@ public static function fromGlobals(
6161
$server = static::normalizeServer($server ?: $_SERVER);
6262
$files = static::normalizeFiles($files ?: $_FILES);
6363
$headers = static::marshalHeaders($server);
64-
$request = new ServerRequest(
64+
65+
return new ServerRequest(
6566
$server,
6667
$files,
6768
static::marshalUriFromServer($server, $headers),
6869
static::get('REQUEST_METHOD', $server, 'GET'),
6970
'php://input',
70-
$headers
71+
$headers,
72+
$cookies ?: $_COOKIE,
73+
$query ?: $_GET,
74+
$body ?: $_POST,
75+
static::marshalProtocolVersion($server)
7176
);
7277

73-
return $request
74-
->withProtocolVersion(static::marshalProtocolVersion($server))
75-
->withCookieParams($cookies ?: $_COOKIE)
76-
->withQueryParams($query ?: $_GET)
77-
->withParsedBody($body ?: $_POST);
7878
}
7979

8080
/**

test/ServerRequestFactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,23 +440,23 @@ public function testNormalizeFilesReturnsOnlyActualFilesWhenOriginalFilesContain
440440

441441
public function testMarshalProtocolVersionReturnsHttpVersion()
442442
{
443-
$method = new ReflectionMethod(ServerRequestFactory::class, 'marshalProtocolVersion');
443+
$method = new ReflectionMethod('Zend\Diactoros\ServerRequestFactory', 'marshalProtocolVersion');
444444
$method->setAccessible(true);
445445
$version = $method->invoke(null, ['SERVER_PROTOCOL' => 'HTTP/1.0']);
446446
$this->assertEquals('1.0', $version);
447447
}
448448

449449
public function testMarshalProtocolVersionRisesExceptionIfVersionIsNotRecognized()
450450
{
451-
$method = new ReflectionMethod(ServerRequestFactory::class, 'marshalProtocolVersion');
451+
$method = new ReflectionMethod('Zend\Diactoros\ServerRequestFactory', 'marshalProtocolVersion');
452452
$method->setAccessible(true);
453453
$this->setExpectedException('UnexpectedValueException');
454454
$method->invoke(null, ['SERVER_PROTOCOL' => 'dadsa/1.0']);
455455
}
456456

457457
public function testMarshalProtocolReturnsDefaultValueIfHeaderIsNotPresent()
458458
{
459-
$method = new ReflectionMethod(ServerRequestFactory::class, 'marshalProtocolVersion');
459+
$method = new ReflectionMethod('Zend\Diactoros\ServerRequestFactory', 'marshalProtocolVersion');
460460
$method->setAccessible(true);
461461
$version = $method->invoke(null, []);
462462
$this->assertEquals('1.1', $version);

test/ServerRequestTest.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,38 @@ public function testUsesProvidedConstructorArguments($parameterMethod, $methodRe
135135
$headers = [
136136
'host' => ['example.com'],
137137
];
138+
$cookies = [
139+
'boo' => 'foo',
140+
];
141+
$queryParams = [
142+
'bar' => 'bat',
143+
];
144+
$parsedBody = 'bazbar';
145+
$protocol = '1.2';
138146

139147
$request = new ServerRequest(
140148
$server,
141149
$files,
142150
$uri,
143151
$parameterMethod,
144152
'php://memory',
145-
$headers
153+
$headers,
154+
$cookies,
155+
$queryParams,
156+
$parsedBody,
157+
$protocol
146158
);
147159

148-
$this->assertEquals($server, $request->getServerParams());
149-
$this->assertEquals($files, $request->getUploadedFiles());
160+
$this->assertSame($server, $request->getServerParams());
161+
$this->assertSame($files, $request->getUploadedFiles());
150162

151163
$this->assertSame($uri, $request->getUri());
152-
$this->assertEquals($methodReturned, $request->getMethod());
153-
$this->assertEquals($headers, $request->getHeaders());
164+
$this->assertSame($methodReturned, $request->getMethod());
165+
$this->assertSame($headers, $request->getHeaders());
166+
$this->assertSame($cookies, $request->getCookieParams());
167+
$this->assertSame($queryParams, $request->getQueryParams());
168+
$this->assertSame($parsedBody, $request->getParsedBody());
169+
$this->assertSame($protocol, $request->getProtocolVersion());
154170

155171
$body = $request->getBody();
156172
$r = new ReflectionProperty($body, 'stream');

0 commit comments

Comments
 (0)