/tests/virtuosoTest.php

https://github.com/BorderCloud/SPARQL · PHP · 251 lines · 196 code · 26 blank · 29 comment · 13 complexity · 546196f9e86f4513b86496ccd9f1adc3 MD5 · raw file

  1. <?php
  2. declare(strict_types=1);
  3. namespace BorderCloud\SPARQL\Tests;
  4. $loader = require __DIR__ . '/../vendor/autoload.php';
  5. $loader->addPsr4('BorderCloud\\SPARQL\\', __DIR__ . '/../src');
  6. use BorderCloud\SPARQL\SparqlClient;
  7. use Exception;
  8. use PHPUnit\Framework\TestCase;
  9. final class virtuosoTest extends TestCase
  10. {
  11. /**
  12. * @var SparqlClient
  13. */
  14. private $_client;
  15. private $_endpoint = "http://172.17.0.2:8890/sparql-auth/";
  16. private $_login = "dba";
  17. private $_password = "dba";
  18. public function setUp():void
  19. {
  20. $this->_client = new SparqlClient(false);
  21. $this->_client->setEndpointRead($this->_endpoint);
  22. $this->_client->setEndpointWrite($this->_endpoint);
  23. $this->_client->setLogin($this->_login);
  24. $this->_client->setPassword($this->_password);
  25. //check delete
  26. $q = <<<EOT
  27. PREFIX a: <http://example.com/test/a/>
  28. PREFIX b: <http://example.com/test/b/>
  29. DELETE DATA {
  30. GRAPH <http://truc.fr/> {
  31. a:A b:Name "Test1" .
  32. a:A b:Name "Test2" .
  33. a:A b:Name "Test3" .
  34. }}
  35. EOT;
  36. $res = $this->_client->query($q,'raw');
  37. $err = $this->_client->getErrors();
  38. if ($err) {
  39. print_r($err);
  40. throw new Exception(print_r($err,true));
  41. }
  42. echo "Delete :";
  43. var_dump($res);
  44. }
  45. public function testVirtuosoRead()
  46. {
  47. $q = "select * where {?x ?y ?z.} LIMIT 5";
  48. $rows = $this->_client->query($q, 'rows');
  49. $err = $this->_client->getErrors();
  50. if ($err) {
  51. //print_r($err);
  52. throw new Exception(print_r($err, true));
  53. }
  54. $this->assertCount(5, $rows["result"]["rows"]);
  55. }
  56. public function testVirtuosoAsk()
  57. {
  58. //read if empty
  59. $q = "PREFIX a: <http://example.com/test/a/>
  60. select * where {a:A ?y ?z.} LIMIT 5";
  61. $rows = $this->_client->query($q, 'rows');
  62. $err = $this->_client->getErrors();
  63. if ($err) {
  64. //print_r($err);
  65. throw new Exception(print_r($err, true));
  66. }
  67. $this->assertCount(0, $rows["result"]["rows"]);
  68. //check ask false
  69. $q = "PREFIX a: <http://example.com/test/a/>
  70. PREFIX b: <http://example.com/test/b/>
  71. ask where { GRAPH <http://truc.fr/> {a:A b:Name \"Test3\" .}} ";
  72. $res = $this->_client->query($q);
  73. $err = $this->_client->getErrors();
  74. if ($err) {
  75. //print_r($err);
  76. throw new Exception(print_r($err,true));
  77. }
  78. $this->assertFalse($res);
  79. //check write
  80. $q = <<<EOT
  81. PREFIX a: <http://example.com/test/a/>
  82. PREFIX b: <http://example.com/test/b/>
  83. INSERT DATA {
  84. GRAPH <http://truc.fr/> {
  85. a:A b:Name "Test1" .
  86. a:A b:Name "Test2" .
  87. a:A b:Name "Test3" .
  88. }}
  89. EOT;
  90. $res = $this->_client->query($q,'raw');
  91. $err = $this->_client->getErrors();
  92. if ($err) {
  93. print_r($err);
  94. throw new Exception(print_r($err,true));
  95. }
  96. echo "Write :";
  97. var_dump($res);
  98. // check if write is OK
  99. $q = "PREFIX a: <http://example.com/test/a/>
  100. select * where {a:A ?y ?z.} LIMIT 5";
  101. $rows = $this->_client->query($q, 'rows');
  102. $err = $this->_client->getErrors();
  103. if ($err) {
  104. //print_r($err);
  105. throw new Exception(print_r($err, true));
  106. }
  107. $this->assertCount(3, $rows["result"]["rows"]);
  108. //check ask is true
  109. $q = "PREFIX a: <http://example.com/test/a/>
  110. PREFIX b: <http://example.com/test/b/>
  111. ask where { GRAPH <http://truc.fr/> {a:A b:Name \"Test3\" .}} ";
  112. $res = $this->_client->query($q, 'raw');
  113. $err = $this->_client->getErrors();
  114. if ($err) {
  115. //print_r($err);
  116. throw new Exception(print_r($err,true));
  117. }
  118. $this->assertTrue($res);
  119. //check delete
  120. $q = <<<EOT
  121. PREFIX a: <http://example.com/test/a/>
  122. PREFIX b: <http://example.com/test/b/>
  123. DELETE DATA {
  124. GRAPH <http://truc.fr/> {
  125. a:A b:Name "Test1" .
  126. a:A b:Name "Test2" .
  127. a:A b:Name "Test3" .
  128. }}
  129. EOT;
  130. $res = $this->_client->query($q,'raw');
  131. $err = $this->_client->getErrors();
  132. if ($err) {
  133. print_r($err);
  134. throw new Exception(print_r($err,true));
  135. }
  136. echo "Delete :";
  137. var_dump($res);
  138. // check if write is OK
  139. $q = "PREFIX a: <http://example.com/test/a/>
  140. select * where {a:A ?y ?z.} LIMIT 5";
  141. $rows = $this->_client->query($q, 'rows');
  142. $err = $this->_client->getErrors();
  143. if ($err) {
  144. //print_r($err);
  145. throw new Exception(print_r($err, true));
  146. }
  147. $this->assertCount(0, $rows["result"]["rows"]);
  148. }
  149. public function testErrorQuery()
  150. {
  151. //read if empty
  152. $q = "se * where {?x ?y ?z.} LIMIT 5";
  153. $rows = $this->_client->query($q, 'rows');
  154. $err = $this->_client->getErrors();
  155. $isError = false;
  156. $errorMessage = "";
  157. if ($err) {
  158. $isError = true;
  159. //print_r($err);
  160. //throw new Exception(print_r($err, true));
  161. $errorMessage = $this->_client->getLastError();
  162. }
  163. $this->assertTrue($isError);
  164. $this->assertEquals($errorMessage,"line 3: syntax error at 'se' before '*'");
  165. $sc = new SparqlClient(true);
  166. $sc->setEndpointRead($this->_endpoint);
  167. $sc->setLogin($this->_login);
  168. $sc->setPassword("dba");
  169. //error in the query
  170. $rows = $sc->query($q, 'rows');
  171. $err = $sc->getErrors();
  172. $isError = false;
  173. if ($err) {
  174. $isError = true;
  175. //print_r($err);
  176. //throw new Exception(print_r($err, true));
  177. $errorMessage = $sc->getLastError();
  178. }
  179. $this->assertTrue($isError);
  180. $this->assertEquals($errorMessage,"line 3: syntax error at 'se' before '*'");
  181. }
  182. public function testErrorUpdate()
  183. {
  184. //read if empty
  185. $q = <<<EOT
  186. PREFIX a: <http://example.com/test/a/>
  187. PREFIX b: <http://example.com/test/b/>
  188. DELETE DATA {
  189. GRAPH <http://truc.fr/> {
  190. a:A b:Name "Test1" .
  191. a:A b:Name "Test2" .
  192. a:A b:Name "Test3" .
  193. EOT;
  194. $rows = $this->_client->query($q, 'rows');
  195. $err = $this->_client->getErrors();
  196. $isError = false;
  197. $errorMessage = "";
  198. if ($err) {
  199. $isError = true;
  200. //print_r($err);
  201. //throw new Exception(print_r($err, true));
  202. $errorMessage = $this->_client->getLastError();
  203. }
  204. $this->assertTrue($isError);
  205. $this->assertEquals($errorMessage,"line 9: syntax error");
  206. $sc = new SparqlClient(true);
  207. $sc->setEndpointRead($this->_endpoint);
  208. $sc->setEndpointWrite($this->_endpoint);
  209. $sc->setLogin($this->_login);
  210. $sc->setPassword($this->_password);
  211. //error in the query
  212. $q = "s * where {?x ?y ?z.} LIMIT 5";
  213. $rows = $sc->query($q, 'rows');
  214. $err = $sc->getErrors();
  215. $isError = false;
  216. if ($err) {
  217. $isError = true;
  218. //print_r($err);
  219. //throw new Exception(print_r($err, true));
  220. $errorMessage = $this->_client->getLastError();
  221. }
  222. $this->assertTrue($isError);
  223. $this->assertEquals($errorMessage,"line 9: syntax error");
  224. }
  225. }