Skip to content

Commit

Permalink
bug #27927 [HttpFoundation] Suppress side effects in 'get' and 'has' …
Browse files Browse the repository at this point in the history
…methods of NamespacedAttributeBag (webnet-fr)

This PR was submitted for the 3.4 branch but it was merged into the 2.8 branch instead (closes #27927).

Discussion
----------

[HttpFoundation] Suppress side effects in 'get' and 'has' methods of NamespacedAttributeBag

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #27912
| License       | MIT
| Doc PR        | -

As @Gemineye reported there was a bug in `get` and `has` methods of NamespacedAttributeBag. These methods accept composite names as an argument (like 'foo/bar' or 'foo/bar/baz') to reach the elements of stored arrays. Up to now these methods erroneously created entries (`->get('foo/bar')` created `['foo' => null]`, `->get('foo/bar/baz')` created `['foo' => ['bar' => null]]`).

Commits
-------

5f59ad4 suppress side effects in 'get' or 'has' methods of NamespacedAttributeBag
  • Loading branch information
fabpot committed Jul 13, 2018
2 parents 3b90bc7 + 5f59ad4 commit e2c0239
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ protected function &resolveAttributePath($name, $writeContext = false)

foreach ($parts as $part) {
if (null !== $array && !array_key_exists($part, $array)) {
$array[$part] = $writeContext ? array() : null;
if (!$writeContext) {
$null = null;

return $null;
}

$array[$part] = array();
}

$array = &$array[$part];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ public function testHas($key, $value, $exists)
$this->assertEquals($exists, $this->bag->has($key));
}

/**
* @dataProvider attributesProvider
*/
public function testHasNoSideEffect($key, $value, $expected)
{
$expected = json_encode($this->bag->all());
$this->bag->has($key);

$this->assertEquals($expected, json_encode($this->bag->all()));
}

/**
* @dataProvider attributesProvider
*/
Expand All @@ -96,6 +107,17 @@ public function testGetDefaults()
$this->assertEquals('default', $this->bag->get('user2.login', 'default'));
}

/**
* @dataProvider attributesProvider
*/
public function testGetNoSideEffect($key, $value, $expected)
{
$expected = json_encode($this->bag->all());
$this->bag->get($key);

$this->assertEquals($expected, json_encode($this->bag->all()));
}

/**
* @dataProvider attributesProvider
*/
Expand Down

0 comments on commit e2c0239

Please sign in to comment.