Skip to content

Commit 16db312

Browse files
committed
100% code coverage
1 parent 34afc14 commit 16db312

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

src/ApiProblem.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,6 @@ public function __get(string $name): mixed
183183
return $this->{$prop};
184184
}
185185

186-
if (isset($this->additionalDetails[$name])) {
187-
return $this->additionalDetails[$name];
188-
}
189-
190186
if (isset($this->additionalDetails[$normalized])) {
191187
return $this->additionalDetails[$normalized];
192188
}
@@ -221,7 +217,8 @@ public function toArray(): array
221217
*
222218
* @param mixed[] $params
223219
*/
224-
public function response(array ...$params): JsonResponse
220+
// phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
221+
public function response(...$params): JsonResponse
225222
{
226223
$apProblem = null;
227224

@@ -314,11 +311,7 @@ protected function getTitle(): string
314311
return get_class($this->detail);
315312
}
316313

317-
if ($this->title === null) {
318-
return 'Unknown';
319-
}
320-
321-
return $this->title;
314+
return 'Unknown';
322315
}
323316

324317
/**

test/ApiProblemTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
use ApiSkeletons\Laravel\ApiProblem\ApiProblem;
88
use ApiSkeletons\Laravel\ApiProblem\Exception;
9+
use ApiSkeletons\Laravel\ApiProblem\Facades\ApiProblem as ApiProblemFacade;
10+
use http\Exception\InvalidArgumentException;
11+
use Illuminate\Http\JsonResponse;
912
use ReflectionObject;
1013
use TypeError;
1114

@@ -27,6 +30,18 @@ public function statusCodes(): array
2730
];
2831
}
2932

33+
public function testResponseWithObject(): void
34+
{
35+
$apiProblem = new ApiProblem(500, 'Testing');
36+
37+
$this->assertInstanceOf(JsonResponse::class, $apiProblem->response());
38+
}
39+
40+
public function testResponseWithFacade(): void
41+
{
42+
$this->assertInstanceOf(JsonResponse::class, ApiProblemFacade::response('Testing', 500));
43+
}
44+
3045
/**
3146
* @dataProvider statusCodes
3247
*/
@@ -255,4 +270,52 @@ public function testInvalidHttpStatusCodesAreCastTo500(int $code): void
255270
$problem = new ApiProblem($code, $e);
256271
$this->assertEquals(500, $problem->status);
257272
}
273+
274+
/**
275+
* @dataProvider statusCodes
276+
* @group api-tools-118
277+
*/
278+
public function testMagicGetInvalidArgument(int $code): void
279+
{
280+
$this->expectException(Exception\InvalidArgumentException::class);
281+
$apiProblem = new ApiProblem($code, 'Testing', null, null, ['more' => 'testing']);
282+
283+
$this->assertEquals('testing', $apiProblem->code);
284+
}
285+
286+
/**
287+
* @dataProvider statusCodes
288+
* @group api-tools-118
289+
*/
290+
public function testMagicGetNormalizedProperties(int $code): void
291+
{
292+
$apiProblem = new ApiProblem($code, 'Testing', 'test', 'title test', ['more' => 'testing']);
293+
294+
$this->assertEquals('test', $apiProblem->__get('type'));
295+
$this->assertEquals($code, $apiProblem->__get('status'));
296+
$this->assertEquals('title test', $apiProblem->__get('title'));
297+
$this->assertEquals('Testing', $apiProblem->__get('detail'));
298+
}
299+
300+
/**
301+
* @dataProvider statusCodes
302+
* @group api-tools-118
303+
*/
304+
public function testMagicGetAdditionalDetails(int $code): void
305+
{
306+
$apiProblem = new ApiProblem($code, 'Testing', 'test', 'title test', ['xxmore' => 'testing']);
307+
308+
$this->assertEquals('testing', $apiProblem->__get('xxmore'));
309+
}
310+
311+
/**
312+
* @dataProvider statusCodes
313+
* @group api-tools-118
314+
*/
315+
public function testMagicGetAdditionalDetailsNormalized(int $code): void
316+
{
317+
$apiProblem = new ApiProblem($code, 'Testing', 'test', 'title test', ['code' => 'testing']);
318+
319+
$this->assertEquals('testing', $apiProblem->__get('code'));
320+
}
258321
}

0 commit comments

Comments
 (0)