4
|
1 |
<?php |
|
2 |
|
|
3 |
use CorpusParole\Libraries\Sparql\SparqlClient; |
|
4 |
use CorpusParole\Libraries\CorpusParoleException; |
|
5 |
use CorpusParole\Models\Document; |
|
6 |
|
|
7 |
use GuzzleHttp\Client; |
|
8 |
use GuzzleHttp\Handler\MockHandler; |
|
9 |
use GuzzleHttp\HandlerStack; |
|
10 |
use GuzzleHttp\Psr7\Response; |
|
11 |
use GuzzleHttp\Middleware; |
|
12 |
|
|
13 |
use Mockery as m; |
|
14 |
|
|
15 |
class SparqlClientTest extends TestCase { |
|
16 |
|
|
17 |
const TEST_DOC_ADD = <<<EOT |
|
18 |
<http://cocoon.huma-num.fr/data/ala/ALA_608_22km.wav> <http://purl.org/dc/elements/1.1/type> "dialogue"^^<http://www.language-archives.org/OLAC/1.1/discourse-type> . |
|
19 |
<http://cocoon.huma-num.fr/data/ala/ALA_608_22km.wav> <http://purl.org/dc/elements/1.1/type> "drama"^^<http://www.language-archives.org/OLAC/1.1/discourse-type> . |
|
20 |
EOT; |
|
21 |
|
|
22 |
const TEST_DOC_DELETE = <<<EOT |
|
23 |
<http://cocoon.huma-num.fr/data/ala/ALA_608_22km.wav> <http://purl.org/dc/elements/1.1/type> "dialogue"^^<http://www.language-archives.org/OLAC/1.1/discourse-type> . |
|
24 |
EOT; |
|
25 |
|
|
26 |
|
|
27 |
private $container; |
|
28 |
private $sparqlClients; |
|
29 |
private $transactionUrl; |
|
30 |
|
|
31 |
private $responsesArray; |
|
32 |
|
|
33 |
function __construct(string $name = null) { |
|
34 |
parent::__construct($name); |
|
35 |
} |
|
36 |
|
|
37 |
private function getSparqlClient($responses, &$container, $sparqlClient) { |
|
38 |
$mock = new MockHandler($responses); |
|
39 |
$handler = HandlerStack::create($mock); |
|
40 |
$history = Middleware::history($container); |
|
41 |
$handler->push($history); |
|
42 |
$httpClient = new Client(['handler' => $handler, 'http_errors' => false]); |
|
43 |
|
|
44 |
return new SparqlClient($httpClient, $sparqlClient); |
|
45 |
} |
|
46 |
|
|
47 |
public function setUp() { |
|
48 |
parent::setUp(); |
|
49 |
$this->sesameRepository = config('corpusparole.sesame_repository'); |
|
50 |
$this->transactionUrl = config('corpusparole.sesame_query_url').'/transactions/64a5937f-c112-d014-a044-f0123b93'; |
|
51 |
|
|
52 |
$this->addGraph = new EasyRdf\Graph("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", SparqlClientTest::TEST_DOC_ADD); |
|
53 |
$this->deleteGraph = new EasyRdf\Graph("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", SparqlClientTest::TEST_DOC_DELETE); |
|
54 |
|
|
55 |
} |
|
56 |
|
|
57 |
public function tearDown() { |
|
58 |
m::close(); |
|
59 |
parent::tearDown(); |
|
60 |
} |
|
61 |
|
|
62 |
public function testCreateTransaction() { |
|
63 |
$responses = [ |
|
64 |
new Response(201, ['Location' => "$this->transactionUrl"]), |
|
65 |
]; |
|
66 |
$container = []; |
|
67 |
$sparqlClientMock = m::mock('EasyRdf\Sparql\Client'); |
|
68 |
|
|
69 |
$documentRepository = $this->getSparqlClient($responses, $container, $sparqlClientMock); |
|
70 |
|
|
71 |
$documentRepository->startTransaction(); |
|
72 |
|
|
73 |
$this->assertEquals($this->transactionUrl, $documentRepository->getCurrentTransactionUrl(), 'Must have correct transaction url'); |
|
74 |
} |
|
75 |
|
|
76 |
public function testCreateTransactionHistory() { |
|
77 |
$responses = [ |
|
78 |
new Response(201, ['Location' => "$this->transactionUrl"]), |
|
79 |
]; |
|
80 |
$container = []; |
|
81 |
$sparqlClientMock = m::mock('EasyRdf\Sparql\Client'); |
|
82 |
|
|
83 |
$documentRepository = $this->getSparqlClient($responses, $container, $sparqlClientMock); |
|
84 |
|
|
85 |
$documentRepository->startTransaction(); |
|
86 |
|
|
87 |
$this->assertCount(1, $container, 'One request'); |
|
88 |
$req = $container[0]['request']; |
|
89 |
$this->assertEquals("$this->sesameRepository/transactions", (string)$req->getUri(), "url must be ok"); |
|
90 |
$this->assertEquals('POST', $container[0]['request']->getMethod(), "methos is POST"); |
|
91 |
} |
|
92 |
|
|
93 |
public function testRollbackTransaction() { |
|
94 |
$responses = [ |
|
95 |
new Response(201, ['Location' => "$this->transactionUrl"]), |
|
96 |
new Response(204) |
|
97 |
]; |
|
98 |
$container = []; |
|
99 |
$sparqlClientMock = m::mock('EasyRdf\Sparql\Client'); |
|
100 |
|
|
101 |
$documentRepository = $this->getSparqlClient($responses, $container, $sparqlClientMock); |
|
102 |
|
|
103 |
$documentRepository->startTransaction(); |
|
104 |
$documentRepository->rollback(); |
|
105 |
|
|
106 |
$this->assertCount(2, $container, '2 requests'); |
|
107 |
|
|
108 |
$this->assertNull($documentRepository->getCurrentTransactionUrl(), "Current Transaction url must be null"); |
|
109 |
|
|
110 |
$req = $container[1]['request']; |
|
111 |
|
|
112 |
$this->assertEquals($this->transactionUrl, (string)$req->getUri(), "uri must be the transaction url"); |
|
113 |
$this->assertEquals('DELETE', $req->getMethod(), "Method must be DELETE"); |
|
114 |
|
|
115 |
} |
|
116 |
|
|
117 |
public function testCommitTransaction() { |
|
118 |
$responses = [ |
|
119 |
new Response(201, ['Location' => "$this->transactionUrl"]), |
|
120 |
new Response(200) |
|
121 |
]; |
|
122 |
$container = []; |
|
123 |
$sparqlClientMock = m::mock('EasyRdf\Sparql\Client'); |
|
124 |
|
|
125 |
$documentRepository = $this->getSparqlClient($responses, $container, $sparqlClientMock); |
|
126 |
|
|
127 |
$documentRepository->startTransaction(); |
|
128 |
$documentRepository->commit(); |
|
129 |
|
|
130 |
|
|
131 |
$this->assertCount(2, $container, '2 requests'); |
|
132 |
|
|
133 |
$this->assertNull($documentRepository->getCurrentTransactionUrl(), "Current Transaction url must be null"); |
|
134 |
|
|
135 |
$req = $container[1]['request']; |
|
136 |
|
|
137 |
$this->assertEquals($this->transactionUrl."?action=COMMIT", (string)$req->getUri(), "uri must be the transaction url"); |
|
138 |
$this->assertEquals('PUT', $req->getMethod(), "Method must be PUT"); |
|
139 |
|
|
140 |
} |
|
141 |
|
|
142 |
/** |
|
143 |
* @expectedException CorpusParole\Libraries\CorpusParoleException |
|
144 |
*/ |
|
145 |
public function testCommitTransactionFail() { |
|
146 |
$responses = [ |
|
147 |
new Response(201, ['Location' => "$this->transactionUrl"]), |
|
148 |
new Response(404, [], "Not found") |
|
149 |
]; |
|
150 |
$container = []; |
|
151 |
$sparqlClientMock = m::mock('EasyRdf\Sparql\Client'); |
|
152 |
|
|
153 |
$documentRepository = $this->getSparqlClient($responses, $container, $sparqlClientMock); |
|
154 |
|
|
155 |
$documentRepository->startTransaction(); |
|
156 |
$documentRepository->commit(); |
|
157 |
|
|
158 |
} |
|
159 |
|
|
160 |
|
|
161 |
public function testAdd() { |
|
162 |
|
|
163 |
$responses = [ |
|
164 |
new Response(201, ['Location' => "$this->transactionUrl"]), |
|
165 |
new Response(204) |
|
166 |
]; |
|
167 |
$container = []; |
|
168 |
$sparqlClientMock = m::mock('EasyRdf\Sparql\Client'); |
|
169 |
|
|
170 |
$documentRepository = $this->getSparqlClient($responses, $container, $sparqlClientMock); |
|
171 |
|
|
172 |
$documentRepository->startTransaction(); |
|
173 |
$documentRepository->add($this->addGraph); |
|
174 |
|
|
175 |
$this->assertCount(2, $container, '2 requests'); |
|
176 |
|
|
177 |
$this->assertNotNull($documentRepository->getCurrentTransactionUrl(), "Current Transaction url must be not null"); |
|
178 |
|
|
179 |
$req = $container[1]['request']; |
|
180 |
|
|
181 |
$this->assertEquals($this->transactionUrl."?action=UPDATE", (string)$req->getUri(), "uri must be the transaction url"); |
|
182 |
$this->assertEquals('PUT', $req->getMethod(), "Method must be PUT"); |
|
183 |
$this->assertEquals(['application/x-www-form-urlencoded'], $req->getHeader('Content-type'), "content type must be form urlencoded"); |
|
184 |
|
|
185 |
$data = []; |
|
186 |
parse_str($req->getBody(), $data); |
|
187 |
|
|
188 |
$this->assertArrayHasKey('update', $data, "Submitted data must have updarte parameter"); |
|
189 |
$this->assertContains('INSERT DATA {', $data['update'], 'update parameter must contain INSERT'); |
|
190 |
$this->assertContains('GRAPH <'.$this->addGraph->getUri().'> {', $data['update'], 'update parameter must contain GRAPH id'); |
|
191 |
$this->assertContains('<http://cocoon.huma-num.fr/data/ala/ALA_608_22km.wav> <http://purl.org/dc/elements/1.1/type> "dialogue"^^<http://www.language-archives.org/OLAC/1.1/discourse-type>', $data['update'], 'update parameter must contain dialogue'); |
|
192 |
$this->assertContains('<http://cocoon.huma-num.fr/data/ala/ALA_608_22km.wav> <http://purl.org/dc/elements/1.1/type> "drama"^^<http://www.language-archives.org/OLAC/1.1/discourse-type> .', $data['update'], 'update parameter must contain drama'); |
|
193 |
} |
|
194 |
|
|
195 |
public function testDelete() { |
|
196 |
$responses = [ |
|
197 |
new Response(201, ['Location' => "$this->transactionUrl"]), |
|
198 |
new Response(204) |
|
199 |
]; |
|
200 |
$container = []; |
|
201 |
$sparqlClientMock = m::mock('EasyRdf\Sparql\Client'); |
|
202 |
|
|
203 |
$documentRepository = $this->getSparqlClient($responses, $container, $sparqlClientMock); |
|
204 |
|
|
205 |
$documentRepository->startTransaction(); |
|
206 |
$documentRepository->delete($this->deleteGraph); |
|
207 |
|
|
208 |
$this->assertCount(2, $container, '2 requests'); |
|
209 |
|
|
210 |
$this->assertNotNull($documentRepository->getCurrentTransactionUrl(), "Current Transaction url must be not null"); |
|
211 |
|
|
212 |
$req = $container[1]['request']; |
|
213 |
|
|
214 |
$this->assertEquals($this->transactionUrl."?action=UPDATE", (string)$req->getUri(), "uri must be the transaction url"); |
|
215 |
$this->assertEquals('PUT', $req->getMethod(), "Method must be PUT"); |
|
216 |
$this->assertEquals(['application/x-www-form-urlencoded'], $req->getHeader('Content-type'), "content type must be form urlencoded"); |
|
217 |
|
|
218 |
$data = []; |
|
219 |
parse_str($req->getBody(), $data); |
|
220 |
|
|
221 |
$this->assertArrayHasKey('update', $data, "Submitted data must have updarte parameter"); |
|
222 |
$this->assertContains('DELETE DATA {', $data['update'], 'update parameter must contain DELETE'); |
|
223 |
$this->assertContains('GRAPH <'.$this->addGraph->getUri().'> {', $data['update'], 'update parameter must contain GRAPH id'); |
|
224 |
$this->assertContains('<http://cocoon.huma-num.fr/data/ala/ALA_608_22km.wav> <http://purl.org/dc/elements/1.1/type> "dialogue"^^<http://www.language-archives.org/OLAC/1.1/discourse-type>', $data['update'], 'update parameter must contain GRAPH id'); |
|
225 |
|
|
226 |
} |
|
227 |
|
|
228 |
} |