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