Skip to content

Commit 98dd4bb

Browse files
committed
Merge branch '3.0'
* 3.0: [DependencyInjection] fix dumped YAML snytax Remove InputOption::VALUE_REQUIRED mode from $default parameter description as InputOption::setDefault() throws an exception only when called in InputOption::VALUE_NONE mode. In practice the $default value could still be accessed in InputOption::VALUE_REQUIRED mode in case InputOption was never set but accessed from InputDefinition::getOption() method [Yaml] always restore the error handler in tests [FrameworkBundle] fix YAML syntax fix YAML syntax in functional tests config [HttpFoundation] [Session] Removed unnecessary PHP version check as minimum requirement is now 5.5.9 [Form] Fixed violation mapping if multiple forms are using the same (or part of the same) property path fix FQCN in tests added by #17694 Fix locale and written standard inconsistencies for Norwegian translations [Form] [Validator] Fix locale inconsistencies in Norwegian translations [TwigBridge] Symfony 3.1 forward compatibility fixed CS [DependencyInjection] fixed exceptions thrown by get method of ContainerBuilder [Yaml] properly parse lists in object maps [FrameworkBundle] Remove unused private method. [Form] remove useless code in ResizeFormListener [Config] Fix EnumNodeDefinition to allow building enum nodes with one element [Form] remove deprecated empty_value_in_choices fix choice_value option in EntityType and add some tests
2 parents 18bde48 + 23dbd56 commit 98dd4bb

File tree

7 files changed

+33
-24
lines changed

7 files changed

+33
-24
lines changed

ContainerBuilder.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1919
use Symfony\Component\DependencyInjection\Exception\LogicException;
2020
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
21+
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
22+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2123
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
2224
use Symfony\Component\Config\Resource\FileResource;
2325
use Symfony\Component\Config\Resource\ResourceInterface;
@@ -398,8 +400,9 @@ public function has($id)
398400
*
399401
* @return object The associated service
400402
*
401-
* @throws InvalidArgumentException when no definitions are available
402-
* @throws LogicException when a circular dependency is detected
403+
* @throws InvalidArgumentException when no definitions are available
404+
* @throws ServiceCircularReferenceException When a circular reference is detected
405+
* @throws ServiceNotFoundException When the service is not defined
403406
* @throws \Exception
404407
*
405408
* @see Reference
@@ -418,7 +421,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
418421

419422
try {
420423
$definition = $this->getDefinition($id);
421-
} catch (InvalidArgumentException $e) {
424+
} catch (ServiceNotFoundException $e) {
422425
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
423426
return;
424427
}
@@ -758,14 +761,14 @@ public function hasDefinition($id)
758761
*
759762
* @return Definition A Definition instance
760763
*
761-
* @throws InvalidArgumentException if the service definition does not exist
764+
* @throws ServiceNotFoundException if the service definition does not exist
762765
*/
763766
public function getDefinition($id)
764767
{
765768
$id = strtolower($id);
766769

767770
if (!array_key_exists($id, $this->definitions)) {
768-
throw new InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id));
771+
throw new ServiceNotFoundException($id);
769772
}
770773

771774
return $this->definitions[$id];
@@ -780,7 +783,7 @@ public function getDefinition($id)
780783
*
781784
* @return Definition A Definition instance
782785
*
783-
* @throws InvalidArgumentException if the service definition does not exist
786+
* @throws ServiceNotFoundException if the service definition does not exist
784787
*/
785788
public function findDefinition($id)
786789
{

Dumper/YamlDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private function addService($id, $definition)
6565
$class = substr($class, 1);
6666
}
6767

68-
$code .= sprintf(" class: %s\n", $class);
68+
$code .= sprintf(" class: %s\n", $this->dumper->dump($class));
6969
}
7070

7171
if (!$definition->isPublic()) {

Tests/ContainerBuilderTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
use Symfony\Component\DependencyInjection\ContainerInterface;
2121
use Symfony\Component\DependencyInjection\Definition;
2222
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
23+
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
24+
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
25+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2326
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
2427
use Symfony\Component\DependencyInjection\Reference;
2528
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
@@ -49,9 +52,9 @@ public function testDefinitions()
4952

5053
try {
5154
$builder->getDefinition('baz');
52-
$this->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
53-
} catch (\InvalidArgumentException $e) {
54-
$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');
5558
}
5659
}
5760

@@ -101,9 +104,9 @@ public function testGet()
101104
$builder = new ContainerBuilder();
102105
try {
103106
$builder->get('foo');
104-
$this->fail('->get() throws an InvalidArgumentException if the service does not exist');
105-
} catch (\InvalidArgumentException $e) {
106-
$this->assertEquals('The service definition "foo" does not exist.', $e->getMessage(), '->get() throws an InvalidArgumentException if the service does not exist');
107+
$this->fail('->get() throws a ServiceNotFoundException if the service does not exist');
108+
} catch (ServiceNotFoundException $e) {
109+
$this->assertEquals('You have requested a non-existent service "foo".', $e->getMessage(), '->get() throws a ServiceNotFoundException if the service does not exist');
107110
}
108111

109112
$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');

Tests/Dumper/YamlDumperTest.php

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

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
16+
use Symfony\Component\Yaml\Yaml;
1617

1718
class YamlDumperTest extends \PHPUnit_Framework_TestCase
1819
{
@@ -27,24 +28,21 @@ public function testDump()
2728
{
2829
$dumper = new YamlDumper($container = new ContainerBuilder());
2930

30-
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services1.yml', $dumper->dump(), '->dump() dumps an empty container as an empty YAML file');
31-
32-
$container = new ContainerBuilder();
33-
$dumper = new YamlDumper($container);
31+
$this->assertEqualYamlStructure(self::$fixturesPath.'/yaml/services1.yml', $dumper->dump(), '->dump() dumps an empty container as an empty YAML file');
3432
}
3533

3634
public function testAddParameters()
3735
{
3836
$container = include self::$fixturesPath.'/containers/container8.php';
3937
$dumper = new YamlDumper($container);
40-
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services8.yml', $dumper->dump(), '->dump() dumps parameters');
38+
$this->assertEqualYamlStructure(self::$fixturesPath.'/yaml/services8.yml', $dumper->dump(), '->dump() dumps parameters');
4139
}
4240

4341
public function testAddService()
4442
{
4543
$container = include self::$fixturesPath.'/containers/container9.php';
4644
$dumper = new YamlDumper($container);
47-
$this->assertEquals(str_replace('%path%', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR, file_get_contents(self::$fixturesPath.'/yaml/services9.yml')), $dumper->dump(), '->dump() dumps services');
45+
$this->assertEqualYamlStructure(str_replace('%path%', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR, file_get_contents(self::$fixturesPath.'/yaml/services9.yml')), $dumper->dump(), '->dump() dumps services');
4846

4947
$dumper = new YamlDumper($container = new ContainerBuilder());
5048
$container->register('foo', 'FooClass')->addArgument(new \stdClass());
@@ -63,4 +61,9 @@ public function testDumpAutowireData()
6361
$dumper = new YamlDumper($container);
6462
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services24.yml', $dumper->dump());
6563
}
64+
65+
private function assertEqualYamlStructure($yaml, $expected, $message = '')
66+
{
67+
$this->assertEquals(Yaml::parse($expected), Yaml::parse($yaml), $message);
68+
}
6669
}

Tests/Fixtures/yaml/services10.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ services:
66
class: BAR
77

88
project:
9-
test: %project.parameter.foo%
9+
test: '%project.parameter.foo%'

Tests/Fixtures/yaml/services6.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ services:
22
foo: { class: FooClass }
33
baz: { class: BazClass }
44
not_shared: { class: FooClass, shared: false }
5-
file: { class: FooClass, file: %path%/foo.php }
5+
file: { class: FooClass, file: '%path%/foo.php' }
66
arguments: { class: FooClass, arguments: [foo, '@foo', [true, false]] }
77
configurator1: { class: FooClass, configurator: sc_configure }
88
configurator2: { class: FooClass, configurator: ['@baz', configure] }

Tests/Fixtures/yaml/services9.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ services:
1818
factory: [Bar\FooClass, getInstance]
1919
configurator: sc_configure
2020
foo.baz:
21-
class: %baz_class%
21+
class: '%baz_class%'
2222
factory: ['%baz_class%', getInstance]
2323
configurator: ['%baz_class%', configureStatic1]
2424
bar:
2525
class: Bar\FooClass
2626
arguments: [foo, '@foo.baz', '%foo_bar%']
2727
configurator: ['@foo.baz', configure]
2828
foo_bar:
29-
class: %foo_class%
29+
class: '%foo_class%'
3030
shared: false
3131
method_call1:
3232
class: Bar\FooClass
33-
file: %path%foo.php
33+
file: '%path%foo.php'
3434
calls:
3535
- [setBar, ['@foo']]
3636
- [setBar, ['@?foo2']]

0 commit comments

Comments
 (0)