-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathEventSubControllerTest.php
99 lines (80 loc) · 3.16 KB
/
EventSubControllerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace romanzipp\Twitch\Tests;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Event;
use Ramsey\Uuid\Uuid;
use romanzipp\Twitch\Enums\EventSubType;
use romanzipp\Twitch\Events\EventSubHandled;
use romanzipp\Twitch\Events\EventSubReceived;
use romanzipp\Twitch\Http\Controllers\EventSubController;
use romanzipp\Twitch\Tests\TestCases\TestCase;
class EventSubControllerTest extends TestCase
{
public function testProperMethodAreCalledOnEventSubEvent(): void
{
$request = $this->request(EventSubType::STREAM_ONLINE);
Event::fake([
EventSubHandled::class,
EventSubReceived::class,
]);
$response = (new EventSubControllerTestStub())->handleWebhook($request);
Event::assertDispatched(EventSubReceived::class, function (EventSubReceived $event) use ($request) {
return $request->getContent() === json_encode($event->payload)
&& '1337' === $event->payload['event']['user_id'];
});
Event::assertDispatched(EventSubHandled::class, function (EventSubHandled $event) use ($request) {
return $request->getContent() === json_encode($event->payload)
&& '1337' === $event->payload['event']['user_id'];
});
self::assertEquals('Webhook Handled', $response->getContent());
}
public function testNormalResponseIsReturnedIfMethodIsMissing(): void
{
$request = $this->request(EventSubType::STREAM_OFFLINE);
Event::fake([
EventSubHandled::class,
EventSubReceived::class,
]);
$response = (new EventSubControllerTestStub())->handleWebhook($request);
Event::assertDispatched(EventSubReceived::class, function (EventSubReceived $event) use ($request) {
return $request->getContent() === json_encode($event->payload)
&& '1337' === $event->payload['event']['user_id'];
});
Event::assertNotDispatched(EventSubHandled::class);
self::assertEquals(200, $response->getStatusCode());
}
private function request(string $event): Request
{
$request = Request::create(
'/',
'POST',
[],
[],
[],
[],
json_encode([
'subscription' => ['type' => $event],
'event' => ['user_id' => '1337'],
])
);
$request->headers->set('twitch-eventsub-message-type', 'notification');
$request->headers->set('twitch-eventsub-message-id', Uuid::uuid4());
$request->headers->set('twitch-eventsub-message-retry', 0);
$request->headers->set('twitch-eventsub-message-timestamp', Carbon::now()->toIso8601ZuluString());
return $request;
}
}
class EventSubControllerTestStub extends EventSubController
{
/** @noinspection PhpMissingParentConstructorInspection */
public function __construct()
{
// Don't call parent constructor to prevent setting middleware...
}
public function handleStreamOnlineNotification(array $payload): Response
{
return new Response('Webhook Handled', 200);
}
}