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