minor #36613 [Form] provide a useful message when extension types don't match (xabbuh)

This PR was merged into the 4.4 branch.

Discussion
----------

[Form] provide a useful message when extension types don't match

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36610
| License       | MIT
| Doc PR        |

Commits
-------

88d836643a provide a useful message when extension types don't match
This commit is contained in:
Christian Flothmann 2020-05-02 08:23:09 +02:00
commit a804333b25
2 changed files with 4 additions and 2 deletions

View File

@ -53,7 +53,7 @@ class DependencyInjectionExtension implements FormExtensionInterface
$extensions = [];
if (isset($this->typeExtensionServices[$name])) {
foreach ($this->typeExtensionServices[$name] as $serviceId => $extension) {
foreach ($this->typeExtensionServices[$name] as $extension) {
$extensions[] = $extension;
if (method_exists($extension, 'getExtendedTypes')) {
@ -68,7 +68,7 @@ class DependencyInjectionExtension implements FormExtensionInterface
// validate the result of getExtendedTypes()/getExtendedType() to ensure it is consistent with the service definition
if (!\in_array($name, $extendedTypes, true)) {
throw new InvalidArgumentException(sprintf('The extended type specified for the service "%s" does not match the actual extended type. Expected "%s", given "%s".', $serviceId, $name, implode(', ', $extendedTypes)));
throw new InvalidArgumentException(sprintf('The extended type "%s" specified for the type extension class "%s" does not match any of the actual extended types (["%s"]).', $name, \get_class($extension), implode('", "', $extendedTypes)));
}
}
}

View File

@ -44,6 +44,8 @@ class DependencyInjectionExtensionTest extends TestCase
public function testThrowExceptionForInvalidExtendedType()
{
$this->expectException('Symfony\Component\Form\Exception\InvalidArgumentException');
$this->expectExceptionMessage(sprintf('The extended type "unmatched" specified for the type extension class "%s" does not match any of the actual extended types (["test"]).', TestTypeExtension::class));
$extensions = [
'unmatched' => new \ArrayIterator([new TestTypeExtension()]),
];