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