Skip to content

Commit 7ea3773

Browse files
fix: Updated tests for new exceptions with extra properties.
Signed-off-by: Johannes Tegnér <johannes@jitesoft.com>
1 parent 86e55f5 commit 7ea3773

8 files changed

+87
-10
lines changed

src/Http/Client/HttpUnavailableForLegalReasonsException.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
* due to legal reasons hence can't be served.
1717
*/
1818
class HttpUnavailableForLegalReasonsException extends HttpException {
19-
20-
private ?string $reason;
19+
protected ?string $reason;
2120

2221
/**
2322
* Reason for unavailability.
@@ -47,4 +46,10 @@ public function __construct(
4746
$this->reason = $reason;
4847
}
4948

49+
public function toArray(): array {
50+
$arr = parent::toArray();
51+
$arr['reason'] = $this->getReason();
52+
return $arr;
53+
}
54+
5055
}

src/Http/Server/HttpServiceUnavailableException.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,26 @@ public function getRetryAfter(): ?int {
3333
* HttpServiceUnavailableException constructor.
3434
*
3535
* @param string $message
36-
* @param int $code
3736
* @param ?int $retryAfter
37+
* @param int $code
3838
* @param Throwable|null $previous
3939
*/
4040
public function __construct(
4141
string $message
4242
= 'Service is currently not able to handle the request.',
43-
int $code = 503,
4443
?int $retryAfter = null,
44+
int $code = 503,
4545
?Throwable $previous = null
4646
) {
4747
parent::__construct($message, $code, $previous);
4848

4949
$this->retryAfter = $retryAfter;
5050
}
5151

52+
public function toArray(): array {
53+
$arr = parent::toArray();
54+
$arr['retry-after'] = $this->getRetryAfter();
55+
return $arr;
56+
}
57+
5258
}

src/Validation/ValidationException.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,38 @@
1616
* Mainly used as a base class for more specific validation exception types.
1717
*/
1818
class ValidationException extends JitesoftException {
19+
protected ?array $errors;
20+
21+
/**
22+
* Get list of validation errors.
23+
*
24+
* @return array|null
25+
*/
26+
public function getErrors(): ?array {
27+
return $this->errors;
28+
}
1929

2030
/**
2131
* ValidationException constructor.
2232
*
2333
* @param string $message
34+
* @param null|array $errors
2435
* @param integer $code
2536
* @param null|Throwable $previous
2637
*/
2738
public function __construct(string $message = 'Validation failed.',
39+
?array $errors = null,
2840
int $code = 0,
2941
?Throwable $previous = null) {
3042
parent::__construct($message, $code, $previous);
43+
44+
$this->errors = $errors;
45+
}
46+
47+
public function toArray(): array {
48+
$arr = parent::toArray();
49+
$arr['errors'] = $this->getErrors();
50+
return $arr;
3151
}
3252

3353
}

tests/ExceptionTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ static function(ReflectionParameter $parm) {
3030
);
3131
$message = array_values($message);
3232

33-
/** @var $message ReflectionParameter[] */
3433
self::assertNotEmpty($message);
3534

35+
/** @noinspection PhpUnhandledExceptionInspection */
3636
$message = $message[0]->getDefaultValue();
3737
$count = substr_count($message, '%s');
3838
$args = array_fill(0, $count, '');
@@ -78,7 +78,7 @@ final public function testHasProperties(): void {
7878
try {
7979
$this->throwDefaultException();
8080
} catch (JitesoftException $ex) {
81-
self::assertHasProperties($ex->toArray(), static::getTestProperties());
81+
$this->assertHasProperties($ex->toArray(), static::getTestProperties());
8282
}
8383
}
8484

tests/Http/Client/HttpUnavailableForLegalReasonsExceptionTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,27 @@
1818
*/
1919
class HttpUnavailableForLegalReasonsExceptionTest extends HttpExceptionTest {
2020

21+
protected static function getTestProperties(): array {
22+
return [...parent::getTestProperties(), 'reason'];
23+
}
24+
2125
protected $expectedErrorCode = 451;
2226

2327
protected function throwDefaultException(): void {
2428
throw new HttpUnavailableForLegalReasonsException();
2529
}
2630

2731
public function throwMessageException(string $message): void {
28-
throw new HttpUnavailableForLegalReasonsException($message);
32+
throw new HttpUnavailableForLegalReasonsException($message, 'test reason');
33+
}
34+
35+
public function testGetReason(): void {
36+
try {
37+
$this->throwMessageException('test');
38+
} catch (HttpUnavailableForLegalReasonsException $ex) {
39+
self::assertEquals('test reason', $ex->getReason());
40+
self::assertEquals('test reason', $ex->reason);
41+
}
2942
}
3043

3144
}

tests/Http/Server/HttpServiceUnavailableExceptionTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
* @group HttpServerExceptions
1212
*/
1313
class HttpServiceUnavailableExceptionTest extends HttpExceptionTest {
14+
protected static function getTestProperties(): array {
15+
return [...parent::getTestProperties(), 'retry-after'];
16+
}
1417

1518
protected $expectedErrorCode = 503;
1619

@@ -19,7 +22,16 @@ protected function throwDefaultException(): void {
1922
}
2023

2124
public function throwMessageException(string $message): void {
22-
throw new HttpServiceUnavailableException($message);
25+
throw new HttpServiceUnavailableException($message, 100);
26+
}
27+
28+
public function testRetryAfter(): void {
29+
try {
30+
$this->throwMessageException('Test');
31+
} catch (HttpServiceUnavailableException $ex) {
32+
self::assertEquals(100, $ex->getRetryAfter());
33+
self::assertEquals(100, $ex->retryAfter);
34+
}
2335
}
2436

2537
}

tests/IO/FileExceptionTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,4 @@ public function testGetFileName() {
3737
}
3838
}
3939

40-
4140
}

tests/Validation/ValidationExceptionTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,35 @@
1313
* @group ValidationExceptions
1414
*/
1515
class ValidationExceptionTest extends ExceptionTestCase {
16+
protected static function getTestProperties(): array {
17+
return [...parent::getTestProperties(), 'errors'];
18+
}
1619

1720
protected function throwDefaultException(): void {
1821
throw new ValidationException();
1922
}
2023

2124
public function throwMessageException(string $message): void {
22-
throw new ValidationException($message);
25+
throw new ValidationException($message, [
26+
'something' => 'else',
27+
'and' => 'something'
28+
]);
29+
}
30+
31+
public function testErrors(): void {
32+
try {
33+
$this->throwMessageException('test');
34+
} catch (ValidationException $ex) {
35+
self::assertEquals([
36+
'something' => 'else',
37+
'and' => 'something'
38+
], $ex->getErrors());
39+
self::assertEquals([
40+
'something' => 'else',
41+
'and' => 'something'
42+
], $ex->errors);
43+
}
44+
2345
}
2446

2547
}

0 commit comments

Comments
 (0)