Skip to content

Commit 9aeb3c4

Browse files
committed
Use Guzzle 6
1 parent f339ea6 commit 9aeb3c4

File tree

4 files changed

+51
-75
lines changed

4 files changed

+51
-75
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
],
1717
"require": {
1818
"php": ">=5.5.0",
19-
"serialization/serialization": "3.*",
19+
"serialization/serialization": "~3.0",
2020
"wikibase/data-model": "~4.0|~3.0|~2.0|~1.0|~0.8",
2121
"data-values/geo": "~1.0|~0.1",
2222
"data-values/time": "~0.6",
2323
"data-values/number": "~0.4",
24-
"guzzle/http": "~3.7"
24+
"guzzlehttp/guzzle": "~6.1"
2525
},
2626
"autoload": {
2727
"psr-4": {

src/Guzzle/WikidataQueryApiClient.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/WikidataQueryApi.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace WikidataQueryApi;
44

5-
use InvalidArgumentException;
6-
use WikidataQueryApi\Guzzle\WikidataQueryApiClient;
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\ClientInterface;
77

88
/**
99
* @licence GPLv2+
@@ -12,21 +12,25 @@
1212
class WikidataQueryApi {
1313

1414
/**
15-
* @var WikidataQueryApiClient
15+
* @var string
16+
*/
17+
private $apiUrl;
18+
19+
/**
20+
* @var ClientInterface
1621
*/
1722
private $client;
1823

1924
/**
20-
* @param string|WikidataQueryApiClient $client either the url or the api or a custom Client
25+
* @param string $apiUrl the URL of the Wikidata Query API
26+
* @param ClientInterface|null $client the Guzzle client to use
2127
*/
22-
public function __construct( $client ) {
23-
if ( is_string( $client ) ) {
24-
$client = WikidataQueryApiClient::factory( [ 'base_url' => $client ] );
25-
}
26-
if ( !( $client instanceof WikidataQueryApiClient ) ) {
27-
throw new InvalidArgumentException();
28+
public function __construct( $apiUrl, ClientInterface $client = null ) {
29+
if( $client === null ) {
30+
$client = new Client();
2831
}
2932

33+
$this->apiUrl = $apiUrl;
3034
$this->client = $client;
3135
}
3236

@@ -37,9 +41,12 @@ public function __construct( $client ) {
3741
* @throws WikibaseQueryApiException
3842
*/
3943
public function doQuery( $params ) {
40-
$result = $this->client->apiGet( $params );
44+
$result = json_decode( $this->client->get(
45+
$this->apiUrl,
46+
[ 'query' => $params ]
47+
)->getBody(), true );
4148

42-
if ( $result['status']['error'] != 'OK' ) {
49+
if ( $result['status']['error'] !== 'OK' ) {
4350
throw new WikibaseQueryApiException( $result['status']['error'] );
4451
}
4552

tests/unit/WikidataQueryApiTest.php

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace WikidataQueryApi;
44

5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Handler\MockHandler;
7+
use GuzzleHttp\HandlerStack;
8+
use GuzzleHttp\Psr7\Response;
9+
510
/**
611
* @covers WikidataQueryApi\WikidataQueryApi
712
*
@@ -11,18 +16,20 @@
1116
class WikidataQueryApiTest extends \PHPUnit_Framework_TestCase {
1217

1318
public function testDoQuery() {
14-
$clientMock = $this->getMock( 'WikidataQueryApi\Guzzle\WikidataQueryApiClient' );
15-
$clientMock->expects( $this->any() )
16-
->method( 'apiGet' )
17-
->with( $this->equalTo( [
18-
'q' => 'claim[42:42]'
19-
] ) )
20-
->will( $this->returnValue( [
21-
'status' => [ 'error' => 'OK' ],
22-
'items' => [ 42 ]
23-
] ) );
19+
$mock = new MockHandler( [
20+
new Response(
21+
200,
22+
[],
23+
json_encode( [
24+
'status' => [ 'error' => 'OK' ],
25+
'items' => [ 42 ]
26+
] )
27+
)
28+
] );
29+
$handler = HandlerStack::create( $mock );
30+
$client = new Client( [ 'handler' => $handler ] );
31+
$wikidataQueryApi = new WikidataQueryApi( '', $client );
2432

25-
$wikidataQueryApi = new WikidataQueryApi( $clientMock );
2633
$this->assertEquals(
2734
[
2835
'status' => [ 'error' => 'OK' ],
@@ -35,17 +42,18 @@ public function testDoQuery() {
3542
}
3643

3744
public function testDoQueryWithError() {
38-
$clientMock = $this->getMock( 'WikidataQueryApi\Guzzle\WikidataQueryApiClient' );
39-
$clientMock->expects( $this->any() )
40-
->method( 'apiGet' )
41-
->with( $this->equalTo( [
42-
'q' => 'claim[42:42]'
43-
] ) )
44-
->will( $this->returnValue( [
45-
'status' => [ 'error' => 'Error' ]
46-
] ) );
47-
48-
$wikidataQueryApi = new WikidataQueryApi( $clientMock );
45+
$mock = new MockHandler( [
46+
new Response(
47+
200,
48+
[],
49+
json_encode( [
50+
'status' => [ 'error' => 'Error' ]
51+
] )
52+
)
53+
] );
54+
$handler = HandlerStack::create( $mock );
55+
$client = new Client( [ 'handler' => $handler ] );
56+
$wikidataQueryApi = new WikidataQueryApi( '', $client );
4957

5058
$this->setExpectedException( 'WikidataQueryApi\WikibaseQueryApiException' );
5159
$wikidataQueryApi->doQuery( [

0 commit comments

Comments
 (0)