Skip to content

Commit

Permalink
Do not ignore the choice groups for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Dec 26, 2018
1 parent 3be0445 commit 9007911
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
Expand Up @@ -62,30 +62,6 @@ public static function generateHash($value, $namespace = '')
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;
Expand Down Expand Up @@ -113,12 +89,7 @@ public function createListFromChoices($choices, $value = null)
// 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);
Expand Down
Expand Up @@ -64,19 +64,24 @@ public function testCreateFromChoicesComparesTraversableChoicesAsArray()
$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));
}

/**
Expand Down

0 comments on commit 9007911

Please sign in to comment.