Debian Codegraph @

tests/phpunit/mocks/MockEnvironment.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests;
4
5 use Exception;
6 use MediaWiki\Config\HashConfig;
7 use MediaWiki\Config\MultiConfig;
8 use MediaWiki\Context\RequestContext;
9 use MediaWiki\EntryPointEnvironment;
10 use MediaWiki\Request\FauxRequest;
11 use MediaWiki\Request\FauxResponse;
12 use PHPUnit\Framework\Assert;
13
14 /**
15 * @internal For testing MediaWikiEntryPoint subclasses.
16 * Should be revised before wider use.
17 */
18 class MockEnvironment extends EntryPointEnvironment {
19
20 public const MOCK_REQUEST_URL = '/just/a/test';
21
22 private ?FauxRequest $request = null;
23
24 private array $serverInfo = [];
25
26 public function __construct( ?FauxRequest $request = null ) {
27 if ( $request ) {
28 if ( !$request->hasRequestURL() ) {
29 $request->setRequestURL( self::MOCK_REQUEST_URL );
30 }
31
32 // Note that setRequestInfo() will reset $this->request to null
33 $this->setRequestInfo(
34 $request->getRequestURL(),
35 $request->getQueryValuesOnly(),
36 $request->getMethod()
37 );
38 }
39
40 $this->request = $request;
41 }
42
43 public function setRequestInfo( string $requestUrl, $params = '', $method = 'GET' ) {
44 $this->request = null;
45
46 $this->setServerInfo(
47 'REQUEST_URI',
48 $requestUrl
49 );
50 $this->setServerInfo(
51 'REQUEST_METHOD',
52 $method
53 );
54 $this->setServerInfo(
55 'QUERY_STRING',
56 is_string( $params ) ? $params : wfArrayToCgi( $params )
57 );
58 }
59
60 public function getFauxRequest(): FauxRequest {
61 if ( !$this->request ) {
62 $data = wfCgiToArray( $this->getServerInfo( 'QUERY_STRING', '' ) );
63 $wasPosted = $this->getServerInfo( 'REQUEST_METHOD', 'GET' ) === 'POST';
64 $requestUrl = $this->getServerInfo( 'REQUEST_URI' ) ?? self::MOCK_REQUEST_URL;
65
66 $request = new FauxRequest( $data, $wasPosted );
67 $request->setServerInfo( $this->serverInfo );
68 $request->setRequestURL( $requestUrl );
69
70 // This adds a virtual 'title' query parameter. Normally called from Setup.php
71 $request->interpolateTitle();
72 $this->request = $request;
73 }
74
75 return $this->request;
76 }
77
78 public function getFauxResponse(): FauxResponse {
79 return $this->getFauxRequest()->response();
80 }
81
82 public function makeFauxContext( array $config = [] ): RequestContext {
83 $context = new RequestContext();
84 $context->setRequest( $this->getFauxRequest() );
85 $context->setLanguage( 'qqx' );
86 $context->setConfig( new MultiConfig( [
87 new HashConfig( $config ),
88 $context->getConfig()
89 ] ) );
90
91 return $context;
92 }
93
94 public function isCli(): bool {
95 return false;
96 }
97
98 public function hasFastCgi(): bool {
99 return true;
100 }
101
102 public function fastCgiFinishRequest(): bool {
103 return true;
104 }
105
106 public function setServerInfo( string $key, $value ) {
107 $this->serverInfo[$key] = $value;
108 }
109
110 public function getServerInfo( string $key, $default = null ) {
111 return $this->serverInfo[$key] ?? $default;
112 }
113
114 public function exit( int $code = 0 ) {
115 throw new Exception( $code );
116 }
117
118 public function disableModDeflate(): void {
119 // no-op
120 }
121
122 public function getEnv( string $name ) {
123 // Implement when needed.
124 return false;
125 }
126
127 public function getIni( string $name ) {
128 // Implement when needed.
129 return false;
130 }
131
132 public function setIniOption( string $name, $value ) {
133 // Implement when needed.
134 return false;
135 }
136
137 public function assertStatusCode( int $expected, $message = null ) {
138 $message ??= "HTTP status";
139 $code = $this->getFauxResponse()->getStatusCode() ?? 200;
140 Assert::assertSame( $expected, $code, $message );
141 }
142
143 public function assertHeaderValue( ?string $expected, string $name, $message = null ) {
144 $message ??= "$name header";
145 Assert::assertSame( $expected, $this->getFauxResponse()->getHeader( $name ), $message );
146 }
147
148 public function assertCookieValue( ?string $expected, string $name, $message = null ) {
149 $message ??= "$name header";
150 Assert::assertSame( $expected, $this->getFauxResponse()->getCookie( $name ), $message );
151 }
152
153 }