Skip to content

Commit c224be1

Browse files
Testing the services definition
1 parent d8dc0f6 commit c224be1

File tree

9 files changed

+158
-13
lines changed

9 files changed

+158
-13
lines changed

.idea/php.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/problem-details-symfony-bundle.iml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"phpmd/phpmd": "^2.5",
1515
"phpstan/phpstan": "^2.0",
1616
"phpunit/phpunit": "^11.5.0",
17-
"squizlabs/php_codesniffer": "^3.7.2"
17+
"squizlabs/php_codesniffer": "^3.7.2",
18+
"symfony/yaml": "^7.2"
1819
},
1920
"license": "MIT",
2021
"autoload": {

composer.lock

+75-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/services.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ services:
2020
tags: ['phauthentic.problem_details.exception_converter']
2121

2222
Phauthentic\Symfony\ProblemDetails\ExceptionConversion\GenericExceptionConverter:
23+
arguments:
24+
$problemDetailsFactory: '@Phauthentic\Symfony\ProblemDetails\ProblemDetailsFactoryInterface'
2325
tags: ['phauthentic.problem_details.exception_converter']
2426

2527
Phauthentic\Symfony\ProblemDetails\ThrowableToProblemDetailsKernelListener:
28+
public: true
2629
arguments:
2730
$exceptionConverters: !tagged_iterator phauthentic.problem_details.exception_converter
2831
tags:

readme.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ This bundle provides support for [RFC 9457](https://www.rfc-editor.org/rfc/rfc94
1212
composer require phauthentic/problem-details-symfony-bundle
1313
```
1414

15-
## Docs
15+
## Documentation
16+
17+
Simply inject the `ProblemDetailsFactoryInterface` into your handlers and use it to create `ProblemDetails` responses.
1618

1719
```php
1820
class ExampleController
@@ -37,6 +39,31 @@ class ExampleController
3739
}
3840
```
3941

42+
### Service Configuration
43+
44+
To add more converters to the kernel listener, you can tag additional services with `phauthentic.problem_details.exception_converter`. They must implement the [ExceptionConverterInterface](src/ExceptionConversion/ExceptionConverterInterface.php).
45+
46+
```yaml
47+
Phauthentic\Symfony\ProblemDetails\ExceptionConversion\ValidationFailedExceptionConverter:
48+
arguments:
49+
$validationErrorsBuilder: '@Phauthentic\Symfony\ProblemDetails\Validation\ValidationErrorsBuilder'
50+
$problemDetailsResponseFactory: '@Phauthentic\Symfony\ProblemDetails\FromExceptionEventFactoryInterface'
51+
tags: ['phauthentic.problem_details.exception_converter']
52+
```
53+
54+
To completely override the list of already configured listeners override
55+
56+
```yaml
57+
Phauthentic\Symfony\ProblemDetails\ThrowableToProblemDetailsKernelListener:
58+
public: true
59+
arguments:
60+
$exceptionConverters: !tagged_iterator phauthentic.problem_details.exception_converter
61+
tags:
62+
- { name: 'kernel.event_listener', event: 'kernel.exception', priority: 0 }
63+
```
64+
65+
in your `services.yaml`.
66+
4067
## Problem Details Example
4168

4269
```text

src/ExceptionConversion/GenericExceptionConverter.php

-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@
1515
* Notice that you might need to adjust the priority of the listener in your services.yaml file to make sure it is
1616
* executed in the right order if you have other listeners.
1717
*
18-
* <code>
19-
* Phauthentic\Symfony\ProblemDetails\ThrowableToProblemDetailsKernelListener:
20-
* arguments: ['%kernel.environment%', { }]
21-
* tags:
22-
* - { name: kernel.event_listener, event: kernel.exception, priority: -20 }
23-
* </code>
24-
*
2518
* @link https://www.rfc-editor.org/rfc/rfc9457.html
2619
*/
2720
class GenericExceptionConverter implements ExceptionConverterInterface

tests/Unit/ExceptionConversion/GenericThrowableConverterTest.php renamed to tests/Unit/ExceptionConversion/GenericExceptionConverterTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
*
2020
*/
21-
class GenericThrowableConverterTest extends TestCase
21+
class GenericExceptionConverterTest extends TestCase
2222
{
2323
public function setUp(): void
2424
{

tests/Unit/ServiceLoadingTest.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phauthentic\Symfony\ProblemDetails\Tests\Unit;
6+
7+
use Phauthentic\Symfony\ProblemDetails\ThrowableToProblemDetailsKernelListener;
8+
use PHPUnit\Framework\TestCase;
9+
use ReflectionClass;
10+
use Symfony\Component\Config\FileLocator;
11+
use Symfony\Component\DependencyInjection\ContainerBuilder;
12+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
13+
use Phauthentic\Symfony\ProblemDetails\ExceptionConversion\ValidationFailedExceptionConverter;
14+
use Phauthentic\Symfony\ProblemDetails\ExceptionConversion\HttpExceptionConverter;
15+
use Phauthentic\Symfony\ProblemDetails\ExceptionConversion\GenericExceptionConverter;
16+
17+
/**
18+
*
19+
*/
20+
class ServiceLoadingTest extends TestCase
21+
{
22+
public function testCreateValidResponse(): void
23+
{
24+
// Arrange
25+
$container = new ContainerBuilder();
26+
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
27+
$loader->load('services.yaml');
28+
$container->compile();
29+
30+
// Assert
31+
$listener = $container->get(ThrowableToProblemDetailsKernelListener::class);
32+
$this->assertInstanceOf(ThrowableToProblemDetailsKernelListener::class, $listener);
33+
34+
$reflectionClass = new ReflectionClass($listener);
35+
$converters = $reflectionClass->getProperty('exceptionConverters')->getValue($listener);
36+
$result = [];
37+
foreach ($converters as $converter) {
38+
$result[] = get_class($converter);
39+
}
40+
$this->assertCount(3, $result);
41+
$this->assertEquals([
42+
ValidationFailedExceptionConverter::class,
43+
HttpExceptionConverter::class,
44+
GenericExceptionConverter::class,
45+
], $result);
46+
}
47+
}

0 commit comments

Comments
 (0)