Skip to content

Commit 7bc2dd3

Browse files
committed
Add test to make sure all event handler exist
1 parent e723e5d commit 7bc2dd3

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

src/Sentry/Laravel/EventHandler.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,14 @@ public function subscribeQueueEvents()
150150
*/
151151
public function __call($method, $arguments)
152152
{
153-
if (!method_exists($this, $method . 'handler')) {
154-
throw new RuntimeException('Missing event handler:' . $method . 'handler');
153+
$handlerMethod = $handlerMethod = "{$method}Handler";
154+
155+
if (!method_exists($this, $handlerMethod)) {
156+
throw new RuntimeException("Missing event handler: {$handlerMethod}");
155157
}
156158

157159
try {
158-
call_user_func_array([$this, $method . 'handler'], $arguments);
160+
call_user_func_array([$this, $handlerMethod], $arguments);
159161
} catch (Exception $exception) {
160162
// Ignore
161163
}

test/Sentry/EventHandlerTest.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,62 @@
22

33
namespace Sentry\Laravel\Tests;
44

5-
class EventHandlerTest extends \Orchestra\Testbench\TestCase
5+
use ReflectionClass;
6+
use Sentry\Laravel\EventHandler;
7+
use Orchestra\Testbench\TestCase;
8+
9+
class EventHandlerTest extends TestCase
610
{
711
/**
812
* @expectedException \RuntimeException
913
*/
1014
public function test_missing_event_handler_throws_exception()
1115
{
12-
$handler = new \Sentry\Laravel\EventHandler($this->app->events, []);
16+
$handler = new EventHandler($this->app->events, []);
1317

1418
$handler->thisIsNotAHandlerAndShouldThrowAnException();
1519
}
20+
21+
public function test_all_mapped_event_handlers_exist()
22+
{
23+
$this->tryAllEventHandlerMethods(
24+
$this->getStaticPropertyValueFromClass(EventHandler::class, 'eventHandlerMap')
25+
);
26+
}
27+
28+
public function test_all_mapped_auth_event_handlers_exist()
29+
{
30+
$this->tryAllEventHandlerMethods(
31+
$this->getStaticPropertyValueFromClass(EventHandler::class, 'authEventHandlerMap')
32+
);
33+
}
34+
35+
public function test_all_mapped_queue_event_handlers_exist()
36+
{
37+
$this->tryAllEventHandlerMethods(
38+
$this->getStaticPropertyValueFromClass(EventHandler::class, 'queueEventHandlerMap')
39+
);
40+
}
41+
42+
private function tryAllEventHandlerMethods(array $methods): void
43+
{
44+
$handler = new EventHandler($this->app->events, []);
45+
46+
$methods = array_map(static function ($method) {
47+
return "{$method}Handler";
48+
}, array_unique(array_values($methods)));
49+
50+
foreach ($methods as $handlerMethod) {
51+
$this->assertTrue(method_exists($handler, $handlerMethod));
52+
}
53+
}
54+
55+
private function getStaticPropertyValueFromClass($className, $attributeName)
56+
{
57+
$class = new ReflectionClass($className);
58+
59+
$attributes = $class->getStaticProperties();
60+
61+
return $attributes[$attributeName];
62+
}
1663
}

0 commit comments

Comments
 (0)