ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
PathConditionsCheckerTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
34use PHPUnit\Framework\TestCase;
35
36class PathConditionsCheckerTest extends TestCase
37{
38 protected function getElementMock(): ElementInterface
39 {
40 return new NullElement();
41 }
42
43 public function getStepMock(string|StepToken $name): StepInterface
44 {
45 return new class ($name) extends NullStep {
46 public function __construct(protected string|StepToken $name)
47 {
48 }
49
50 public function name(): string|StepToken
51 {
52 return $this->name;
53 }
54 };
55 }
56
60 public function getPathMock(
61 array $steps,
62 bool $is_relative,
63 bool $leads_exactly_to_one
64 ): PathInterface {
65 return new class ($steps, $is_relative, $leads_exactly_to_one) extends NullPath {
69 public function __construct(
70 protected array $steps,
71 protected bool $is_relative,
72 protected bool $leads_exactly_to_one
73 ) {
74 }
75
76 public function isRelative(): bool
77 {
78 return $this->is_relative;
79 }
80
81 public function leadsToExactlyOneElement(): bool
82 {
83 return $this->leads_exactly_to_one;
84 }
85
86 public function steps(): \Generator
87 {
88 yield from $this->steps;
89 }
90 };
91 }
92
97 PathInterface $path_without_conditions,
98 array $step_conditions_dict
100 return new class ($path_without_conditions, $step_conditions_dict) extends NullPathConditionsCollection {
104 public function __construct(
105 protected PathInterface $path_without_conditions,
106 protected array $conditions
107 ) {
108 }
109
110 public function getPathWithoutConditions(): PathInterface
111 {
112 return $this->path_without_conditions;
113 }
114
115 public function getConditionPathByStepName(string $name): PathInterface
116 {
117 if (!key_exists($name, $this->conditions)) {
118 return new NullPath();
119 }
120 return $this->conditions[$name];
121 }
122 };
123 }
124
128 public function getNavigatorMock(
129 array $steps_has_elements
131 return new class ($steps_has_elements) extends NullNavigator {
132 protected int $index;
136 public function __construct(protected array $step_has_elements)
137 {
138 $this->index = -1;
139 }
140
141 public function nextStep(): ?NavigatorInterface
142 {
143 $clone = clone $this;
144 $clone->index++;
145 if ($clone->index >= count($this->step_has_elements)) {
146 return null;
147 }
148 return $clone;
149 }
150
151 public function previousStep(): ?NavigatorInterface
152 {
153 $clone = clone $this;
154 $clone->index--;
155 if ($clone->index < 0) {
156 return null;
157 }
158 return $clone;
159 }
160
161 public function hasNextStep(): bool
162 {
163 return $this->index < (count($this->step_has_elements) - 1);
164 }
165
166 public function hasPreviousStep(): bool
167 {
168 return $this->index > 0;
169 }
170
171 public function hasElements(): bool
172 {
173 return $this->index === -1 || $this->step_has_elements[$this->index];
174 }
175 };
176 }
177
179 {
180 return new class ($this) extends NullNavigatorFactory {
181 public function __construct(protected PathConditionsCheckerTest $test)
182 {
183 }
184
188 protected function stepsToBoolArray(StepInterface ...$steps): array
189 {
190 $has_elements = [];
191 foreach ($steps as $step) {
192 $step_str = $step->name() instanceof StepToken ? $step->name()->value : $step->name();
193 $has_elements[] = str_ends_with($step_str, 'has_elements');
194 }
195 return $has_elements;
196 }
197
198 public function navigator(PathInterface $path, ElementInterface $start_element): NavigatorInterface
199 {
200 return $this->test->getNavigatorMock($this->stepsToBoolArray(...$path->steps()));
201 }
202 };
203 }
204
205 public function testIsPathConditionMet_001(): void
206 {
207 $path_without_conditions = $this->getPathMock(
208 [
209 $this->getStepMock('start'),
210 $this->getStepMock('step1'),
211 $this->getStepMock('step2'),
212 $this->getStepMock('end'),
213 ],
214 false,
215 false
216 );
217
218 $path_checker = new PathConditionsChecker(
219 $this->getPathConditionsCollectionMock(
220 $path_without_conditions,
221 []
222 ),
223 $this->getNavigatorFactoryMock()
224 );
225
226 $this->assertTrue(
227 $path_checker->isPathConditionMet($this->getStepMock('start'), $this->getElementMock())
228 );
229 $this->assertTrue(
230 $path_checker->isPathConditionMet($this->getStepMock('step1'), $this->getElementMock())
231 );
232 $this->assertTrue(
233 $path_checker->isPathConditionMet($this->getStepMock('step2'), $this->getElementMock())
234 );
235 $this->assertTrue(
236 $path_checker->isPathConditionMet($this->getStepMock('end'), $this->getElementMock())
237 );
238 $this->assertTrue(
239 $path_checker->isPathConditionMet($this->getStepMock('not_a_step'), $this->getElementMock())
240 );
241 }
242
243 public function testIsPathConditionMet_002(): void
244 {
245 $path_without_conditions = $this->getPathMock(
246 [
247 $this->getStepMock('start'),
248 $this->getStepMock('step1'),
249 $this->getStepMock('step2'),
250 $this->getStepMock('end'),
251 ],
252 false,
253 false
254 );
255
256 $path_conditions = $this->getPathConditionsCollectionMock(
257 $path_without_conditions,
258 [
259 'start' => $this->getPathMock(
260 [
261 $this->getStepMock('has_elements')
262 ],
263 true,
264 false
265 ),
266 'step1' => $this->getPathMock(
267 [
268 $this->getStepMock('has_elements'),
269 $this->getStepMock('has_elements')
270 ],
271 true,
272 false
273 ),
274 'step2' => $this->getPathMock(
275 [
276 $this->getStepMock('has_elements'),
277 $this->getStepMock('no_elements')
278 ],
279 true,
280 false
281 ),
282 'end' => $this->getPathMock(
283 [
284 $this->getStepMock('no_elements')
285 ],
286 true,
287 false
288 )
289 ]
290 );
291
292 $path_checker = new PathConditionsChecker(
293 $path_conditions,
294 $this->getNavigatorFactoryMock()
295 );
296
297 $this->assertTrue(
298 $path_checker->isPathConditionMet($this->getStepMock('start'), $this->getElementMock())
299 );
300 $this->assertTrue(
301 $path_checker->isPathConditionMet($this->getStepMock('step1'), $this->getElementMock())
302 );
303 $this->assertFalse(
304 $path_checker->isPathConditionMet($this->getStepMock('step2'), $this->getElementMock())
305 );
306 $this->assertFalse(
307 $path_checker->isPathConditionMet($this->getStepMock('end'), $this->getElementMock())
308 );
309 $this->assertTrue(
310 $path_checker->isPathConditionMet($this->getStepMock('not_a_step'), $this->getElementMock())
311 );
312 }
313}
getPathConditionsCollectionMock(PathInterface $path_without_conditions, array $step_conditions_dict)
getPathMock(array $steps, bool $is_relative, bool $leads_exactly_to_one)
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$path
Definition: ltiservices.php:30
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
StepToken
The string representation of these tokens must not occur as names of metadata elements.
Definition: StepToken.php:28