This moves a code out of file scope into classes to make it testable. The code is left in the same structure as it was before, global functions have been converted into methods on the new ThumbnailEntryPoint and Thumbnail404EntryPoint classes. This test introduces comprehensive phpunit tests covering all functional code paths in ThumbnailEntryPoint. This is intended to support refactoring of this code. Change-Id: I459abc7b11d0ab4ee682a863c9525a945048296f
153 lines
3.7 KiB
PHP
153 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Tests;
|
|
|
|
use Exception;
|
|
use HashConfig;
|
|
use MediaWiki\Context\RequestContext;
|
|
use MediaWiki\EntryPointEnvironment;
|
|
use MediaWiki\Request\FauxRequest;
|
|
use MediaWiki\Request\FauxResponse;
|
|
use MultiConfig;
|
|
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 );
|
|
}
|
|
|
|
}
|