Skip to content

Commit

Permalink
feat: Adds wrapper for HTTP code 422. (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavofreze committed Mar 11, 2024
1 parent bd8983b commit ed7a9ba
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 57 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"require-dev": {
"infection/infection": "^0.27",
"phpmd/phpmd": "^2.15",
"phpunit/phpunit": "^10",
"phpunit/phpunit": "^10.5",
"squizlabs/php_codesniffer": "^3.8"
},
"suggest": {
Expand Down
27 changes: 9 additions & 18 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
{
"timeout": 10,
"testFramework": "phpunit",
"$schema": "vendor/infection/infection/resources/schema.json",
"tmpDir": "report/",
"logs": {
"text": "report/logs/infection-text.log",
"summary": "report/logs/infection-summary.log"
},
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "report/logs/infection-text.log",
"summary": "report/logs/infection-summary.log"
},
"timeout": 10,
"mutators": {
"@default": true,
"ArrayItem": false,
"LogicalOr": false,
"IfNegation": false,
"InstanceOf_": false,
"UnwrapArrayMap": false,
"ArrayItemRemoval": false,
"UnwrapArrayUnique": false,
"MethodCallRemoval": false,
"LogicalOrAllSubExprNegation": false,
"LogicalOrSingleSubExprNegation": false
"MethodCallRemoval": false
},
"phpUnit": {
"configDir": "",
"customPath": "./vendor/bin/phpunit"
}
}
"testFramework": "phpunit"
}
5 changes: 1 addition & 4 deletions src/HttpHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ public function hasHeader(string $key): bool

public function toArray(): array
{
return array_map(
fn(array $values): array => [end($values)],
array_map(fn(array $values): array => array_unique($values), $this->values)
);
return array_map(fn(array $values): array => array_unique($values), $this->values);
}
}
5 changes: 5 additions & 0 deletions src/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public static function conflict(mixed $data, ?HttpHeaders $headers = null): Resp
return Response::from(code: HttpCode::CONFLICT, data: $data, headers: $headers);
}

public static function unprocessableEntity(mixed $data, ?HttpHeaders $headers = null): ResponseInterface
{
return Response::from(code: HttpCode::UNPROCESSABLE_ENTITY, data: $data, headers: $headers);
}

# Server error (500 – 599)

public static function internalServerError(mixed $data, ?HttpHeaders $headers = null): ResponseInterface
Expand Down
6 changes: 3 additions & 3 deletions src/Internal/Stream/StreamMetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

final readonly class StreamMetaData
{
public function __construct(
private function __construct(
private string $uri,
private string $mode,
private bool $seekable,
Expand Down Expand Up @@ -36,8 +36,8 @@ public function toArray(): array
{
return [
'uri' => $this->uri,
'mode' => $this->getMode(),
'seekable' => $this->isSeekable(),
'mode' => $this->mode,
'seekable' => $this->seekable,
'streamType' => $this->streamType
];
}
Expand Down
14 changes: 14 additions & 0 deletions tests/HttpResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ public function testResponseConflict(mixed $data, mixed $expected): void
self::assertEquals($this->defaultHeaderFrom(code: HttpCode::CONFLICT), $response->getHeaders());
}

/**
* @dataProvider providerData
*/
public function testResponseUnprocessableEntity(mixed $data, mixed $expected): void
{
$response = HttpResponse::unprocessableEntity(data: $data);

self::assertEquals($expected, $response->getBody()->__toString());
self::assertEquals($expected, $response->getBody()->getContents());
self::assertEquals(HttpCode::UNPROCESSABLE_ENTITY->value, $response->getStatusCode());
self::assertEquals(HttpCode::UNPROCESSABLE_ENTITY->message(), $response->getReasonPhrase());
self::assertEquals($this->defaultHeaderFrom(code: HttpCode::UNPROCESSABLE_ENTITY), $response->getHeaders());
}

/**
* @dataProvider providerData
*/
Expand Down
28 changes: 0 additions & 28 deletions tests/Internal/HeadersTest.php

This file was deleted.

61 changes: 61 additions & 0 deletions tests/Internal/HttpHeadersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace TinyBlocks\Http\Internal;

use PHPUnit\Framework\TestCase;
use TinyBlocks\Http\HttpCode;
use TinyBlocks\Http\HttpContentType;
use TinyBlocks\Http\HttpHeaders;

class HttpHeadersTest extends TestCase
{
public function testAddAndRemoveHeaders(): void
{
$actual = HttpHeaders::build()
->addFrom(key: 'X-Custom-Header', value: 'value1')
->addFrom(key: 'X-Custom-Header', value: 'value2')
->removeFrom(key: 'X-Custom-Header');

self::assertTrue($actual->hasNoHeaders());
self::assertFalse($actual->hasHeader(key: 'X-Custom-Header'));
}

public function testAddFromCode(): void
{
$actual = HttpHeaders::build()->addFromCode(code: HttpCode::OK);
$expected = ['Status' => [HttpCode::OK->message()]];

self::assertEquals($expected, $actual->toArray());
}

public function testAddFromContentType(): void
{
$headers = HttpHeaders::build()->addFromContentType(header: HttpContentType::APPLICATION_JSON);
$actual = $headers->toArray();
$expected = ['Content-Type' => [HttpContentType::APPLICATION_JSON->value]];

self::assertEquals($expected, $actual);
}

public function testGetHeader(): void
{
$headers = HttpHeaders::build()
->addFrom(key: 'X-Custom-Header', value: 'value1')
->addFrom(key: 'X-Custom-Header', value: 'value2');
$actual = $headers->getHeader(key: 'X-Custom-Header');
$expected = ['value1', 'value2'];

self::assertEquals($expected, $actual);
}

public function testToArrayWithNonUniqueValues(): void
{
$headers = HttpHeaders::build()
->addFrom(key: 'X-Custom-Header', value: 'value1')
->addFrom(key: 'X-Custom-Header', value: 'value1');
$actual = $headers->toArray();
$expected = ['X-Custom-Header' => ['value1']];

self::assertEquals($expected, $actual);
}
}
24 changes: 21 additions & 3 deletions tests/Internal/Stream/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,18 @@ public function testGetMetadata(): void
$actual = $stream->getMetadata();
$expected = StreamMetaData::from(data: stream_get_meta_data($this->resource))->toArray();

self::assertNull($stream->getMetadata(key: ''));
self::assertEquals($expected, $actual);
self::assertEquals($expected['mode'], $stream->getMetadata(key: 'mode'));
self::assertEquals($expected['uri'], $actual['uri']);
self::assertEquals($expected['mode'], $actual['mode']);
self::assertEquals($expected['seekable'], $actual['seekable']);
self::assertEquals($expected['streamType'], $actual['streamType']);
}

public function testGetMetadataWhenKeyIsUnknown(): void
{
$stream = Stream::from(resource: $this->resource);
$actual = $stream->getMetadata(key: 'UNKNOWN');

self::assertNull($actual);
}

public function testSeekMovesCursorPosition(): void
Expand Down Expand Up @@ -125,6 +134,15 @@ public function testExceptionWhenMissingResourceStreamOnTell(): void
$stream->tell();
}

public function testToStringRewindsStreamIfNotSeekable(): void
{
$stream = Stream::from(resource: $this->resource);
$stream->write(string: 'Hello, world!');
$actual = (string)$stream;

self::assertEquals('Hello, world!', $actual);
}

public function testExceptionWhenNonSeekableStream(): void
{
$stream = Stream::from(resource: $this->resource);
Expand Down

0 comments on commit ed7a9ba

Please sign in to comment.