‪TYPO3CMS  10.4
Permutation.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 
23 {
27  private ‪$targets;
28 
32  private ‪$applicableSets;
33 
37  private ‪$variables;
38 
42  private ‪$results = [];
43 
44  public static function ‪create(‪Variables ‪$variables): self
45  {
46  return new static(‪$variables);
47  }
48 
49  private function ‪__construct(‪Variables ‪$variables)
50  {
51  $this->variables = ‪$variables;
52  }
53 
54  public function ‪permute(): self
55  {
56  $this->results = [];
57  foreach ($this->targets as $target) {
58  if (!$target instanceof ‪TestSet) {
59  throw new \LogicException('Expected DataSet', 1578045577);
60  }
61  $target = $target->withMergedVariables($this->variables);
62  foreach (‪PermutationUtility::meltArrayItems($this->applicableSets) as $applicables) {
63  try {
64  $this->results[] = $this->‪applyApplicables($target, ...$applicables);
65  } catch (SkipException $exception) {
66  continue;
67  }
68  }
69  }
70  return $this;
71  }
72 
73  private function ‪applyApplicables(‪TestSet $target, Applicable ...$applicables): ‪TestSet
74  {
75  foreach ($applicables as $index => $applicable) {
76  $target = $this->‪withVariableContext($target, $applicable, ...$applicables);
77  }
78  return $target->‪withMergedApplicables(...$applicables);
79  }
80 
81  private function ‪withVariableContext(‪TestSet $target, Applicable $candidate, Applicable ...$applicables): ‪TestSet
82  {
83  if ($candidate instanceof ‪ApplicableConjunction) {
84  foreach ($candidate->filter(VariablesContext::class) as $variableContext) {
85  // in case SkipException is thrown, skip the whole(!) conjunction
86  // (that's why this is not caught here explicitly)
87  $target = $this->‪withVariableContext($target, $variableContext, ...$applicables);
88  }
89  } elseif ($candidate instanceof VariablesContext) {
90  // apply variables for matching VariableContext
91  $targetApplicables = $this->‪includeTargetApplicables($target, ...$applicables);
92  if ($candidate->matchesRequiredApplicables(...$targetApplicables)) {
93  return $target->‪withMergedVariables($candidate->getVariables());
94  }
95  // otherwise don't apply variables & skip this TestSet
96  throw new SkipException('skip', 1578162207);
97  }
98  return $target;
99  }
100 
104  public function ‪getResults(): array
105  {
106  return ‪$this->results;
107  }
108 
109  public function ‪getTargetsForDataProvider(): array
110  {
111  $keys = array_map(
112  function (‪TestSet $testSet) {
113  return $testSet->‪describe();
114  },
116  );
117  $values = array_map(
118  function (‪TestSet $testSet) {
119  return [$testSet];
120  },
122  );
123  return array_combine($keys, $values);
124  }
125 
126  public function ‪withTargets(‪TestSet ...‪$targets): self
127  {
128  $target = clone $this;
129  $target->targets = ‪$targets;
130  return $target;
131  }
132 
133  public function ‪withApplicableSet(‪Applicable ...$applicables): self
134  {
135  $target = clone $this;
136  $target->applicableSets[] = $applicables;
137  return $target;
138  }
139 
140  public function ‪withApplicableItems(array $applicableItems, ‪Applicable ...$applicables): self
141  {
142  $applicableItems = array_values($applicableItems);
143  $applicableItems = array_merge($applicableItems, $applicables);
144  return $this->‪withApplicableSet(...$applicableItems);
145  }
146 
152  private function ‪includeTargetApplicables(‪TestSet $target, Applicable ...$applicables): array
153  {
154  if (empty($target->‪getApplicables())) {
155  return $applicables;
156  }
157  $targetApplicables = $applicables;
158  foreach ($target->‪getApplicables() as $targetApplicable) {
159  if (!in_array($targetApplicable, $targetApplicables, true)) {
160  $targetApplicables[] = $targetApplicable;
161  }
162  }
163  return $targetApplicables;
164  }
165 }
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\withApplicableSet
‪withApplicableSet(Applicable ... $applicables)
Definition: Permutation.php:129
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\TestSet\describe
‪describe()
Definition: TestSet.php:133
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\$targets
‪TestSet[] $targets
Definition: Permutation.php:26
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\applyApplicables
‪applyApplicables(TestSet $target, Applicable ... $applicables)
Definition: Permutation.php:69
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation
Definition: Permutation.php:23
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\permute
‪permute()
Definition: Permutation.php:50
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variables
Definition: Variables.php:21
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\$applicableSets
‪Applicable[][] $applicableSets
Definition: Permutation.php:30
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariablesContext
Definition: VariablesContext.php:21
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\create
‪static create(Variables $variables)
Definition: Permutation.php:40
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\TestSet
Definition: TestSet.php:21
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\withVariableContext
‪withVariableContext(TestSet $target, Applicable $candidate, Applicable ... $applicables)
Definition: Permutation.php:77
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Applicable
Definition: Applicable.php:21
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\__construct
‪__construct(Variables $variables)
Definition: Permutation.php:45
‪TYPO3\CMS\Core\Utility\PermutationUtility
Definition: PermutationUtility.php:24
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\TestSet\getApplicables
‪Applicable[] getApplicables(string $type=null)
Definition: TestSet.php:66
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\withTargets
‪withTargets(TestSet ... $targets)
Definition: Permutation.php:122
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\includeTargetApplicables
‪Applicable[] includeTargetApplicables(TestSet $target, Applicable ... $applicables)
Definition: Permutation.php:148
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\TestSet\withMergedVariables
‪withMergedVariables(Variables $variables)
Definition: TestSet.php:112
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder
Definition: Applicable.php:18
‪TYPO3\CMS\Core\Utility\PermutationUtility\meltArrayItems
‪static array meltArrayItems(array $payload, array $previousResult=[])
Definition: PermutationUtility.php:66
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\$variables
‪Variables $variables
Definition: Permutation.php:34
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\withApplicableItems
‪withApplicableItems(array $applicableItems, Applicable ... $applicables)
Definition: Permutation.php:136
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\getTargetsForDataProvider
‪getTargetsForDataProvider()
Definition: Permutation.php:105
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\ApplicableConjunction
Definition: ApplicableConjunction.php:21
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\$results
‪TestSet[] $results
Definition: Permutation.php:38
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\getResults
‪TestSet[] getResults()
Definition: Permutation.php:100
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\TestSet\withMergedApplicables
‪withMergedApplicables(Applicable ... $applicables)
Definition: TestSet.php:94
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\SkipException
Definition: SkipException.php:21