5 * @covers RESTBagOStuff
7 class RESTBagOStuffTest
extends \MediaWikiUnitTestCase
{
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/' ] );
29 * @dataProvider dataGet
31 public function testGet( $serializationType, $hmacKey, $data ) {
32 $classReflect = new ReflectionClass( RESTBagOStuff
::class );
34 $serializationTypeReflect = $classReflect->getProperty( 'serializationType' );
35 $serializationTypeReflect->setAccessible( true );
36 $serializationTypeReflect->setValue( $this->bag
, $serializationType );
38 $hmacKeyReflect = $classReflect->getProperty( 'hmacKey' );
39 $hmacKeyReflect->setAccessible( true );
40 $hmacKeyReflect->setValue( $this->bag
, $hmacKey );
42 $this->client
->expects( $this->once() )->method( 'run' )->with( [
44 'url' => 'http://test/rest/42xyz42',
46 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
47 ] )->willReturn( [ 200, 'OK', [], $data, 0 ] );
48 $result = $this->bag
->get( '42xyz42' );
49 $this->assertEquals( 'somedata', $result );
52 public static function dataGet() {
53 // Make sure the defaults are last, so the $bag is left as expected for the next test
55 [ 'JSON', '12345', 'JSON.Us1wli82zEJ6DNQnCG//w+MShOFrdx9wCdfTUhPPA2w=."somedata"' ],
56 [ 'JSON', '', 'JSON.."somedata"' ],
57 [ 'PHP', '12345', 'PHP.t2EKhUF4l65kZqWhoAnKW8ZPzekDYfrDxTkQcVmGsuM=.s:8:"somedata";' ],
58 [ 'PHP', '', 'PHP..s:8:"somedata";' ],
59 [ 'legacy', '', 's:8:"somedata";' ],
63 public function testGetNotExist() {
64 $this->client
->expects( $this->once() )->method( 'run' )->with( [
66 'url' => 'http://test/rest/42xyz42',
68 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
69 ] )->willReturn( [ 404, 'Not found', [], 'Nothing to see here', 0 ] );
70 $result = $this->bag
->get( '42xyz42' );
71 $this->assertFalse( $result );
74 public function testGetBadClient() {
75 $this->client
->expects( $this->once() )->method( 'run' )->with( [
77 'url' => 'http://test/rest/42xyz42',
79 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
80 ] )->willReturn( [ 0, '', [], '', 'cURL has failed you today' ] );
81 $result = $this->bag
->get( '42xyz42' );
82 $this->assertFalse( $result );
83 $this->assertEquals( BagOStuff
::ERR_UNREACHABLE
, $this->bag
->getLastError() );
86 public function testGetBadServer() {
87 $this->client
->expects( $this->once() )->method( 'run' )->with( [
89 'url' => 'http://test/rest/42xyz42',
91 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
92 ] )->willReturn( [ 500, 'Too busy', [], 'Server is too busy', '' ] );
93 $result = $this->bag
->get( '42xyz42' );
94 $this->assertFalse( $result );
95 $this->assertEquals( BagOStuff
::ERR_UNEXPECTED
, $this->bag
->getLastError() );
99 * @dataProvider dataPut
101 public function testPut( $serializationType, $hmacKey, $data ) {
102 $classReflect = new ReflectionClass( RESTBagOStuff
::class );
104 $serializationTypeReflect = $classReflect->getProperty( 'serializationType' );
105 $serializationTypeReflect->setAccessible( true );
106 $serializationTypeReflect->setValue( $this->bag
, $serializationType );
108 $hmacKeyReflect = $classReflect->getProperty( 'hmacKey' );
109 $hmacKeyReflect->setAccessible( true );
110 $hmacKeyReflect->setValue( $this->bag
, $hmacKey );
112 $this->client
->expects( $this->once() )->method( 'run' )->with( [
114 'url' => 'http://test/rest/42xyz42',
117 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
118 ] )->willReturn( [ 200, 'OK', [], 'Done', 0 ] );
119 $result = $this->bag
->set( '42xyz42', 'somedata' );
120 $this->assertTrue( $result );
123 public static function dataPut() {
124 // Make sure the defaults are last, so the $bag is left as expected for the next test
126 [ 'JSON', '12345', 'JSON.Us1wli82zEJ6DNQnCG//w+MShOFrdx9wCdfTUhPPA2w=."somedata"' ],
127 [ 'JSON', '', 'JSON.."somedata"' ],
128 [ 'PHP', '12345', 'PHP.t2EKhUF4l65kZqWhoAnKW8ZPzekDYfrDxTkQcVmGsuM=.s:8:"somedata";' ],
129 [ 'PHP', '', 'PHP..s:8:"somedata";' ],
130 [ 'legacy', '', 's:8:"somedata";' ],
134 public function testDelete() {
135 $this->client
->expects( $this->once() )->method( 'run' )->with( [
136 'method' => 'DELETE',
137 'url' => 'http://test/rest/42xyz42',
139 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
140 ] )->willReturn( [ 200, 'OK', [], 'Done', 0 ] );
141 $result = $this->bag
->delete( '42xyz42' );
142 $this->assertTrue( $result );