ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
HandlerTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
35
36class 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 getHTTPWrapper(): HTTPWrapperInterface
46 {
47 return new class () extends NullWrapper {
48 public array $exposed_responses = [];
49
50 public function sendResponseAndClose(
51 int $status_code,
52 string $message = '',
53 ?\DOMDocument $body = null
54 ): void {
55 $this->exposed_responses[] = [
56 'status' => $status_code,
57 'message' => $message,
58 'body' => $body?->saveXML($body->documentElement)
59 ];
60 }
61 };
62 }
63
64 protected function getSettings(bool $activated): SettingsInterface
65 {
66 return new class ($activated) extends NullSettings {
67 public function __construct(
68 protected bool $activated
69 ) {
70 }
71
72 public function isOAIPMHActive(): bool
73 {
74 return $this->activated;
75 }
76 };
77 }
78
79 protected function getRequestParser(string $content): RequestParserInterface
80 {
81 return new class ($content) extends NullParser {
82 public function __construct(protected string $content)
83 {
84 }
85
86 public function parseFromHTTP(URI $base_url): RequestInterface
87 {
88 return new class ($this->content, $base_url) extends NullRequest {
89 public function __construct(
90 protected string $content,
91 protected URI $base_url
92 ) {
93 }
94
95 public function baseURL(): URI
96 {
97 return $this->base_url;
98 }
99
100 public function exposeContent(): string
101 {
102 return $this->content;
103 }
104 };
105 }
106 };
107 }
108
109 protected function getRequestProcessor(
110 bool $throws_exception = false,
111 bool $throws_error = false,
112 bool $triggers_error = false
114 return new class (
115 $throws_exception,
116 $throws_error,
117 $triggers_error
118 ) extends NullRequestProcessor {
119 public function __construct(
120 protected bool $throws_exception,
121 protected bool $throws_error,
122 protected bool $triggers_error
123 ) {
124 }
125
126 public function getResponseToRequest(RequestInterface $request): \DomDocument
127 {
128 if ($this->throws_exception) {
129 throw new \ilMDOERExposerException('exception message');
130 }
131 if ($this->throws_error) {
132 throw new \Error('thrown error message');
133 }
134 if ($this->triggers_error) {
137 1 / 0;
138 }
139
140 $url = (string) $request->baseURL();
141 $content = $request->exposeContent();
142 $doc = new \DOMDocument();
143 $doc->appendChild($doc->createElement('content', $url . '~!~' . $content));
144 return $doc;
145 }
146 };
147 }
148
149 protected function getHandler(
150 string $base_url,
151 SettingsInterface $settings,
152 HTTPWrapperInterface $http_wrapper,
153 RequestParserInterface $request_parser,
154 RequestProcessorInterface $request_processor,
155 ): Handler {
156 $base_url = $this->getURI($base_url);
157 return new class (
158 $settings,
159 $http_wrapper,
160 $request_parser,
161 $request_processor,
162 $base_url
163 ) extends Handler {
164 public array $exposed_logged_errors = [];
165
166 public function __construct(
167 protected SettingsInterface $settings,
168 protected HTTPWrapperInterface $http_wrapper,
169 protected RequestParserInterface $request_parser,
170 protected RequestProcessorInterface $request_processor,
171 protected readonly URI $base_url
172 ) {
173 }
174
175 protected function logError(string $message): void
176 {
177 $this->exposed_logged_errors[] = $message;
178 }
179 };
180 }
181
183 {
184 $handler = $this->getHandler(
185 'some url',
186 $this->getSettings(true),
187 $wrapper = $this->getHTTPWrapper(),
188 $this->getRequestParser('some content'),
189 $this->getRequestProcessor()
190 );
191
192 $handler->sendResponseToRequest();
193
194 $this->assertCount(1, $wrapper->exposed_responses);
195 $this->assertEquals(
196 ['status' => 200, 'message' => '', 'body' => '<content>some url~!~some content</content>'],
197 $wrapper->exposed_responses[0] ?? []
198 );
199 }
200
202 {
203 $handler = $this->getHandler(
204 'some url',
205 $this->getSettings(false),
206 $wrapper = $this->getHTTPWrapper(),
207 $this->getRequestParser('some content'),
208 $this->getRequestProcessor()
209 );
210
211 $handler->sendResponseToRequest();
212
213 $this->assertCount(1, $wrapper->exposed_responses);
214 $this->assertEquals(
215 ['status' => 404, 'message' => '', 'body' => null],
216 $wrapper->exposed_responses[0] ?? []
217 );
218 }
219
221 {
222 $handler = $this->getHandler(
223 'some url',
224 $this->getSettings(true),
225 $wrapper = $this->getHTTPWrapper(),
226 $this->getRequestParser(''),
227 $this->getRequestProcessor(true)
228 );
229
230 $handler->sendResponseToRequest();
231
232 $this->assertCount(1, $wrapper->exposed_responses);
233 $this->assertEquals(
234 ['status' => 500, 'message' => 'exception message', 'body' => null],
235 $wrapper->exposed_responses[0] ?? []
236 );
237 $this->assertEquals(
238 ['exception message'],
239 $handler->exposed_logged_errors
240 );
241 }
242
244 {
245 $handler = $this->getHandler(
246 'some url',
247 $this->getSettings(true),
248 $wrapper = $this->getHTTPWrapper(),
249 $this->getRequestParser(''),
250 $this->getRequestProcessor(false, true)
251 );
252
253 $handler->sendResponseToRequest();
254
255 $this->assertCount(1, $wrapper->exposed_responses);
256 $this->assertEquals(
257 ['status' => 500, 'message' => 'thrown error message', 'body' => null],
258 $wrapper->exposed_responses[0] ?? []
259 );
260 $this->assertEquals(
261 ['thrown error message'],
262 $handler->exposed_logged_errors
263 );
264 }
265
267 {
268 $handler = $this->getHandler(
269 'some url',
270 $this->getSettings(true),
271 $wrapper = $this->getHTTPWrapper(),
272 $this->getRequestParser(''),
273 $this->getRequestProcessor(false, false, true)
274 );
275
276 $handler->sendResponseToRequest();
277
278 $this->assertCount(1, $wrapper->exposed_responses);
279 $this->assertEquals(
280 ['status' => 500, 'message' => 'Division by zero', 'body' => null],
281 $wrapper->exposed_responses[0] ?? []
282 );
283 $this->assertEquals(
284 ['Division by zero'],
285 $handler->exposed_logged_errors
286 );
287 }
288}
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
getRequestProcessor(bool $throws_exception=false, bool $throws_error=false, bool $triggers_error=false)
getHandler(string $base_url, SettingsInterface $settings, HTTPWrapperInterface $http_wrapper, RequestParserInterface $request_parser, RequestProcessorInterface $request_processor,)
__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...
Processes OAI PMH requests according to https://www.openarchives.org/OAI/openarchivesprotocol....
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
if( $request_wrapper->has( 'ui_mainbar')) getURI()
Definition: ui_mainbar.php:301
$handler
Definition: oai.php:31
$url
Definition: shib_logout.php:70