Merge "Add @covers tags to objectcache tests"
[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' )
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 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
33 ] )->willReturn( [ 200, 'OK', [], 's:8:"somedata";', 0 ] );
34 $result = $this->bag->get( '42xyz42' );
35 $this->assertEquals( 'somedata', $result );
36 }
37
38 public function testGetNotExist() {
39 $this->client->expects( $this->once() )->method( 'run' )->with( [
40 'method' => 'GET',
41 'url' => 'http://test/rest/42xyz42'
42 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
43 ] )->willReturn( [ 404, 'Not found', [], 'Nothing to see here', 0 ] );
44 $result = $this->bag->get( '42xyz42' );
45 $this->assertFalse( $result );
46 }
47
48 public function testGetBadClient() {
49 $this->client->expects( $this->once() )->method( 'run' )->with( [
50 'method' => 'GET',
51 'url' => 'http://test/rest/42xyz42'
52 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
53 ] )->willReturn( [ 0, '', [], '', 'cURL has failed you today' ] );
54 $result = $this->bag->get( '42xyz42' );
55 $this->assertFalse( $result );
56 $this->assertEquals( BagOStuff::ERR_UNREACHABLE, $this->bag->getLastError() );
57 }
58
59 public function testGetBadServer() {
60 $this->client->expects( $this->once() )->method( 'run' )->with( [
61 'method' => 'GET',
62 'url' => 'http://test/rest/42xyz42'
63 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
64 ] )->willReturn( [ 500, 'Too busy', [], 'Server is too busy', '' ] );
65 $result = $this->bag->get( '42xyz42' );
66 $this->assertFalse( $result );
67 $this->assertEquals( BagOStuff::ERR_UNEXPECTED, $this->bag->getLastError() );
68 }
69
70 public function testPut() {
71 $this->client->expects( $this->once() )->method( 'run' )->with( [
72 'method' => 'PUT',
73 'url' => 'http://test/rest/42xyz42',
74 'body' => 's:8:"postdata";'
75 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
76 ] )->willReturn( [ 200, 'OK', [], 'Done', 0 ] );
77 $result = $this->bag->set( '42xyz42', 'postdata' );
78 $this->assertTrue( $result );
79 }
80
81 public function testDelete() {
82 $this->client->expects( $this->once() )->method( 'run' )->with( [
83 'method' => 'DELETE',
84 'url' => 'http://test/rest/42xyz42',
85 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
86 ] )->willReturn( [ 200, 'OK', [], 'Done', 0 ] );
87 $result = $this->bag->delete( '42xyz42' );
88 $this->assertTrue( $result );
89 }
90 }