bug #27927 [HttpFoundation] Suppress side effects in 'get' and 'has' 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
-------
5f59ad4600
suppress side effects in 'get' or 'has' methods of NamespacedAttributeBag
This commit is contained in:
commit
e2c0239414
@ -124,7 +124,13 @@ class NamespacedAttributeBag extends AttributeBag
|
|||||||
|
|
||||||
foreach ($parts as $part) {
|
foreach ($parts as $part) {
|
||||||
if (null !== $array && !array_key_exists($part, $array)) {
|
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];
|
$array = &$array[$part];
|
||||||
|
@ -82,6 +82,17 @@ class NamespacedAttributeBagTest extends TestCase
|
|||||||
$this->assertEquals($exists, $this->bag->has($key));
|
$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
|
* @dataProvider attributesProvider
|
||||||
*/
|
*/
|
||||||
@ -96,6 +107,17 @@ class NamespacedAttributeBagTest extends TestCase
|
|||||||
$this->assertEquals('default', $this->bag->get('user2.login', 'default'));
|
$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
|
* @dataProvider attributesProvider
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user