Skip to content

Commit

Permalink
merged branch GromNaN/dic-camelize (PR #8536)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.2 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #7431
| License       | MIT
| Doc PR        | n/a

I'm using strtr to make the conversion in order to ensure that the behavior is the same as `Container::get`.

From the test cases I've added, the following were not passing:

Commits
-------

485d53a [DependencyInjection] Fix Container::camelize to convert beginning and ending chars
  • Loading branch information
fabpot committed Jul 21, 2013
2 parents 4970770 + 485d53a commit d8b42aa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Component/DependencyInjection/Container.php
Expand Up @@ -471,7 +471,7 @@ public function isScopeActive($name)
*/
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(' ' => ''));
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Expand Up @@ -30,6 +30,29 @@ public function testConstructor()
$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
*/
Expand Down

0 comments on commit d8b42aa

Please sign in to comment.