133
|
1 |
<?php |
|
2 |
|
|
3 |
use Mockery as m; |
|
4 |
|
|
5 |
use EasyRdf\Http; |
|
6 |
|
|
7 |
class BnfResolverTest extends TestCase |
|
8 |
{ |
|
9 |
const BNF_EMPTY_RDF_RESP = <<<EOT |
|
10 |
{ |
|
11 |
"head" : { |
|
12 |
"vars" : [ |
|
13 |
"s", |
|
14 |
"o" |
|
15 |
] |
|
16 |
}, |
|
17 |
"results" : { |
|
18 |
"bindings" : [ ] |
|
19 |
} |
|
20 |
} |
|
21 |
EOT; |
|
22 |
const BNF_RDF_RESP = <<<EOT |
|
23 |
{ |
|
24 |
"head":{ |
|
25 |
"link":[ |
|
26 |
|
|
27 |
], |
|
28 |
"vars":[ |
|
29 |
"s", |
|
30 |
"o" |
|
31 |
] |
|
32 |
}, |
|
33 |
"results":{ |
|
34 |
"distinct":false, |
|
35 |
"ordered":true, |
|
36 |
"bindings":[ |
|
37 |
{ |
|
38 |
"s":{ |
|
39 |
"type":"uri", |
|
40 |
"value":"http://data.bnf.fr/ark:/12148/cb11946662b" |
|
41 |
}, |
|
42 |
"o":{ |
|
43 |
"type":"literal", |
|
44 |
"xml:lang":"fr", |
|
45 |
"value":"Parents et enfants" |
|
46 |
} |
|
47 |
} |
|
48 |
] |
|
49 |
} |
|
50 |
} |
|
51 |
EOT; |
|
52 |
|
|
53 |
const BNF_2_ID_RDF_RESP = <<<EOT |
|
54 |
{ |
|
55 |
"head":{ |
|
56 |
"link":[ |
|
57 |
|
|
58 |
], |
|
59 |
"vars":[ |
|
60 |
"s", |
|
61 |
"o" |
|
62 |
] |
|
63 |
}, |
|
64 |
"results":{ |
|
65 |
"distinct":false, |
|
66 |
"ordered":true, |
|
67 |
"bindings":[ |
|
68 |
{ |
|
69 |
"s":{ |
|
70 |
"type":"uri", |
|
71 |
"value":"http://data.bnf.fr/ark:/12148/cb11946662b" |
|
72 |
}, |
|
73 |
"o":{ |
|
74 |
"type":"literal", |
|
75 |
"xml:lang":"fr", |
|
76 |
"value":"Parents et enfants" |
|
77 |
} |
|
78 |
}, |
|
79 |
{ |
|
80 |
"s":{ |
|
81 |
"type":"uri", |
|
82 |
"value":"http://data.bnf.fr/ark:/12148/cb11965628b" |
|
83 |
}, |
|
84 |
"o":{ |
|
85 |
"type":"literal", |
|
86 |
"xml:lang":"fr", |
|
87 |
"value":"Frères et soeurs" |
|
88 |
} |
|
89 |
} |
|
90 |
] |
|
91 |
} |
|
92 |
} |
|
93 |
EOT; |
|
94 |
|
|
95 |
|
|
96 |
private function setUpSparqlClient($queryResult) { |
|
97 |
$response = m::mock('EasyRdf\Http\Response') |
|
98 |
->shouldReceive('isSuccessful')->andReturn(true) |
|
99 |
->shouldReceive('getBody')->andReturn($queryResult) |
|
100 |
->shouldReceive('getStatus')->andReturn(200) |
|
101 |
->shouldReceive('getHeader')->andReturn('application/sparql-results+json;charset=UTF-8') |
|
102 |
->mock(); |
|
103 |
$this->httpClient = m::mock('EasyRdf\Http\Client') |
|
104 |
->shouldReceive('setConfig') |
|
105 |
->shouldReceive('resetParameters') |
|
106 |
->shouldReceive('setHeaders')//->with(m::on(function($headers) { print("HEADER => $headers\n"); return true;}),m::on(function($value) { print("VALUE => $value\n"); return true;})) |
|
107 |
->shouldReceive('setMethod') |
|
108 |
->shouldReceive('setUri')//->with(m::on(function($uri) { print($uri."\n"); return true;})) |
|
109 |
->shouldReceive('request')->andReturn($response) |
|
110 |
->mock(); |
|
111 |
Http::setDefaultHttpClient($this->httpClient); |
|
112 |
$this->bnfResolver = $this->app->make('CorpusParole\Services\BnfResolverInterface'); |
|
113 |
} |
|
114 |
|
|
115 |
public function setUp() { |
|
116 |
parent::setUp(); |
|
117 |
} |
|
118 |
|
|
119 |
public function tearDown() { |
|
120 |
parent::tearDown(); |
|
121 |
m::close(); |
|
122 |
} |
|
123 |
|
|
124 |
/** |
|
125 |
* Just test the setup |
|
126 |
* |
|
127 |
* @return void |
|
128 |
*/ |
|
129 |
public function testSetUp() { |
|
130 |
$this->assertTrue(true); |
|
131 |
} |
|
132 |
|
|
133 |
/** |
|
134 |
* resolve parents et enfants |
|
135 |
* @return void |
|
136 |
*/ |
|
137 |
public function testResolveSingleId() { |
|
138 |
|
|
139 |
$this->setUpSparqlClient(self::BNF_RDF_RESP); |
|
140 |
|
|
141 |
$reslabel = $this->bnfResolver->getLabel("http://data.bnf.fr/ark:/12148/cb11946662b"); |
|
142 |
|
|
143 |
$this->assertEquals('parents et enfants', $reslabel, "Result must be parents et enfants"); |
|
144 |
} |
|
145 |
|
|
146 |
/** |
|
147 |
* resolve parents et enfants ark url |
|
148 |
* @return void |
|
149 |
*/ |
|
150 |
public function testResolveSingleIdArkUrl() { |
|
151 |
|
|
152 |
$this->setUpSparqlClient(self::BNF_RDF_RESP); |
|
153 |
|
|
154 |
$reslabel = $this->bnfResolver->getLabel("http://ark.bnf.fr/ark:/12148/cb11946662b"); |
|
155 |
|
|
156 |
$this->assertEquals('parents et enfants', $reslabel, "Result must be parents et enfants"); |
|
157 |
} |
|
158 |
|
|
159 |
/** |
|
160 |
* resolve parents et enfants, ark id |
|
161 |
* @return void |
|
162 |
*/ |
|
163 |
public function testResolveSingleIdArk() { |
|
164 |
|
|
165 |
$this->setUpSparqlClient(self::BNF_RDF_RESP); |
|
166 |
|
|
167 |
$reslabel = $this->bnfResolver->getLabel("ark:/12148/cb11946662b"); |
|
168 |
|
|
169 |
$this->assertEquals('parents et enfants', $reslabel, "Result must be parents et enfants"); |
|
170 |
} |
|
171 |
|
|
172 |
|
|
173 |
/** |
|
174 |
* resolve foo |
|
175 |
* @return void |
|
176 |
*/ |
|
177 |
public function testResolveBadId() { |
|
178 |
|
|
179 |
$this->setUpSparqlClient(self::BNF_EMPTY_RDF_RESP); |
|
180 |
|
|
181 |
$reslabel = $this->bnfResolver->getLabel('ark:/12148/cb12345678b'); |
|
182 |
|
|
183 |
$this->assertNull($reslabel, "Result must be null"); |
|
184 |
} |
|
185 |
|
|
186 |
/** |
|
187 |
* resolve foo |
|
188 |
* @return void |
|
189 |
* @expectedException CorpusParole\Services\BnfResolverException |
|
190 |
* @expectedExceptionMessage the provided id "21dsasd;;" is not a BNF id |
|
191 |
* @expectedExceptionCode 0 |
|
192 |
*/ |
|
193 |
public function testResolveBadFormat() { |
|
194 |
|
|
195 |
$this->setUpSparqlClient(self::BNF_EMPTY_RDF_RESP); |
|
196 |
|
|
197 |
$reslabel = $this->bnfResolver->getLabel('21dsasd;;'); |
|
198 |
} |
|
199 |
|
|
200 |
/** |
|
201 |
* resolve foo |
|
202 |
* @return void |
|
203 |
* @expectedException CorpusParole\Services\BnfResolverException |
|
204 |
* @expectedExceptionMessage the provided id "http://sdsasd.org/foo" is not a BNF id |
|
205 |
* @expectedExceptionCode 0 |
|
206 |
*/ |
|
207 |
public function testResolveBadFormatFullId() { |
|
208 |
|
|
209 |
$this->setUpSparqlClient(self::BNF_EMPTY_RDF_RESP); |
|
210 |
|
|
211 |
$reslabel = $this->bnfResolver->getLabel('http://sdsasd.org/foo'); |
|
212 |
} |
|
213 |
|
|
214 |
|
|
215 |
/** |
|
216 |
* resolve parents et enfants and frères et soeurs |
|
217 |
* @return void |
|
218 |
*/ |
|
219 |
public function testResolveMultipleId() { |
|
220 |
|
|
221 |
$this->setUpSparqlClient(self::BNF_2_ID_RDF_RESP); |
|
222 |
|
|
223 |
$reslabels = $this->bnfResolver->getLabels(['ark:/12148/cb11965628b','ark:/12148/cb11946662b']); |
|
224 |
|
|
225 |
$this->assertCount(2, $reslabels, "Must have 2 results"); |
|
226 |
$this->assertArrayHasKey('ark:/12148/cb11946662b', $reslabels); |
|
227 |
$this->assertArrayHasKey('ark:/12148/cb11965628b', $reslabels); |
|
228 |
|
|
229 |
$this->assertEquals('parents et enfants', $reslabels['ark:/12148/cb11946662b'], "Result for ark:/12148/cb11965628b must be parents et enfants"); |
|
230 |
$this->assertEquals('frères et soeurs', $reslabels['ark:/12148/cb11965628b'], "Result for ark:/12148/cb11946662b must be frères et soeurs"); |
|
231 |
} |
|
232 |
|
|
233 |
/** |
|
234 |
* resolve parents et enfants and frères et soeurs |
|
235 |
* @return void |
|
236 |
*/ |
|
237 |
public function testResolveMultipleFullIdArkUrl() { |
|
238 |
|
|
239 |
$this->setUpSparqlClient(self::BNF_2_ID_RDF_RESP); |
|
240 |
|
|
241 |
$reslabels = $this->bnfResolver->getLabels(['http://ark.bnf.fr/ark:/12148/cb11946662b','http://ark.bnf.fr/ark:/12148/cb11965628b']); |
|
242 |
|
|
243 |
$this->assertCount(2, $reslabels, "Must have 2 results"); |
|
244 |
$this->assertArrayHasKey('http://ark.bnf.fr/ark:/12148/cb11946662b', $reslabels); |
|
245 |
$this->assertArrayHasKey('http://ark.bnf.fr/ark:/12148/cb11965628b', $reslabels); |
|
246 |
|
|
247 |
$this->assertEquals('parents et enfants', $reslabels['http://ark.bnf.fr/ark:/12148/cb11946662b'], "Result for http://ark.bnf.fr/ark:/12148/cb11946662b must be parents et enfants"); |
|
248 |
$this->assertEquals('frères et soeurs', $reslabels['http://ark.bnf.fr/ark:/12148/cb11965628b'], "Result for http://ark.bnf.fr/ark:/12148/cb11965628b must be frères et soeurs"); |
|
249 |
} |
|
250 |
|
|
251 |
/** |
|
252 |
* resolve parents et enfants and frères et soeurs |
|
253 |
* @return void |
|
254 |
*/ |
|
255 |
public function testResolveMultipleFullIdDataUrl() { |
|
256 |
|
|
257 |
$this->setUpSparqlClient(self::BNF_2_ID_RDF_RESP); |
|
258 |
|
|
259 |
$reslabels = $this->bnfResolver->getLabels(['http://data.bnf.fr/ark:/12148/cb11946662b','http://data.bnf.fr/ark:/12148/cb11965628b']); |
|
260 |
|
|
261 |
$this->assertCount(2, $reslabels, "Must have 2 results"); |
|
262 |
$this->assertArrayHasKey('http://data.bnf.fr/ark:/12148/cb11946662b', $reslabels); |
|
263 |
$this->assertArrayHasKey('http://data.bnf.fr/ark:/12148/cb11965628b', $reslabels); |
|
264 |
|
|
265 |
$this->assertEquals('parents et enfants', $reslabels['http://data.bnf.fr/ark:/12148/cb11946662b'], "Result for http://data.bnf.fr/ark:/12148/cb11946662b must be parents et enfants"); |
|
266 |
$this->assertEquals('frères et soeurs', $reslabels['http://data.bnf.fr/ark:/12148/cb11965628b'], "Result for http://data.bnf.fr/ark:/12148/cb11965628b must be frères et soeurs"); |
|
267 |
} |
|
268 |
|
|
269 |
/** |
|
270 |
* check query |
|
271 |
* @return void |
|
272 |
*/ |
|
273 |
public function testQuery() { |
|
274 |
|
|
275 |
$expectedUri = config('corpusparole.bnf_query_url')."?query=SELECT+%3Fs+%3Fo+WHERE+%7B%7B+%3Chttp%3A%2F%2Fdata.bnf.fr%2Fark%3A%2F12148%2Fcb11946662b%3E+%3Chttp%3A%2F%2Fwww.w3.org%2F2004%2F02%2Fskos%2Fcore%23prefLabel%3E+%3Fo.+%3Fs+%3Chttp%3A%2F%2Fwww.w3.org%2F2004%2F02%2Fskos%2Fcore%23prefLabel%3E+%3Fo.+FILTER%28%3Fs+%3D+%3Chttp%3A%2F%2Fdata.bnf.fr%2Fark%3A%2F12148%2Fcb11946662b%3E+%26%26+lang%28%3Fo%29+%3D+%22fr%22%29%7D+UNION+%7B+%3Chttp%3A%2F%2Fdata.bnf.fr%2Fark%3A%2F12148%2Fcb11965628b%3E+%3Chttp%3A%2F%2Fwww.w3.org%2F2004%2F02%2Fskos%2Fcore%23prefLabel%3E+%3Fo.+%3Fs+%3Chttp%3A%2F%2Fwww.w3.org%2F2004%2F02%2Fskos%2Fcore%23prefLabel%3E+%3Fo.+FILTER%28%3Fs+%3D+%3Chttp%3A%2F%2Fdata.bnf.fr%2Fark%3A%2F12148%2Fcb11965628b%3E+%26%26+lang%28%3Fo%29+%3D+%22fr%22%29%7D%7D"; |
|
276 |
$response = m::mock('EasyRdf\Http\Response') |
|
277 |
->shouldReceive('isSuccessful')->andReturn(true) |
|
278 |
->shouldReceive('getBody')->andReturn(self::BNF_EMPTY_RDF_RESP) //result not important |
|
279 |
->shouldReceive('getStatus')->andReturn(200) |
|
280 |
->shouldReceive('getHeader')->andReturn('application/sparql-results+json;charset=UTF-8') |
|
281 |
->mock(); |
|
282 |
$this->httpClient = m::mock('EasyRdf\Http\Client') |
|
283 |
->shouldReceive('setConfig') |
|
284 |
->shouldReceive('resetParameters') |
|
285 |
->shouldReceive('setHeaders')//->with(m::on(function($headers) { print("HEADER => $headers\n"); return true;}),m::on(function($value) { print("VALUE => $value\n"); return true;})) |
|
286 |
->shouldReceive('setMethod') |
|
287 |
->shouldReceive('setUri') |
|
288 |
->with($expectedUri) |
|
289 |
//->with(m::on(function($uri) { print($uri."\n"); return true;})) |
|
290 |
->shouldReceive('request')->andReturn($response) |
|
291 |
->mock(); |
|
292 |
Http::setDefaultHttpClient($this->httpClient); |
|
293 |
$this->bnfResolver = $this->app->make('CorpusParole\Services\BnfResolverInterface'); |
|
294 |
|
|
295 |
$reslabels = $this->bnfResolver->getLabels(['ark:/12148/cb11946662b','ark:/12148/cb11965628b']); |
|
296 |
|
|
297 |
} |
|
298 |
|
|
299 |
} |