3 use MediaWiki\Config\ServiceOptions
;
4 use MediaWiki\Http\HttpRequestFactory
;
5 use MediaWiki\MainConfigNames
;
6 use PHPUnit\Framework\Assert
;
7 use PHPUnit\Framework\AssertionFailedError
;
8 use Psr\Log\NullLogger
;
11 * A simple {@link HttpRequestFactory} implementation that can be used to prevent
12 * HTTP requests in tests. All attempts to create requests will fail.
14 * Use MockHttpTrait for creating a mock factory and controlling responses.
16 * @author Daniel Kinzler
17 * @license GPL-2.0-or-later
19 class NullHttpRequestFactory
extends HttpRequestFactory
{
21 public function __construct() {
22 $options = new ServiceOptions(
23 self
::CONSTRUCTOR_OPTIONS
, [
24 MainConfigNames
::HTTPTimeout
=> 1,
25 MainConfigNames
::HTTPConnectTimeout
=> 1,
26 MainConfigNames
::HTTPMaxTimeout
=> 2,
27 MainConfigNames
::HTTPMaxConnectTimeout
=> 2,
28 MainConfigNames
::LocalVirtualHosts
=> [],
29 MainConfigNames
::LocalHTTPProxy
=> false,
33 parent
::__construct( $options, new NullLogger() );
40 * @param array $options
41 * @param string $caller
43 * @throws AssertionFailedError always
45 public function create( $url, array $options = [], $caller = __METHOD__
) {
46 Assert
::fail( "HTTP request blocked: $url by $caller. Use MockHttpTrait." );
50 * Returns a NullMultiHttpClient that will fail to make any requests.
52 * @param array $options
54 * @return NullMultiHttpClient
56 public function createMultiClient( $options = [] ) {
57 return new NullMultiHttpClient( $options );
61 * @param array $config
63 * @return \GuzzleHttp\Client
65 public function createGuzzleClient( array $config = [] ): \GuzzleHttp\Client
{
66 return new NullGuzzleClient( $config );