‪TYPO3CMS  9.5
Permutation.php
Go to the documentation of this file.
1 <?php
2 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 {
25  private ‪$targets;
26 
30  private ‪$applicableSets;
31 
35  private ‪$variables;
36 
40  private ‪$results = [];
41 
42  public static function ‪create(‪Variables ‪$variables): self
43  {
44  return new static(‪$variables);
45  }
46 
47  private function ‪__construct(‪Variables ‪$variables)
48  {
49  $this->variables = ‪$variables;
50  }
51 
52  public function ‪permute(): self
53  {
54  $this->results = [];
55  foreach ($this->targets as $target) {
56  if (!$target instanceof ‪TestSet) {
57  throw new \LogicException('Expected DataSet', 1578045577);
58  }
59  $target = $target->withMergedVariables($this->variables);
60  foreach (‪PermutationUtility::meltArrayItems($this->applicableSets) as $applicables) {
61  try {
62  $this->results[] = $this->‪applyApplicables($target, ...$applicables);
63  } catch (SkipException $exception) {
64  continue;
65  }
66  }
67  }
68  return $this;
69  }
70 
71  private function ‪applyApplicables(‪TestSet $target, Applicable ...$applicables): ‪TestSet
72  {
73  foreach ($applicables as $index => $applicable) {
74  $target = $this->‪withVariableContext($target, $applicable, ...$applicables);
75  }
76  return $target->‪withMergedApplicables(...$applicables);
77  }
78 
79  private function ‪withVariableContext(‪TestSet $target, Applicable $candidate, Applicable ...$applicables): ‪TestSet
80  {
81  if ($candidate instanceof ‪ApplicableConjunction) {
82  foreach ($candidate->filter(VariablesContext::class) as $variableContext) {
83  // in case SkipException is thrown, skip the whole(!) conjunction
84  // (that's why this is not caught here explicitly)
85  $target = $this->‪withVariableContext($target, $variableContext, ...$applicables);
86  }
87  } elseif ($candidate instanceof VariablesContext) {
88  // apply variables for matching VariableContext
89  $targetApplicables = $this->‪includeTargetApplicables($target, ...$applicables);
90  if ($candidate->matchesRequiredApplicables(...$targetApplicables)) {
91  return $target->‪withMergedVariables($candidate->getVariables());
92  }
93  // otherwise don't apply variables & skip this TestSet
94  throw new SkipException('skip', 1578162207);
95  }
96  return $target;
97  }
98 
102  public function ‪getResults(): array
103  {
104  return ‪$this->results;
105  }
106 
107  public function ‪getTargetsForDataProvider(): array
108  {
109  $keys = array_map(
110  function (‪TestSet $testSet) {
111  return $testSet->‪describe();
112  },
114  );
115  $values = array_map(
116  function (‪TestSet $testSet) {
117  return [$testSet];
118  },
120  );
121  return array_combine($keys, $values);
122  }
123 
124  public function ‪withTargets(‪TestSet ...‪$targets): self
125  {
126  $target = clone $this;
127  $target->targets = ‪$targets;
128  return $target;
129  }
130 
131  public function ‪withApplicableSet(‪Applicable ...$applicables): self
132  {
133  $target = clone $this;
134  $target->applicableSets[] = $applicables;
135  return $target;
136  }
137 
138  public function ‪withApplicableItems(array $applicableItems, ‪Applicable ...$applicables): self
139  {
140  $applicableItems = array_values($applicableItems);
141  $applicableItems = array_merge($applicableItems, $applicables);
142  return $this->‪withApplicableSet(...$applicableItems);
143  }
144 
150  private function ‪includeTargetApplicables(‪TestSet $target, Applicable ...$applicables): array
151  {
152  if (empty($target->‪getApplicables())) {
153  return $applicables;
154  }
155  $targetApplicables = $applicables;
156  foreach ($target->‪getApplicables() as $targetApplicable) {
157  if (!in_array($targetApplicable, $targetApplicables, true)) {
158  $targetApplicables[] = $targetApplicable;
159  }
160  }
161  return $targetApplicables;
162  }
163 }
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\withApplicableSet
‪withApplicableSet(Applicable ... $applicables)
Definition: Permutation.php:127
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\TestSet\describe
‪describe()
Definition: TestSet.php:131
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\$targets
‪TestSet[] $targets
Definition: Permutation.php:24
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\applyApplicables
‪applyApplicables(TestSet $target, Applicable ... $applicables)
Definition: Permutation.php:67
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation
Definition: Permutation.php:21
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\permute
‪permute()
Definition: Permutation.php:48
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variables
Definition: Variables.php:19
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\$applicableSets
‪Applicable[][] $applicableSets
Definition: Permutation.php:28
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariablesContext
Definition: VariablesContext.php:19
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\create
‪static create(Variables $variables)
Definition: Permutation.php:38
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\TestSet
Definition: TestSet.php:19
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\withVariableContext
‪withVariableContext(TestSet $target, Applicable $candidate, Applicable ... $applicables)
Definition: Permutation.php:75
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Applicable
Definition: Applicable.php:19
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\__construct
‪__construct(Variables $variables)
Definition: Permutation.php:43
‪TYPO3\CMS\Core\Utility\PermutationUtility
Definition: PermutationUtility.php:22
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\TestSet\getApplicables
‪Applicable[] getApplicables(string $type=null)
Definition: TestSet.php:64
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\withTargets
‪withTargets(TestSet ... $targets)
Definition: Permutation.php:120
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\includeTargetApplicables
‪Applicable[] includeTargetApplicables(TestSet $target, Applicable ... $applicables)
Definition: Permutation.php:146
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\TestSet\withMergedVariables
‪withMergedVariables(Variables $variables)
Definition: TestSet.php:110
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder
Definition: Applicable.php:3
‪TYPO3\CMS\Core\Utility\PermutationUtility\meltArrayItems
‪static array meltArrayItems(array $payload, array $previousResult=[])
Definition: PermutationUtility.php:64
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\$variables
‪Variables $variables
Definition: Permutation.php:32
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\withApplicableItems
‪withApplicableItems(array $applicableItems, Applicable ... $applicables)
Definition: Permutation.php:134
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\getTargetsForDataProvider
‪getTargetsForDataProvider()
Definition: Permutation.php:103
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\ApplicableConjunction
Definition: ApplicableConjunction.php:19
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\$results
‪TestSet[] $results
Definition: Permutation.php:36
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Permutation\getResults
‪TestSet[] getResults()
Definition: Permutation.php:98
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\TestSet\withMergedApplicables
‪withMergedApplicables(Applicable ... $applicables)
Definition: TestSet.php:92
‪TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\SkipException
Definition: SkipException.php:19