Skip to content

Commit 44a3130

Browse files
committed
feature #18654 [Bridge/Doctrine] Use better exception in the register mapping pass (dantleech)
This PR was merged into the 3.1-dev branch. Discussion ---------- [Bridge/Doctrine] Use better exception in the register mapping pass | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no/yes ? | Deprecations? | no | Tests pass? | ? | Fixed tickets | | License | MIT | Doc PR | Current when this mapping pass cannot find (any of) the configured managers the user receives the following exception: ``` [Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException] You have requested a non-existent parameter "Could not determine the Doctrine manager. Either Doctrine is not configured or a bundle is misconfigured.". ``` Which is misleading and strange. This PR changes the exception to an `InvalidArgumentException` and provides the following message: ``` [InvalidArgumentException] Could not find the object manager name parameter. Tried: "cmf_routing.dynamic.persistence.orm.manager_name", "doctrine.default_entity_manager" ``` Commits ------- 0eeedee Use better exception in the register mapping pass
2 parents aa4abe0 + 53527b6 commit 44a3130

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

DependencyInjection/CompilerPass/RegisterMappingsPass.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass;
1313

14-
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
1514
use Symfony\Component\DependencyInjection\ContainerBuilder;
1615
use Symfony\Component\DependencyInjection\Definition;
1716
use Symfony\Component\DependencyInjection\Reference;
@@ -175,8 +174,8 @@ public function process(ContainerBuilder $container)
175174
*
176175
* @return string The name of the chain driver service
177176
*
178-
* @throws ParameterNotFoundException if non of the managerParameters has a
179-
* non-empty value.
177+
* @throws InvalidArgumentException if non of the managerParameters has a
178+
* non-empty value.
180179
*/
181180
protected function getChainDriverServiceName(ContainerBuilder $container)
182181
{
@@ -203,8 +202,8 @@ protected function getDriver(ContainerBuilder $container)
203202
*
204203
* @return string a service definition name
205204
*
206-
* @throws ParameterNotFoundException if none of the managerParameters has a
207-
* non-empty value.
205+
* @throws InvalidArgumentException if none of the managerParameters has a
206+
* non-empty value.
208207
*/
209208
private function getConfigurationServiceName(ContainerBuilder $container)
210209
{
@@ -221,7 +220,7 @@ private function getConfigurationServiceName(ContainerBuilder $container)
221220
*
222221
* @return string The name of the active manager.
223222
*
224-
* @throws ParameterNotFoundException If none of the managerParameters is found in the container.
223+
* @throws InvalidArgumentException If none of the managerParameters is found in the container.
225224
*/
226225
private function getManagerName(ContainerBuilder $container)
227226
{
@@ -234,7 +233,10 @@ private function getManagerName(ContainerBuilder $container)
234233
}
235234
}
236235

237-
throw new ParameterNotFoundException('Could not determine the Doctrine manager. Either Doctrine is not configured or a bundle is misconfigured.');
236+
throw new \InvalidArgumentException(sprintf(
237+
'Could not find the manager name parameter in the container. Tried the following parameter names: "%s"',
238+
implode('", "', $this->managerParameters)
239+
));
238240
}
239241

240242
/**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Symfony\Bridge\Doctrine\Tests\DependencyInjection\CompilerPass;
4+
5+
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterMappingsPass;
6+
use Symfony\Component\DependencyInjection\Definition;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
9+
class RegisterMappingsPassTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @expectedException \InvalidArgumentException
13+
* @expectedExceptionMessageould Could not find the manager name parameter in the container. Tried the following parameter names: "manager.param.one", "manager.param.two"
14+
*/
15+
public function testNoDriverParmeterException()
16+
{
17+
$container = $this->createBuilder([
18+
19+
]);
20+
$this->process($container, [
21+
'manager.param.one',
22+
'manager.param.two',
23+
]);
24+
}
25+
26+
private function process(ContainerBuilder $container, array $managerParamNames)
27+
{
28+
$pass = new ConcreteMappingsPass(
29+
new Definition('\stdClass'),
30+
[],
31+
$managerParamNames,
32+
'some.%s.metadata_driver'
33+
);
34+
35+
$pass->process($container);
36+
}
37+
38+
private function createBuilder()
39+
{
40+
$container = new ContainerBuilder();
41+
42+
return $container;
43+
}
44+
}
45+
46+
class ConcreteMappingsPass extends RegisterMappingsPass
47+
{
48+
}

0 commit comments

Comments
 (0)