Merge "RCFilters UI: Add popup footer with feedback link"
[lhc/web/wiklou.git] / tests / phpunit / includes / objectcache / RESTBagOStuffTest.php
1 <?php
2 /**
3 * @group BagOStuff
4 */
5 class RESTBagOStuffTest extends MediaWikiTestCase {
6
7 /**
8 * @var MultiHttpClient
9 */
10 private $client;
11 /**
12 * @var RESTBagOStuff
13 */
14 private $bag;
15
16 public function setUp() {
17 parent::setUp();
18 $this->client =
19 $this->getMockBuilder( 'MultiHttpClient' )
20 ->setConstructorArgs( [ [] ] )
21 ->setMethods( [ 'run' ] )
22 ->getMock();
23 $this->bag = new RESTBagOStuff( [ 'client' => $this->client, 'url' => 'http://test/rest/' ] );
24 }
25
26 public function testGet() {
27 $this->client->expects( $this->once() )->method( 'run' )->with( [
28 'method' => 'GET',
29 'url' => 'http://test/rest/42xyz42'
30 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
31 ] )->willReturn( [ 200, 'OK', [], 's:8:"somedata";', 0 ] );
32 $result = $this->bag->get( '42xyz42' );
33 $this->assertEquals( 'somedata', $result );
34 }
35
36 public function testGetNotExist() {
37 $this->client->expects( $this->once() )->method( 'run' )->with( [
38 'method' => 'GET',
39 'url' => 'http://test/rest/42xyz42'
40 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
41 ] )->willReturn( [ 404, 'Not found', [], 'Nothing to see here', 0 ] );
42 $result = $this->bag->get( '42xyz42' );
43 $this->assertFalse( $result );
44 }
45
46 public function testGetBadClient() {
47 $this->client->expects( $this->once() )->method( 'run' )->with( [
48 'method' => 'GET',
49 'url' => 'http://test/rest/42xyz42'
50 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
51 ] )->willReturn( [ 0, '', [], '', 'cURL has failed you today' ] );
52 $result = $this->bag->get( '42xyz42' );
53 $this->assertFalse( $result );
54 $this->assertEquals( BagOStuff::ERR_UNREACHABLE, $this->bag->getLastError() );
55 }
56
57 public function testGetBadServer() {
58 $this->client->expects( $this->once() )->method( 'run' )->with( [
59 'method' => 'GET',
60 'url' => 'http://test/rest/42xyz42'
61 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
62 ] )->willReturn( [ 500, 'Too busy', [], 'Server is too busy', '' ] );
63 $result = $this->bag->get( '42xyz42' );
64 $this->assertFalse( $result );
65 $this->assertEquals( BagOStuff::ERR_UNEXPECTED, $this->bag->getLastError() );
66 }
67
68 public function testPut() {
69 $this->client->expects( $this->once() )->method( 'run' )->with( [
70 'method' => 'PUT',
71 'url' => 'http://test/rest/42xyz42',
72 'body' => 's:8:"postdata";'
73 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
74 ] )->willReturn( [ 200, 'OK', [], 'Done', 0 ] );
75 $result = $this->bag->set( '42xyz42', 'postdata' );
76 $this->assertTrue( $result );
77 }
78
79 public function testDelete() {
80 $this->client->expects( $this->once() )->method( 'run' )->with( [
81 'method' => 'DELETE',
82 'url' => 'http://test/rest/42xyz42',
83 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
84 ] )->willReturn( [ 200, 'OK', [], 'Done', 0 ] );
85 $result = $this->bag->delete( '42xyz42' );
86 $this->assertTrue( $result );
87 }
88 }