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