You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bug #17719 [DependencyInjection] fixed exceptions thrown by get method of ContainerBuilder (lukaszmakuch)
This PR was squashed before being merged into the 2.3 branch (closes#17719).
Discussion
----------
[DependencyInjection] fixed exceptions thrown by get method of ContainerBuilder
[DependencyInjection] fixed exceptions thrown by get method of ContainerBuilder
Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets |
| License | MIT
| Doc PR |
The ContainerBuilder class wasn't implementing the ContainerInterface interface as it should according to the Liskov substitution principle.
It caused dependency on implementation instead than on the interface when using an instance of the ContainerBuilder class.
For example this code:
```php
<?php
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ContainerInterface;
/* @var $container ContainerInterface */
try {
$container->get("wrong_service_key");
} catch (ServiceNotFoundException $e) {
//action on a wrong key
}
```
works for correct implementations of the ContainerInterface interface, but the ContainerBuilder class was breaking that by throwing more abstract exceptions.
As the ServiceNotFoundException exceptions inherits from the InvalidArgumentException exception, this change shouldn't break code which catches the InvalidArgumentException exception while fetching values from a ContainerInterface interface implementation.
Commits
-------
aecb0fa [DependencyInjection] fixed exceptions thrown by get method of ContainerBuilder
@@ -50,9 +52,9 @@ public function testDefinitions()
50
52
51
53
try {
52
54
$builder->getDefinition('baz');
53
-
$this->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
54
-
} catch (\InvalidArgumentException$e) {
55
-
$this->assertEquals('The service definition "baz" does not exist.', $e->getMessage(), '->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
55
+
$this->fail('->getDefinition() throws a ServiceNotFoundException if the service definition does not exist');
56
+
} catch (ServiceNotFoundException$e) {
57
+
$this->assertEquals('You have requested a non-existent service "baz".', $e->getMessage(), '->getDefinition() throws a ServiceNotFoundException if the service definition does not exist');
56
58
}
57
59
}
58
60
@@ -79,9 +81,9 @@ public function testGet()
79
81
$builder = newContainerBuilder();
80
82
try {
81
83
$builder->get('foo');
82
-
$this->fail('->get() throws an InvalidArgumentException if the service does not exist');
83
-
} catch (\InvalidArgumentException$e) {
84
-
$this->assertEquals('The service definition "foo" does not exist.', $e->getMessage(), '->get() throws an InvalidArgumentException if the service does not exist');
84
+
$this->fail('->get() throws a ServiceNotFoundException if the service does not exist');
85
+
} catch (ServiceNotFoundException$e) {
86
+
$this->assertEquals('You have requested a non-existent service "foo".', $e->getMessage(), '->get() throws a ServiceNotFoundException if the service does not exist');
85
87
}
86
88
87
89
$this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service does not exist and NULL_ON_INVALID_REFERENCE is passed as a second argument');
0 commit comments