provide a useful message when extension types don't match

This commit is contained in:
Christian Flothmann 2020-04-28 19:42:54 +02:00
parent 1bc3ee798d
commit 88d836643a
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()]),
];