ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
RequestProcessorTestCase.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
39
40abstract class RequestProcessorTestCase extends TestCase
41{
42 protected function getDate(string $string): \DateTimeImmutable
43 {
44 return new \DateTimeImmutable($string, new \DateTimeZone('UTC'));
45 }
46
47 protected function getURI(string $string): URI
48 {
49 $url = $this->createMock(URI::class);
50 $url->method('__toString')->willReturn($string);
51 return $url;
52 }
53
57 protected function getRequest(
58 string $base_url,
59 Verb $verb,
60 array $arguments_with_values,
61 bool $correct_arguments = true
63 $base_url = $this->getURI($base_url);
64
65 return new class ($base_url, $verb, $arguments_with_values, $correct_arguments) extends NullRequest {
66 public function __construct(
67 protected URI $base_url,
68 protected Verb $verb,
69 protected array $arguments_with_values,
70 protected bool $correct_values
71 ) {
72 }
73
74 public function baseURL(): URI
75 {
76 return $this->base_url;
77 }
78
79 public function verb(): Verb
80 {
81 return $this->verb;
82 }
83
84 public function argumentKeys(): \Generator
85 {
86 foreach ($this->arguments_with_values as $argument_key => $argument_value) {
87 if (!is_null($r = Argument::tryFrom($argument_key))) {
88 yield $r;
89 }
90 }
91 }
92
93 public function hasArgument(Argument $argument): bool
94 {
95 return in_array($argument->value, array_keys($this->arguments_with_values));
96 }
97
98 public function argumentValue(Argument $argument): string
99 {
100 return $this->arguments_with_values[$argument->value] ?? '';
101 }
102
103 public function hasCorrectArguments(
104 array $required,
105 array $optional,
106 array $exclusive
107 ): bool {
108 return $this->correct_values;
109 }
110 };
111 }
112
113 protected function getWriter(): WriterInterface
114 {
115 return new class () extends NullWriter {
116 public function writeError(Error $error, string $message): \DOMDocument
117 {
118 $doc = new \DOMDocument();
119 $doc->appendChild($doc->createElement('error', $error->value));
120 return $doc;
121 }
122
123 public function writeIdentifyElements(
124 string $repository_name,
125 URI $base_url,
126 \DateTimeImmutable $earliest_datestamp,
127 string $first_admin_email,
128 string ...$further_admin_emails
129 ): \Generator {
130 $els = [
131 $repository_name,
132 (string) $base_url,
133 $earliest_datestamp->format('Y-m-d'),
134 $first_admin_email
135 ];
136 foreach ($els as $idx => $el) {
137 $doc = new \DOMDocument();
138 $doc->appendChild($doc->createElement('info', $el));
139 yield $doc;
140 }
141 }
142
146 public function writeMetadataFormat(): \DOMDocument
147 {
148 $doc = new \DOMDocument();
149 $doc->appendChild($doc->createElement('md_format', 'some metadata'));
150 return $doc;
151 }
152
153 public function writeRecordHeader(
154 string $identifier,
155 \DateTimeImmutable $datestamp
156 ): \DOMDocument {
157 $doc = new \DOMDocument();
158 $doc->appendChild($doc->createElement(
159 'header',
160 $identifier . ':' . $datestamp->format('Y-m-d')
161 ));
162 return $doc;
163 }
164
168 public function writeRecord(
169 string $identifier,
170 \DateTimeImmutable $datestamp,
171 \DOMDocument $metadata
172 ): \DOMDocument {
173 $doc = new \DOMDocument();
174 $doc->appendChild($root = $doc->createElement('record'));
175 $root->appendChild(
176 $doc->createElement(
177 'record_info',
178 $identifier . ':' . $datestamp->format('Y-m-d')
179 )
180 );
181 $root->appendChild($doc->importNode($metadata->documentElement, true));
182 return $doc;
183 }
184
185 public function writeSet(string $spec, string $name): \DOMDocument
186 {
187 $doc = new \DOMDocument();
188 $doc->appendChild($doc->createElement('set', $spec . ':' . $name));
189 return $doc;
190 }
191
192 public function writeResumptionToken(
193 string $token,
194 int $complete_list_size,
195 int $cursor
196 ): \DOMDocument {
197 $doc = new \DOMDocument();
198 $doc->appendChild($doc->createElement(
199 'token',
200 $token . ',fullsize=' . $complete_list_size . ',cursor=' . $cursor
201 ));
202 return $doc;
203 }
204
205 public function writeResponse(
206 RequestInterface $request,
207 \DOMDocument ...$contents
208 ): \DOMDocument {
209 return $this->writeResponseOrErrorResponse(
210 'response',
211 $request,
212 ...$contents
213 );
214 }
215
216 public function writeErrorResponse(
217 RequestInterface $request,
218 \DOMDocument ...$errors
219 ): \DOMDocument {
220 return $this->writeResponseOrErrorResponse(
221 'error_response',
222 $request,
223 ...$errors
224 );
225 }
226
227 protected function writeResponseOrErrorResponse(
228 string $root_name,
229 RequestInterface $request,
230 \DOMDocument ...$contents
231 ): \DOMDocument {
232 $args = [];
233 foreach ($request->argumentKeys() as $key) {
234 $args[] = $key->value . '=' . $request->argumentValue($key);
235 }
236
237 $doc = new \DOMDocument();
238 $doc->appendChild($root = $doc->createElement($root_name));
239 $root->appendChild($doc->createElement(
240 'response_info',
241 $request->baseURL() . ':' . $request->verb()->value . ':' . implode(',', $args)
242 ));
243 foreach ($contents as $content) {
244 $root->appendChild($doc->importNode($content->documentElement, true));
245 }
246 return $doc;
247 }
248 };
249 }
250
251 protected function getSettings(
252 string $prefix = '',
253 string $repo_name = '',
254 string $contact_mail = ''
256 return new class ($prefix, $repo_name, $contact_mail) extends NullSettings {
257 public function __construct(
258 protected string $prefix,
259 protected string $repo_name,
260 protected string $contact_mail
261 ) {
262 }
263
264 public function getOAIIdentifierPrefix(): string
265 {
266 return $this->prefix;
267 }
268
269 public function getOAIContactMail(): string
270 {
271 return $this->contact_mail;
272 }
273
274 public function getOAIRepositoryName(): string
275 {
276 return $this->repo_name;
277 }
278 };
279 }
280
284 protected function getRepository(
285 ?string $earliest_datestamp = null,
286 int $record_count = 0,
287 string ...$identifiers
289 $earliest_datestamp = $this->getDate($earliest_datestamp ?? '@0');
290 $records = [];
291 foreach ($identifiers as $identifier) {
292 $records[$identifier] = new class ($identifier) extends NullRecord {
293 public function __construct(protected string $identifier)
294 {
295 }
296
297 public function infos(): RecordInfosInterface
298 {
299 return new class ($this->identifier) extends NullRecordInfos {
300 public function __construct(protected string $identifier)
301 {
302 }
303
304 public function datestamp(): \DateTimeImmutable
305 {
306 return new \DateTimeImmutable(
307 explode('+', $this->identifier)[1],
308 new \DateTimeZone('UTC')
309 );
310 }
311
312 public function identfifier(): string
313 {
314 return $this->identifier;
315 }
316 };
317 }
318
319 public function metadata(): \DOMDocument
320 {
321 $doc = new \DOMDocument();
322 $doc->appendChild($doc->createElement('md', 'md for ' . $this->identifier));
323 return $doc;
324 }
325 };
326 }
327
328 return new class ($earliest_datestamp, $record_count, $records) extends NullRepository {
329 public array $exposed_parameters = [];
330
331 public function __construct(
332 protected \DateTimeImmutable $earliest_datestamp,
333 protected int $record_count,
334 protected array $records
335 ) {
336 }
337
338 public function getEarliestDatestamp(): \DateTimeImmutable
339 {
340 return $this->earliest_datestamp;
341 }
342
343 public function doesRecordWithIdentifierExist(string $identifier): bool
344 {
345 return in_array($identifier, array_keys($this->records));
346 }
347
348 public function getRecordByIdentifier(string $identifier): ?RecordInterface
349 {
350 return $this->records[$identifier] ?? null;
351 }
352
353 public function getRecords(
354 ?\DateTimeImmutable $from = null,
355 ?\DateTimeImmutable $until = null,
356 ?int $limit = null,
357 ?int $offset = null
358 ): \Generator {
359 $this->exposed_parameters[] = [
360 'from' => $from?->format('Y-m-d'),
361 'until' => $until?->format('Y-m-d'),
362 'limit' => $limit,
363 'offset' => $offset,
364 ];
365 yield from $this->records;
366 }
367
368 public function getRecordInfos(
369 ?\DateTimeImmutable $from = null,
370 ?\DateTimeImmutable $until = null,
371 ?int $limit = null,
372 ?int $offset = null
373 ): \Generator {
374 $this->exposed_parameters[] = [
375 'from' => $from?->format('Y-m-d'),
376 'until' => $until?->format('Y-m-d'),
377 'limit' => $limit,
378 'offset' => $offset,
379 ];
380 foreach ($this->records as $record) {
381 yield $record->infos();
382 }
383 }
384
385 public function getRecordCount(
386 ?\DateTimeImmutable $from = null,
387 ?\DateTimeImmutable $until = null
388 ): int {
389 return $this->record_count;
390 }
391 };
392 }
393
394 protected function getTokenHandler(
395 bool $valid_token = true,
396 ?RequestInterface $appended_request = null
398 return new class ($valid_token, $appended_request) extends NullTokenHandler {
399 public function __construct(
400 protected bool $valid_token,
401 protected ?RequestInterface $appended_request
402 ) {
403 }
404
405 public function generateToken(
406 int $offset,
407 ?\DateTimeImmutable $from_date,
408 ?\DateTimeImmutable $until_date
409 ): string {
410 return 'next_offset=' . $offset . ':' .
411 'from=' . $from_date?->format('Y-m-d') . ':' .
412 'until=' . $until_date?->format('Y-m-d');
413 }
414
415 public function isTokenValid(string $token): bool
416 {
417 return $this->valid_token;
418 }
419
420 public function appendArgumentsFromTokenToRequest(
421 RequestInterface $request,
422 string $token
423 ): RequestInterface {
424 return $this->appended_request;
425 }
426
427 public function getOffsetFromToken(string $token): int
428 {
429 return (int) str_replace('next_offset=', '', explode(':', $token)[0]);
430 }
431 };
432 }
433}
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
getRequest(string $base_url, Verb $verb, array $arguments_with_values, bool $correct_arguments=true)
Argument names are keys, their values values (all as strings)
getSettings(string $prefix='', string $repo_name='', string $contact_mail='')
getRepository(?string $earliest_datestamp=null, int $record_count=0, string ... $identifiers)
Append datestamps to identifiers with +YYYY-MM-DD.
getTokenHandler(bool $valid_token=true, ?RequestInterface $appended_request=null)
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
ilErrorHandling $error
Definition: class.ilias.php:69
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$url
Definition: shib_logout.php:68
$message
Definition: xapiexit.php:31
$token
Definition: xapitoken.php:70