Skip to content

Commit 805b23c

Browse files
committed
test: initial upload
#1
1 parent b1ff13f commit 805b23c

File tree

4 files changed

+259
-0
lines changed

4 files changed

+259
-0
lines changed

tests/ExceptionHandlerTest.php

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
3+
/*
4+
* This file is part of https://github.com/josantonius/php-exception-handler repository.
5+
*
6+
* (c) Josantonius <hello@josantonius.dev>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Josantonius\ErrorHandler\Tests;
13+
14+
use ReflectionClass;
15+
use PHPUnit\Framework\TestCase;
16+
use Josantonius\ExceptionHandler\ExceptionHandler;
17+
use Josantonius\ExceptionHandler\Tests\Resources\Handler;
18+
use Josantonius\ExceptionHandler\Tests\Resources\History;
19+
use Josantonius\ExceptionHandler\Exceptions\WrongMethodNameException;
20+
use Josantonius\ExceptionHandler\Exceptions\NotCallableException;
21+
use Josantonius\ExceptionHandler\Tests\Resources\Exceptions\TestException;
22+
23+
class ExceptionHandlerTest extends TestCase
24+
{
25+
private Handler $handler;
26+
27+
public function setUp(): void
28+
{
29+
parent::setUp();
30+
31+
$this->handler = new Handler();
32+
}
33+
34+
public function testShouldFailIfCallableCallbackIsNotPassed(): void
35+
{
36+
$this->expectException(NotCallableException::class);
37+
38+
new ExceptionHandler(callback: 'foo');
39+
}
40+
41+
public function testShouldFailIfTheMethodNamesNotContainValidDataType(): void
42+
{
43+
$this->expectException(WrongMethodNameException::class);
44+
45+
new ExceptionHandler(
46+
callback: $this->handler->init(...),
47+
runBeforeCallback: [0, 8]
48+
);
49+
}
50+
51+
public function testShouldSetTheHandlerOnlyWithTheCallback(): void
52+
{
53+
$this->assertInstanceOf(
54+
ExceptionHandler::class,
55+
new ExceptionHandler(callback: $this->handler->init(...))
56+
);
57+
}
58+
59+
public function testShouldSetTheHandlerOnlyWithCallsToRunBefore(): void
60+
{
61+
$this->assertInstanceOf(
62+
ExceptionHandler::class,
63+
new ExceptionHandler(
64+
callback: $this->handler->init(...),
65+
runBeforeCallback: ['context']
66+
)
67+
);
68+
}
69+
70+
public function testShouldSetTheHandlerOnlyWithCallsToAfter(): void
71+
{
72+
$this->assertInstanceOf(
73+
ExceptionHandler::class,
74+
new ExceptionHandler(
75+
callback: $this->handler->init(...),
76+
runAfterCallback: ['report', 'render']
77+
)
78+
);
79+
}
80+
81+
public function testShouldCallTheCallbackWhenAnExceptionIsThrow(): void
82+
{
83+
$exceptionHandler = new ExceptionHandler(callback: $this->handler->init(...));
84+
85+
History::clear();
86+
87+
$this->simulateException($exceptionHandler);
88+
89+
$this->assertCount(1, History::get());
90+
91+
$this->assertInstanceOf(Handler::class, History::get(0));
92+
$this->assertInstanceOf(TestException::class, History::get(0)->exception);
93+
94+
$this->assertEquals('init', History::get(0)->methodName);
95+
}
96+
97+
public function testShouldCallTheCallbackBeforeRunMethodsWhenAnExceptionIsThrow(): void
98+
{
99+
$exceptionHandler = new ExceptionHandler(
100+
callback: $this->handler->init(...),
101+
runBeforeCallback: ['context', 'unknown'],
102+
);
103+
104+
History::clear();
105+
106+
$this->simulateException($exceptionHandler);
107+
108+
$this->assertCount(2, History::get());
109+
110+
$this->assertInstanceOf(TestException::class, History::get(0));
111+
$this->assertInstanceOf(Handler::class, History::get(1));
112+
113+
$this->assertEquals('context', History::get(0)->methodName);
114+
}
115+
116+
public function testShouldCallTheCallbackAfterRunMethodsWhenAnExceptionIsThrow(): void
117+
{
118+
$exceptionHandler = new ExceptionHandler(
119+
callback: $this->handler->init(...),
120+
runAfterCallback: ['report', 'render', 'unknown'],
121+
);
122+
123+
History::clear();
124+
125+
$this->simulateException($exceptionHandler);
126+
127+
$this->assertCount(3, History::get());
128+
129+
$this->assertInstanceOf(Handler::class, History::get(0));
130+
$this->assertInstanceOf(TestException::class, History::get(1));
131+
$this->assertInstanceOf(TestException::class, History::get(2));
132+
133+
$this->assertEquals('report', History::get(1)->methodName);
134+
$this->assertEquals('render', History::get(2)->methodName);
135+
}
136+
137+
public function testShouldCallTheCallbackAfterAndBeforeRunMethodsWhenAnExceptionIsThrow(): void
138+
{
139+
$exceptionHandler = new ExceptionHandler(
140+
callback: $this->handler->init(...),
141+
runBeforeCallback: ['context', 'unknown'],
142+
runAfterCallback: ['report', 'render', 'unknown'],
143+
);
144+
145+
History::clear();
146+
147+
$this->simulateException($exceptionHandler);
148+
149+
$this->assertCount(4, History::get());
150+
151+
$this->assertInstanceOf(TestException::class, History::get(0));
152+
$this->assertInstanceOf(Handler::class, History::get(1));
153+
$this->assertInstanceOf(TestException::class, History::get(2));
154+
$this->assertInstanceOf(TestException::class, History::get(3));
155+
156+
$this->assertEquals('context', History::get(0)->methodName);
157+
$this->assertEquals('report', History::get(2)->methodName);
158+
$this->assertEquals('render', History::get(3)->methodName);
159+
}
160+
161+
private function simulateException(ExceptionHandler $object): void
162+
{
163+
$reflection = new ReflectionClass($object);
164+
$reflection = $reflection->getMethod('handler');
165+
$reflection->setAccessible(true);
166+
$reflection->invoke($object, new TestException());
167+
}
168+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of https://github.com/josantonius/php-exception-handler repository.
5+
*
6+
* (c) Josantonius <hello@josantonius.dev>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Josantonius\ExceptionHandler\Tests\Resources\Exceptions;
13+
14+
use Josantonius\ExceptionHandler\Tests\Resources\History;
15+
16+
class TestException extends \Exception
17+
{
18+
public function context(): void
19+
{
20+
History::set(new self(), 'context');
21+
}
22+
23+
public function render(): void
24+
{
25+
History::set(new self(), 'render');
26+
}
27+
28+
public function report(): void
29+
{
30+
History::set(new self(), 'report');
31+
}
32+
}

tests/Resources/Handler.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of https://github.com/josantonius/php-exception-handler repository.
5+
*
6+
* (c) Josantonius <hello@josantonius.dev>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Josantonius\ExceptionHandler\Tests\Resources;
13+
14+
use Throwable;
15+
16+
class Handler
17+
{
18+
public function init(Throwable $exception): void
19+
{
20+
History::set(new self(), 'init', $exception);
21+
}
22+
}

tests/Resources/History.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of https://github.com/josantonius/php-exception-handler repository.
5+
*
6+
* (c) Josantonius <hello@josantonius.dev>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Josantonius\ExceptionHandler\Tests\Resources;
13+
14+
use Exception;
15+
16+
class History
17+
{
18+
private static array $history = [];
19+
20+
public static function set(object $object, string $methodName, ?Exception $exception = null)
21+
{
22+
$object->exception = $exception;
23+
$object->methodName = $methodName;
24+
25+
self::$history[] = $object;
26+
}
27+
28+
public static function get(?int $key = null): mixed
29+
{
30+
return $key !== null ? (self::$history[$key] ?? null) : self::$history;
31+
}
32+
33+
public static function clear(): void
34+
{
35+
self::$history = [];
36+
}
37+
}

0 commit comments

Comments
 (0)