Skip to content

Commit 6127f1b

Browse files
author
Samuel
committed
Added tests to abstract handler class and fixed errors on validation
1 parent cfd0e01 commit 6127f1b

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/Handlers/AbstractHandler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ public function handleException()
110110
*/
111111
public function validatedHandledException($errors)
112112
{
113-
if (is_array($errors) || get_class($errors) === Collection::class) {
113+
if (is_array($errors) ||
114+
(is_object($errors) && get_class($errors) === Collection::class)
115+
) {
114116
$errors = new ErrorCollection($errors);
115117
} elseif ($errors instanceof Error) {
116118
$errors = (new ErrorCollection)->push($errors)->setStatusCode($errors->getStatus());

tests/Unit/AbstractHandlerTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace SMartins\Exceptions\Tests\Unit;
44

5+
use InvalidArgumentException;
6+
use SMartins\Exceptions\JsonApi\Error;
57
use SMartins\Exceptions\Tests\TestCase;
68
use SMartins\Exceptions\Handlers\Handler;
9+
use SMartins\Exceptions\JsonApi\ErrorCollection;
710

811
class AbstractHandlerTest extends TestCase
912
{
@@ -13,4 +16,39 @@ public function testShouldReturnsTheHandlerClassOnGetExceptionHandler()
1316

1417
$this->assertInstanceOf(Handler::class, $handler->getExceptionHandler());
1518
}
19+
20+
public function testGetDefaultHandler()
21+
{
22+
$handler = new Handler(new \Exception);
23+
24+
$this->assertInstanceOf(Handler::class, $handler->defaultHandler());
25+
}
26+
27+
public function testValidateHandledExceptionWithArrayOfErrors()
28+
{
29+
$errors = [new Error];
30+
31+
$handler = new Handler(new \Exception);
32+
$validated = $handler->validatedHandledException($errors);
33+
34+
$this->assertInstanceOf(ErrorCollection::class, $validated);
35+
}
36+
37+
public function testValidateHandledExceptionWithCollectionOfErrors()
38+
{
39+
$errors = collect([new Error]);
40+
41+
$handler = new Handler(new \Exception);
42+
$validated = $handler->validatedHandledException($errors);
43+
44+
$this->assertInstanceOf(ErrorCollection::class, $validated);
45+
}
46+
47+
public function testValidateHandledExceptionWithInvalidArgument()
48+
{
49+
$this->expectException(InvalidArgumentException::class);
50+
51+
$handler = new Handler(new \Exception);
52+
$handler->validatedHandledException('invalid');
53+
}
1654
}

0 commit comments

Comments
 (0)