Skip to content

Commit 9e053f6

Browse files
Jean-Berunicolas-grekas
authored andcommitted
[FrameworkBundle][Webhook] Throw when required services are missing when using the Webhook component
1 parent 7c0b57b commit 9e053f6

File tree

8 files changed

+115
-3
lines changed

8 files changed

+115
-3
lines changed

DependencyInjection/FrameworkExtension.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ public function load(array $configs, ContainerBuilder $container)
281281

282282
// If the slugger is used but the String component is not available, we should throw an error
283283
if (!ContainerBuilder::willBeAvailable('symfony/string', SluggerInterface::class, ['symfony/framework-bundle'])) {
284-
$container->register('slugger', 'stdClass')
284+
$container->register('slugger', SluggerInterface::class)
285285
->addError('You cannot use the "slugger" service since the String component is not installed. Try running "composer require symfony/string".');
286286
} else {
287287
if (!ContainerBuilder::willBeAvailable('symfony/translation', LocaleAwareInterface::class, ['symfony/framework-bundle'])) {
288-
$container->register('slugger', 'stdClass')
288+
$container->register('slugger', SluggerInterface::class)
289289
->addError('You cannot use the "slugger" service since the Translation contracts are not installed. Try running "composer require symfony/translation".');
290290
}
291291

@@ -379,7 +379,7 @@ public function load(array $configs, ContainerBuilder $container)
379379
$container->getDefinition('argument_resolver.request_payload')
380380
->setArguments([])
381381
->addError('You can neither use "#[MapRequestPayload]" nor "#[MapQueryString]" since the Serializer component is not '
382-
.(class_exists(Serializer::class) ? 'enabled. Try setting "framework.serializer" to true.' : 'installed. Try running "composer require symfony/serializer-pack".')
382+
.(class_exists(Serializer::class) ? 'enabled. Try setting "framework.serializer.enabled" to true.' : 'installed. Try running "composer require symfony/serializer-pack".')
383383
)
384384
->addTag('container.error')
385385
->clearTag('kernel.event_subscriber');
@@ -531,6 +531,24 @@ public function load(array $configs, ContainerBuilder $container)
531531

532532
if ($this->readConfigEnabled('webhook', $container, $config['webhook'])) {
533533
$this->registerWebhookConfiguration($config['webhook'], $container, $loader);
534+
535+
// If Webhook is installed but the HttpClient or Serializer components are not available, we should throw an error
536+
if (!$this->readConfigEnabled('http_client', $container, $config['http_client'])) {
537+
$container->getDefinition('webhook.transport')
538+
->setArguments([])
539+
->addError('You cannot use the "webhook transport" service since the HttpClient component is not '
540+
.(class_exists(ScopingHttpClient::class) ? 'enabled. Try setting "framework.http_client.enabled" to true.' : 'installed. Try running "composer require symfony/http-client".')
541+
)
542+
->addTag('container.error');
543+
}
544+
if (!$this->readConfigEnabled('serializer', $container, $config['serializer'])) {
545+
$container->getDefinition('webhook.body_configurator.json')
546+
->setArguments([])
547+
->addError('You cannot use the "webhook transport" service since the Serializer component is not '
548+
.(class_exists(Serializer::class) ? 'enabled. Try setting "framework.serializer.enabled" to true.' : 'installed. Try running "composer require symfony/serializer-pack".')
549+
)
550+
->addTag('container.error');
551+
}
534552
}
535553

536554
if ($this->readConfigEnabled('remote-event', $container, $config['remote-event'])) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'http_method_override' => false,
5+
'webhook' => ['enabled' => true],
6+
'http_client' => ['enabled' => true],
7+
'serializer' => ['enabled' => true],
8+
]);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'http_method_override' => false,
5+
'webhook' => ['enabled' => true],
6+
'http_client' => ['enabled' => true],
7+
'serializer' => ['enabled' => false],
8+
]);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config http-method-override="false">
10+
<framework:webhook enabled="true" />
11+
<framework:http-client enabled="true" />
12+
<framework:serializer enabled="true" />
13+
</framework:config>
14+
</container>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config http-method-override="false">
10+
<framework:webhook enabled="true" />
11+
<framework:http-client enabled="true" />
12+
<framework:serializer enabled="false" />
13+
</framework:config>
14+
</container>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
framework:
2+
http_method_override: false
3+
webhook:
4+
enabled: true
5+
http_client:
6+
enabled: true
7+
serializer:
8+
enabled: true
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
framework:
2+
http_method_override: false
3+
webhook:
4+
enabled: true
5+
http_client:
6+
enabled: true
7+
serializer:
8+
enabled: false

Tests/DependencyInjection/FrameworkExtensionTestCase.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
8383
use Symfony\Component\Validator\Validation;
8484
use Symfony\Component\Validator\Validator\ValidatorInterface;
85+
use Symfony\Component\Webhook\Client\RequestParser;
86+
use Symfony\Component\Webhook\Controller\WebhookController;
8587
use Symfony\Component\Workflow;
8688
use Symfony\Component\Workflow\Exception\InvalidDefinitionException;
8789
use Symfony\Component\Workflow\Metadata\InMemoryMetadataStore;
@@ -2276,6 +2278,38 @@ public function testNotifierWithSpecificMessageBus()
22762278
$this->assertEquals(new Reference('app.another_bus'), $container->getDefinition('notifier.channel.sms')->getArgument(1));
22772279
}
22782280

2281+
public function testWebhook()
2282+
{
2283+
if (!class_exists(WebhookController::class)) {
2284+
$this->markTestSkipped('Webhook not available.');
2285+
}
2286+
2287+
$container = $this->createContainerFromFile('webhook');
2288+
2289+
$this->assertTrue($container->hasAlias(RequestParser::class));
2290+
$this->assertSame('webhook.request_parser', (string) $container->getAlias(RequestParser::class));
2291+
$this->assertSame(RequestParser::class, $container->getDefinition('webhook.request_parser')->getClass());
2292+
2293+
$this->assertFalse($container->getDefinition('webhook.transport')->hasErrors());
2294+
$this->assertFalse($container->getDefinition('webhook.body_configurator.json')->hasErrors());
2295+
}
2296+
2297+
public function testWebhookWithoutSerializer()
2298+
{
2299+
if (!class_exists(WebhookController::class)) {
2300+
$this->markTestSkipped('Webhook not available.');
2301+
}
2302+
2303+
$container = $this->createContainerFromFile('webhook_without_serializer');
2304+
2305+
$this->assertFalse($container->getDefinition('webhook.transport')->hasErrors());
2306+
$this->assertTrue($container->getDefinition('webhook.body_configurator.json')->hasErrors());
2307+
$this->assertSame(
2308+
['You cannot use the "webhook transport" service since the Serializer component is not enabled. Try setting "framework.serializer.enabled" to true.'],
2309+
$container->getDefinition('webhook.body_configurator.json')->getErrors()
2310+
);
2311+
}
2312+
22792313
protected function createContainer(array $data = [])
22802314
{
22812315
return new ContainerBuilder(new EnvPlaceholderParameterBag(array_merge([

0 commit comments

Comments
 (0)