| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Sparql; |
| 4 |
|
| 5 |
use MediaWiki\Http\HttpRequestFactory; |
| 6 |
use MediaWiki\Sparql\SparqlClient; |
| 7 |
use MWHttpRequest; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class SparqlClientTest extends \PHPUnit\Framework\TestCase { |
| 13 |
|
| 14 |
private function getRequestFactory( $request ) { |
| 15 |
$requestFactory = $this->createMock( HttpRequestFactory::class ); |
| 16 |
$requestFactory->method( 'create' )->willReturn( $request ); |
| 17 |
return $requestFactory; |
| 18 |
} |
| 19 |
|
| 20 |
private function getRequestMock( $content ) { |
| 21 |
$request = $this->createMock( MWHttpRequest::class ); |
| 22 |
$request->method( 'execute' )->willReturn( \MediaWiki\Status\Status::newGood( 200 ) ); |
| 23 |
$request->method( 'getContent' )->willReturn( $content ); |
| 24 |
return $request; |
| 25 |
} |
| 26 |
|
| 27 |
public function testQuery() { |
| 28 |
$json = <<<JSON |
| 29 |
{ |
| 30 |
"head" : { |
| 31 |
"vars" : [ "x", "y", "z" ] |
| 32 |
}, |
| 33 |
"results" : { |
| 34 |
"bindings" : [ { |
| 35 |
"x" : { |
| 36 |
"type" : "uri", |
| 37 |
"value" : "http://wikiba.se/ontology#Dump" |
| 38 |
}, |
| 39 |
"y" : { |
| 40 |
"type" : "uri", |
| 41 |
"value" : "http://creativecommons.org/ns#license" |
| 42 |
}, |
| 43 |
"z" : { |
| 44 |
"type" : "uri", |
| 45 |
"value" : "http://creativecommons.org/publicdomain/zero/1.0/" |
| 46 |
} |
| 47 |
}, { |
| 48 |
"x" : { |
| 49 |
"type" : "uri", |
| 50 |
"value" : "http://wikiba.se/ontology#Dump" |
| 51 |
}, |
| 52 |
"z" : { |
| 53 |
"type" : "literal", |
| 54 |
"value" : "0.1.0" |
| 55 |
} |
| 56 |
} ] |
| 57 |
} |
| 58 |
} |
| 59 |
JSON; |
| 60 |
|
| 61 |
$request = $this->getRequestMock( $json ); |
| 62 |
$client = new SparqlClient( 'http://acme.test/', $this->getRequestFactory( $request ) ); |
| 63 |
|
| 64 |
|
| 65 |
$result = $client->query( "TEST SPARQL" ); |
| 66 |
$this->assertCount( 2, $result ); |
| 67 |
$this->assertEquals( 'http://wikiba.se/ontology#Dump', $result[0]['x'] ); |
| 68 |
$this->assertEquals( 'http://creativecommons.org/ns#license', $result[0]['y'] ); |
| 69 |
$this->assertSame( '0.1.0', $result[1]['z'] ); |
| 70 |
$this->assertNull( $result[1]['y'] ); |
| 71 |
|
| 72 |
$result = $client->query( "TEST SPARQL 2", true ); |
| 73 |
$this->assertCount( 2, $result ); |
| 74 |
$this->assertEquals( 'uri', $result[0]['x']['type'] ); |
| 75 |
$this->assertEquals( 'http://wikiba.se/ontology#Dump', $result[0]['x']['value'] ); |
| 76 |
$this->assertEquals( 'literal', $result[1]['z']['type'] ); |
| 77 |
$this->assertSame( '0.1.0', $result[1]['z']['value'] ); |
| 78 |
$this->assertNull( $result[1]['y'] ); |
| 79 |
} |
| 80 |
|
| 81 |
public function testBadQuery() { |
| 82 |
$request = $this->createMock( MWHttpRequest::class ); |
| 83 |
$client = new SparqlClient( 'http://acme.test/', $this->getRequestFactory( $request ) ); |
| 84 |
|
| 85 |
$request->method( 'execute' )->willReturn( \MediaWiki\Status\Status::newFatal( "Bad query" ) ); |
| 86 |
$this->expectException( \MediaWiki\Sparql\SparqlException::class ); |
| 87 |
$result = $client->query( "TEST SPARQL 3" ); |
| 88 |
} |
| 89 |
|
| 90 |
public static function optionsProvider() { |
| 91 |
return [ |
| 92 |
'defaults' => [ |
| 93 |
'TEST ัะตัั SPARQL 4 ', |
| 94 |
null, |
| 95 |
null, |
| 96 |
[ |
| 97 |
'http://acme.test/', |
| 98 |
'query=TEST+%D1%82%D0%B5%D1%81%D1%82+SPARQL+4+', |
| 99 |
'format=json', |
| 100 |
'maxQueryTimeMillis=30000', |
| 101 |
], |
| 102 |
[ |
| 103 |
'method' => 'GET', |
| 104 |
'userAgent' => 'testOptions SparqlClient', |
| 105 |
'timeout' => 30 |
| 106 |
] |
| 107 |
], |
| 108 |
'big query' => [ |
| 109 |
str_repeat( 'ZZ', SparqlClient::MAX_GET_SIZE ), |
| 110 |
null, |
| 111 |
null, |
| 112 |
[ |
| 113 |
'format=json', |
| 114 |
'maxQueryTimeMillis=30000', |
| 115 |
], |
| 116 |
[ |
| 117 |
'method' => 'POST', |
| 118 |
'postData' => 'query=' . str_repeat( 'ZZ', SparqlClient::MAX_GET_SIZE ), |
| 119 |
] |
| 120 |
], |
| 121 |
'timeout 1s' => [ |
| 122 |
'TEST SPARQL 4', |
| 123 |
null, |
| 124 |
1, |
| 125 |
[ |
| 126 |
'maxQueryTimeMillis=1000', |
| 127 |
], |
| 128 |
[ |
| 129 |
'timeout' => 1 |
| 130 |
] |
| 131 |
], |
| 132 |
'more options' => [ |
| 133 |
'TEST SPARQL 5', |
| 134 |
[ |
| 135 |
'userAgent' => 'My Test', |
| 136 |
'randomOption' => 'duck', |
| 137 |
], |
| 138 |
null, |
| 139 |
[], |
| 140 |
[ |
| 141 |
'userAgent' => 'My Test', |
| 142 |
'randomOption' => 'duck', |
| 143 |
] |
| 144 |
], |
| 145 |
|
| 146 |
]; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function testOptions( $sparql, $options, $timeout, $expectedUrl, $expectedOptions ) { |
| 158 |
$requestFactory = $this->createMock( HttpRequestFactory::class ); |
| 159 |
$requestFactory->method( 'getUserAgent' )->willReturn( 'testOptions' ); |
| 160 |
$client = new SparqlClient( 'http://acme.test/', $requestFactory ); |
| 161 |
|
| 162 |
$request = $this->getRequestMock( '{}' ); |
| 163 |
|
| 164 |
$requestFactory->method( 'create' )->willReturnCallback( |
| 165 |
function ( $url, $options ) use ( $request, $expectedUrl, $expectedOptions ) { |
| 166 |
foreach ( $expectedUrl as $eurl ) { |
| 167 |
$this->assertStringContainsString( $eurl, $url ); |
| 168 |
} |
| 169 |
foreach ( $expectedOptions as $ekey => $evalue ) { |
| 170 |
$this->assertArrayHasKey( $ekey, $options ); |
| 171 |
$this->assertEquals( $options[$ekey], $evalue ); |
| 172 |
} |
| 173 |
return $request; |
| 174 |
} |
| 175 |
); |
| 176 |
|
| 177 |
if ( $options !== null ) { |
| 178 |
$client->setClientOptions( $options ); |
| 179 |
} |
| 180 |
if ( $timeout !== null ) { |
| 181 |
$client->setTimeout( $timeout ); |
| 182 |
} |
| 183 |
|
| 184 |
$result = $client->query( $sparql ); |
| 185 |
} |
| 186 |
|
| 187 |
} |