Skip to content

Commit 2b0cea6

Browse files
author
Robin de Graaf
committed
Make Request instantiable without params
1 parent 1bc76bd commit 2b0cea6

9 files changed

Lines changed: 59 additions & 203 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Parable Http
22

3+
## 0.5.1
4+
5+
_Changes_
6+
- `Request` can now be instantiated without passing all values, in which cases it will set itself up by using `RequestFactory::getValuesFromServer`.
7+
38
## 0.5.0
49

510
_Changes_

Makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ tests: dependencies
99

1010
coverage: dependencies
1111
rm -rf ./coverage
12-
vendor/bin/phpunit --coverage-html ./coverage tests
12+
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage tests
1313

1414
tests-clean:
1515
vendor/bin/phpunit --verbose tests
16-
17-
coverage-clean:
18-
rm -rf ./coverage
19-
vendor/bin/phpunit --coverage-html ./coverage tests

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"ralouphie/getallheaders": "^3.0"
1919
},
2020
"require-dev": {
21-
"phpunit/phpunit": "^8.0"
21+
"phpunit/phpunit": "^9.0"
2222
},
2323
"autoload": {
2424
"psr-4": {

phpunit.xml

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
<?xml version="1.0" encoding="UTF-8" ?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.4/phpunit.xsd"
3-
backupGlobals="false"
4-
backupStaticAttributes="false"
5-
beStrictAboutTestsThatDoNotTestAnything="true"
6-
beStrictAboutOutputDuringTests="true"
7-
colors="true"
8-
cacheResult="false"
9-
>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
beStrictAboutTestsThatDoNotTestAnything="true"
8+
beStrictAboutOutputDuringTests="true"
9+
colors="true"
10+
cacheResult="false"
11+
bootstrap="vendor/autoload.php"
12+
>
13+
<coverage processUncoveredFiles="true">
14+
<include>
15+
<directory suffix=".php">./src</directory>
16+
</include>
17+
</coverage>
1018
<php>
11-
<ini name="memory_limit" value="1024M" />
12-
<ini name="display_errors" value="stdout" />
13-
<ini name="error_log" value="/dev/null" />
19+
<ini name="memory_limit" value="1024M"/>
20+
<ini name="display_errors" value="stdout"/>
21+
<ini name="error_log" value="/dev/null"/>
1422
</php>
15-
<filter>
16-
<whitelist processUncoveredFilesFromWhitelist="true">
17-
<directory suffix=".php">./src</directory>
18-
</whitelist>
19-
</filter>
20-
</phpunit>
23+
</phpunit>

src/HeaderSender.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static function setTestMode(bool $testMode): void
1616

1717
public static function send(string $header): void
1818
{
19-
if (self::$testMode) {
19+
if (self::$testMode === true) {
2020
self::$headers[] = $header;
2121

2222
return;
@@ -27,7 +27,7 @@ public static function send(string $header): void
2727

2828
public static function alreadySent(): bool
2929
{
30-
if (self::$testMode) {
30+
if (self::$testMode === true) {
3131
return false;
3232
}
3333

@@ -36,7 +36,7 @@ public static function alreadySent(): bool
3636

3737
public static function list(): array
3838
{
39-
if (self::$testMode) {
39+
if (self::$testMode === true) {
4040
return self::$headers;
4141
}
4242

src/HttpStatusCode.php

Lines changed: 0 additions & 171 deletions
This file was deleted.

src/Request.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,24 @@ class Request
1616
protected ?string $body = null;
1717

1818
public function __construct(
19-
string $method,
20-
string $uri,
19+
string $method = null,
20+
string|Uri $uri = null,
2121
array $headers = [],
2222
string $protocol = 'HTTP/1.1'
2323
) {
24+
if ($method === null || $uri === null) {
25+
[$method, $uri, $headers, $protocol] = RequestFactory::getValuesFromServer();
26+
}
27+
2428
$this->method = strtoupper($method);
25-
$this->uri = new Uri($uri);
2629
$this->protocol = $protocol;
27-
2830
$this->addHeaders($headers);
31+
32+
if (!($uri instanceof Uri)) {
33+
$uri = new Uri($uri);
34+
}
35+
36+
$this->uri = $uri;
2937
}
3038

3139
public function getMethod(): string

src/RequestFactory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
class RequestFactory
66
{
77
public static function createFromServer(): Request
8+
{
9+
return new Request(...self::getValuesFromServer());
10+
}
11+
12+
public static function getValuesFromServer(): array
813
{
914
$method = self::getMethodFromServerArray($_SERVER);
1015
$uri = self::buildUriFromServerArray($_SERVER);
@@ -15,7 +20,7 @@ public static function createFromServer(): Request
1520
throw new HttpException('Could not build uri from $_SERVER array.');
1621
}
1722

18-
return new Request($method, $uri->getUriString(), $headers, $protocol);
23+
return [$method, $uri, $headers, $protocol];
1924
}
2025

2126
protected static function buildUriFromServerArray(array $serverArray): ?Uri

tests/RequestTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77

88
class RequestTest extends TestCase
99
{
10+
public function testIsPopulatedFromServerIfNoConstructorParametersPassed(): void
11+
{
12+
$_SERVER['HTTP_HOST'] = 'test.dev';
13+
14+
$request = new Request();
15+
16+
self::assertSame('GET', $request->getMethod());
17+
self::assertSame('http://test.dev', $request->getUri()->__toString());
18+
}
19+
1020
public function testGetUri(): void
1121
{
1222
$request = new Request('GET', 'http://test.dev/folder/being/requested');

0 commit comments

Comments
 (0)