ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
HandlerTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 use ILIAS\Data\URI;
35 
36 class HandlerTest extends TestCase
37 {
38  protected function getURI(string $string): URI
39  {
40  $url = $this->createMock(URI::class);
41  $url->method('__toString')->willReturn($string);
42  return $url;
43  }
44 
45  protected function getInitiator(
46  bool $activated,
47  string $content,
48  bool $processor_throws_exception = false,
49  bool $processor_throws_error = false,
50  bool $processor_triggers_error = false
52  return new class (
53  $activated,
54  $content,
55  $processor_throws_exception,
56  $processor_throws_error,
57  $processor_triggers_error
58  ) extends NullInitiator {
59  protected HTTPWrapperInterface $wrapper;
60 
61  public function __construct(
62  protected bool $activated,
63  protected string $content,
64  protected bool $processor_throws_exception,
65  protected bool $processor_throws_error,
66  protected bool $processor_triggers_error
67  ) {
68  $this->wrapper = new class () extends NullWrapper {
69  public array $exposed_responses = [];
70 
71  public function sendResponseAndClose(
72  int $status_code,
73  string $message = '',
74  ?\DOMDocument $body = null
75  ): void {
76  $this->exposed_responses[] = [
77  'status' => $status_code,
78  'message' => $message,
79  'body' => $body?->saveXML($body->documentElement)
80  ];
81  }
82  };
83  }
84 
85  public function settings(): SettingsInterface
86  {
87  return new class ($this->activated) extends NullSettings {
88  public function __construct(
89  protected bool $activated
90  ) {
91  }
92 
93  public function isOAIPMHActive(): bool
94  {
95  return $this->activated;
96  }
97  };
98  }
99 
100  public function requestParser(): RequestParserInterface
101  {
102  return new class ($this->content) extends NullParser {
103  public function __construct(protected string $content)
104  {
105  }
106 
107  public function parseFromHTTP(URI $base_url): RequestInterface
108  {
109  return new class ($this->content, $base_url) extends NullRequest {
110  public function __construct(
111  protected string $content,
112  protected URI $base_url
113  ) {
114  }
115 
116  public function baseURL(): URI
117  {
118  return $this->base_url;
119  }
120 
121  public function exposeContent(): string
122  {
123  return $this->content;
124  }
125  };
126  }
127  };
128  }
129 
130  public function requestProcessor(): RequestProcessorInterface
131  {
132  return new class (
133  $this->processor_throws_exception,
134  $this->processor_throws_error,
135  $this->processor_triggers_error
136  ) extends NullRequestProcessor {
137  public function __construct(
138  protected bool $throws_exception,
139  protected bool $throws_error,
140  protected bool $triggers_error
141  ) {
142  }
143 
144  public function getResponseToRequest(RequestInterface $request): \DomDocument
145  {
146  if ($this->throws_exception) {
147  throw new \ilMDOERExposerException('exception message');
148  }
149  if ($this->throws_error) {
150  throw new \Error('thrown error message');
151  }
152  if ($this->triggers_error) {
154  1 / 0;
155  }
156 
157  $url = (string) $request->baseURL();
158  $content = $request->exposeContent();
159  $doc = new \DOMDocument();
160  $doc->appendChild($doc->createElement('content', $url . '~!~' . $content));
161  return $doc;
162  }
163  };
164  }
165 
166  public function httpWrapper(): HTTPWrapperInterface
167  {
168  // must always be the same instance to make the expose work
169  return $this->wrapper;
170  }
171 
172  public function exposeHTTPResponses(): array
173  {
174  return $this->httpWrapper()->exposed_responses;
175  }
176  };
177  }
178 
179  protected function getHandler(
180  string $base_url,
181  InitiatorInterface $initiator
182  ): Handler {
183  $base_url = $this->getURI($base_url);
184  return new class ($initiator, $base_url) extends Handler {
185  public array $exposed_logged_errors = [];
186 
187  public function __construct(
188  protected InitiatorInterface $initiator,
189  protected readonly URI $base_url
190  ) {
191  }
192 
193  protected function logError(string $message): void
194  {
195  $this->exposed_logged_errors[] = $message;
196  }
197  };
198  }
199 
200  public function testSendResponseToRequestAvailable(): void
201  {
202  $initiator = $this->getInitiator(
203  true,
204  'some content'
205  );
206  $handler = $this->getHandler(
207  'some url',
208  $initiator
209  );
210 
211  $handler->sendResponseToRequest();
212 
213  $this->assertCount(1, $initiator->exposeHTTPResponses());
214  $this->assertEquals(
215  ['status' => 200, 'message' => '', 'body' => '<content>some url~!~some content</content>'],
216  $initiator->exposeHTTPResponses()[0] ?? []
217  );
218  }
219 
221  {
222  $initiator = $this->getInitiator(
223  false,
224  'some content'
225  );
226  $handler = $this->getHandler(
227  'some url',
228  $initiator
229  );
230 
231  $handler->sendResponseToRequest();
232 
233  $this->assertCount(1, $initiator->exposeHTTPResponses());
234  $this->assertEquals(
235  ['status' => 404, 'message' => '', 'body' => null],
236  $initiator->exposeHTTPResponses()[0] ?? []
237  );
238  }
239 
241  {
242  $initiator = $this->getInitiator(
243  true,
244  '',
245  true,
246  false,
247  false
248  );
249  $handler = $this->getHandler(
250  'some url',
251  $initiator
252  );
253 
254  $handler->sendResponseToRequest();
255 
256  $this->assertCount(1, $initiator->exposeHTTPResponses());
257  $this->assertEquals(
258  ['status' => 500, 'message' => 'exception message', 'body' => null],
259  $initiator->exposeHTTPResponses()[0] ?? []
260  );
261  $this->assertEquals(
262  ['exception message'],
263  $handler->exposed_logged_errors
264  );
265  }
266 
268  {
269  $initiator = $this->getInitiator(
270  true,
271  '',
272  false,
273  true,
274  false
275  );
276  $handler = $this->getHandler(
277  'some url',
278  $initiator
279  );
280 
281  $handler->sendResponseToRequest();
282 
283  $this->assertCount(1, $initiator->exposeHTTPResponses());
284  $this->assertEquals(
285  ['status' => 500, 'message' => 'thrown error message', 'body' => null],
286  $initiator->exposeHTTPResponses()[0] ?? []
287  );
288  $this->assertEquals(
289  ['thrown error message'],
290  $handler->exposed_logged_errors
291  );
292  }
293 
295  {
296  $initiator = $this->getInitiator(
297  true,
298  '',
299  false,
300  false,
301  true
302  );
303  $handler = $this->getHandler(
304  'some url',
305  $initiator
306  );
307 
308  $handler->sendResponseToRequest();
309 
310  $this->assertCount(1, $initiator->exposeHTTPResponses());
311  $this->assertEquals(
312  ['status' => 500, 'message' => 'Division by zero', 'body' => null],
313  $initiator->exposeHTTPResponses()[0] ?? []
314  );
315  $this->assertEquals(
316  ['Division by zero'],
317  $handler->exposed_logged_errors
318  );
319  }
320 }
getHandler(string $base_url, InitiatorInterface $initiator)
$url
Definition: shib_logout.php:68
getInitiator(bool $activated, string $content, bool $processor_throws_exception=false, bool $processor_throws_error=false, bool $processor_triggers_error=false)
Definition: HandlerTest.php:45
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
The OAI PMH interface does not use the usual internal services of the MetaData component, as it should be lightweight and decoupled from everything else as much as possible.
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:76
Processes OAI PMH requests according to https://www.openarchives.org/OAI/openarchivesprotocol.html.
$handler
Definition: oai.php:29
$message
Definition: xapiexit.php:31