ComplianceTestCase::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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