MediaWiki  master
ResponseFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Tests\Rest;
4 
9 
12  public static function provideEncodeJson() {
13  return [
14  [ (object)[], '{}' ],
15  [ '/', '"/"' ],
16  [ '£', '"£"' ],
17  [ [], '[]' ],
18  ];
19  }
20 
22  public function testEncodeJson( $input, $expected ) {
23  $rf = new ResponseFactory;
24  $this->assertSame( $expected, $rf->encodeJson( $input ) );
25  }
26 
27  public function testCreateJson() {
28  $rf = new ResponseFactory;
29  $response = $rf->createJson( [] );
30  $response->getBody()->rewind();
31  $this->assertSame( 'application/json', $response->getHeaderLine( 'Content-Type' ) );
32  $this->assertSame( '[]', $response->getBody()->getContents() );
33  // Make sure getSize() is functional, since testCreateNoContent() depends on it
34  $this->assertSame( 2, $response->getBody()->getSize() );
35  }
36 
37  public function testCreateNoContent() {
38  $rf = new ResponseFactory;
39  $response = $rf->createNoContent();
40  $this->assertSame( [], $response->getHeader( 'Content-Type' ) );
41  $this->assertSame( 0, $response->getBody()->getSize() );
42  $this->assertSame( 204, $response->getStatusCode() );
43  }
44 
45  public function testCreatePermanentRedirect() {
46  $rf = new ResponseFactory;
47  $response = $rf->createPermanentRedirect( 'http://www.example.com/' );
48  $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
49  $this->assertSame( 301, $response->getStatusCode() );
50  }
51 
53  $rf = new ResponseFactory;
54  $response = $rf->createLegacyTemporaryRedirect( 'http://www.example.com/' );
55  $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
56  $this->assertSame( 302, $response->getStatusCode() );
57  }
58 
59  public function testCreateTemporaryRedirect() {
60  $rf = new ResponseFactory;
61  $response = $rf->createTemporaryRedirect( 'http://www.example.com/' );
62  $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
63  $this->assertSame( 307, $response->getStatusCode() );
64  }
65 
66  public function testCreateSeeOther() {
67  $rf = new ResponseFactory;
68  $response = $rf->createSeeOther( 'http://www.example.com/' );
69  $this->assertSame( [ 'http://www.example.com/' ], $response->getHeader( 'Location' ) );
70  $this->assertSame( 303, $response->getStatusCode() );
71  }
72 
73  public function testCreateNotModified() {
74  $rf = new ResponseFactory;
75  $response = $rf->createNotModified();
76  $this->assertSame( 0, $response->getBody()->getSize() );
77  $this->assertSame( 304, $response->getStatusCode() );
78  }
79 
81  public function testCreateHttpErrorInvalid() {
82  $rf = new ResponseFactory;
83  $rf->createHttpError( 200 );
84  }
85 
86  public function testCreateHttpError() {
87  $rf = new ResponseFactory;
88  $response = $rf->createHttpError( 415, [ 'message' => '...' ] );
89  $this->assertSame( 415, $response->getStatusCode() );
90  $body = $response->getBody();
91  $body->rewind();
92  $data = json_decode( $body->getContents(), true );
93  $this->assertSame( 415, $data['httpCode'] );
94  $this->assertSame( '...', $data['message'] );
95  }
96 
97  public function testCreateFromExceptionUnlogged() {
98  $rf = new ResponseFactory;
99  $response = $rf->createFromException( new HttpException( 'hello', 415 ) );
100  $this->assertSame( 415, $response->getStatusCode() );
101  $body = $response->getBody();
102  $body->rewind();
103  $data = json_decode( $body->getContents(), true );
104  $this->assertSame( 415, $data['httpCode'] );
105  $this->assertSame( 'hello', $data['message'] );
106  }
107 
108  public function testCreateFromExceptionLogged() {
109  $rf = new ResponseFactory;
110  $response = $rf->createFromException( new \Exception( "hello", 415 ) );
111  $this->assertSame( 500, $response->getStatusCode() );
112  $body = $response->getBody();
113  $body->rewind();
114  $data = json_decode( $body->getContents(), true );
115  $this->assertSame( 500, $data['httpCode'] );
116  $this->assertSame( 'Error: exception of type Exception', $data['message'] );
117  }
118 
119  public static function provideCreateFromReturnValue() {
120  return [
121  [ 'hello', '{"value":"hello"}' ],
122  [ true, '{"value":true}' ],
123  [ [ 'x' => 'y' ], '{"x":"y"}' ],
124  [ [ 'x', 'y' ], '["x","y"]' ],
125  [ [ 'a', 'x' => 'y' ], '{"0":"a","x":"y"}' ],
126  [ (object)[ 'a', 'x' => 'y' ], '{"0":"a","x":"y"}' ],
127  [ [], '[]' ],
128  [ (object)[], '{}' ],
129  ];
130  }
131 
133  public function testCreateFromReturnValue( $input, $expected ) {
134  $rf = new ResponseFactory;
135  $response = $rf->createFromReturnValue( $input );
136  $body = $response->getBody();
137  $body->rewind();
138  $this->assertSame( $expected, $body->getContents() );
139  }
140 
143  $rf = new ResponseFactory;
145  }
146 }
testCreateFromReturnValue( $input, $expected)
provideCreateFromReturnValue
if(is_array( $mode)) switch( $mode) $input
\MediaWiki\Rest\ResponseFactory
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition: globals.txt:25
createHttpError( $errorCode, array $bodyData=[])
Create a HTTP 4xx or 5xx response.
This is the base exception class for non-fatal exceptions thrown from REST handlers.
Generates standardized response objects.
this hook is for auditing only $response
Definition: hooks.txt:767
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1971
createFromReturnValue( $value)
Create a JSON response from an arbitrary value.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
testCreateHttpErrorInvalid()
\InvalidArgumentException
testCreateFromReturnValueInvalid()
\InvalidArgumentException
testEncodeJson( $input, $expected)
provideEncodeJson