Debian Codegraph @

tests/phpunit/unit/includes/Rest/Handler/EchoHandler.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Rest\Handler;
4
5 use LogicException;
6 use MediaWiki\Rest\Handler;
7 use MediaWiki\Rest\RequestInterface;
8 use Wikimedia\ParamValidator\ParamValidator;
9
10 /**
11 * Test mock for asserting parameter processing and validation in Handler and Router.
12 */
13 class EchoHandler extends Handler {
14 /** @var bool */
15 private $postValidationSetupCalled = false;
16
17 public function execute() {
18 if ( !$this->postValidationSetupCalled ) {
19 throw new LogicException( 'postValidationSetup was not called' );
20 }
21
22 $request = $this->getRequest();
23 return [
24 'method' => $request->getMethod(),
25 'uri' => $request->getUri(),
26 'protocolVersion' => $request->getProtocolVersion(),
27 'body' => $request->getBody()->getContents(),
28 'serverParams' => $request->getServerParams(),
29 'cookieParams' => $request->getCookieParams(),
30 'queryParams' => $request->getQueryParams(),
31 'postParams' => $request->getPostParams(),
32 'pathParams' => $request->getPathParams(),
33 'headers' => $request->getHeaders(),
34 'parsedBody' => $request->getParsedBody(),
35 'validatedBody' => $this->getValidatedBody(),
36 'validatedParams' => $this->getValidatedParams(),
37 ];
38 }
39
40 public function getBodyParamSettings(): array {
41 if ( $this->getConfig()['postParam'] ?? false ) {
42 // Don't mix "post" and "body" params, it confuses
43 // Validator::detectExtraneousBodyFields()
44 return [];
45 }
46
47 return [
48 'bodyParam' => [
49 self::PARAM_SOURCE => 'body',
50 ParamValidator::PARAM_TYPE => 'string',
51 ParamValidator::PARAM_REQUIRED => false
52 ],
53 ];
54 }
55
56 public function getParamSettings() {
57 $paramSettings = [
58 'q' => [
59 self::PARAM_SOURCE => 'query',
60 ParamValidator::PARAM_TYPE => 'string',
61 ],
62 'pathParam' => [
63 self::PARAM_SOURCE => 'path',
64 ParamValidator::PARAM_TYPE => 'string',
65 ],
66 ];
67
68 if ( $this->getConfig()['postParam'] ?? false ) {
69 // Deprecated, will trigger a warning!
70 $paramSettings['postParam'] = [
71 self::PARAM_SOURCE => 'post',
72 ParamValidator::PARAM_TYPE => 'string',
73 ];
74 }
75
76 return $paramSettings;
77 }
78
79 protected function postValidationSetup() {
80 $this->postValidationSetupCalled = true;
81 }
82
83 public function getSupportedRequestTypes(): array {
84 return [
85 RequestInterface::JSON_CONTENT_TYPE,
86 RequestInterface::FORM_URLENCODED_CONTENT_TYPE,
87 RequestInterface::MULTIPART_FORM_DATA_CONTENT_TYPE,
88 ];
89 }
90
91 public function needsWriteAccess() {
92 return false;
93 }
94 }