3 namespace MediaWiki\Tests
;
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
;
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 ) {
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(),
40 $this->request
= $request;
43 public function setRequestInfo( string $requestUrl, $params = '', $method = 'GET' ) {
44 $this->request
= null;
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 ),
94 public function isCli(): bool {
98 public function hasFastCgi(): bool {
102 public function fastCgiFinishRequest(): bool {
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
{
122 public function getEnv( string $name ) {
123 // Implement when needed.
127 public function getIni( string $name ) {
128 // Implement when needed.
132 public function setIniOption( string $name, $value ) {
133 // Implement when needed.
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 );