Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @param array $collection
- * @param array $lists
- * @param array $mappers
- * @param boolean $allowDuplications
- */
- public function assertContainsCombinations($collection, array $lists, array $mappers, $allowOnlyMentioned = TRUE, $allowDuplications = FALSE)
- {
- $mappers = array_map('callback', $mappers);
- $this->assertSame(count($lists), count($mappers), "Count of given lists equals to count of given mappers");
- $valueCounts = array();
- foreach ($collection as $item) {
- $keys = array();
- foreach ($mappers as $i => $mapper) {
- $keys[$i] = $mapper($item);
- }
- $value = Nette\Utils\Arrays::getRef($valueCounts, $keys);
- $value = ($value ?: 0) + 1;
- }
- if ($allowDuplications === FALSE) {
- $counts = array_values(array_unique(Kdyby\Tools\Arrays::flatMap($valueCounts)));
- $this->assertSame(array(1), $counts, "Collection contains duplications");
- }
- $foundLists = array_fill(0, count($lists), array());
- $inList = Kdyby\Tools\Arrays::flatMapAssoc($valueCounts, function ($value, $keys) use ($lists, &$foundLists) {
- $return = TRUE;
- foreach ($keys as $i => $key) {
- $foundLists[$i][] = $key;
- if (!in_array($key, $lists[$i])) {
- $return = FALSE;
- }
- }
- return $return;
- });
- if ($allowOnlyMentioned === TRUE) {
- $notMentioned = array_filter($inList, function ($isIn) { return $isIn === FALSE; });
- $this->assertEmpty($notMentioned, "The collection contains combinations, that cannot be assembled from given lists.");
- }
- foreach ($foundLists as $i => $list) {
- $this->assertEmpty(array_diff($lists[$i], $list), "There are all values '" . implode("', '", $lists[$i]) . "' in collection");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment