Skip to content

Commit bbebb56

Browse files
authored
Release/3.1.0 - (#30)
1 parent bd8983b commit bbebb56

File tree

9 files changed

+115
-57
lines changed

9 files changed

+115
-57
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"require-dev": {
5050
"infection/infection": "^0.27",
5151
"phpmd/phpmd": "^2.15",
52-
"phpunit/phpunit": "^10",
52+
"phpunit/phpunit": "^10.5",
5353
"squizlabs/php_codesniffer": "^3.8"
5454
},
5555
"suggest": {

infection.json.dist

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
{
2-
"timeout": 10,
3-
"testFramework": "phpunit",
2+
"$schema": "vendor/infection/infection/resources/schema.json",
43
"tmpDir": "report/",
4+
"logs": {
5+
"text": "report/logs/infection-text.log",
6+
"summary": "report/logs/infection-summary.log"
7+
},
58
"source": {
69
"directories": [
710
"src"
811
]
912
},
10-
"logs": {
11-
"text": "report/logs/infection-text.log",
12-
"summary": "report/logs/infection-summary.log"
13-
},
13+
"timeout": 10,
1414
"mutators": {
1515
"@default": true,
16-
"ArrayItem": false,
1716
"LogicalOr": false,
18-
"IfNegation": false,
1917
"InstanceOf_": false,
2018
"UnwrapArrayMap": false,
21-
"ArrayItemRemoval": false,
22-
"UnwrapArrayUnique": false,
23-
"MethodCallRemoval": false,
24-
"LogicalOrAllSubExprNegation": false,
25-
"LogicalOrSingleSubExprNegation": false
19+
"MethodCallRemoval": false
2620
},
27-
"phpUnit": {
28-
"configDir": "",
29-
"customPath": "./vendor/bin/phpunit"
30-
}
31-
}
21+
"testFramework": "phpunit"
22+
}

src/HttpHeaders.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ public function hasHeader(string $key): bool
6363

6464
public function toArray(): array
6565
{
66-
return array_map(
67-
fn(array $values): array => [end($values)],
68-
array_map(fn(array $values): array => array_unique($values), $this->values)
69-
);
66+
return array_map(fn(array $values): array => array_unique($values), $this->values);
7067
}
7168
}

src/HttpResponse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public static function conflict(mixed $data, ?HttpHeaders $headers = null): Resp
5151
return Response::from(code: HttpCode::CONFLICT, data: $data, headers: $headers);
5252
}
5353

54+
public static function unprocessableEntity(mixed $data, ?HttpHeaders $headers = null): ResponseInterface
55+
{
56+
return Response::from(code: HttpCode::UNPROCESSABLE_ENTITY, data: $data, headers: $headers);
57+
}
58+
5459
# Server error (500 – 599)
5560

5661
public static function internalServerError(mixed $data, ?HttpHeaders $headers = null): ResponseInterface

src/Internal/Stream/StreamMetaData.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
final readonly class StreamMetaData
66
{
7-
public function __construct(
7+
private function __construct(
88
private string $uri,
99
private string $mode,
1010
private bool $seekable,
@@ -36,8 +36,8 @@ public function toArray(): array
3636
{
3737
return [
3838
'uri' => $this->uri,
39-
'mode' => $this->getMode(),
40-
'seekable' => $this->isSeekable(),
39+
'mode' => $this->mode,
40+
'seekable' => $this->seekable,
4141
'streamType' => $this->streamType
4242
];
4343
}

tests/HttpResponseTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ public function testResponseConflict(mixed $data, mixed $expected): void
103103
self::assertEquals($this->defaultHeaderFrom(code: HttpCode::CONFLICT), $response->getHeaders());
104104
}
105105

106+
/**
107+
* @dataProvider providerData
108+
*/
109+
public function testResponseUnprocessableEntity(mixed $data, mixed $expected): void
110+
{
111+
$response = HttpResponse::unprocessableEntity(data: $data);
112+
113+
self::assertEquals($expected, $response->getBody()->__toString());
114+
self::assertEquals($expected, $response->getBody()->getContents());
115+
self::assertEquals(HttpCode::UNPROCESSABLE_ENTITY->value, $response->getStatusCode());
116+
self::assertEquals(HttpCode::UNPROCESSABLE_ENTITY->message(), $response->getReasonPhrase());
117+
self::assertEquals($this->defaultHeaderFrom(code: HttpCode::UNPROCESSABLE_ENTITY), $response->getHeaders());
118+
}
119+
106120
/**
107121
* @dataProvider providerData
108122
*/

tests/Internal/HeadersTest.php

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

tests/Internal/HttpHeadersTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace TinyBlocks\Http\Internal;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use TinyBlocks\Http\HttpCode;
7+
use TinyBlocks\Http\HttpContentType;
8+
use TinyBlocks\Http\HttpHeaders;
9+
10+
class HttpHeadersTest extends TestCase
11+
{
12+
public function testAddAndRemoveHeaders(): void
13+
{
14+
$actual = HttpHeaders::build()
15+
->addFrom(key: 'X-Custom-Header', value: 'value1')
16+
->addFrom(key: 'X-Custom-Header', value: 'value2')
17+
->removeFrom(key: 'X-Custom-Header');
18+
19+
self::assertTrue($actual->hasNoHeaders());
20+
self::assertFalse($actual->hasHeader(key: 'X-Custom-Header'));
21+
}
22+
23+
public function testAddFromCode(): void
24+
{
25+
$actual = HttpHeaders::build()->addFromCode(code: HttpCode::OK);
26+
$expected = ['Status' => [HttpCode::OK->message()]];
27+
28+
self::assertEquals($expected, $actual->toArray());
29+
}
30+
31+
public function testAddFromContentType(): void
32+
{
33+
$headers = HttpHeaders::build()->addFromContentType(header: HttpContentType::APPLICATION_JSON);
34+
$actual = $headers->toArray();
35+
$expected = ['Content-Type' => [HttpContentType::APPLICATION_JSON->value]];
36+
37+
self::assertEquals($expected, $actual);
38+
}
39+
40+
public function testGetHeader(): void
41+
{
42+
$headers = HttpHeaders::build()
43+
->addFrom(key: 'X-Custom-Header', value: 'value1')
44+
->addFrom(key: 'X-Custom-Header', value: 'value2');
45+
$actual = $headers->getHeader(key: 'X-Custom-Header');
46+
$expected = ['value1', 'value2'];
47+
48+
self::assertEquals($expected, $actual);
49+
}
50+
51+
public function testToArrayWithNonUniqueValues(): void
52+
{
53+
$headers = HttpHeaders::build()
54+
->addFrom(key: 'X-Custom-Header', value: 'value1')
55+
->addFrom(key: 'X-Custom-Header', value: 'value1');
56+
$actual = $headers->toArray();
57+
$expected = ['X-Custom-Header' => ['value1']];
58+
59+
self::assertEquals($expected, $actual);
60+
}
61+
}

tests/Internal/Stream/StreamTest.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,18 @@ public function testGetMetadata(): void
6767
$actual = $stream->getMetadata();
6868
$expected = StreamMetaData::from(data: stream_get_meta_data($this->resource))->toArray();
6969

70-
self::assertNull($stream->getMetadata(key: ''));
71-
self::assertEquals($expected, $actual);
72-
self::assertEquals($expected['mode'], $stream->getMetadata(key: 'mode'));
70+
self::assertEquals($expected['uri'], $actual['uri']);
71+
self::assertEquals($expected['mode'], $actual['mode']);
72+
self::assertEquals($expected['seekable'], $actual['seekable']);
73+
self::assertEquals($expected['streamType'], $actual['streamType']);
74+
}
75+
76+
public function testGetMetadataWhenKeyIsUnknown(): void
77+
{
78+
$stream = Stream::from(resource: $this->resource);
79+
$actual = $stream->getMetadata(key: 'UNKNOWN');
80+
81+
self::assertNull($actual);
7382
}
7483

7584
public function testSeekMovesCursorPosition(): void
@@ -125,6 +134,15 @@ public function testExceptionWhenMissingResourceStreamOnTell(): void
125134
$stream->tell();
126135
}
127136

137+
public function testToStringRewindsStreamIfNotSeekable(): void
138+
{
139+
$stream = Stream::from(resource: $this->resource);
140+
$stream->write(string: 'Hello, world!');
141+
$actual = (string)$stream;
142+
143+
self::assertEquals('Hello, world!', $actual);
144+
}
145+
128146
public function testExceptionWhenNonSeekableStream(): void
129147
{
130148
$stream = Stream::from(resource: $this->resource);

0 commit comments

Comments
 (0)