Skip to content

Commit

Permalink
Adjust MediawikiApi to use ClientInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
addshore committed Jan 18, 2016
1 parent 75ba537 commit 99f61d1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
17 changes: 9 additions & 8 deletions src/MediawikiApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Promise\PromiseInterface;
use InvalidArgumentException;
use Mediawiki\Api\Guzzle\ClientFactory;
use Mediawiki\Api\Guzzle\MiddlewareFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -28,7 +25,7 @@
class MediawikiApi implements MediawikiApiInterface, LoggerAwareInterface {

/**
* @var Client|null Should be accessed through getClient
* @var ClientInterface|null Should be accessed through getClient
*/
private $client = null;

Expand Down Expand Up @@ -142,7 +139,8 @@ public function setLogger( LoggerInterface $logger ) {
* Can throw UsageExceptions or RejectionExceptions
*/
public function getRequestAsync( Request $request ) {
$promise = $this->getClient()->getAsync(
$promise = $this->getClient()->requestAsync(
'GET',
$this->apiUrl,
$this->getClientRequestOptions( $request, 'query' )
);
Expand All @@ -162,7 +160,8 @@ public function getRequestAsync( Request $request ) {
* Can throw UsageExceptions or RejectionExceptions
*/
public function postRequestAsync( Request $request ) {
$promise = $this->getClient()->postAsync(
$promise = $this->getClient()->requestAsync(
'POST',
$this->apiUrl,
$this->getClientRequestOptions( $request, 'form_params' )
);
Expand All @@ -180,7 +179,8 @@ public function postRequestAsync( Request $request ) {
* @return mixed Normally an array
*/
public function getRequest( Request $request ) {
$response = $this->getClient()->get(
$response = $this->getClient()->request(
'GET',
$this->apiUrl,
$this->getClientRequestOptions( $request, 'query' )
);
Expand All @@ -196,7 +196,8 @@ public function getRequest( Request $request ) {
* @return mixed Normally an array
*/
public function postRequest( Request $request ) {
$response = $this->getClient()->post(
$response = $this->getClient()->request(
'POST',
$this->apiUrl,
$this->getClientRequestOptions( $request, 'form_params' )
);
Expand Down
46 changes: 21 additions & 25 deletions tests/unit/MediawikiApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ public function testInvalidConstruction( $apiLocation ) {
}

private function getMockClient() {
$mock = $this->getMockBuilder( 'GuzzleHttp\Client' )
->disableOriginalConstructor()
->setMethods( array( 'get', 'post' ) )
->getMock();
return $mock;
return $this->getMock( 'GuzzleHttp\ClientInterface' );
}

private function getMockResponse( $responseValue ) {
Expand All @@ -80,7 +76,7 @@ private function getExpectedRequestOpts( $params, $wasPosted = false ) {
public function testGetRequestThrowsUsageExceptionOnError() {
$client = $this->getMockClient();
$client->expects( $this->once() )
->method( 'get' )
->method( 'request' )
->will( $this->returnValue(
$this->getMockResponse( array( 'error' => array(
'code' => 'imacode',
Expand All @@ -102,7 +98,7 @@ public function testGetRequestThrowsUsageExceptionOnError() {
public function testPostRequestThrowsUsageExceptionOnError() {
$client = $this->getMockClient();
$client->expects( $this->once() )
->method( 'post' )
->method( 'request' )
->will( $this->returnValue(
$this->getMockResponse( array( 'error' => array(
'code' => 'imacode',
Expand All @@ -127,8 +123,8 @@ public function testPostRequestThrowsUsageExceptionOnError() {
public function testGetActionReturnsResult( $expectedResult, $action, $params = array() ) {
$client = $this->getMockClient();
$client->expects( $this->once() )
->method( 'get' )
->with( null, $this->getExpectedRequestOpts( array_merge( array( 'action' => $action ), $params ) ) )
->method( 'request' )
->with( 'GET', null, $this->getExpectedRequestOpts( array_merge( array( 'action' => $action ), $params ) ) )
->will( $this->returnValue( $this->getMockResponse( $expectedResult ) ) );
$api = new MediawikiApi( '', $client );

Expand All @@ -143,8 +139,8 @@ public function testGetActionReturnsResult( $expectedResult, $action, $params =
public function testPostActionReturnsResult( $expectedResult, $action, $params = array() ) {
$client = $this->getMockClient();
$client->expects( $this->once() )
->method( 'post' )
->with( null, $this->getExpectedRequestOpts( array_merge( array( 'action' => $action ), $params ), true ))
->method( 'request' )
->with( 'POST', null, $this->getExpectedRequestOpts( array_merge( array( 'action' => $action ), $params ), true ))
->will( $this->returnValue( $this->getMockResponse( $expectedResult ) ) );
$api = new MediawikiApi( '', $client );

Expand All @@ -171,15 +167,15 @@ public function testGoodLoginSequence() {
'lgpassword' => 'P1',
);
$client->expects( $this->at( 0 ) )
->method( 'post' )
->with( null, $this->getExpectedRequestOpts( $eq1, true ) )
->method( 'request' )
->with( 'POST', null, $this->getExpectedRequestOpts( $eq1, true ) )
->will( $this->returnValue( $this->getMockResponse( array( 'login' => array(
'result' => 'NeedToken',
'token' => 'IamLoginTK',
) ) ) ) );
$client->expects( $this->at( 1 ) )
->method( 'post' )
->with( null, $this->getExpectedRequestOpts( array_merge( $eq1, array( 'lgtoken' => 'IamLoginTK' ) ), true ) )
->method( 'request' )
->with( 'POST', null, $this->getExpectedRequestOpts( array_merge( $eq1, array( 'lgtoken' => 'IamLoginTK' ) ), true ) )
->will( $this->returnValue( $this->getMockResponse( array( 'login' => array( 'result' => 'Success' ) ) ) ) );
$api = new MediawikiApi( '', $client );

Expand All @@ -196,15 +192,15 @@ public function testBadLoginSequence() {
'lgpassword' => 'P1',
);
$client->expects( $this->at( 0 ) )
->method( 'post' )
->with( null, $this->getExpectedRequestOpts( $eq1, true ) )
->method( 'request' )
->with( 'POST', null, $this->getExpectedRequestOpts( $eq1, true ) )
->will( $this->returnValue( $this->getMockResponse( array( 'login' => array(
'result' => 'NeedToken',
'token' => 'IamLoginTK',
) ) ) ) );
$client->expects( $this->at( 1 ) )
->method( 'post' )
->with( null, $this->getExpectedRequestOpts( array_merge( $eq1, array( 'lgtoken' => 'IamLoginTK' ) ), true ) )
->method( 'request' )
->with( 'POST', null, $this->getExpectedRequestOpts( array_merge( $eq1, array( 'lgtoken' => 'IamLoginTK' ) ), true ) )
->will( $this->returnValue( $this->getMockResponse( array( 'login' => array( 'result' => 'BADTOKENorsmthin' ) ) ) ) );
$api = new MediawikiApi( '', $client );

Expand All @@ -215,8 +211,8 @@ public function testBadLoginSequence() {
public function testLogout() {
$client = $this->getMockClient();
$client->expects( $this->at( 0 ) )
->method( 'post' )
->with( null, $this->getExpectedRequestOpts( array( 'action' => 'logout' ), true ) )
->method( 'request' )
->with( 'POST', null, $this->getExpectedRequestOpts( array( 'action' => 'logout' ), true ) )
->will( $this->returnValue( $this->getMockResponse( array( ) ) ) );
$api = new MediawikiApi( '', $client );

Expand All @@ -226,8 +222,8 @@ public function testLogout() {
public function testLogoutOnFailure() {
$client = $this->getMockClient();
$client->expects( $this->at( 0 ) )
->method( 'post' )
->with( null, $this->getExpectedRequestOpts( array( 'action' => 'logout' ), true ) )
->method( 'request' )
->with( 'POST', null, $this->getExpectedRequestOpts( array( 'action' => 'logout' ), true ) )
->will( $this->returnValue( $this->getMockResponse( null ) ) );
$api = new MediawikiApi( '', $client );

Expand All @@ -240,8 +236,8 @@ public function testLogoutOnFailure() {
public function testGetVersion( $apiValue, $expectedVersion ) {
$client = $this->getMockClient();
$client->expects( $this->exactly( 1 ) )
->method( 'get' )
->with( null, $this->getExpectedRequestOpts( array( 'action' => 'query', 'meta' => 'siteinfo', 'continue' => '' ) ) )
->method( 'request' )
->with( 'GET', null, $this->getExpectedRequestOpts( array( 'action' => 'query', 'meta' => 'siteinfo', 'continue' => '' ) ) )
->will( $this->returnValue( $this->getMockResponse( array(
'query' => array(
'general' => array(
Expand Down

0 comments on commit 99f61d1

Please sign in to comment.