|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the sweetrdf/InMemoryStoreSqlite package and licensed under |
|
5
|
|
|
* the terms of the GPL-2 license. |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Konrad Abicht <[email protected]> |
|
8
|
|
|
* (c) Benjamin Nowack |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Tests\Integration\Store\InMemoryStoreSqlite\SPARQL11; |
|
15
|
|
|
|
|
16
|
|
|
use sweetrdf\InMemoryStoreSqlite\Log\Logger; |
|
17
|
|
|
use sweetrdf\InMemoryStoreSqlite\Parser\TurtleParser; |
|
18
|
|
|
use sweetrdf\InMemoryStoreSqlite\Store\InMemoryStoreSqlite; |
|
19
|
|
|
use sweetrdf\InMemoryStoreSqlite\StringReader; |
|
20
|
|
|
use Tests\TestCase; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Runs W3C tests from https://www.w3.org/2009/sparql/docs/tests/. |
|
24
|
|
|
* |
|
25
|
|
|
* Version: 2012-10-23 20:52 (sparql11-test-suite-20121023.tar.gz) |
|
26
|
|
|
* |
|
27
|
|
|
* Tests are located in the w3c-tests folder. |
|
28
|
|
|
*/ |
|
29
|
|
|
class ComplianceTestCase extends TestCase |
|
30
|
|
|
{ |
|
31
|
|
|
protected InMemoryStoreSqlite $store; |
|
32
|
|
|
|
|
33
|
|
|
protected string $dataGraphUri; |
|
34
|
|
|
|
|
35
|
|
|
protected string $manifestGraphUri; |
|
36
|
|
|
|
|
37
|
|
|
protected string $testPref; |
|
38
|
|
|
|
|
39
|
|
|
protected string $w3cTestsFolderPath; |
|
40
|
|
|
|
|
41
|
|
|
protected function setUp(): void |
|
42
|
|
|
{ |
|
43
|
|
|
parent::setUp(); |
|
44
|
|
|
|
|
45
|
|
|
// set graphs |
|
46
|
|
|
$this->dataGraphUri = 'http://arc/data/'; |
|
47
|
|
|
$this->manifestGraphUri = 'http://arc/manifest/'; |
|
48
|
|
|
|
|
49
|
|
|
/* |
|
50
|
|
|
* Setup a store instance to load test information and data. |
|
51
|
|
|
*/ |
|
52
|
|
|
$this->store = InMemoryStoreSqlite::createInstance(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Helper function to get expected query result. |
|
57
|
|
|
* |
|
58
|
|
|
* @return \SimpleXMLElement instance of \SimpleXMLElement representing the result |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function getExpectedResult(string $testUri) |
|
61
|
|
|
{ |
|
62
|
|
|
/* |
|
63
|
|
|
example: |
|
64
|
|
|
|
|
65
|
|
|
:group1 mf:result <group01.srx> |
|
66
|
|
|
*/ |
|
67
|
|
|
$res = $this->store->query(' |
|
68
|
|
|
PREFIX mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#> . |
|
69
|
|
|
SELECT * FROM <'.$this->manifestGraphUri.'> WHERE { |
|
70
|
|
|
<'.$testUri.'> mf:result ?resultFile . |
|
71
|
|
|
} |
|
72
|
|
|
'); |
|
73
|
|
|
|
|
74
|
|
|
// if no result was given, expect test is of type NegativeSyntaxTest11, |
|
75
|
|
|
// which has no data (group-data-X.ttl) and result (.srx) file. |
|
76
|
|
|
if (0 < \count($res['result']['rows'])) { |
|
77
|
|
|
return new \SimpleXMLElement(file_get_contents($res['result']['rows'][0]['resultFile'])); |
|
78
|
|
|
} else { |
|
79
|
|
|
return null; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Helper function to get test query for a given test. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $testUri |
|
87
|
|
|
* |
|
88
|
|
|
* @return string query to test |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function getTestQuery($testUri) |
|
91
|
|
|
{ |
|
92
|
|
|
/* |
|
93
|
|
|
example: |
|
94
|
|
|
|
|
95
|
|
|
:group1 mf:action [ |
|
96
|
|
|
qt:query <group01.rq> |
|
97
|
|
|
] |
|
98
|
|
|
*/ |
|
99
|
|
|
$query = $this->store->query(' |
|
100
|
|
|
PREFIX mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#> . |
|
101
|
|
|
PREFIX qt: <http://www.w3.org/2001/sw/DataAccess/tests/test-query#> . |
|
102
|
|
|
SELECT * FROM <'.$this->manifestGraphUri.'> WHERE { |
|
103
|
|
|
<'.$testUri.'> mf:action [ qt:query ?queryFile ] . |
|
104
|
|
|
} |
|
105
|
|
|
'); |
|
106
|
|
|
|
|
107
|
|
|
// if test is of type NegativeSyntaxTest11, mf:action points not to a blank node, |
|
108
|
|
|
// but directly to the query file. |
|
109
|
|
|
if (0 == \count($query['result']['rows'])) { |
|
110
|
|
|
$query = $this->store->query(' |
|
111
|
|
|
PREFIX mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#> . |
|
112
|
|
|
SELECT * FROM <'.$this->manifestGraphUri.'> WHERE { |
|
113
|
|
|
<'.$testUri.'> mf:action ?queryFile . |
|
114
|
|
|
} |
|
115
|
|
|
'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$query = file_get_contents($query['result']['rows'][0]['queryFile']); |
|
119
|
|
|
|
|
120
|
|
|
// add data graph information as FROM clause, because store can't handle default graph |
|
121
|
|
|
// queries. for more information see https://github.com/semsol/arc2/issues/72. |
|
122
|
|
|
if (false !== strpos($query, 'ASK') |
|
123
|
|
|
|| false !== strpos($query, 'CONSTRUCT') |
|
124
|
|
|
|| false !== strpos($query, 'SELECT')) { |
|
125
|
|
|
$query = str_replace('WHERE', 'FROM <'.$this->dataGraphUri.'> WHERE', $query); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $query; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Helper function to get test type. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $testUri |
|
135
|
|
|
* |
|
136
|
|
|
* @return string Type URI |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function getTestType($testUri) |
|
139
|
|
|
{ |
|
140
|
|
|
$type = $this->store->query(' |
|
141
|
|
|
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . |
|
142
|
|
|
SELECT * FROM <'.$this->manifestGraphUri.'> WHERE { |
|
143
|
|
|
<'.$testUri.'> rdf:type ?type . |
|
144
|
|
|
} |
|
145
|
|
|
'); |
|
146
|
|
|
|
|
147
|
|
|
return $type['result']['rows'][0]['type']; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Transforms query result to a \SimpleXMLElement instance for later comparison. |
|
152
|
|
|
* |
|
153
|
|
|
* @return \SimpleXMLElement |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function getXmlVersionOfResult(array $result) |
|
156
|
|
|
{ |
|
157
|
|
|
$w = new \XMLWriter(); |
|
158
|
|
|
$w->openMemory(); |
|
159
|
|
|
$w->startDocument('1.0'); |
|
160
|
|
|
|
|
161
|
|
|
// sparql (root element) |
|
162
|
|
|
$w->startElement('sparql'); |
|
163
|
|
|
$w->writeAttribute('xmlns', 'http://www.w3.org/2005/sparql-results#'); |
|
164
|
|
|
|
|
165
|
|
|
// sparql > head |
|
166
|
|
|
$w->startElement('head'); |
|
167
|
|
|
|
|
168
|
|
|
foreach ($result['result']['variables'] as $var) { |
|
169
|
|
|
$w->startElement('variable'); |
|
170
|
|
|
$w->writeAttribute('name', $var); |
|
171
|
|
|
$w->endElement(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
// end sparql > head |
|
175
|
|
|
$w->endElement(); |
|
176
|
|
|
|
|
177
|
|
|
// sparql > results |
|
178
|
|
|
$w->startElement('results'); |
|
179
|
|
|
|
|
180
|
|
|
foreach ($result['result']['rows'] as $row) { |
|
181
|
|
|
/* |
|
182
|
|
|
example: |
|
183
|
|
|
|
|
184
|
|
|
<result> |
|
185
|
|
|
<binding name="s"> |
|
186
|
|
|
<uri>http://example/s1</uri> |
|
187
|
|
|
</binding> |
|
188
|
|
|
</result> |
|
189
|
|
|
*/ |
|
190
|
|
|
|
|
191
|
|
|
// new result element |
|
192
|
|
|
$w->startElement('result'); |
|
193
|
|
|
|
|
194
|
|
|
foreach ($result['result']['variables'] as $var) { |
|
195
|
|
|
if (empty($row[$var])) { |
|
196
|
|
|
continue; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
// sparql > results > result > binding |
|
200
|
|
|
$w->startElement('binding'); |
|
201
|
|
|
$w->writeAttribute('name', $var); |
|
202
|
|
|
|
|
203
|
|
|
// if a variable type is set |
|
204
|
|
|
if (isset($row[$var.' type'])) { |
|
205
|
|
|
// uri |
|
206
|
|
|
if ('uri' == $row[$var.' type']) { |
|
207
|
|
|
// example: <uri>http://example/s1</uri> |
|
208
|
|
|
$w->startElement('uri'); |
|
209
|
|
|
$w->text($row[$var]); |
|
210
|
|
|
$w->endElement(); |
|
211
|
|
|
} elseif ('literal' == $row[$var.' type']) { |
|
212
|
|
|
// example: <literal datatype="http://www.w3.org/2001/XMLSchema#integer">9</literal> |
|
213
|
|
|
$w->startElement('literal'); |
|
214
|
|
|
|
|
215
|
|
|
// its not part of the result set, but expected later on |
|
216
|
|
|
if (true === ctype_digit($row[$var])) { |
|
217
|
|
|
$w->writeAttribute('datatype', 'http://www.w3.org/2001/XMLSchema#integer'); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
$w->text($row[$var]); |
|
221
|
|
|
$w->endElement(); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
// end sparql > results > result > binding |
|
226
|
|
|
$w->endElement(); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
// end result |
|
230
|
|
|
$w->endElement(); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
// add <result></result> if no data were found |
|
234
|
|
|
if (0 == \count($result['result']['rows'])) { |
|
235
|
|
|
$w->startElement('result'); |
|
236
|
|
|
$w->endElement(); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
// end sparql > results |
|
240
|
|
|
$w->endElement(); |
|
241
|
|
|
|
|
242
|
|
|
// end sparql |
|
243
|
|
|
$w->endElement(); |
|
244
|
|
|
|
|
245
|
|
|
return new \SimpleXMLElement($w->outputMemory(true)); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @param string $query |
|
250
|
|
|
*/ |
|
251
|
|
|
protected function makeQueryA1Liner($query) |
|
252
|
|
|
{ |
|
253
|
|
|
return preg_replace('/\s\s+/', ' ', $query); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|