[DependencyInjection] Fix Container::camelize to convert beginning and ending chars

To convert a service ID exactly like Container::get
This commit is contained in:
Jérôme Tamarelle 2013-07-21 11:38:59 +02:00
parent 4970770d9c
commit 485d53aead
2 changed files with 24 additions and 1 deletions

View File

@ -471,7 +471,7 @@ class Container implements IntrospectableContainerInterface
*/
public static function camelize($id)
{
return preg_replace_callback('/(^|_|\.)+(.)/', function ($match) { return ('.' === $match[1] ? '_' : '').strtoupper($match[2]); }, $id);
return strtr(ucwords(strtr($id, array('_' => ' ', '.' => '_ '))), array(' ' => ''));
}
/**

View File

@ -30,6 +30,29 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '__construct() takes an array of parameters as its first argument');
}
/**
* @dataProvider dataForTestCamelize
*/
public function testCamelize($id, $expected)
{
$this->assertEquals($expected, Container::camelize($id), sprintf('Container::camelize("%s")', $id));
}
public function dataForTestCamelize()
{
return array(
array('foo_bar', 'FooBar'),
array('foo.bar', 'Foo_Bar'),
array('foo.bar_baz', 'Foo_BarBaz'),
array('foo._bar', 'Foo_Bar'),
array('foo_.bar', 'Foo_Bar'),
array('_foo', 'Foo'),
array('.foo', '_Foo'),
array('foo_', 'Foo'),
array('foo.', 'Foo_'),
);
}
/**
* @covers Symfony\Component\DependencyInjection\Container::compile
*/