Skip to content

Commit

Permalink
fix: handle defaultResponseHandlers config flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche committed Jun 1, 2021
1 parent 030db18 commit 0c2cb54
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 47 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ Now you can just create a `App\Responder\MyGoodRuntimeExceptionHandler` as descr
### Default response handlers

The bundle automatically adds some response handlers for basic types with negative priority so that they will be called if none of your response handlers stops propagation earlier.
If you don't want the default handlers to be added, you can set `pitch_adr.defaultResponseHandlers: false` on your container parameters.
If you don't want the default handlers to be added, you can modify this behavior per bundle configuration.
```yaml
pitch_adr:
defaultResponseHandlers: false # defaults to true
```

### Prioritised response handlers

Expand Down
48 changes: 23 additions & 25 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Pitch\AdrBundle\DependencyInjection;

use RuntimeException;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -13,31 +14,28 @@ public function getConfigTreeBuilder()

/** @var ArrayNodeDefinition */
$root = $treeBuilder->getRootNode();

$root->children()
->arrayNode('graceful')
->defaultValue([
['value' => RuntimeException::class]
])
->arrayPrototype()
->beforeNormalization()
->ifString()->then(function ($v) {
return [ 'value' => $v ];
})
->end()
->children()
->scalarNode('value')->end()
->arrayNode('not')
->beforeNormalization()
->ifString()->then(function ($v) {
return [ $v ];
})
->end()
->scalarPrototype()->end()
->end()
->end()
->end()
->end();
$rootChildren = $root->children();

$graceful = $rootChildren->arrayNode('graceful');
$graceful->defaultValue([
['value' => RuntimeException::class]
]);
$gracefulPrototype = $graceful->arrayPrototype();
$gracefulPrototype->beforeNormalization()
->ifString()->then(function ($v) {
return [ 'value' => $v ];
});
$gracefulChildren = $gracefulPrototype->children();
$gracefulChildren->scalarNode('value');
$gracefulNot = $gracefulChildren->arrayNode('not');
$gracefulNot->beforeNormalization()
->ifString()->then(function ($v) {
return [ $v ];
});
$gracefulNot->scalarPrototype();

$rootChildren->booleanNode('defaultResponseHandlers')
->defaultValue(true);

return $treeBuilder;
}
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/PitchAdrExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('adr.php');
$loader->load('debug.php');

if ($config[static::ALIAS . '.defaultResponseHandlers'] ?? true) {
if ($config['defaultResponseHandlers']) {
$loader->load('handler.php');
}

Expand Down
51 changes: 31 additions & 20 deletions test/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,25 @@ public function provideNormalize(): array
{
return [
[
[],
null,
[
'graceful' => [
['value' => RuntimeException::class],
],
['value' => RuntimeException::class],
],
],
[
[
'graceful' => [
RuntimeException::class,
]
RuntimeException::class,
],
[
'graceful' => [
['value' => RuntimeException::class, 'not' => []],
],
['value' => RuntimeException::class, 'not' => []],
],
],
[
[
'graceful' => [
['value' => RuntimeException::class, 'not' => OutOfBoundsException::class],
],
['value' => RuntimeException::class, 'not' => OutOfBoundsException::class],
],
[
'graceful' => [
['value' => RuntimeException::class, 'not' => [OutOfBoundsException::class]],
],
['value' => RuntimeException::class, 'not' => [OutOfBoundsException::class]],
],
],
];
Expand All @@ -48,12 +38,33 @@ public function provideNormalize(): array
/**
* @dataProvider provideNormalize
*/
public function testNormalize(
public function testNormalizeGraceful(
$config,
$processedConfig
$expectedProcessedGraceful
) {
$processor = new Processor();
$processedConfig = $this->processConfig(isset($config) ? [
'graceful' => $config
] : []);

$this->assertEquals($processedConfig, $processor->processConfiguration(new Configuration(), [$config]));
$this->assertArrayHasKey('graceful', $processedConfig);
$this->assertEquals($expectedProcessedGraceful, $processedConfig['graceful']);
}

public function testDefaultResponseHandlers()
{
$this->assertTrue($this->processConfig()['defaultResponseHandlers']);

$this->assertFalse($this->processConfig([
'defaultResponseHandlers' => false,
])['defaultResponseHandlers']);
}

private function processConfig(
array ...$configs
) {
return (new Processor())->processConfiguration(
new Configuration(),
$configs,
);
}
}

0 comments on commit 0c2cb54

Please sign in to comment.