| 1 |
<?php |
| 2 |
|
| 3 |
namespace Test\Parsoid\Config\Api; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use Wikimedia\Parsoid\Config\Api\ApiHelper; |
| 7 |
use Wikimedia\Parsoid\Utils\PHPUtils; |
| 8 |
|
| 9 |
class TestApiHelper extends ApiHelper { |
| 10 |
|
| 11 |
/** @var TestCase */ |
| 12 |
private $test; |
| 13 |
|
| 14 |
/** @var array|null */ |
| 15 |
private $params; |
| 16 |
|
| 17 |
/** @var array|null */ |
| 18 |
private $ret; |
| 19 |
|
| 20 |
/** |
| 21 |
* @param TestCase $test |
| 22 |
* @param string $filename Data file name to use |
| 23 |
*/ |
| 24 |
public function __construct( TestCase $test, string $filename ) { |
| 25 |
$file = __DIR__ . '/data/' . $filename . '.reqdata'; |
| 26 |
$data = explode( "\n", trim( file_get_contents( $file ) ), 3 ); |
| 27 |
if ( count( $data ) !== 2 ) { |
| 28 |
throw new \InvalidArgumentException( "Bad request data file: $file" ); |
| 29 |
} |
| 30 |
|
| 31 |
$this->test = $test; |
| 32 |
$this->params = json_decode( $data[0], true ); |
| 33 |
$this->ret = json_decode( $data[1], true ); |
| 34 |
} |
| 35 |
|
| 36 |
/** @inheritDoc */ |
| 37 |
public function makeRequest( array $params ): array { |
| 38 |
if ( $this->ret === null ) { |
| 39 |
$this->test->fail( __METHOD__ . ' should only be called once' ); |
| 40 |
} |
| 41 |
$this->test->assertEquals( $this->params, $params ); |
| 42 |
|
| 43 |
$ret = $this->ret; |
| 44 |
$this->ret = null; |
| 45 |
return $ret; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Create the reqdata file |
| 50 |
* @param string $filename Data file to write |
| 51 |
* @param array $params API request parameters |
| 52 |
*/ |
| 53 |
public static function writeRequestFile( string $filename, array $params ): void { |
| 54 |
$out = PHPUtils::jsonEncode( $params ) . "\n"; |
| 55 |
|
| 56 |
$ch = curl_init( 'https://en.wikipedia.org/w/api.php' ); |
| 57 |
if ( !$ch ) { |
| 58 |
throw new \RuntimeException( "Failed to open curl handle" ); |
| 59 |
} |
| 60 |
|
| 61 |
$params['format'] = 'json'; |
| 62 |
$params['formatversion'] ??= '2'; |
| 63 |
|
| 64 |
$curlopt = [ |
| 65 |
CURLOPT_USERAGENT => 'ApiEnv/1.0 Parsoid-PHP/0.1', |
| 66 |
CURLOPT_CONNECTTIMEOUT => 60, |
| 67 |
CURLOPT_TIMEOUT => 60, |
| 68 |
CURLOPT_FOLLOWLOCATION => false, |
| 69 |
CURLOPT_ENCODING => '', // Enable compression |
| 70 |
CURLOPT_SAFE_UPLOAD => true, |
| 71 |
CURLOPT_RETURNTRANSFER => true, |
| 72 |
CURLOPT_POST => true, |
| 73 |
CURLOPT_POSTFIELDS => $params, |
| 74 |
]; |
| 75 |
if ( !curl_setopt_array( $ch, $curlopt ) ) { |
| 76 |
throw new \RuntimeException( "Error setting curl options: " . curl_error( $ch ) ); |
| 77 |
} |
| 78 |
|
| 79 |
$res = curl_exec( $ch ); |
| 80 |
|
| 81 |
if ( curl_errno( $ch ) !== 0 ) { |
| 82 |
throw new \RuntimeException( "HTTP request failed: " . curl_error( $ch ) ); |
| 83 |
} |
| 84 |
|
| 85 |
$code = curl_getinfo( $ch, CURLINFO_RESPONSE_CODE ); |
| 86 |
if ( $code !== 200 ) { |
| 87 |
throw new \RuntimeException( "HTTP request failed: HTTP code $code" ); |
| 88 |
} |
| 89 |
|
| 90 |
if ( !$res ) { |
| 91 |
throw new \RuntimeException( "HTTP request failed: Empty response" ); |
| 92 |
} |
| 93 |
|
| 94 |
$data = json_decode( $res, true ); |
| 95 |
if ( !is_array( $data ) ) { |
| 96 |
throw new \RuntimeException( "HTTP request failed: Response was not a JSON array" ); |
| 97 |
} |
| 98 |
|
| 99 |
if ( isset( $data['error'] ) ) { |
| 100 |
$e = $data['error']; |
| 101 |
throw new \RuntimeException( "MediaWiki API error: [{$e['code']}] {$e['info']}" ); |
| 102 |
} |
| 103 |
|
| 104 |
$out .= rtrim( $res ) . "\n"; |
| 105 |
$file = __DIR__ . '/data/' . $filename . '.reqdata'; |
| 106 |
file_put_contents( $file, $out ); |
| 107 |
} |
| 108 |
|
| 109 |
} |