Skip to content

Commit

Permalink
Make Request instantiable without params
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin de Graaf committed Feb 4, 2021
1 parent 1bc76bd commit 2b0cea6
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 203 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Parable Http

## 0.5.1

_Changes_
- `Request` can now be instantiated without passing all values, in which cases it will set itself up by using `RequestFactory::getValuesFromServer`.

## 0.5.0

_Changes_
Expand Down
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ tests: dependencies

coverage: dependencies
rm -rf ./coverage
vendor/bin/phpunit --coverage-html ./coverage tests
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage tests

tests-clean:
vendor/bin/phpunit --verbose tests

coverage-clean:
rm -rf ./coverage
vendor/bin/phpunit --coverage-html ./coverage tests
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"ralouphie/getallheaders": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-4": {
Expand Down
39 changes: 21 additions & 18 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.4/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
colors="true"
cacheResult="false"
>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
colors="true"
cacheResult="false"
bootstrap="vendor/autoload.php"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<php>
<ini name="memory_limit" value="1024M" />
<ini name="display_errors" value="stdout" />
<ini name="error_log" value="/dev/null" />
<ini name="memory_limit" value="1024M"/>
<ini name="display_errors" value="stdout"/>
<ini name="error_log" value="/dev/null"/>
</php>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
</phpunit>
6 changes: 3 additions & 3 deletions src/HeaderSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static function setTestMode(bool $testMode): void

public static function send(string $header): void
{
if (self::$testMode) {
if (self::$testMode === true) {
self::$headers[] = $header;

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

public static function alreadySent(): bool
{
if (self::$testMode) {
if (self::$testMode === true) {
return false;
}

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

public static function list(): array
{
if (self::$testMode) {
if (self::$testMode === true) {
return self::$headers;
}

Expand Down
171 changes: 0 additions & 171 deletions src/HttpStatusCode.php

This file was deleted.

16 changes: 12 additions & 4 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ class Request
protected ?string $body = null;

public function __construct(
string $method,
string $uri,
string $method = null,
string|Uri $uri = null,
array $headers = [],
string $protocol = 'HTTP/1.1'
) {
if ($method === null || $uri === null) {
[$method, $uri, $headers, $protocol] = RequestFactory::getValuesFromServer();
}

$this->method = strtoupper($method);
$this->uri = new Uri($uri);
$this->protocol = $protocol;

$this->addHeaders($headers);

if (!($uri instanceof Uri)) {
$uri = new Uri($uri);
}

$this->uri = $uri;
}

public function getMethod(): string
Expand Down
7 changes: 6 additions & 1 deletion src/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
class RequestFactory
{
public static function createFromServer(): Request
{
return new Request(...self::getValuesFromServer());
}

public static function getValuesFromServer(): array
{
$method = self::getMethodFromServerArray($_SERVER);
$uri = self::buildUriFromServerArray($_SERVER);
Expand All @@ -15,7 +20,7 @@ public static function createFromServer(): Request
throw new HttpException('Could not build uri from $_SERVER array.');
}

return new Request($method, $uri->getUriString(), $headers, $protocol);
return [$method, $uri, $headers, $protocol];
}

protected static function buildUriFromServerArray(array $serverArray): ?Uri
Expand Down
10 changes: 10 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

class RequestTest extends TestCase
{
public function testIsPopulatedFromServerIfNoConstructorParametersPassed(): void
{
$_SERVER['HTTP_HOST'] = 'test.dev';

$request = new Request();

self::assertSame('GET', $request->getMethod());
self::assertSame('http://test.dev', $request->getUri()->__toString());
}

public function testGetUri(): void
{
$request = new Request('GET', 'http://test.dev/folder/being/requested');
Expand Down

0 comments on commit 2b0cea6

Please sign in to comment.