| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\Tests; |
| 4 |
|
| 5 |
use MockHttpTrait; |
| 6 |
use PHPUnit\Framework\MockObject\MockObject; |
| 7 |
use Wikimedia\Http\MultiHttpClient; |
| 8 |
|
| 9 |
trait MathMockHttpTrait { |
| 10 |
use MockHttpTrait; |
| 11 |
|
| 12 |
protected function setupBadMathRestBaseMockHttp() { |
| 13 |
$this->installMockHttp( |
| 14 |
$this->makeFakeHttpMultiClient( [ [ |
| 15 |
'code' => 400, |
| 16 |
'body' => file_get_contents( __DIR__ . '/InputCheck/data/restbase/fail.json' ), |
| 17 |
] ] ) |
| 18 |
); |
| 19 |
} |
| 20 |
|
| 21 |
protected function setupSyntaxErrorRestBaseMockHttp() { |
| 22 |
$this->installMockHttp( |
| 23 |
$this->makeFakeHttpMultiClient( [ [ |
| 24 |
'code' => 400, |
| 25 |
'body' => file_get_contents( __DIR__ . '/InputCheck/data/restbase/syntax_error.json' ), |
| 26 |
] ] ) |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
protected function setupGoodMathRestBaseMockHttp( bool $withSvg = false ) { |
| 31 |
$requests = [ |
| 32 |
$this->makeFakeHttpMultiClient( [ [ |
| 33 |
'code' => 200, |
| 34 |
'headers' => [ |
| 35 |
'x-resource-location' => 'RESOURCE_LOCATION', |
| 36 |
], |
| 37 |
'body' => file_get_contents( __DIR__ . '/InputCheck/data/restbase/sinx.json' ), |
| 38 |
] ] ), |
| 39 |
$this->makeFakeHttpMultiClient( [ file_get_contents( __DIR__ . '/data/restbase/sinx.mml' ), ] ), |
| 40 |
]; |
| 41 |
if ( $withSvg ) { |
| 42 |
$requests[] = $this->makeFakeHttpMultiClient( [ 'SVGSVSVG', ] ); |
| 43 |
} |
| 44 |
$this->installMockHttp( $requests ); |
| 45 |
} |
| 46 |
|
| 47 |
protected function setupGoodChemRestBaseMockHttp() { |
| 48 |
$this->installMockHttp( [ |
| 49 |
$this->makeFakeHttpMultiClient( [ [ |
| 50 |
'code' => 200, |
| 51 |
'headers' => [ |
| 52 |
'x-resource-location' => 'RESOURCE_LOCATION', |
| 53 |
], |
| 54 |
'body' => file_get_contents( __DIR__ . '/InputCheck/data/restbase/chem.json' ), |
| 55 |
] ] ), |
| 56 |
$this->makeFakeHttpMultiClient( [ file_get_contents( __DIR__ . '/data/restbase/h2o.mml' ), ] ), |
| 57 |
] ); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
protected function expectMathRestBaseMockHttpRequest( array $expectedList, array $responseList ) { |
| 66 |
|
| 67 |
$mockHttpRequestMulti = $this->createNoOpMock( |
| 68 |
MultiHttpClient::class, |
| 69 |
[ 'run', 'runMulti' ] |
| 70 |
); |
| 71 |
|
| 72 |
$mockHttpRequestMulti->method( 'run' )->willReturnCallback( |
| 73 |
static function ( array $req, array $opts = [] ) use ( $mockHttpRequestMulti ) { |
| 74 |
return $mockHttpRequestMulti->runMulti( [ $req ], $opts )[0]['response']; |
| 75 |
} |
| 76 |
); |
| 77 |
|
| 78 |
$mockHttpRequestMulti->method( 'runMulti' ) |
| 79 |
->willReturnCallback( function ( array $reqs, array $opts = [] ) use ( &$expectedList, $responseList ) { |
| 80 |
|
| 81 |
foreach ( $reqs as $i => $req ) { |
| 82 |
$expected = array_shift( $expectedList ); |
| 83 |
$response = array_shift( $responseList ); |
| 84 |
|
| 85 |
if ( $expected ) { |
| 86 |
|
| 87 |
foreach ( $expected as $k => $v ) { |
| 88 |
$this->assertSame( $v, $req[$k], "Request field $k" ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
$reqs[$i]['response'] = [ 'code' => 200, ] + $response; |
| 93 |
} |
| 94 |
|
| 95 |
return $reqs; |
| 96 |
} ); |
| 97 |
|
| 98 |
$this->installMockHttp( $mockHttpRequestMulti ); |
| 99 |
} |
| 100 |
} |