Debian Codegraph @

tests/phpunit/unit/includes/objectcache/RESTBagOStuffTest.php

raw symbols SCIP
1 <?php
2
3 use Wikimedia\Http\MultiHttpClient;
4 use Wikimedia\ObjectCache\BagOStuff;
5 use Wikimedia\ObjectCache\RESTBagOStuff;
6
7 /**
8 * @group BagOStuff
9 *
10 * @covers \Wikimedia\ObjectCache\RESTBagOStuff
11 */
12 class RESTBagOStuffTest extends \MediaWikiUnitTestCase {
13
14 /**
15 * @var MultiHttpClient
16 */
17 private $client;
18 /**
19 * @var RESTBagOStuff
20 */
21 private $bag;
22
23 protected function setUp(): void {
24 parent::setUp();
25 $this->client =
26 $this->getMockBuilder( MultiHttpClient::class )
27 ->setConstructorArgs( [ [] ] )
28 ->onlyMethods( [ 'run' ] )
29 ->getMock();
30 $this->bag = new RESTBagOStuff( [ 'client' => $this->client, 'url' => 'http://test/rest/' ] );
31 }
32
33 /**
34 * @dataProvider dataGet
35 */
36 public function testGet( $serializationType, $hmacKey, $data ) {
37 $classReflect = new ReflectionClass( RESTBagOStuff::class );
38
39 $serializationTypeReflect = $classReflect->getProperty( 'serializationType' );
40 $serializationTypeReflect->setValue( $this->bag, $serializationType );
41
42 $hmacKeyReflect = $classReflect->getProperty( 'hmacKey' );
43 $hmacKeyReflect->setValue( $this->bag, $hmacKey );
44
45 $this->client->expects( $this->once() )->method( 'run' )->with( [
46 'method' => 'GET',
47 'url' => 'http://test/rest/42xyz42',
48 'headers' => []
49 // [ $rcode, $rdesc, $rhdrs, $rbody, $rerr ]
50 ] )->willReturn( [ 200, 'OK', [], $data, 0 ] );
51 $result = $this->bag->get( '42xyz42' );
52 $this->assertEquals( 'somedata', $result );
53 }
54
55 public static function dataGet() {
56 // Make sure the defaults are last, so the $bag is left as expected for the next test
57 return [
58 [ 'JSON', '12345', 'JSON.Us1wli82zEJ6DNQnCG//w+MShOFrdx9wCdfTUhPPA2w=."somedata"' ],
59 [ 'JSON', '', 'JSON.."somedata"' ],
60 [ 'PHP', '12345', 'PHP.t2EKhUF4l65kZqWhoAnKW8ZPzekDYfrDxTkQcVmGsuM=.s:8:"somedata";' ],
61 [ 'PHP', '', 'PHP..s:8:"somedata";' ],
62 ];
63 }
64
65 public function testGetNotExist() {
66 $this->client->expects( $this->once() )->method( 'run' )->with( [
67 'method' => 'GET',
68 'url' => 'http://test/rest/42xyz42',
69 'headers' => []
70 // [ $rcode, $rdesc, $rhdrs, $rbody, $rerr ]
71 ] )->willReturn( [ 404, 'Not found', [], 'Nothing to see here', 0 ] );
72 $result = $this->bag->get( '42xyz42' );
73 $this->assertFalse( $result );
74 }
75
76 public function testGetBadClient() {
77 $this->client->expects( $this->once() )->method( 'run' )->with( [
78 'method' => 'GET',
79 'url' => 'http://test/rest/42xyz42',
80 'headers' => []
81 // [ $rcode, $rdesc, $rhdrs, $rbody, $rerr ]
82 ] )->willReturn( [ 0, '', [], '', 'cURL has failed you today' ] );
83 $result = $this->bag->get( '42xyz42' );
84 $this->assertFalse( $result );
85 $this->assertEquals( BagOStuff::ERR_UNREACHABLE, $this->bag->getLastError() );
86 }
87
88 public function testGetBadServer() {
89 $this->client->expects( $this->once() )->method( 'run' )->with( [
90 'method' => 'GET',
91 'url' => 'http://test/rest/42xyz42',
92 'headers' => []
93 // [ $rcode, $rdesc, $rhdrs, $rbody, $rerr ]
94 ] )->willReturn( [ 500, 'Too busy', [], 'Server is too busy', '' ] );
95 $result = $this->bag->get( '42xyz42' );
96 $this->assertFalse( $result );
97 $this->assertEquals( BagOStuff::ERR_UNEXPECTED, $this->bag->getLastError() );
98 }
99
100 /**
101 * @dataProvider dataGet
102 */
103 public function testPut( $serializationType, $hmacKey, $data ) {
104 $classReflect = new ReflectionClass( RESTBagOStuff::class );
105
106 $serializationTypeReflect = $classReflect->getProperty( 'serializationType' );
107 $serializationTypeReflect->setValue( $this->bag, $serializationType );
108
109 $hmacKeyReflect = $classReflect->getProperty( 'hmacKey' );
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 // [ $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 function testDelete() {
124 $this->client->expects( $this->once() )->method( 'run' )->with( [
125 'method' => 'DELETE',
126 'url' => 'http://test/rest/42xyz42',
127 'headers' => []
128 // [ $rcode, $rdesc, $rhdrs, $rbody, $rerr ]
129 ] )->willReturn( [ 200, 'OK', [], 'Done', 0 ] );
130 $result = $this->bag->delete( '42xyz42' );
131 $this->assertTrue( $result );
132 }
133 }