5 * @covers RESTBagOStuff
7 class RESTBagOStuffTest
extends MediaWikiTestCase
{
10 * @var MultiHttpClient
18 public function setUp() {
21 $this->getMockBuilder( MultiHttpClient
::class )
22 ->setConstructorArgs( [ [] ] )
23 ->setMethods( [ 'run' ] )
25 $this->bag
= new RESTBagOStuff( [ 'client' => $this->client
, 'url' => 'http://test/rest/' ] );
28 public function testGet() {
29 $this->client
->expects( $this->once() )->method( 'run' )->with( [
31 'url' => 'http://test/rest/42xyz42',
33 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
34 ] )->willReturn( [ 200, 'OK', [], '"somedata"', 0 ] );
35 $result = $this->bag
->get( '42xyz42' );
36 $this->assertEquals( 'somedata', $result );
39 public function testGetNotExist() {
40 $this->client
->expects( $this->once() )->method( 'run' )->with( [
42 'url' => 'http://test/rest/42xyz42',
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 );
50 public function testGetBadClient() {
51 $this->client
->expects( $this->once() )->method( 'run' )->with( [
53 'url' => 'http://test/rest/42xyz42',
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() );
62 public function testGetBadServer() {
63 $this->client
->expects( $this->once() )->method( 'run' )->with( [
65 'url' => 'http://test/rest/42xyz42',
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() );
74 public function testPut() {
75 $this->client
->expects( $this->once() )->method( 'run' )->with( [
77 'url' => 'http://test/rest/42xyz42',
78 'body' => '"postdata"',
80 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
81 ] )->willReturn( [ 200, 'OK', [], 'Done', 0 ] );
82 $result = $this->bag
->set( '42xyz42', 'postdata' );
83 $this->assertTrue( $result );
86 public function testDelete() {
87 $this->client
->expects( $this->once() )->method( 'run' )->with( [
89 'url' => 'http://test/rest/42xyz42',
91 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
92 ] )->willReturn( [ 200, 'OK', [], 'Done', 0 ] );
93 $result = $this->bag
->delete( '42xyz42' );
94 $this->assertTrue( $result );