3 namespace MediaWiki\Tests\Rest\Handler
;
6 use GuzzleHttp\Psr7\Uri
;
7 use MediaWiki\Permissions\PermissionManager
;
8 use MediaWiki\Rest\BasicAccess\StaticBasicAuthorizer
;
9 use MediaWiki\Rest\RequestData
;
10 use MediaWiki\Rest\ResponseFactory
;
11 use MediaWiki\Rest\Router
;
12 use MediaWiki\Rest\Validator\Validator
;
13 use Psr\Container\ContainerInterface
;
14 use Wikimedia\ObjectFactory
;
18 * @covers \MediaWiki\Rest\Handler\HelloHandler
20 class HelloHandlerTest
extends \MediaWikiUnitTestCase
{
21 public static function provideTestViaRouter() {
26 'uri' => self
::makeUri( '/user/Tim/hello' ),
30 'reasonPhrase' => 'OK',
31 'protocolVersion' => '1.1',
32 'body' => '{"message":"Hello, Tim!"}',
35 'method not allowed' => [
38 'uri' => self
::makeUri( '/user/Tim/hello' ),
42 'reasonPhrase' => 'Method Not Allowed',
43 'protocolVersion' => '1.1',
44 'body' => '{"httpCode":405,"httpReason":"Method Not Allowed"}',
50 private static function makeUri( $path ) {
51 return new Uri( "http://www.example.com/rest$path" );
54 /** @dataProvider provideTestViaRouter */
55 public function testViaRouter( $requestInfo, $responseInfo ) {
56 $objectFactory = new ObjectFactory(
57 $this->getMockForAbstractClass( ContainerInterface
::class )
59 $permissionManager = $this->createMock( PermissionManager
::class );
60 $request = new RequestData( $requestInfo );
62 [ __DIR__
. '/../testRoutes.json' ],
66 new ResponseFactory( [] ),
67 new StaticBasicAuthorizer(),
69 new Validator( $objectFactory, $permissionManager, $request, new User
)
71 $response = $router->execute( $request );
72 if ( isset( $responseInfo['statusCode'] ) ) {
73 $this->assertSame( $responseInfo['statusCode'], $response->getStatusCode() );
75 if ( isset( $responseInfo['reasonPhrase'] ) ) {
76 $this->assertSame( $responseInfo['reasonPhrase'], $response->getReasonPhrase() );
78 if ( isset( $responseInfo['protocolVersion'] ) ) {
79 $this->assertSame( $responseInfo['protocolVersion'], $response->getProtocolVersion() );
81 if ( isset( $responseInfo['body'] ) ) {
82 $this->assertSame( $responseInfo['body'], $response->getBody()->getContents() );
86 array_diff( array_keys( $responseInfo ), [
92 '$responseInfo may not contain unknown keys' );