wiki.techinc.nl/tests/phpunit/mocks/NullHttpRequestFactory.php
Tim Starling 0dbcf3362b Use mock Guzzle client during tests
Instead of failing when createGuzzleClient() is called, return a mock
which fails when a request is actually dispatched, following the
suggestion of the comment.

Otherwise PHPUnit fails to start up when RemoteIcuCollation and Shellbox
are configured.

Change-Id: Iac8d1e73512e7073264e79bbef8e4fa02fedefe2
2022-01-18 13:14:51 +11:00

68 lines
1.6 KiB
PHP

<?php
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Http\HttpRequestFactory;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\AssertionFailedError;
use Psr\Log\NullLogger;
/**
* A simple {@link HttpRequestFactory} implementation that can be used to prevent
* HTTP requests in tests. All attempts to create requests will fail.
*
* Use MockHttpTrait for creating a mock factory and controlling responses.
*
* @author Daniel Kinzler
* @license GPL-2.0-or-later
*/
class NullHttpRequestFactory extends HttpRequestFactory {
public function __construct() {
$options = new ServiceOptions(
self::CONSTRUCTOR_OPTIONS, [
'HTTPTimeout' => 1,
'HTTPConnectTimeout' => 1,
'HTTPMaxTimeout' => 2,
'HTTPMaxConnectTimeout' => 2,
'LocalVirtualHosts' => [],
'LocalHTTPProxy' => false,
]
);
parent::__construct( $options, new NullLogger() );
}
/**
* Always fails.
*
* @param string $url
* @param array $options
* @param string $caller
*
* @throws AssertionFailedError always
*/
public function create( $url, array $options = [], $caller = __METHOD__ ) {
Assert::fail( "HTTP request blocked: $url by $caller. Use MockHttpTrait." );
}
/**
* Returns a NullMultiHttpClient that will fail to make any requests.
*
* @param array $options
*
* @return NullMultiHttpClient
*/
public function createMultiClient( $options = [] ) {
return new NullMultiHttpClient( $options );
}
/**
* @param array $config
*
* @return \GuzzleHttp\Client
*/
public function createGuzzleClient( array $config = [] ): \GuzzleHttp\Client {
return new NullGuzzleClient( $config );
}
}