8000 [Webhook] add WebhookController tests · symfony/symfony@5bcd490 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5bcd490

Browse files
committed
[Webhook] add WebhookController tests
1 parent 44395ab commit 5bcd490

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\Webhook\Tests\Controller;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\Messenger\Envelope;
18+
use Symfony\Component\Messenger\MessageBusInterface;
19+
use Symfony\Component\RemoteEvent\Messenger\ConsumeRemoteEventMessage;
20+
use Symfony\Component\RemoteEvent\RemoteEvent;
21+
use Symfony\Component\Webhook\Client\RequestParserInterface;
22+
use Symfony\Component\Webhook\Controller\WebhookController;
23+
24+
class WebhookControllerTest extends TestCase
25+
{
26+
public function testNoParserAvailable()
27+
{
28+
$controller = new WebhookController([], $this->createMock(MessageBusInterface::class));
29+
30+
$response = $controller->handle('foo', new Request());
31+
32+
$this->assertSame(404, $response->getStatusCode());
33+
}
34+
35+
public function testParserRejectsPayload()
36+
{
37+
$secret = '1234';
38+
$request = new Request();
39+
$parser = $this->createMock(RequestParserInterface::class);
40+
$parser
41+
->expects($this->once())
42+
->method('parse')
43+
->with($request, $secret)
44+
->willReturn(null);
45+
$parser
46+
->expects($this->once())
47+
->method('createRejectedResponse')
48+
->with('Unable to parse the webhook payload.', $request)
49+
->willReturn(new Response('Unable to parse the webhook payload.', 406));
50+
51+
$controller = new WebhookController(
52+
['foo' => ['parser' => $parser, 'secret' => $secret]],
53+
$this->createMock(MessageBusInterface::class)
54+
);
55+
56+
$response = $controller->handle('foo', $request);
57+
58+
$this->assertSame(406, $response->getStatusCode());
59+
$this->assertSame('Unable to parse the webhook payload.', $response->getContent());
60+
}
61+
62+
public function testParserAcceptsPayload()
63+
{
64+
$secret = '1234';
65+
$request = new Request();
66+
$event = new RemoteEvent('name', 'id', ['payload']);
67+
$parser = $this->createMock(RequestParserInterface::class);
68+
$parser
69+
->expects($this->once())
70+
->method('parse')
71+
->with($request, $secret)
72+
->willReturn($event);
73+
$parser
74+
->expects($this->once())
75+
->method('createSuccessfulResponse')
76+
->with($request)
77+
->willReturn(new Response('', 202));
78+
$bus = new class implements MessageBusInterface {
79+
public ?object $message = null;
80+
81+
public function dispatch(object $message, array $stamps = []): Envelope
82+
{
83+
return new Envelope($this->message = $message, $stamps);
84+
}
85+
};
86+
87+
$controller = new WebhookController(
88+
['foo' => ['parser' => $parser, 'secret' => $secret]],
89+
$bus,
90+
);
91+
92+
$response = $controller->handle('foo', $request);
93+
94+
$this->assertSame(202, $response->getStatusCode());
95+
$this->assertSame('', $response->getContent());
96+
$this->assertInstanceOf(ConsumeRemoteEventMessage::class, $bus->message);
97+
$this->assertSame('foo', $bus->message->getType());
98+
$this->assertEquals($event, $bus->message->getEvent());
99+
}
100+
}

0 commit comments

Comments
 (0)
0