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