Add optional serialization_type and hmac_key param values to RESTBagOStuff
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / objectcache / RESTBagOStuffTest.php
1 <?php
2 /**
3 * @group BagOStuff
4 *
5 * @covers RESTBagOStuff
6 */
7 class RESTBagOStuffTest extends \MediaWikiUnitTestCase {
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 /**
29 * @dataProvider dataGet
30 */
31 public function testGet( $serializationType, $hmacKey, $data ) {
32 $classReflect = new ReflectionClass( RESTBagOStuff::class );
33
34 $serializationTypeReflect = $classReflect->getProperty( 'serializationType' );
35 $serializationTypeReflect->setAccessible( true );
36 $serializationTypeReflect->setValue( $this->bag, $serializationType );
37
38 $hmacKeyReflect = $classReflect->getProperty( 'hmacKey' );
39 $hmacKeyReflect->setAccessible( true );
40 $hmacKeyReflect->setValue( $this->bag, $hmacKey );
41
42 $this->client->expects( $this->once() )->method( 'run' )->with( [
43 'method' => 'GET',
44 'url' => 'http://test/rest/42xyz42',
45 'headers' => []
46 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
47 ] )->willReturn( [ 200, 'OK', [], $data, 0 ] );
48 $result = $this->bag->get( '42xyz42' );
49 $this->assertEquals( 'somedata', $result );
50 }
51
52 public static function dataGet() {
53 // Make sure the defaults are last, so the $bag is left as expected for the next test
54 return [
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";' ],
60 ];
61 }
62
63 public function testGetNotExist() {
64 $this->client->expects( $this->once() )->method( 'run' )->with( [
65 'method' => 'GET',
66 'url' => 'http://test/rest/42xyz42',
67 'headers' => []
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 );
72 }
73
74 public function testGetBadClient() {
75 $this->client->expects( $this->once() )->method( 'run' )->with( [
76 'method' => 'GET',
77 'url' => 'http://test/rest/42xyz42',
78 'headers' => []
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() );
84 }
85
86 public function testGetBadServer() {
87 $this->client->expects( $this->once() )->method( 'run' )->with( [
88 'method' => 'GET',
89 'url' => 'http://test/rest/42xyz42',
90 'headers' => []
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() );
96 }
97
98 /**
99 * @dataProvider dataPut
100 */
101 public function testPut( $serializationType, $hmacKey, $data ) {
102 $classReflect = new ReflectionClass( RESTBagOStuff::class );
103
104 $serializationTypeReflect = $classReflect->getProperty( 'serializationType' );
105 $serializationTypeReflect->setAccessible( true );
106 $serializationTypeReflect->setValue( $this->bag, $serializationType );
107
108 $hmacKeyReflect = $classReflect->getProperty( 'hmacKey' );
109 $hmacKeyReflect->setAccessible( true );
110 $hmacKeyReflect->setValue( $this->bag, $hmacKey );
111
112 $this->client->expects( $this->once() )->method( 'run' )->with( [
113 'method' => 'PUT',
114 'url' => 'http://test/rest/42xyz42',
115 'body' => $data,
116 'headers' => []
117 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
118 ] )->willReturn( [ 200, 'OK', [], 'Done', 0 ] );
119 $result = $this->bag->set( '42xyz42', 'somedata' );
120 $this->assertTrue( $result );
121 }
122
123 public static function dataPut() {
124 // Make sure the defaults are last, so the $bag is left as expected for the next test
125 return [
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";' ],
131 ];
132 }
133
134 public function testDelete() {
135 $this->client->expects( $this->once() )->method( 'run' )->with( [
136 'method' => 'DELETE',
137 'url' => 'http://test/rest/42xyz42',
138 'headers' => []
139 // list( $rcode, $rdesc, $rhdrs, $rbody, $rerr )
140 ] )->willReturn( [ 200, 'OK', [], 'Done', 0 ] );
141 $result = $this->bag->delete( '42xyz42' );
142 $this->assertTrue( $result );
143 }
144 }