Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / mocks / MockEnvironment.php
blob1b0190f98afffbb3a17c110ab5125590a5858156
1 <?php
3 namespace MediaWiki\Tests;
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;
14 /**
15 * @internal For testing MediaWikiEntryPoint subclasses.
16 * Should be revised before wider use.
18 class MockEnvironment extends EntryPointEnvironment {
20 public const MOCK_REQUEST_URL = '/just/a/test';
22 private ?FauxRequest $request = null;
24 private array $serverInfo = [];
26 public function __construct( ?FauxRequest $request = null ) {
27 if ( $request ) {
28 if ( !$request->hasRequestURL() ) {
29 $request->setRequestURL( self::MOCK_REQUEST_URL );
32 // Note that setRequestInfo() will reset $this->request to null
33 $this->setRequestInfo(
34 $request->getRequestURL(),
35 $request->getQueryValuesOnly(),
36 $request->getMethod()
40 $this->request = $request;
43 public function setRequestInfo( string $requestUrl, $params = '', $method = 'GET' ) {
44 $this->request = null;
46 $this->setServerInfo(
47 'REQUEST_URI',
48 $requestUrl
50 $this->setServerInfo(
51 'REQUEST_METHOD',
52 $method
54 $this->setServerInfo(
55 'QUERY_STRING',
56 is_string( $params ) ? $params : wfArrayToCgi( $params )
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;
66 $request = new FauxRequest( $data, $wasPosted );
67 $request->setServerInfo( $this->serverInfo );
68 $request->setRequestURL( $requestUrl );
70 // This adds a virtual 'title' query parameter. Normally called from Setup.php
71 $request->interpolateTitle();
72 $this->request = $request;
75 return $this->request;
78 public function getFauxResponse(): FauxResponse {
79 return $this->getFauxRequest()->response();
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 ] ) );
91 return $context;
94 public function isCli(): bool {
95 return false;
98 public function hasFastCgi(): bool {
99 return true;
102 public function fastCgiFinishRequest(): bool {
103 return true;
106 public function setServerInfo( string $key, $value ) {
107 $this->serverInfo[$key] = $value;
110 public function getServerInfo( string $key, $default = null ) {
111 return $this->serverInfo[$key] ?? $default;
114 public function exit( int $code = 0 ) {
115 throw new Exception( $code );
118 public function disableModDeflate(): void {
119 // no-op
122 public function getEnv( string $name ) {
123 // Implement when needed.
124 return false;
127 public function getIni( string $name ) {
128 // Implement when needed.
129 return false;
132 public function setIniOption( string $name, $value ) {
133 // Implement when needed.
134 return false;
137 public function assertStatusCode( int $expected, $message = null ) {
138 $message ??= "HTTP status";
139 $code = $this->getFauxResponse()->getStatusCode() ?? 200;
140 Assert::assertSame( $expected, $code, $message );
143 public function assertHeaderValue( ?string $expected, string $name, $message = null ) {
144 $message ??= "$name header";
145 Assert::assertSame( $expected, $this->getFauxResponse()->getHeader( $name ), $message );
148 public function assertCookieValue( ?string $expected, string $name, $message = null ) {
149 $message ??= "$name header";
150 Assert::assertSame( $expected, $this->getFauxResponse()->getCookie( $name ), $message );