PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/FourStore/FourStoreMatchingTest.php

https://github.com/BorderCloud/4store-php
PHP | 448 lines | 436 code | 10 blank | 2 comment | 3 complexity | 2a8565794294fc7a3988fe68793ceedc MD5 | raw file
  1. <?php
  2. require_once 'PHPUnit/Autoload.php';
  3. //require_once 'PHPUnit/Framework.php';
  4. require_once (dirname(__FILE__) . '/../../lib/php4store/Endpoint.php');
  5. class FourStoreMatchingTest extends PHPUnit_Framework_TestCase
  6. {
  7. protected function setUp()
  8. {
  9. global $EndPoint4store,$modeDebug,$prefixSparql,$prefixTurtle,$graph1,$graph2;
  10. $s = new Endpoint($EndPoint4store,false,$modeDebug);
  11. $r = $s->delete($graph1);
  12. $r = $s->delete($graph2);
  13. $r = $s->delete("default:");
  14. $r = $s->delete("http://fr.example.com/test");
  15. $r = $s->delete("http://en.example.com/test");
  16. }
  17. public function testMatchingLiteralsWithoutLanguageTags()
  18. {
  19. global $EndPoint4store,$modeDebug,$prefixSparql,$prefixTurtle,$graph1,$graph2;
  20. $s = new Endpoint($EndPoint4store,false,$modeDebug);
  21. $this->checkIfInitialState($s);
  22. $q = $prefixSparql." \n
  23. INSERT DATA {
  24. GRAPH <".$graph1."> {
  25. a:A1 b:Name \"Test\".
  26. }}";
  27. $res = $s->query($q,'raw' );
  28. $err = $s->getErrors();
  29. if ($err) {
  30. print_r($err);
  31. $this->assertTrue(false);
  32. }
  33. $this->assertTrue($res);
  34. $q = $prefixSparql." \n select * where {
  35. GRAPH <".$graph1."> {?a ?b \"Test\" .}} ";
  36. $rows = $s->query($q, 'rows');
  37. //print_r($rows);
  38. $err = $s->getErrors();
  39. if ($err) {
  40. print_r($err);
  41. $this->assertTrue(false);
  42. }
  43. $this->assertEquals(1, count($rows));
  44. $r = $s->delete($graph1);
  45. $this->checkIfInitialState($s);
  46. }
  47. public function testMatchingLiteralsWithLanguageTags()
  48. {
  49. global $modeSkipProblem;
  50. if($modeSkipProblem)
  51. $this->markTestSkipped(
  52. "Matching Literals with Language Tags doesn't work."
  53. );
  54. global $EndPoint4store,$modeDebug,$prefixSparql,$prefixTurtle,$graph1,$graph2;
  55. $s = new Endpoint($EndPoint4store,false,$modeDebug);
  56. $this->checkIfInitialState($s);
  57. $q = $prefixSparql." \n
  58. INSERT DATA {
  59. GRAPH <".$graph1."> {
  60. a:A1 b:Name \"Test\"@en.
  61. }}";
  62. $res = $s->query($q,'raw' );
  63. $err = $s->getErrors();
  64. if ($err) {
  65. print_r($err);
  66. $this->assertTrue(false);
  67. }
  68. $this->assertTrue($res);
  69. $q = $prefixSparql." \n select * where {
  70. GRAPH <".$graph1."> {?a ?b \"Test\"@en .}} ";
  71. $rows = $s->query($q, 'rows');
  72. //print_r($rows);
  73. $err = $s->getErrors();
  74. if ($err) {
  75. print_r($err);
  76. $this->assertTrue(false);
  77. }
  78. $this->assertEquals(1, count($rows));
  79. $r = $s->delete($graph1);
  80. $this->checkIfInitialState($s);
  81. }
  82. public function testHackLanguage()
  83. {
  84. global $EndPoint4store,$modeDebug,$prefixSparql,$prefixTurtle,$graph1,$graph2;
  85. $s = new Endpoint($EndPoint4store,false,$modeDebug);
  86. $this->checkIfInitialState($s);
  87. $graphfr = "http://fr.example.com/test";
  88. $graphen = "http://en.example.com/test";
  89. $r = $s->set($graphen,
  90. $prefixTurtle ."\n
  91. <http://en.example.com/test/Apple> b:Name \"Apple\"."
  92. );
  93. $this->assertTrue($r);
  94. $r = $s->set($graphfr,
  95. $prefixTurtle ."\n
  96. <http://fr.example.com/test/Pomme> b:Name \"Pomme\".
  97. <http://fr.example.com/test/Pomme> <http://www.w3.org/2002/07/owl#sameas> <http://en.example.com/test/Apple>.
  98. ");
  99. $this->assertTrue($r);
  100. $this->assertEquals(3, $s->count());
  101. //Not bidirectional
  102. $q = $prefixSparql." \n SELECT * WHERE
  103. {?id b:Name \"Apple\" .
  104. ?id <http://www.w3.org/2002/07/owl#sameas> ?idfr .
  105. {GRAPH <".$graphfr."> {?idfr b:Name ?c .}}
  106. } ";
  107. $rows = $s->query($q, 'rows');
  108. //print_r($rows);
  109. $err = $s->getErrors();
  110. if ($err) {
  111. print_r($err);
  112. $this->assertTrue(false);
  113. }
  114. $this->assertEquals(0,count($rows));
  115. $q = $prefixSparql." \n SELECT * WHERE
  116. {?id b:Name \"Apple\" .
  117. {{?id <http://www.w3.org/2002/07/owl#sameas> ?idfr . }
  118. UNION
  119. {?idfr <http://www.w3.org/2002/07/owl#sameas> ?id . }}.
  120. {GRAPH <".$graphfr."> {?idfr b:Name ?c .}}
  121. } ";
  122. $rows = $s->query($q, 'rows');
  123. //print_r($rows);
  124. $err = $s->getErrors();
  125. if ($err) {
  126. print_r($err);
  127. $this->assertTrue(false);
  128. }
  129. $this->assertEquals(1,count($rows));
  130. $r = $s->delete($graphfr);
  131. $r = $s->delete($graphen);
  132. $this->checkIfInitialState($s);
  133. }
  134. public function testMatchingLiteralsWithDateTypesErrorDate()
  135. {
  136. global $modeSkipProblem;
  137. if($modeSkipProblem)
  138. $this->markTestSkipped(
  139. "Error with Date."
  140. );
  141. global $EndPoint4store,$modeDebug,$prefixSparql,$prefixTurtle,$graph1,$graph2;
  142. $s = new Endpoint($EndPoint4store,false,$modeDebug);
  143. $this->checkIfInitialState($s);
  144. $prefixt = $prefixTurtle . "\n@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n";
  145. $prefix = $prefixSparql . "\n PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> \n";
  146. $r = $s->set($graph1,
  147. $prefixt ."\n
  148. a:id5 b:date \"2010-03-09T21:30:00Z\"^^xsd:dateTime .
  149. a:id6 b:date \"2010-03-09T22:30:00Z\"^^xsd:dateTime .
  150. a:id7 b:date \"2010-03-09\"^^xsd:dateTime .
  151. a:id8 b:date \"2010-03-10\"^^xsd:dateTime .
  152. "
  153. );
  154. $this->assertTrue($r);
  155. $q = $prefix."\n ASK WHERE { GRAPH <".$graph1."> {
  156. a:id7 b:date ?date1.
  157. a:id8 b:date ?date2.
  158. FILTER ( ?date1 < ?date2 ) .}}";
  159. $res = $s->query($q,'raw');
  160. $err = $s->getErrors();
  161. if ($err) {
  162. print_r($err);
  163. $this->assertTrue(false);
  164. }
  165. $this->assertTrue($res);
  166. $r = $s->delete($graph1);
  167. $this->checkIfInitialState($s);
  168. }
  169. public function testMatchingLiteralsWithDateTypes()
  170. {
  171. global $EndPoint4store,$modeDebug,$prefixSparql,$prefixTurtle,$graph1,$graph2;
  172. $s = new Endpoint($EndPoint4store,false,$modeDebug);
  173. $this->checkIfInitialState($s);
  174. $prefixt = $prefixTurtle . "\n@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n";
  175. $prefix = $prefixSparql . "\n PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> \n";
  176. $r = $s->set($graph1,
  177. $prefixt ."\n
  178. a:id5 b:date \"2010-03-09T21:30:00Z\"^^xsd:dateTime .
  179. a:id6 b:date \"2010-03-09T22:30:00Z\"^^xsd:dateTime .
  180. a:id7 b:date \"2010-03-09\"^^xsd:dateTime .
  181. a:id8 b:date \"2010-03-10\"^^xsd:dateTime .
  182. "
  183. );
  184. $this->assertTrue($r);
  185. //$this->printAll();
  186. $q = $prefix."\n ASK WHERE { GRAPH <".$graph1."> {
  187. a:id5 b:date ?date1.
  188. a:id6 b:date ?date2.
  189. FILTER ( ?date1 < ?date2 ) .}}";
  190. $res = $s->query($q,'raw');
  191. $err = $s->getErrors();
  192. if ($err) {
  193. print_r($err);
  194. $this->assertTrue(false);
  195. }
  196. $this->assertTrue($res);
  197. $q = $prefix."\n ASK WHERE { GRAPH <".$graph1."> {
  198. a:id5 b:date ?date1.
  199. a:id6 b:date ?date2.
  200. FILTER ( ?date1 > ?date2 ) .}}";
  201. $res = $s->query($q,'raw');
  202. $err = $s->getErrors();
  203. if ($err) {
  204. print_r($err);
  205. $this->assertTrue(false);
  206. }
  207. $this->assertFalse($res);
  208. /* $q = $prefix."\n ASK WHERE { GRAPH <".$graph1."> {
  209. a:id7 b:date ?date1.
  210. a:id8 b:date ?date2.
  211. FILTER ( ?date1 < ?date2 ) .}}";
  212. $res = $s->query($q,'raw');
  213. $err = $s->getErrors();
  214. if ($err) {
  215. print_r($err);
  216. $this->assertTrue(false);
  217. }
  218. $this->assertTrue($res); */
  219. $q = $prefix."\n ASK WHERE { GRAPH <".$graph1."> {
  220. a:id7 b:date ?date1.
  221. a:id8 b:date ?date2.
  222. FILTER ( ?date1 > ?date2 ) .}}";
  223. $res = $s->query($q,'raw');
  224. $err = $s->getErrors();
  225. if ($err) {
  226. print_r($err);
  227. $this->assertTrue(false);
  228. }
  229. $this->assertFalse($res);
  230. $q = $prefix."\n ASK WHERE { GRAPH <".$graph1."> {
  231. a:id7 b:date ?date1.
  232. a:id6 b:date ?date2.
  233. FILTER ( ?date1 < ?date2 ) .}}";
  234. $res = $s->query($q,'raw');
  235. $err = $s->getErrors();
  236. if ($err) {
  237. print_r($err);
  238. $this->assertTrue(false);
  239. }
  240. $this->assertTrue($res);
  241. $q = $prefix."\n ASK WHERE { GRAPH <".$graph1."> {
  242. a:id7 b:date ?date1.
  243. a:id6 b:date ?date2.
  244. FILTER ( ?date1 > ?date2 ) .}}";
  245. $res = $s->query($q,'raw');
  246. $err = $s->getErrors();
  247. if ($err) {
  248. print_r($err);
  249. $this->assertTrue(false);
  250. }
  251. $this->assertFalse($res);
  252. //example with text
  253. $q = $prefix."\n ASK WHERE { GRAPH <".$graph1."> {
  254. a:id7 b:date ?date.
  255. FILTER ( ?date < \"2010-03-09T00:00:01Z\"^^xsd:dateTime ) .}}";
  256. $res = $s->query($q,'raw');
  257. $err = $s->getErrors();
  258. if ($err) {
  259. print_r($err);
  260. $this->assertTrue(false);
  261. }
  262. $this->assertTrue($res);
  263. $r = $s->delete($graph1);
  264. $this->checkIfInitialState($s);
  265. }
  266. public function testMatchingLiteralsWithStringTypes()
  267. {
  268. global $modeSkipProblem;
  269. if($modeSkipProblem)
  270. $this->markTestSkipped(
  271. "Matching Literals with type String."
  272. );
  273. global $EndPoint4store,$modeDebug,$prefixSparql,$prefixTurtle,$graph1,$graph2;
  274. $s = new Endpoint($EndPoint4store,false,$modeDebug);
  275. $this->checkIfInitialState($s);
  276. $prefix = $prefixSparql . "\n PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> \n";
  277. $q = $prefix." \n
  278. INSERT DATA {
  279. GRAPH <".$graph1."> {
  280. a:A b:Name \"Test\"^^xsd:string .
  281. a:A b:Name \"Test2\" .
  282. }}";
  283. $res = $s->query($q );
  284. //print_r($res);
  285. $this->assertEquals(2, $s->count($graph1));
  286. $q = $prefix." \n select * where {
  287. GRAPH <".$graph1."> {?a ?b \"Test\"^^xsd:string.}} ";
  288. $rows = $s->query($q, 'rows');
  289. //print_r($rows);
  290. $err = $s->getErrors();
  291. if ($err) {
  292. print_r($err);
  293. $this->assertTrue(false);
  294. }
  295. $this->assertEquals(1, count($rows));
  296. $q = $prefix." \n select * where {
  297. GRAPH <".$graph1."> {?a ?b \"Test2\"^^xsd:string.}} ";
  298. $rows = $s->query($q, 'rows');
  299. //print_r($rows);
  300. $err = $s->getErrors();
  301. if ($err) {
  302. print_r($err);
  303. $this->assertTrue(false);
  304. }
  305. $this->assertEquals(1, count($rows));
  306. $q = $prefix." \n select * where {
  307. GRAPH <".$graph1."> {?a ?b \"Test\".}} ";
  308. $rows = $s->query($q, 'rows');
  309. //print_r($rows);
  310. $err = $s->getErrors();
  311. if ($err) {
  312. print_r($err);
  313. $this->assertTrue(false);
  314. }
  315. $this->assertEquals(1, count($rows));
  316. $q = $prefix." \n select * where {
  317. GRAPH <".$graph1."> {?a ?b \"Test2\".}} ";
  318. $rows = $s->query($q, 'rows');
  319. //print_r($rows);
  320. $err = $s->getErrors();
  321. if ($err) {
  322. print_r($err);
  323. $this->assertTrue(false);
  324. }
  325. $this->assertEquals(1, count($rows));
  326. $r = $s->delete($graph1);
  327. $this->checkIfInitialState($s);
  328. }
  329. public function testSelectDistinct()
  330. {
  331. global $EndPoint4store,$modeDebug,$prefixSparql,$prefixTurtle,$graph1,$graph2;
  332. $s = new Endpoint($EndPoint4store,false,$modeDebug);
  333. $this->checkIfInitialState($s);
  334. $prefix = $prefixSparql . "\n PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> \n";
  335. $q = $prefix." \n
  336. INSERT DATA {
  337. GRAPH <".$graph1."> {
  338. a:A2 b:Name \"Test2\"^^xsd:String .
  339. a:A3 b:Name \"Test3\"^^xsd:String .
  340. a:A4 b:Name \"Test4\"^^xsd:String .
  341. a:A2 b:Name2 \"t2\"^^xsd:String.
  342. a:A3 b:Name2 \"t3\"^^xsd:String.
  343. a:A4 b:Name2 \"t4\"^^xsd:String.
  344. }}";
  345. $res = $s->query($q );
  346. //print_r($res);
  347. $this->assertEquals(6, $s->count($graph1));
  348. $q = $prefix."\n select * where {?x b:Name ?name. ?x b:Name2 ?name2. } ";
  349. $rows = $s->query($q, 'rows');
  350. print_r($rows);
  351. $err = $s->getErrors();
  352. if ($err) {
  353. print_r($err);
  354. $this->assertTrue(false);
  355. }
  356. $this->assertEquals(3,count($rows));
  357. $r = $s->delete($graph1);
  358. $this->checkIfInitialState($s);
  359. }
  360. private function checkIfInitialState($s){
  361. global $EndPoint4store,$modeDebug,$prefixSparql,$prefixTurtle,$graph1,$graph2;
  362. $this->assertEquals(0, $s->count($graph1));
  363. $this->assertEquals(0, $s->count($graph2));
  364. $this->assertEquals(0, $s->count());
  365. }
  366. private function printAll(){
  367. global $EndPoint4store,$modeDebug,$prefixSparql,$prefixTurtle,$graph1,$graph2;
  368. $sp = new EndpointPlus($EndPoint4store,false,$modeDebug);
  369. $q = $prefixSparql." \n select * where {
  370. GRAPH ?graph {?o ?p ?v .}} ";
  371. $rows = $sp->query($q, 'rows');
  372. print_r($rows);
  373. $err = $sp->getErrors();
  374. if ($err) {
  375. print_r($err);
  376. }
  377. }
  378. }