Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / tests / phpunit / includes / http / HttpRequestFactoryTest.php
1 <?php
2
3 use MediaWiki\Http\HttpRequestFactory;
4
5 /**
6 * @covers MediaWiki\Http\HttpRequestFactory
7 */
8 class HttpRequestFactoryTest extends MediaWikiTestCase {
9
10 /**
11 * @return HttpRequestFactory
12 */
13 private function newFactory() {
14 return new HttpRequestFactory();
15 }
16
17 /**
18 * @return HttpRequestFactory
19 */
20 private function newFactoryWithFakeRequest(
21 MWHttpRequest $req,
22 $expectedUrl,
23 $expectedOptions = []
24 ) {
25 $factory = $this->getMockBuilder( HttpRequestFactory::class )
26 ->setMethods( [ 'create' ] )
27 ->getMock();
28
29 $factory->method( 'create' )
30 ->willReturnCallback(
31 function ( $url, array $options = [], $caller = __METHOD__ )
32 use ( $req, $expectedUrl, $expectedOptions )
33 {
34 $this->assertSame( $url, $expectedUrl );
35
36 foreach ( $expectedOptions as $opt => $exp ) {
37 $this->assertArrayHasKey( $opt, $options );
38 $this->assertSame( $exp, $options[$opt] );
39 }
40
41 return $req;
42 }
43 );
44
45 return $factory;
46 }
47
48 /**
49 * @return MWHttpRequest
50 */
51 private function newFakeRequest( $result ) {
52 $req = $this->getMockBuilder( MWHttpRequest::class )
53 ->disableOriginalConstructor()
54 ->setMethods( [ 'getContent', 'execute' ] )
55 ->getMock();
56
57 if ( $result instanceof Status ) {
58 $req->method( 'getContent' )
59 ->willReturn( $result->getValue() );
60 $req->method( 'execute' )
61 ->willReturn( $result );
62 } else {
63 $req->method( 'getContent' )
64 ->willReturn( $result );
65 $req->method( 'execute' )
66 ->willReturn( Status::newGood( $result ) );
67 }
68
69 return $req;
70 }
71
72 public function testCreate() {
73 $factory = $this->newFactory();
74 $this->assertInstanceOf( 'MWHttpRequest', $factory->create( 'http://example.test' ) );
75 }
76
77 public function testGetUserAgent() {
78 $factory = $this->newFactory();
79 $this->assertStringStartsWith( 'MediaWiki/', $factory->getUserAgent() );
80 }
81
82 public function testGet() {
83 $req = $this->newFakeRequest( __METHOD__ );
84 $factory = $this->newFactoryWithFakeRequest(
85 $req, 'https://example.test', [ 'method' => 'GET' ]
86 );
87
88 $this->assertSame( __METHOD__, $factory->get( 'https://example.test' ) );
89 }
90
91 public function testPost() {
92 $req = $this->newFakeRequest( __METHOD__ );
93 $factory = $this->newFactoryWithFakeRequest(
94 $req, 'https://example.test', [ 'method' => 'POST' ]
95 );
96
97 $this->assertSame( __METHOD__, $factory->post( 'https://example.test' ) );
98 }
99
100 public function testRequest() {
101 $req = $this->newFakeRequest( __METHOD__ );
102 $factory = $this->newFactoryWithFakeRequest(
103 $req, 'https://example.test', [ 'method' => 'GET' ]
104 );
105
106 $this->assertSame( __METHOD__, $factory->request( 'GET', 'https://example.test' ) );
107 }
108
109 public function testRequest_failed() {
110 $status = Status::newFatal( 'testing' );
111 $req = $this->newFakeRequest( $status );
112 $factory = $this->newFactoryWithFakeRequest(
113 $req, 'https://example.test', [ 'method' => 'POST' ]
114 );
115
116 $this->assertNull( $factory->request( 'POST', 'https://example.test' ) );
117 }
118
119 }