Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / FauxRequestTest.php
blobe3713ab7fe72cb256bcf0e298fa4bb265e912bd7
1 <?php
3 class FauxRequestTest extends PHPUnit_Framework_TestCase {
4 /**
5 * @covers FauxRequest::__construct
6 */
7 public function testConstructInvalidData() {
8 $this->setExpectedException( MWException::class, 'bogus data' );
9 $req = new FauxRequest( 'x' );
12 /**
13 * @covers FauxRequest::__construct
15 public function testConstructInvalidSession() {
16 $this->setExpectedException( MWException::class, 'bogus session' );
17 $req = new FauxRequest( [], false, 'x' );
20 /**
21 * @covers FauxRequest::getText
23 public function testGetText() {
24 $req = new FauxRequest( [ 'x' => 'Value' ] );
25 $this->assertEquals( 'Value', $req->getText( 'x' ) );
26 $this->assertEquals( '', $req->getText( 'z' ) );
29 // Integration test for parent method.
30 public function testGetVal() {
31 $req = new FauxRequest( [ 'crlf' => "A\r\nb" ] );
32 $this->assertSame( "A\r\nb", $req->getVal( 'crlf' ), 'CRLF' );
35 // Integration test for parent method.
36 public function testGetRawVal() {
37 $req = new FauxRequest( [
38 'x' => 'Value',
39 'y' => [ 'a' ],
40 'crlf' => "A\r\nb"
41 ] );
42 $this->assertSame( 'Value', $req->getRawVal( 'x' ) );
43 $this->assertSame( null, $req->getRawVal( 'z' ), 'Not found' );
44 $this->assertSame( null, $req->getRawVal( 'y' ), 'Array is ignored' );
45 $this->assertSame( "A\r\nb", $req->getRawVal( 'crlf' ), 'CRLF' );
48 /**
49 * @covers FauxRequest::getValues
51 public function testGetValues() {
52 $values = [ 'x' => 'Value', 'y' => '' ];
53 $req = new FauxRequest( $values );
54 $this->assertEquals( $values, $req->getValues() );
57 /**
58 * @covers FauxRequest::getQueryValues
60 public function testGetQueryValues() {
61 $values = [ 'x' => 'Value', 'y' => '' ];
63 $req = new FauxRequest( $values );
64 $this->assertEquals( $values, $req->getQueryValues() );
65 $req = new FauxRequest( $values, /*wasPosted*/ true );
66 $this->assertEquals( [], $req->getQueryValues() );
69 /**
70 * @covers FauxRequest::getMethod
72 public function testGetMethod() {
73 $req = new FauxRequest( [] );
74 $this->assertEquals( 'GET', $req->getMethod() );
75 $req = new FauxRequest( [], /*wasPosted*/ true );
76 $this->assertEquals( 'POST', $req->getMethod() );
79 /**
80 * @covers FauxRequest::wasPosted
82 public function testWasPosted() {
83 $req = new FauxRequest( [] );
84 $this->assertFalse( $req->wasPosted() );
85 $req = new FauxRequest( [], /*wasPosted*/ true );
86 $this->assertTrue( $req->wasPosted() );
89 /**
90 * @covers FauxRequest::getCookie
91 * @covers FauxRequest::setCookie
92 * @covers FauxRequest::setCookies
94 public function testCookies() {
95 $req = new FauxRequest();
96 $this->assertSame( null, $req->getCookie( 'z', '' ) );
98 $req->setCookie( 'x', 'Value', '' );
99 $this->assertEquals( 'Value', $req->getCookie( 'x', '' ) );
101 $req->setCookies( [ 'x' => 'One', 'y' => 'Two' ], '' );
102 $this->assertEquals( 'One', $req->getCookie( 'x', '' ) );
103 $this->assertEquals( 'Two', $req->getCookie( 'y', '' ) );
107 * @covers FauxRequest::getCookie
108 * @covers FauxRequest::setCookie
109 * @covers FauxRequest::setCookies
111 public function testCookiesDefaultPrefix() {
112 global $wgCookiePrefix;
113 $oldPrefix = $wgCookiePrefix;
114 $wgCookiePrefix = '_';
116 $req = new FauxRequest();
117 $this->assertSame( null, $req->getCookie( 'z' ) );
119 $req->setCookie( 'x', 'Value' );
120 $this->assertEquals( 'Value', $req->getCookie( 'x' ) );
122 $wgCookiePrefix = $oldPrefix;
126 * @covers FauxRequest::getRequestURL
128 public function testGetRequestURL() {
129 $req = new FauxRequest();
130 $this->setExpectedException( MWException::class );
131 $req->getRequestURL();
135 * @covers FauxRequest::setRequestURL
136 * @covers FauxRequest::getRequestURL
138 public function testSetRequestURL() {
139 $req = new FauxRequest();
140 $req->setRequestURL( 'https://example.org' );
141 $this->assertEquals( 'https://example.org', $req->getRequestURL() );
145 * @covers FauxRequest::__construct
146 * @covers FauxRequest::getProtocol
148 public function testProtocol() {
149 $req = new FauxRequest();
150 $this->assertEquals( 'http', $req->getProtocol() );
151 $req = new FauxRequest( [], false, null, 'http' );
152 $this->assertEquals( 'http', $req->getProtocol() );
153 $req = new FauxRequest( [], false, null, 'https' );
154 $this->assertEquals( 'https', $req->getProtocol() );
158 * @covers FauxRequest::setHeader
159 * @covers FauxRequest::setHeaders
160 * @covers FauxRequest::getHeader
162 public function testGetSetHeader() {
163 $value = 'text/plain, text/html';
165 $request = new FauxRequest();
166 $request->setHeader( 'Accept', $value );
168 $this->assertEquals( $request->getHeader( 'Nonexistent' ), false );
169 $this->assertEquals( $request->getHeader( 'Accept' ), $value );
170 $this->assertEquals( $request->getHeader( 'ACCEPT' ), $value );
171 $this->assertEquals( $request->getHeader( 'accept' ), $value );
172 $this->assertEquals(
173 $request->getHeader( 'Accept', WebRequest::GETHEADER_LIST ),
174 [ 'text/plain', 'text/html' ]
179 * @covers FauxRequest::initHeaders
181 public function testGetAllHeaders() {
182 $_SERVER['HTTP_TEST'] = 'Example';
184 $request = new FauxRequest();
186 $this->assertEquals(
188 $request->getAllHeaders()
191 $this->assertEquals(
192 false,
193 $request->getHeader( 'test' )
198 * @covers FauxRequest::__construct
199 * @covers FauxRequest::getSessionArray
201 public function testSessionData() {
202 $values = [ 'x' => 'Value', 'y' => '' ];
204 $req = new FauxRequest( [], false, /*session*/ $values );
205 $this->assertEquals( $values, $req->getSessionArray() );
207 $req = new FauxRequest();
208 $this->assertSame( null, $req->getSessionArray() );
212 * @covers FauxRequest::getRawQueryString
213 * @covers FauxRequest::getRawPostString
214 * @covers FauxRequest::getRawInput
216 public function testDummies() {
217 $req = new FauxRequest();
218 $this->assertEquals( '', $req->getRawQueryString() );
219 $this->assertEquals( '', $req->getRawPostString() );
220 $this->assertEquals( '', $req->getRawInput() );