Do not ignore the choice groups for caching

This commit is contained in:
Valentin 2018-12-27 02:35:06 +03:00
parent 3be0445596
commit 9007911a85
2 changed files with 12 additions and 36 deletions

View File

@ -62,30 +62,6 @@ class CachingFactoryDecorator implements ChoiceListFactoryInterface
return hash('sha256', $namespace.':'.serialize($value));
}
/**
* Flattens an array into the given output variable.
*
* @param array $array The array to flatten
* @param array $output The flattened output
*
* @internal
*/
private static function flatten(array $array, &$output)
{
if (null === $output) {
$output = array();
}
foreach ($array as $key => $value) {
if (\is_array($value)) {
self::flatten($value, $output);
continue;
}
$output[$key] = $value;
}
}
public function __construct(ChoiceListFactoryInterface $decoratedFactory)
{
$this->decoratedFactory = $decoratedFactory;
@ -113,12 +89,7 @@ class CachingFactoryDecorator implements ChoiceListFactoryInterface
// The value is not validated on purpose. The decorated factory may
// decide which values to accept and which not.
// We ignore the choice groups for caching. If two choice lists are
// requested with the same choices, but a different grouping, the same
// choice list is returned.
self::flatten($choices, $flatChoices);
$hash = self::generateHash(array($flatChoices, $value), 'fromChoices');
$hash = self::generateHash(array($choices, $value), 'fromChoices');
if (!isset($this->lists[$hash])) {
$this->lists[$hash] = $this->decoratedFactory->createListFromChoices($choices, $value);

View File

@ -64,19 +64,24 @@ class CachingFactoryDecoratorTest extends TestCase
$this->assertSame($list, $this->factory->createListFromChoices($choices2));
}
public function testCreateFromChoicesFlattensChoices()
public function testCreateFromChoicesGroupedChoices()
{
$choices1 = array('key' => array('A' => 'a'));
$choices2 = array('A' => 'a');
$list = new \stdClass();
$list1 = new \stdClass();
$list2 = new \stdClass();
$this->decoratedFactory->expects($this->once())
$this->decoratedFactory->expects($this->at(0))
->method('createListFromChoices')
->with($choices1)
->will($this->returnValue($list));
->will($this->returnValue($list1));
$this->decoratedFactory->expects($this->at(1))
->method('createListFromChoices')
->with($choices2)
->will($this->returnValue($list2));
$this->assertSame($list, $this->factory->createListFromChoices($choices1));
$this->assertSame($list, $this->factory->createListFromChoices($choices2));
$this->assertSame($list1, $this->factory->createListFromChoices($choices1));
$this->assertSame($list2, $this->factory->createListFromChoices($choices2));
}
/**