Passed
Push — master ( f535b8...cc5eb6 )
by Konrad
11:06
created

testSparqlClientCompatibility()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 14
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 29
rs 9.7998
1
<?php
2
3
/*
4
 * This file is part of the sweetrdf/InMemoryStoreSqlite package and licensed under
5
 * the terms of the GPL-3 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;
15
16
use GuzzleHttp\Client;
17
use GuzzleHttp\Psr7\Request;
18
use simpleRdf\DataFactory;
19
use sparqlClient\Connection;
20
use sweetrdf\InMemoryStoreSqlite\KeyValueBag;
21
use sweetrdf\InMemoryStoreSqlite\Log\LoggerPool;
22
use sweetrdf\InMemoryStoreSqlite\NamespaceHelper;
23
use sweetrdf\InMemoryStoreSqlite\PDOSQLiteAdapter;
24
use sweetrdf\InMemoryStoreSqlite\Store\InMemoryStoreSqlite;
25
use sweetrdf\InMemoryStoreSqlite\StringReader;
26
use Tests\TestCase;
27
28
class InMemoryStoreSqliteTest extends TestCase
29
{
30
    protected function setUp(): void
31
    {
32
        parent::setUp();
33
34
        $this->subjectUnderTest = InMemoryStoreSqlite::createInstance();
35
    }
36
37
    /*
38
     * Tests for createInstance
39
     */
40
41
    public function testCreateInstance()
42
    {
43
        $expected = new InMemoryStoreSqlite(
44
            new PDOSQLiteAdapter(),
45
            new DataFactory(),
46
            new NamespaceHelper(),
47
            new LoggerPool(),
48
            new KeyValueBag(),
49
            new StringReader()
50
        );
51
        $this->assertEquals($expected, InMemoryStoreSqlite::createInstance());
52
    }
53
54
    /*
55
     * Tests for getDBVersion
56
     */
57
58
    /**
59
     * just check pattern
60
     */
61
    public function testGetDBVersion()
62
    {
63
        $pattern = '/[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}/';
64
        $result = preg_match($pattern, $this->subjectUnderTest->getDBVersion(), $match);
65
        $this->assertEquals(1, $result);
66
    }
67
68
    /**
69
     * This test checks gathering of freshly created resources.
70
     */
71
    public function testInsertSaftRegressionTest2()
72
    {
73
        $res = $this->subjectUnderTest->query('INSERT INTO <http://localhost/Saft/TestGraph/> {<http://foo/1> <http://foo/2> <http://foo/3> . }');
0 ignored issues
show
Unused Code introduced by
The assignment to $res is dead and can be removed.
Loading history...
74
75
        $res1 = $this->subjectUnderTest->query('SELECT * FROM <http://localhost/Saft/TestGraph/> WHERE {?s ?p ?o.}');
76
        $this->assertEquals(1, \count($res1['result']['rows']));
77
78
        $res2 = $this->subjectUnderTest->query('SELECT * WHERE {?s ?p ?o.}');
79
        $this->assertEquals(1, \count($res2['result']['rows']));
80
81
        $res2 = $this->subjectUnderTest->query('SELECT ?s ?p ?o WHERE {?s ?p ?o.}');
82
        $this->assertEquals(1, \count($res2['result']['rows']));
83
    }
84
85
    /**
86
     * This test checks side effects of update operations on different graphs.
87
     *
88
     * We add 1 triple to 1 and another to another graph.
89
     * Afterwards first graph is removed.
90
     * In the end second graph still should contain its triples.
91
     */
92
    public function testInsertSaftRegressionTest3()
93
    {
94
        $this->subjectUnderTest->query(
95
            'INSERT INTO <http://localhost/Saft/TestGraph/> {<http://localhost/Saft/TestGraph/> <http://localhost/Saft/TestGraph/> <http://localhost/Saft/TestGraph/> . }'
96
        );
97
        $this->subjectUnderTest->query(
98
            'INSERT INTO <http://second-graph/> {<http://second-graph/0> <http://second-graph/1> <http://second-graph/2> . }'
99
        );
100
        $this->subjectUnderTest->query(
101
            'DELETE FROM <http://localhost/Saft/TestGraph/>'
102
        );
103
104
        $res = $this->subjectUnderTest->query('SELECT * FROM <http://second-graph/> WHERE {?s ?p ?o.}');
105
        $this->assertEquals(1, \count($res['result']['rows']));
106
    }
107
108
    public function testAddQuads()
109
    {
110
        // check data at the beginning
111
        $res = $this->subjectUnderTest->query('SELECT * WHERE {?s ?p ?o.}');
112
        $this->assertCount(0, $res['result']['rows']);
113
114
        /*
115
         * add quads
116
         */
117
        $df = new DataFactory();
118
        $graph = 'http://graph';
119
120
        // q1
121
        $q1 = $df->quad(
122
            $df->namedNode('http://a'),
123
            $df->namedNode('http://b'),
124
            $df->namedNode('http://c'),
125
            $df->namedNode($graph)
126
        );
127
128
        // q2
129
        $q2 = $df->quad(
130
            $df->blankNode('123'),
131
            $df->namedNode('http://b'),
132
            $df->literal('foobar', 'de'),
133
            $df->namedNode($graph)
134
        );
135
136
        $quads = [$q1, $q2];
137
138
        $this->subjectUnderTest->addQuads($quads);
139
140
        // check after quads were added
141
        $res = $this->subjectUnderTest->query('SELECT * FROM <'.$graph.'> WHERE {?s ?p ?o.}');
142
        $this->assertEquals(
143
            [
144
                [
145
                    's' => 'http://a',
146
                    's type' => 'uri',
147
                    'p' => 'http://b',
148
                    'p type' => 'uri',
149
                    'o' => 'http://c',
150
                    'o type' => 'uri',
151
                ],
152
                [
153
                    's' => $res['result']['rows'][1]['s'], // dynamic value
154
                    's type' => 'bnode',
155
                    'p' => 'http://b',
156
                    'p type' => 'uri',
157
                    'o' => 'foobar',
158
                    'o type' => 'literal',
159
                    'o lang' => 'de',
160
                ],
161
            ],
162
            $res['result']['rows']
163
        );
164
    }
165
166
    /**
167
     * Tests compatibility with sweetrdf/sparqlClient.
168
     *
169
     * It queries a SPARQL endpoint and adds result to store.
170
     */
171
    public function testSparqlClientCompatibility()
172
    {
173
        /*
174
         * get data from a SPARQL endpoint
175
         */
176
        $httpClient = new Client();
177
        $dataFactory = new DataFactory();
178
        $connection = new Connection($httpClient, $dataFactory);
179
        $query = 'SELECT * WHERE {?s ?p ?o} limit 5';
180
        $url = 'https://arche-sparql.acdh-dev.oeaw.ac.at/sparql?query=';
181
        $query = new Request('GET', $url.rawurlencode($query));
182
        $statement = $connection->query($query);
183
184
        /*
185
         * add result to the store
186
         */
187
        $dataFactory = new DataFactory();
188
        $quads = [];
189
        foreach ($statement as $entry) {
190
            $quads[] = $dataFactory->quad($entry->s, $entry->p, $entry->o);
191
        }
192
193
        $store = InMemoryStoreSqlite::createInstance();
194
        $store->addQuads($quads);
195
196
        /*
197
         * check result
198
         */
199
        $this->assertCount(5, $store->query('SELECT * WHERE {?s ?p ?o.}')['result']['rows']);
200
    }
201
}
202