ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ManipulatorTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 use ILIAS\MetaData\Manipulator\NullManipulator as NullInternalManipulator;
29 
30 class ManipulatorTest extends TestCase
31 {
32  protected function getPath(string $string): PathInterface
33  {
34  return new class ($string) extends NullPath {
35  public function __construct(public string $string)
36  {
37  }
38  };
39  }
40 
41  protected function getManipulator(): Manipulator
42  {
43  $internal_manipulator = new class () extends NullInternalManipulator {
44  public array $executed_actions = [];
45 
46  public function prepareCreateOrUpdate(
49  string ...$values
50  ): SetInterface {
51  $cloned_set = clone $set;
52  $cloned_set->actions[] = [
53  'action' => 'create or update',
54  'path' => $path->string,
55  'values' => $values
56  ];
57  return $cloned_set;
58  }
59 
60  public function prepareForceCreate(
61  SetInterface $set,
62  PathInterface $path,
63  string ...$values
64  ): SetInterface {
65  $cloned_set = clone $set;
66  $cloned_set->actions[] = [
67  'action' => 'force create',
68  'path' => $path->string,
69  'values' => $values
70  ];
71  return $cloned_set;
72  }
73 
74  public function prepareDelete(
75  SetInterface $set,
76  PathInterface $path
77  ): SetInterface {
78  $cloned_set = clone $set;
79  $cloned_set->actions[] = [
80  'action' => 'delete',
81  'path' => $path->string
82  ];
83  return $cloned_set;
84  }
85 
86  public function execute(SetInterface $set): void
87  {
88  $set->executed_actions = $set->actions;
89  }
90  };
91 
92  $set = new class () extends NullSet {
93  public array $actions = [];
94  public array $executed_actions = [];
95  };
96 
97  return new class ($internal_manipulator, $set) extends Manipulator {
98  public function exposeSet(): SetInterface
99  {
100  return $this->set;
101  }
102  };
103  }
104 
105  public function testPrepareCreateOrUpdate(): void
106  {
107  $exp1 = [
108  'action' => 'create or update',
109  'path' => 'path 1',
110  'values' => ['value 1']
111  ];
112  $exp2 = [
113  'action' => 'create or update',
114  'path' => 'path 2',
115  'values' => ['value 20', 'value 21', 'value 22']
116  ];
117  $manipulator = $this->getManipulator();
118  $manipulator1 = $manipulator->prepareCreateOrUpdate(
119  $this->getPath($exp1['path']),
120  ...$exp1['values']
121  );
122  $manipulator2 = $manipulator1->prepareCreateOrUpdate(
123  $this->getPath($exp2['path']),
124  ...$exp2['values']
125  );
126 
127 
128  $this->assertSame(
129  [],
130  $manipulator->exposeSet()->actions
131  );
132  $this->assertSame(
133  [$exp1],
134  $manipulator1->exposeSet()->actions
135  );
136  $this->assertSame(
137  [$exp1, $exp2],
138  $manipulator2->exposeSet()->actions
139  );
140  }
141 
142  public function testPrepareForce(): void
143  {
144  $exp1 = [
145  'action' => 'force create',
146  'path' => 'path 1',
147  'values' => ['value 1']
148  ];
149  $exp2 = [
150  'action' => 'force create',
151  'path' => 'path 2',
152  'values' => ['value 20', 'value 21', 'value 22']
153  ];
154  $manipulator = $this->getManipulator();
155  $manipulator1 = $manipulator->prepareForceCreate(
156  $this->getPath($exp1['path']),
157  ...$exp1['values']
158  );
159  $manipulator2 = $manipulator1->prepareForceCreate(
160  $this->getPath($exp2['path']),
161  ...$exp2['values']
162  );
163 
164 
165  $this->assertSame(
166  [],
167  $manipulator->exposeSet()->actions
168  );
169  $this->assertSame(
170  [$exp1],
171  $manipulator1->exposeSet()->actions
172  );
173  $this->assertSame(
174  [$exp1, $exp2],
175  $manipulator2->exposeSet()->actions
176  );
177  }
178 
179  public function testPrepareDelete(): void
180  {
181  $exp1 = [
182  'action' => 'delete',
183  'path' => 'path 1'
184  ];
185  $exp2 = [
186  'action' => 'delete',
187  'path' => 'path 2'
188  ];
189  $manipulator = $this->getManipulator();
190  $manipulator1 = $manipulator->prepareDelete($this->getPath($exp1['path']));
191  $manipulator2 = $manipulator1->prepareDelete($this->getPath($exp2['path']));
192 
193 
194  $this->assertSame(
195  [],
196  $manipulator->exposeSet()->actions
197  );
198  $this->assertSame(
199  [$exp1],
200  $manipulator1->exposeSet()->actions
201  );
202  $this->assertSame(
203  [$exp1, $exp2],
204  $manipulator2->exposeSet()->actions
205  );
206  }
207 
208  public function testExecute(): void
209  {
210  $exp1 = [
211  'action' => 'delete',
212  'path' => 'path 1'
213  ];
214  $exp2 = [
215  'action' => 'force create',
216  'path' => 'path 2',
217  'values' => ['value 20', 'value 21', 'value 22']
218  ];
219  $exp3 = [
220  'action' => 'delete',
221  'path' => 'path 3'
222  ];
223  $exp4 = [
224  'action' => 'create or update',
225  'path' => 'path 4',
226  'values' => []
227  ];
228  $exp5 = [
229  'action' => 'force create',
230  'path' => 'path 5',
231  'values' => ['value 50', 'value 51', 'value 52']
232  ];
233  $exp6 = [
234  'action' => 'delete',
235  'path' => 'path 6'
236  ];
237  $exp7 = [
238  'action' => 'create or update',
239  'path' => 'path 7',
240  'values' => ['value 7']
241  ];
242 
243  $manipulator = $this->getManipulator()
244  ->prepareDelete($this->getPath($exp1['path']))
245  ->prepareForceCreate($this->getPath($exp2['path']), ...$exp2['values'])
246  ->prepareDelete($this->getPath($exp3['path']))
247  ->prepareCreateOrUpdate($this->getPath($exp4['path']), ...$exp4['values']);
248  $manipulator->execute();
249  $manipulator5 = $manipulator
250  ->prepareForceCreate($this->getPath($exp5['path']), ...$exp5['values'])
251  ->prepareDelete($this->getPath($exp6['path']))
252  ->prepareCreateOrUpdate($this->getPath($exp7['path']), ...$exp7['values']);
253  $manipulator5->execute();
254 
255  $this->assertSame(
256  [$exp1, $exp2, $exp3, $exp4],
257  $manipulator->exposeSet()->executed_actions
258  );
259  $this->assertSame(
260  [$exp1, $exp2, $exp3, $exp4, $exp5, $exp6, $exp7],
261  $manipulator5->exposeSet()->executed_actions
262  );
263  }
264 }
$path
Definition: ltiservices.php:32
prepareDelete(PathInterface $path)
All elements specified by the path are set to be deleted.
Definition: Manipulator.php:64
__construct(InternalManipulator $internal_manipulator, SetInterface $set)
Definition: Manipulator.php:32
execute()
Execute all prepared actions.
Definition: Manipulator.php:73
prepareForceCreate(PathInterface $path, string ... $values)
New elements are set to be created as specified by the path, and are filled with the values...
Definition: Manipulator.php:52
prepareCreateOrUpdate(PathInterface $path, string ... $values)
The values are set to be inserted into the elements specified by the path in order.
Definition: Manipulator.php:40