MediaWiki REL1_31
SparqlClientTest.php
Go to the documentation of this file.
1<?php
2namespace MediaWiki\Sparql;
3
4use Http;
7use PHPUnit4And6Compat;
8
12class SparqlClientTest extends \PHPUnit\Framework\TestCase {
13
14 use PHPUnit4And6Compat;
15
16 private function getRequestFactory( $request ) {
17 $requestFactory = $this->getMock( HttpRequestFactory::class );
18 $requestFactory->method( 'create' )->willReturn( $request );
19 return $requestFactory;
20 }
21
22 private function getRequestMock( $content ) {
23 $request = $this->getMockBuilder( MWHttpRequest::class )->disableOriginalConstructor()->getMock();
24 $request->method( 'execute' )->willReturn( \Status::newGood( 200 ) );
25 $request->method( 'getContent' )->willReturn( $content );
26 return $request;
27 }
28
29 public function testQuery() {
30 $json = <<<JSON
31{
32 "head" : {
33 "vars" : [ "x", "y", "z" ]
34 },
35 "results" : {
36 "bindings" : [ {
37 "x" : {
38 "type" : "uri",
39 "value" : "http://wikiba.se/ontology#Dump"
40 },
41 "y" : {
42 "type" : "uri",
43 "value" : "http://creativecommons.org/ns#license"
44 },
45 "z" : {
46 "type" : "uri",
47 "value" : "http://creativecommons.org/publicdomain/zero/1.0/"
48 }
49 }, {
50 "x" : {
51 "type" : "uri",
52 "value" : "http://wikiba.se/ontology#Dump"
53 },
54 "z" : {
55 "type" : "literal",
56 "value" : "0.1.0"
57 }
58 } ]
59 }
60}
61JSON;
62
63 $request = $this->getRequestMock( $json );
64 $client = new SparqlClient( 'http://acme.test/', $this->getRequestFactory( $request ) );
65
66 // values only
67 $result = $client->query( "TEST SPARQL" );
68 $this->assertCount( 2, $result );
69 $this->assertEquals( 'http://wikiba.se/ontology#Dump', $result[0]['x'] );
70 $this->assertEquals( 'http://creativecommons.org/ns#license', $result[0]['y'] );
71 $this->assertEquals( '0.1.0', $result[1]['z'] );
72 $this->assertNull( $result[1]['y'] );
73 // raw data format
74 $result = $client->query( "TEST SPARQL 2", true );
75 $this->assertCount( 2, $result );
76 $this->assertEquals( 'uri', $result[0]['x']['type'] );
77 $this->assertEquals( 'http://wikiba.se/ontology#Dump', $result[0]['x']['value'] );
78 $this->assertEquals( 'literal', $result[1]['z']['type'] );
79 $this->assertEquals( '0.1.0', $result[1]['z']['value'] );
80 $this->assertNull( $result[1]['y'] );
81 }
82
86 public function testBadQuery() {
87 $request = $this->getMockBuilder( MWHttpRequest::class )->disableOriginalConstructor()->getMock();
88 $client = new SparqlClient( 'http://acme.test/', $this->getRequestFactory( $request ) );
89
90 $request->method( 'execute' )->willReturn( \Status::newFatal( "Bad query" ) );
91 $result = $client->query( "TEST SPARQL 3" );
92 }
93
94 public function optionsProvider() {
95 return [
96 'defaults' => [
97 'TEST ั‚ะตัั‚ SPARQL 4 ',
98 null,
99 null,
100 [
101 'http://acme.test/',
102 'query=TEST+%D1%82%D0%B5%D1%81%D1%82+SPARQL+4+',
103 'format=json',
104 'maxQueryTimeMillis=30000',
105 ],
106 [
107 'method' => 'GET',
108 'userAgent' => Http::userAgent() ." SparqlClient",
109 'timeout' => 30
110 ]
111 ],
112 'big query' => [
113 str_repeat( 'ZZ', SparqlClient::MAX_GET_SIZE ),
114 null,
115 null,
116 [
117 'format=json',
118 'maxQueryTimeMillis=30000',
119 ],
120 [
121 'method' => 'POST',
122 'postData' => 'query=' . str_repeat( 'ZZ', SparqlClient::MAX_GET_SIZE ),
123 ]
124 ],
125 'timeout 1s' => [
126 'TEST SPARQL 4',
127 null,
128 1,
129 [
130 'maxQueryTimeMillis=1000',
131 ],
132 [
133 'timeout' => 1
134 ]
135 ],
136 'more options' => [
137 'TEST SPARQL 5',
138 [
139 'userAgent' => 'My Test',
140 'randomOption' => 'duck',
141 ],
142 null,
143 [],
144 [
145 'userAgent' => 'My Test',
146 'randomOption' => 'duck',
147 ]
148 ],
149
150 ];
151 }
152
161 public function testOptions( $sparql, $options, $timeout, $expectedUrl, $expectedOptions ) {
162 $requestFactory = $this->getMock( HttpRequestFactory::class );
163 $client = new SparqlClient( 'http://acme.test/', $requestFactory );
164
165 $request = $this->getRequestMock( '{}' );
166
167 $requestFactory->method( 'create' )->willReturnCallback(
168 function ( $url, $options ) use ( $request, $expectedUrl, $expectedOptions ) {
169 foreach ( $expectedUrl as $eurl ) {
170 $this->assertContains( $eurl, $url );
171 }
172 foreach ( $expectedOptions as $ekey => $evalue ) {
173 $this->assertArrayHasKey( $ekey, $options );
174 $this->assertEquals( $options[$ekey], $evalue );
175 }
176 return $request;
177 }
178 );
179
180 if ( !is_null( $options ) ) {
181 $client->setClientOptions( $options );
182 }
183 if ( !is_null( $timeout ) ) {
184 $client->setTimeout( $timeout );
185 }
186
187 $result = $client->query( $sparql );
188 }
189
190}
Various HTTP related functions.
Definition Http.php:27
static userAgent()
A standard user-agent we can use for external requests.
Definition Http.php:129
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
Factory creating MWHttpRequest objects.
\MediaWiki\Sparql\SparqlClient
testBadQuery()
\Mediawiki\Sparql\SparqlException
testOptions( $sparql, $options, $timeout, $expectedUrl, $expectedOptions)
optionsProvider
Simple SPARQL client.
const MAX_GET_SIZE
Limit on how long can be the query to be sent by GET.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2806
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001