|
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?isolation-level=http%3A%2F%2Fwww.openrdf.org%2Fschema%2Fsesame%23SNAPSHOT_READ", (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/sparql-update; charset=utf-8'], $req->getHeader('Content-type'), "content type must be form urlencoded"); |
|
184 |
|
185 $body = (string)$req->getBody(); |
|
186 |
|
187 $this->assertContains('INSERT DATA {', $body, 'update parameter must contain INSERT'); |
|
188 $this->assertContains('GRAPH <'.$this->addGraph->getUri().'> {', $body, 'update parameter must contain GRAPH id'); |
|
189 $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>', $body, 'update parameter must contain dialogue'); |
|
190 $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> .', $body, 'update parameter must contain drama'); |
|
191 } |
|
192 |
|
193 public function testDelete() { |
|
194 $responses = [ |
|
195 new Response(201, ['Location' => "$this->transactionUrl"]), |
|
196 new Response(204) |
|
197 ]; |
|
198 $container = []; |
|
199 $sparqlClientMock = m::mock('EasyRdf\Sparql\Client'); |
|
200 |
|
201 $documentRepository = $this->getSparqlClient($responses, $container, $sparqlClientMock); |
|
202 |
|
203 $documentRepository->startTransaction(); |
|
204 $documentRepository->delete($this->deleteGraph); |
|
205 |
|
206 $this->assertCount(2, $container, '2 requests'); |
|
207 |
|
208 $this->assertNotNull($documentRepository->getCurrentTransactionUrl(), "Current Transaction url must be not null"); |
|
209 |
|
210 $req = $container[1]['request']; |
|
211 |
|
212 $this->assertEquals($this->transactionUrl."?action=UPDATE", (string)$req->getUri(), "uri must be the transaction url"); |
|
213 $this->assertEquals('PUT', $req->getMethod(), "Method must be PUT"); |
|
214 $this->assertEquals(['application/sparql-update; charset=utf-8'], $req->getHeader('Content-type'), "content type must be form urlencoded"); |
|
215 |
|
216 $body = (string)$req->getBody(); |
|
217 |
|
218 $this->assertContains('DELETE DATA {', $body, 'update parameter must contain DELETE'); |
|
219 $this->assertContains('GRAPH <'.$this->addGraph->getUri().'> {', $body, 'update parameter must contain GRAPH id'); |
|
220 $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>', $body, 'update parameter must contain GRAPH id'); |
|
221 |
|
222 } |
|
223 |
|
224 } |