Skip to content

Commit d3d2e43

Browse files
committed
minor #58265 [Webhook] add WebhookController tests (kbond)
This PR was merged into the 7.2 branch. Discussion ---------- [Webhook] add `WebhookController` tests | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | n/a | License | MIT Just adds tests to WebhookController to prepare for possible changes in #58248. Commits ------- 5bcd490 [Webhook] add WebhookController tests
2 parents 4e70834 + 5bcd490 commit d3d2e43

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy