Skip to content

Commit c52b84c

Browse files
committed
Replace the old exception to status config with the new Symfony's YAML extension for PHP constants
1 parent 290ee61 commit c52b84c

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ private function addExceptionToStatusSection(ArrayNodeDefinition $rootNode)
186186
}
187187

188188
if (defined($httpStatusCodeConstant = sprintf('%s::%s', Response::class, $httpStatusCode))) {
189+
@trigger_error(sprintf('Using a string "%s" as a constant of the "%s" class is deprecated since API Platform 2.1 and will not be possible anymore in API Platform 3. Use the Symfony\'s custom YAML extension for PHP constants instead (i.e. "!php/const:%s").', $httpStatusCode, Response::class, $httpStatusCodeConstant), E_USER_DEPRECATED);
190+
189191
$httpStatusCode = constant($httpStatusCodeConstant);
190192
}
191193
}

tests/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function testExceptionToStatusConfig()
126126
'api_platform' => [
127127
'exception_to_status' => [
128128
\InvalidArgumentException::class => Response::HTTP_BAD_REQUEST,
129-
\RuntimeException::class => 'HTTP_INTERNAL_SERVER_ERROR',
129+
\RuntimeException::class => Response::HTTP_INTERNAL_SERVER_ERROR,
130130
],
131131
],
132132
]);
@@ -138,6 +138,26 @@ public function testExceptionToStatusConfig()
138138
], $config['exception_to_status']);
139139
}
140140

141+
/**
142+
* @group legacy
143+
* @expectedDeprecation Using a string "HTTP_INTERNAL_SERVER_ERROR" as a constant of the "Symfony\Component\HttpFoundation\Response" class is deprecated since API Platform 2.1 and will not be possible anymore in API Platform 3. Use the Symfony's custom YAML extension for PHP constants instead (i.e. "!php/const:Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST").
144+
*/
145+
public function testLegacyExceptionToStatusConfig()
146+
{
147+
$config = $this->processor->processConfiguration($this->configuration, [
148+
'api_platform' => [
149+
'exception_to_status' => [
150+
\RuntimeException::class => 'HTTP_INTERNAL_SERVER_ERROR',
151+
],
152+
],
153+
]);
154+
155+
$this->assertTrue(isset($config['exception_to_status']));
156+
$this->assertSame([
157+
\RuntimeException::class => Response::HTTP_INTERNAL_SERVER_ERROR,
158+
], $config['exception_to_status']);
159+
}
160+
141161
public function invalidHttpStatusCodeProvider()
142162
{
143163
return [

tests/Fixtures/app/config/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ api_platform:
4848
enable_nelmio_api_doc: true
4949
exception_to_status:
5050
Symfony\Component\Serializer\Exception\ExceptionInterface: 400
51-
ApiPlatform\Core\Exception\InvalidArgumentException: 'HTTP_BAD_REQUEST'
51+
ApiPlatform\Core\Exception\InvalidArgumentException: !php/const:Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST
5252
http_cache:
5353
invalidation:
5454
enabled: true

0 commit comments

Comments
 (0)