ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ManipulatorTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
26use ILIAS\MetaData\Manipulator\NullManipulator as NullInternalManipulator;
31
32class ManipulatorTest extends TestCase
33{
34 protected function getPath(string $string): PathInterface
35 {
36 return new class ($string) extends NullPath {
37 public function __construct(public string $string)
38 {
39 }
40 };
41 }
42
43 protected function getManipulator(bool $throw_exception = false): Manipulator
44 {
45 $internal_manipulator = new class ($throw_exception) extends NullInternalManipulator {
46 public array $executed_actions = [];
47
48 public function __construct(protected bool $throw_exception)
49 {
50 }
51
52 public function prepareCreateOrUpdate(
55 string ...$values
56 ): SetInterface {
57 if ($this->throw_exception) {
58 throw new \ilMDPathException('failed');
59 }
60
61 $cloned_set = clone $set;
62 $cloned_set->actions[] = [
63 'action' => 'create or update',
64 'path' => $path->string,
65 'values' => $values
66 ];
67 return $cloned_set;
68 }
69
70 public function prepareForceCreate(
73 string ...$values
74 ): SetInterface {
75 if ($this->throw_exception) {
76 throw new \ilMDPathException('failed');
77 }
78
79 $cloned_set = clone $set;
80 $cloned_set->actions[] = [
81 'action' => 'force create',
82 'path' => $path->string,
83 'values' => $values
84 ];
85 return $cloned_set;
86 }
87
88 public function prepareDelete(
91 ): SetInterface {
92 $cloned_set = clone $set;
93 $cloned_set->actions[] = [
94 'action' => 'delete',
95 'path' => $path->string
96 ];
97 return $cloned_set;
98 }
99 };
100
101 $repository = new class ($throw_exception) extends NullRepository {
102 public array $executed_actions = [];
103
104 public function __construct(protected bool $throw_exception)
105 {
106 }
107
108 public function manipulateMD(SetInterface $set): void
109 {
110 if ($this->throw_exception) {
111 throw new \ilMDRepositoryException('failed');
112 }
113
114 $this->executed_actions[] = $set->actions;
115 }
116 };
117
118 $set = new class () extends NullSet {
119 public array $actions = [];
120 };
121
122 return new class ($internal_manipulator, $repository, $set) extends Manipulator {
123 public function exposeSet(): SetInterface
124 {
125 return $this->set;
126 }
127
128 public function exposeRepository(): RepositoryInterface
129 {
130 return $this->repository;
131 }
132 };
133 }
134
135 public function testPrepareCreateOrUpdate(): void
136 {
137 $exp1 = [
138 'action' => 'create or update',
139 'path' => 'path 1',
140 'values' => ['value 1']
141 ];
142 $exp2 = [
143 'action' => 'create or update',
144 'path' => 'path 2',
145 'values' => ['value 20', 'value 21', 'value 22']
146 ];
147 $manipulator = $this->getManipulator();
148 $manipulator1 = $manipulator->prepareCreateOrUpdate(
149 $this->getPath($exp1['path']),
150 ...$exp1['values']
151 );
152 $manipulator2 = $manipulator1->prepareCreateOrUpdate(
153 $this->getPath($exp2['path']),
154 ...$exp2['values']
155 );
156
157
158 $this->assertSame(
159 [],
160 $manipulator->exposeSet()->actions
161 );
162 $this->assertSame(
163 [$exp1],
164 $manipulator1->exposeSet()->actions
165 );
166 $this->assertSame(
167 [$exp1, $exp2],
168 $manipulator2->exposeSet()->actions
169 );
170 }
171
173 {
174 $manipulator = $this->getManipulator(true);
175
176 $this->expectException(\ilMDServicesException::class);
177 $manipulator->prepareCreateOrUpdate($this->getPath('path'));
178 }
179
180 public function testPrepareForceCreate(): void
181 {
182 $exp1 = [
183 'action' => 'force create',
184 'path' => 'path 1',
185 'values' => ['value 1']
186 ];
187 $exp2 = [
188 'action' => 'force create',
189 'path' => 'path 2',
190 'values' => ['value 20', 'value 21', 'value 22']
191 ];
192 $manipulator = $this->getManipulator();
193 $manipulator1 = $manipulator->prepareForceCreate(
194 $this->getPath($exp1['path']),
195 ...$exp1['values']
196 );
197 $manipulator2 = $manipulator1->prepareForceCreate(
198 $this->getPath($exp2['path']),
199 ...$exp2['values']
200 );
201
202
203 $this->assertSame(
204 [],
205 $manipulator->exposeSet()->actions
206 );
207 $this->assertSame(
208 [$exp1],
209 $manipulator1->exposeSet()->actions
210 );
211 $this->assertSame(
212 [$exp1, $exp2],
213 $manipulator2->exposeSet()->actions
214 );
215 }
216
217 public function testPrepareForceCreateException(): void
218 {
219 $manipulator = $this->getManipulator(true);
220
221 $this->expectException(\ilMDServicesException::class);
222 $manipulator->prepareForceCreate($this->getPath('path'));
223 }
224
225 public function testPrepareDelete(): void
226 {
227 $exp1 = [
228 'action' => 'delete',
229 'path' => 'path 1'
230 ];
231 $exp2 = [
232 'action' => 'delete',
233 'path' => 'path 2'
234 ];
235 $manipulator = $this->getManipulator();
236 $manipulator1 = $manipulator->prepareDelete($this->getPath($exp1['path']));
237 $manipulator2 = $manipulator1->prepareDelete($this->getPath($exp2['path']));
238
239
240 $this->assertSame(
241 [],
242 $manipulator->exposeSet()->actions
243 );
244 $this->assertSame(
245 [$exp1],
246 $manipulator1->exposeSet()->actions
247 );
248 $this->assertSame(
249 [$exp1, $exp2],
250 $manipulator2->exposeSet()->actions
251 );
252 }
253
254 public function testExecute(): void
255 {
256 $exp1 = [
257 'action' => 'delete',
258 'path' => 'path 1'
259 ];
260 $exp2 = [
261 'action' => 'force create',
262 'path' => 'path 2',
263 'values' => ['value 20', 'value 21', 'value 22']
264 ];
265 $exp3 = [
266 'action' => 'delete',
267 'path' => 'path 3'
268 ];
269 $exp4 = [
270 'action' => 'create or update',
271 'path' => 'path 4',
272 'values' => []
273 ];
274 $exp5 = [
275 'action' => 'force create',
276 'path' => 'path 5',
277 'values' => ['value 50', 'value 51', 'value 52']
278 ];
279 $exp6 = [
280 'action' => 'delete',
281 'path' => 'path 6'
282 ];
283 $exp7 = [
284 'action' => 'create or update',
285 'path' => 'path 7',
286 'values' => ['value 7']
287 ];
288
289 $manipulator = $this->getManipulator()
290 ->prepareDelete($this->getPath($exp1['path']))
291 ->prepareForceCreate($this->getPath($exp2['path']), ...$exp2['values'])
292 ->prepareDelete($this->getPath($exp3['path']))
293 ->prepareCreateOrUpdate($this->getPath($exp4['path']), ...$exp4['values']);
294 $manipulator->execute();
295 $manipulator = $manipulator
296 ->prepareForceCreate($this->getPath($exp5['path']), ...$exp5['values'])
297 ->prepareDelete($this->getPath($exp6['path']))
298 ->prepareCreateOrUpdate($this->getPath($exp7['path']), ...$exp7['values']);
299 $manipulator->execute();
300
301 $executed_actions = $manipulator->exposeRepository()->executed_actions;
302 $this->assertCount(2, $executed_actions);
303 $this->assertSame(
304 [$exp1, $exp2, $exp3, $exp4],
305 $executed_actions[0]
306 );
307 $this->assertSame(
308 [$exp1, $exp2, $exp3, $exp4, $exp5, $exp6, $exp7],
309 $executed_actions[1]
310 );
311 }
312
313 public function testExecuteException(): void
314 {
315 $manipulator = $this->getManipulator(true);
316
317 $this->expectException(\ilMDServicesException::class);
318 $manipulator->execute();
319 }
320}
prepareForceCreate(PathInterface $path, string ... $values)
New elements are set to be created as specified by the path, and filled with the values.
Definition: Manipulator.php:64
prepareCreateOrUpdate(PathInterface $path, string ... $values)
The values are set to be inserted into the elements specified by the path in order.
Definition: Manipulator.php:44
prepareDelete(PathInterface $path)
All elements specified by the path are set to be deleted.
Definition: Manipulator.php:84
__construct(InternalManipulator $internal_manipulator, RepositoryInterface $repository, SetInterface $set)
Definition: Manipulator.php:34
$path
Definition: ltiservices.php:30