/tests/cases/NodeTest.php

https://github.com/misja/librdf · PHP · 271 lines · 214 code · 41 blank · 16 comment · 1 complexity · a5c78f67b1c7019a31f1ef0b39d54e57 MD5 · raw file

  1. <?php
  2. namespace librdf\tests\cases;
  3. // test Node and subclasses
  4. use librdf\URI;
  5. use librdf\Node;
  6. use librdf\node\URINode;
  7. use librdf\node\LiteralNode;
  8. use librdf\node\BlankNode;
  9. class NodeTest extends \lithium\test\Unit
  10. {
  11. public function setUp()
  12. {
  13. $this->testURI = "http://www.example.com/";
  14. $this->testURI2 = "http://www.example.org/";
  15. $this->testNodeID = "abcd";
  16. $this->testNodeID2 = "efgh";
  17. $this->xmlDatatype = "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral";
  18. $this->testType = "http://www.example.com/types/#testtype";
  19. $this->testType2 = "http://www.example.com/types/#testtype2";
  20. $this->testLang = "en-us";
  21. $this->testLang2 = "fr";
  22. $this->testLiteral = 'This is the first test literal';
  23. $this->testLiteral2 = "This is the second test literal";
  24. // create an xml test
  25. $xmlstr = <<<EOT
  26. <?xml version="1.0" encoding="utf-8"?>
  27. <!DOCTYPE document [
  28. <!ELEMENT document (nodelist+)>
  29. <!ELEMENT nodelist (child*)>
  30. <!ATTLIST nodelist id ID #REQUIRED>
  31. <!ELEMENT child (#PCDATA)>
  32. ]>
  33. <document>
  34. <nodelist id="list1"><child/></nodelist>
  35. </document>
  36. EOT;
  37. $document = new \DOMDocument();
  38. $document->loadXML($xmlstr);
  39. $document->validate();
  40. $this->xmllist = $document->getElementById("list1")->childNodes;
  41. }
  42. public function testURIConstruct()
  43. {
  44. $uri = new URINode($this->testURI);
  45. $this->assertTrue($uri instanceof \librdf\node\URINode);
  46. }
  47. public function testURIAltConstruct()
  48. {
  49. $uri = new URINode($this->testURI);
  50. $uri2 = new URINode($uri->getNode());
  51. $this->assertTrue($uri2 instanceof \librdf\node\URINode);
  52. }
  53. public function testURIConstructFail()
  54. {
  55. $this->expectException('Argument is not a string or node resource');
  56. $uri = new URINode(47);
  57. $blank_node = new BlankNode();
  58. $uri = new URINode($blank_node->getNode());
  59. }
  60. public function testURIGetNode()
  61. {
  62. $uri = new URINode($this->testURI);
  63. $this->assertTrue(is_resource($uri->getNode()));
  64. }
  65. public function testURIToString()
  66. {
  67. $uri = new URINode($this->testURI);
  68. $this->assertEqual($uri->__toString(),
  69. "<" . $this->testURI . ">");
  70. }
  71. public function testURIClone()
  72. {
  73. $uri1 = new URINode($this->testURI);
  74. $uri2 = clone $uri1;
  75. $this->assertTrue($uri2 instanceof \librdf\node\URINode);
  76. $this->assertNotEqual($uri1, $uri2);
  77. $this->assertTrue($uri1->isEqual($uri2));
  78. $this->assertTrue($uri2->isEqual($uri1));
  79. }
  80. public function testURIIsEqual()
  81. {
  82. $uri1 = new URINode($this->testURI);
  83. $uri2 = new URINode($this->testURI);
  84. $uri3 = new URINode($this->testURI2);
  85. $this->assertTrue($uri1->isEqual($uri2));
  86. $this->assertFalse($uri1->isEqual($uri3));
  87. }
  88. public function testBlankConstruct()
  89. {
  90. $blank1 = new BlankNode();
  91. $blank2 = new BlankNode($this->testNodeID);
  92. $this->assertTrue($blank1 instanceof \librdf\node\BlankNode);
  93. $this->assertTrue($blank2 instanceof \librdf\node\BlankNode);
  94. }
  95. public function testBlankAltConstruct()
  96. {
  97. $blank1 = new BlankNode();
  98. $blank2 = new BlankNode($blank1->getNode());
  99. $this->assertTrue($blank2 instanceof \librdf\node\BlankNode);
  100. }
  101. public function testBlankConstructFail()
  102. {
  103. $this->expectException('Resource argument not a valid node blank node');
  104. // only a bad node is failure: everything else is converted
  105. // to a string
  106. $literal_node = new LiteralNode("value");
  107. $blank = new BlankNode($literal_node->getNode());
  108. }
  109. public function testBlankToString()
  110. {
  111. $blank1 = new BlankNode();
  112. $blank2 = new BlankNode($this->testNodeID);
  113. $this->assertTrue(is_string($blank1->__toString()));
  114. $this->assertEqual($blank2->__toString(),
  115. "_:" . $this->testNodeID);
  116. }
  117. public function testBlankClone()
  118. {
  119. $blank1 = new BlankNode($this->testNodeID);
  120. $blank2 = clone $blank1;
  121. $this->assertTrue($blank2 instanceof \librdf\node\BlankNode);
  122. $this->assertNotEqual($blank1, $blank2);
  123. $this->assertEqual($blank1->__toString(), $blank2->__toString());
  124. }
  125. // just testing whether a node is equal to itself
  126. // librdf actually returns true for any two blank nodes with the same
  127. // nodeID, but this is somewhat ambiguous, since they should only
  128. // be equal if they are from the same document, and therefore the same
  129. // node
  130. public function testBlankIsEqual()
  131. {
  132. $blank1 = new BlankNode($this->testNodeID);
  133. $blank2 = new BlankNode($this->testNodeID2);
  134. $this->assertTrue($blank1->isEqual($blank1));
  135. $this->assertFalse($blank1->isEqual($blank2));
  136. }
  137. public function testLiteralConstructPlain()
  138. {
  139. $literal = new LiteralNode($this->testLiteral);
  140. $literalLang = new LiteralNode($this->testLiteral, NULL,
  141. $this->testLang);
  142. $this->assertTrue($literal instanceof \librdf\node\LiteralNode);
  143. $this->assertTrue($literalLang instanceof \librdf\node\LiteralNode);
  144. }
  145. public function testLiteralConstructTyped()
  146. {
  147. $literal = new LiteralNode($this->testLiteral, $this->testType);
  148. $this->assertTrue($literal instanceof \librdf\node\LiteralNode);
  149. $this->expectException('Unable to create new literal node');
  150. $literal = new LiteralNode($this->testLiteral,
  151. $this->testType, $this->testLang);
  152. $literal = new LiteralNode($this->xmllist);
  153. $this->assertTrue($literal instanceof \librdf\node\LiteralNode);
  154. $this->expectException('Object of class DOMNodeList could not be converted to string');
  155. $literal = new LiteralNode($this->xmllist, $this->testType);
  156. }
  157. public function testLiteralAltConstruct()
  158. {
  159. $literal = new LiteralNode($this->testLiteral, $this->testType);
  160. $literal1 = new LiteralNode($literal->getNode());
  161. $this->assertTrue($literal1 instanceof \librdf\node\LiteralNode);
  162. }
  163. public function testLiteralConstructFail()
  164. {
  165. $this->expectException('Invalid number of arguments');
  166. $literal = new LiteralNode();
  167. $this->fail("Constructor failed to throw exception for invalid arguments");
  168. // one argument, wrong resource type
  169. $this->expectException('Argument 1 not a valid node literal node');
  170. $blank_node = new BlankNode();
  171. $literal = new LiteralNode($blank_node->getNode());
  172. // more than three arguments
  173. $this->expectException('Invalid number of arguments');
  174. $literal = new LiteralNode("value",
  175. "http://www.example.org/",
  176. NULL, NULL);
  177. }
  178. /**
  179. * Enter description here ...
  180. *
  181. * @todo find out why __toString return a doubly quoted value
  182. */
  183. public function testLiteralToString()
  184. {
  185. $literal = new LiteralNode($this->testLiteral);
  186. $this->assertEqual($literal->__toString(), '"'.$this->testLiteral.'"');
  187. }
  188. public function testLiteralClone()
  189. {
  190. $literal1 = new LiteralNode($this->testLiteral);
  191. $literal2 = clone $literal1;
  192. $this->assertTrue($literal2 instanceof \librdf\node\LiteralNode);
  193. $this->assertNotEqual($literal1, $literal2);
  194. $this->assertTrue($literal1->isEqual($literal2));
  195. }
  196. public function testLiteralIsEqual()
  197. {
  198. $literal1 = new LiteralNode($this->testLiteral);
  199. $literal1_1 = new LiteralNode($this->testLiteral);
  200. $literal2 = new LiteralNode($this->testLiteral, $this->testType);
  201. $literal2_2 = new LiteralNode($this->testLiteral, $this->testType);
  202. $literal3 = new LiteralNode($this->testLiteral, $this->testType2);
  203. $literal4 = new LiteralNode($this->testLiteral2);
  204. $literal5 = new LiteralNode($this->testLiteral, NULL, $this->testLang);
  205. $literal5_2 = new LiteralNode($this->testLiteral, NULL, $this->testLang);
  206. $literal6 = new LiteralNode($this->testLiteral, NULL, $this->testLang2);
  207. $this->assertTrue($literal1->isEqual($literal1_1));
  208. $this->assertFalse($literal1->isEqual($literal2));
  209. $this->assertFalse($literal1->isEqual($literal4));
  210. $this->assertFalse($literal1->isEqual($literal5));
  211. $this->assertTrue($literal2->isEqual($literal2_2));
  212. $this->assertFalse($literal2->isEqual($literal3));
  213. $this->assertFalse($literal5->isEqual($literal6));
  214. $this->assertTrue($literal5->isEqual($literal5_2));
  215. }
  216. public function testLiteralGetDataType()
  217. {
  218. $literal = new LiteralNode($this->testLiteral, $this->testType);
  219. $literal1 = new LiteralNode($this->testLiteral);
  220. $this->assertEqual($literal->getDataType(), $this->testType);
  221. $this->assertNull($literal1->getDataType());
  222. }
  223. public function testLiteralGetLanguage()
  224. {
  225. $literal = new LiteralNode($this->testLiteral, NULL, $this->testLang);
  226. $literal1 = new LiteralNode($this->testLiteral);
  227. $this->assertEqual($literal->getLanguage(), $this->testLang);
  228. $this->assertNull($literal1->getLanguage());
  229. }
  230. }