Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / Rest / Handler / HelloHandlerTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Rest\Handler;
4
5 use EmptyBagOStuff;
6 use GuzzleHttp\Psr7\Uri;
7 use MediaWiki\Rest\RequestData;
8 use MediaWiki\Rest\ResponseFactory;
9 use MediaWiki\Rest\Router;
10
11 /**
12 * @covers \MediaWiki\Rest\Handler\HelloHandler
13 */
14 class HelloHandlerTest extends \MediaWikiUnitTestCase {
15 public static function provideTestViaRouter() {
16 return [
17 'normal' => [
18 [
19 'method' => 'GET',
20 'uri' => self::makeUri( '/user/Tim/hello' ),
21 ],
22 [
23 'statusCode' => 200,
24 'reasonPhrase' => 'OK',
25 'protocolVersion' => '1.1',
26 'body' => '{"message":"Hello, Tim!"}',
27 ],
28 ],
29 'method not allowed' => [
30 [
31 'method' => 'POST',
32 'uri' => self::makeUri( '/user/Tim/hello' ),
33 ],
34 [
35 'statusCode' => 405,
36 'reasonPhrase' => 'Method Not Allowed',
37 'protocolVersion' => '1.1',
38 'body' => '{"httpCode":405,"httpReason":"Method Not Allowed"}',
39 ],
40 ],
41 ];
42 }
43
44 private static function makeUri( $path ) {
45 return new Uri( "http://www.example.com/rest$path" );
46 }
47
48 /** @dataProvider provideTestViaRouter */
49 public function testViaRouter( $requestInfo, $responseInfo ) {
50 $router = new Router(
51 [ __DIR__ . '/../testRoutes.json' ],
52 [],
53 '/rest',
54 new EmptyBagOStuff(),
55 new ResponseFactory() );
56 $request = new RequestData( $requestInfo );
57 $response = $router->execute( $request );
58 if ( isset( $responseInfo['statusCode'] ) ) {
59 $this->assertSame( $responseInfo['statusCode'], $response->getStatusCode() );
60 }
61 if ( isset( $responseInfo['reasonPhrase'] ) ) {
62 $this->assertSame( $responseInfo['reasonPhrase'], $response->getReasonPhrase() );
63 }
64 if ( isset( $responseInfo['protocolVersion'] ) ) {
65 $this->assertSame( $responseInfo['protocolVersion'], $response->getProtocolVersion() );
66 }
67 if ( isset( $responseInfo['body'] ) ) {
68 $this->assertSame( $responseInfo['body'], $response->getBody()->getContents() );
69 }
70 $this->assertSame(
71 [],
72 array_diff( array_keys( $responseInfo ), [
73 'statusCode',
74 'reasonPhrase',
75 'protocolVersion',
76 'body'
77 ] ),
78 '$responseInfo may not contain unknown keys' );
79 }
80 }