PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/unit/lib/Everyman/Neo4j/GeoffTest.php

http://github.com/jadell/neo4jphp
PHP | 351 lines | 287 code | 64 blank | 0 comment | 0 complexity | 05dc405838149bf3b03c82e348b12632 MD5 | raw file
  1. <?php
  2. namespace Everyman\Neo4j;
  3. class GeoffTest extends \PHPUnit_Framework_TestCase
  4. {
  5. protected $client = null;
  6. protected $geoff = null;
  7. public function setUp()
  8. {
  9. $this->client = $this->getMock('Everyman\Neo4j\Client', array('runCommand'));
  10. $this->geoff = new Geoff($this->client);
  11. }
  12. public function testLoad_NotAStreamOrString_ThrowsException()
  13. {
  14. $geoffString = 123;
  15. $this->setExpectedException('Everyman\Neo4j\Exception');
  16. $batch = $this->geoff->load($geoffString);
  17. }
  18. public function testLoad_IgnoreEmptyLines_ReturnsBatch()
  19. {
  20. $geoffString = "\n \n\t\n \n \n";
  21. $batch = $this->geoff->load($geoffString);
  22. self::assertEquals(0, count($batch->getOperations()));
  23. }
  24. public function testLoad_IgnoreCommentLines_ReturnsBatch()
  25. {
  26. $geoffString = "#this is a comment\n"
  27. . " #so is this\n"
  28. . "# this too \n";
  29. $batch = $this->geoff->load($geoffString);
  30. self::assertEquals(0, count($batch->getOperations()));
  31. }
  32. public function testLoad_LoadNodeLines_ReturnsBatch()
  33. {
  34. $geoffString = '(Liz) {"name": "Elizabeth", "title": "Queen of the Commonwealth Realms", "birth.date": "1926-04-21"}'.PHP_EOL
  35. . '(Phil) {"name": "Philip", "title": "Duke of Edinburgh", "birth.date": "1921-06-21"}'.PHP_EOL
  36. . '(Chaz)';
  37. $batch = $this->geoff->load($geoffString);
  38. $ops = $batch->getOperations();
  39. self::assertEquals(3, count($ops));
  40. self::assertInstanceOf('Everyman\Neo4j\Batch\Save', $ops[0]);
  41. self::assertInstanceOf('Everyman\Neo4j\Node', $ops[0]->getEntity());
  42. self::assertFalse($ops[0]->getEntity()->hasId());
  43. self::assertEquals('Elizabeth', $ops[0]->getEntity()->getProperty('name'));
  44. self::assertEquals('Queen of the Commonwealth Realms', $ops[0]->getEntity()->getProperty('title'));
  45. self::assertEquals('1926-04-21', $ops[0]->getEntity()->getProperty('birth.date'));
  46. self::assertInstanceOf('Everyman\Neo4j\Batch\Save', $ops[1]);
  47. self::assertInstanceOf('Everyman\Neo4j\Node', $ops[1]->getEntity());
  48. self::assertFalse($ops[1]->getEntity()->hasId());
  49. self::assertEquals('Philip', $ops[1]->getEntity()->getProperty('name'));
  50. self::assertEquals('Duke of Edinburgh', $ops[1]->getEntity()->getProperty('title'));
  51. self::assertEquals('1921-06-21', $ops[1]->getEntity()->getProperty('birth.date'));
  52. self::assertInstanceOf('Everyman\Neo4j\Batch\Save', $ops[2]);
  53. self::assertInstanceOf('Everyman\Neo4j\Node', $ops[2]->getEntity());
  54. self::assertFalse($ops[2]->getEntity()->hasId());
  55. self::assertEquals(array(), $ops[2]->getEntity()->getProperties());
  56. }
  57. public function testLoad_DuplicateNodeLines_ThrowsException()
  58. {
  59. $geoffString = '(Liz) {"name": "Elizabeth", "title": "Queen of the Commonwealth Realms", "birth.date": "1926-04-21"}'.PHP_EOL
  60. . '(Liz) {"name": "Elizabeth", "title": "Queen of the Commonwealth Realms", "birth.date": "1926-04-21"}'.PHP_EOL;
  61. $this->setExpectedException('Everyman\Neo4j\Exception');
  62. $batch = $this->geoff->load($geoffString);
  63. }
  64. public function testLoad_RelationshipEndpointsDefined_ReturnsBatch()
  65. {
  66. $geoffString = '(Liz) {"name": "Elizabeth", "title": "Queen of the Commonwealth Realms", "birth.date": "1926-04-21"}'.PHP_EOL
  67. . '(Phil) {"name": "Philip", "title": "Duke of Edinburgh", "birth.date": "1921-06-21"}'.PHP_EOL
  68. . '(Chaz) {"name": "Charles", "title": "Prince of Wales", "birth.date": "1948-11-14"}'.PHP_EOL
  69. . '(Liz)-[:MARRIED]->(Phil) {"marriage.place": "Westminster Abbey", "marriage.date": "1947-11-20"}'.PHP_EOL
  70. . '(Phil)-[:FATHER_OF]->(Chaz)';
  71. $batch = $this->geoff->load($geoffString);
  72. $ops = $batch->getOperations();
  73. self::assertEquals(5, count($ops));
  74. $op = $ops[3];
  75. self::assertInstanceOf('Everyman\Neo4j\Batch\Save', $op);
  76. self::assertInstanceOf('Everyman\Neo4j\Relationship', $op->getEntity());
  77. self::assertFalse($op->getEntity()->hasId());
  78. self::assertSame($ops[0]->getEntity(), $op->getEntity()->getStartNode());
  79. self::assertSame($ops[1]->getEntity(), $op->getEntity()->getEndNode());
  80. self::assertEquals('MARRIED', $op->getEntity()->getType());
  81. self::assertEquals('Westminster Abbey', $op->getEntity()->getProperty('marriage.place'));
  82. self::assertEquals('1947-11-20', $op->getEntity()->getProperty('marriage.date'));
  83. $op = $ops[4];
  84. self::assertInstanceOf('Everyman\Neo4j\Batch\Save', $op);
  85. self::assertInstanceOf('Everyman\Neo4j\Relationship', $op->getEntity());
  86. self::assertFalse($op->getEntity()->hasId());
  87. self::assertSame($ops[1]->getEntity(), $op->getEntity()->getStartNode());
  88. self::assertSame($ops[2]->getEntity(), $op->getEntity()->getEndNode());
  89. self::assertEquals('FATHER_OF', $op->getEntity()->getType());
  90. self::assertEquals(array(), $op->getEntity()->getProperties());
  91. }
  92. public function testLoad_RelationshipUndefinedStart_ThrowsException()
  93. {
  94. $geoffString = '(Chaz) {"name": "Charles", "title": "Prince of Wales", "birth.date": "1948-11-14"}'.PHP_EOL
  95. . '(Phil)-[:FATHER_OF]->(Chaz)';
  96. $this->setExpectedException('Everyman\Neo4j\Exception');
  97. $batch = $this->geoff->load($geoffString);
  98. }
  99. public function testLoad_RelationshipUndefinedEnd_ThrowsException()
  100. {
  101. $geoffString = '(Liz) {"name": "Elizabeth", "title": "Queen of the Commonwealth Realms", "birth.date": "1926-04-21"}'.PHP_EOL
  102. . '(Liz)-[:MARRIED]->(Phil) {"marriage.place": "Westminster Abbey", "marriage.date": "1947-11-20"}';
  103. $this->setExpectedException('Everyman\Neo4j\Exception');
  104. $batch = $this->geoff->load($geoffString);
  105. }
  106. public function testLoad_DuplicateRelationshipLines_ThrowsException()
  107. {
  108. $geoffString = '(Liz) {"name": "Elizabeth", "title": "Queen of the Commonwealth Realms", "birth.date": "1926-04-21"}'.PHP_EOL
  109. . '(Phil) {"name": "Philip", "title": "Duke of Edinburgh", "birth.date": "1921-06-21"}'.PHP_EOL
  110. . '(Liz)-[LizNPhil:MARRIED]->(Phil)'.PHP_EOL
  111. . '(Liz)-[LizNPhil:MARRIED]->(Phil)';
  112. $this->setExpectedException('Everyman\Neo4j\Exception');
  113. $batch = $this->geoff->load($geoffString);
  114. }
  115. public function testLoad_IndexLines_ReturnsBatch()
  116. {
  117. $geoffString = '(Liz) {"name": "Elizabeth", "title": "Queen of the Commonwealth Realms", "birth.date": "1926-04-21"}'.PHP_EOL
  118. . '(Phil) {"name": "Philip", "title": "Duke of Edinburgh", "birth.date": "1921-06-21"}'.PHP_EOL
  119. . '(Liz)-[LizNPhil:MARRIED]->(Phil) {"marriage.place": "Westminster Abbey", "marriage.date": "1947-11-20"}'.PHP_EOL
  120. . '{People}->(Liz) {"name": "Elizabeth"}'.PHP_EOL
  121. . '{People}->(Phil) {"name": "Philip", "title":"Duke"}'.PHP_EOL
  122. . '{Marriages}->[LizNPhil] {"wife": "Elizabeth", "husband": "Philip"}';
  123. $batch = $this->geoff->load($geoffString);
  124. $ops = $batch->getOperations();
  125. self::assertEquals(8, count($ops));
  126. $op = $ops[3];
  127. self::assertInstanceOf('Everyman\Neo4j\Batch\AddTo', $op);
  128. self::assertInstanceOf('Everyman\Neo4j\Node', $op->getEntity());
  129. self::assertEquals('Elizabeth', $op->getEntity()->getProperty('name'));
  130. self::assertInstanceOf('Everyman\Neo4j\Index', $op->getIndex());
  131. self::assertEquals('People', $op->getIndex()->getName());
  132. self::assertEquals(Index::TypeNode, $op->getIndex()->getType());
  133. self::assertEquals('name', $op->getKey());
  134. self::assertEquals('Elizabeth', $op->getValue());
  135. $op = $ops[4];
  136. self::assertInstanceOf('Everyman\Neo4j\Batch\AddTo', $op);
  137. self::assertInstanceOf('Everyman\Neo4j\Node', $op->getEntity());
  138. self::assertEquals('Philip', $op->getEntity()->getProperty('name'));
  139. self::assertInstanceOf('Everyman\Neo4j\Index', $op->getIndex());
  140. self::assertEquals('People', $op->getIndex()->getName());
  141. self::assertEquals(Index::TypeNode, $op->getIndex()->getType());
  142. self::assertEquals('name', $op->getKey());
  143. self::assertEquals('Philip', $op->getValue());
  144. $op = $ops[5];
  145. self::assertInstanceOf('Everyman\Neo4j\Batch\AddTo', $op);
  146. self::assertInstanceOf('Everyman\Neo4j\Node', $op->getEntity());
  147. self::assertEquals('Philip', $op->getEntity()->getProperty('name'));
  148. self::assertInstanceOf('Everyman\Neo4j\Index', $op->getIndex());
  149. self::assertEquals('People', $op->getIndex()->getName());
  150. self::assertEquals(Index::TypeNode, $op->getIndex()->getType());
  151. self::assertEquals('title', $op->getKey());
  152. self::assertEquals('Duke', $op->getValue());
  153. self::assertSame($ops[4]->getEntity(), $ops[5]->getEntity());
  154. $op = $ops[6];
  155. self::assertInstanceOf('Everyman\Neo4j\Batch\AddTo', $op);
  156. self::assertInstanceOf('Everyman\Neo4j\Relationship', $op->getEntity());
  157. self::assertSame($ops[2]->getEntity(), $op->getEntity());
  158. self::assertInstanceOf('Everyman\Neo4j\Index', $op->getIndex());
  159. self::assertEquals('Marriages', $op->getIndex()->getName());
  160. self::assertEquals(Index::TypeRelationship, $op->getIndex()->getType());
  161. self::assertEquals('wife', $op->getKey());
  162. self::assertEquals('Elizabeth', $op->getValue());
  163. $op = $ops[7];
  164. self::assertInstanceOf('Everyman\Neo4j\Batch\AddTo', $op);
  165. self::assertInstanceOf('Everyman\Neo4j\Relationship', $op->getEntity());
  166. self::assertSame($ops[2]->getEntity(), $op->getEntity());
  167. self::assertInstanceOf('Everyman\Neo4j\Index', $op->getIndex());
  168. self::assertEquals('Marriages', $op->getIndex()->getName());
  169. self::assertEquals(Index::TypeRelationship, $op->getIndex()->getType());
  170. self::assertEquals('husband', $op->getKey());
  171. self::assertEquals('Philip', $op->getValue());
  172. }
  173. public function testLoad_IndexLinesInvalidNode_ThrowsException()
  174. {
  175. $geoffString = '(Liz) {"name": "Elizabeth", "title": "Queen of the Commonwealth Realms", "birth.date": "1926-04-21"}'.PHP_EOL
  176. . '{People}->(Phil) {"name": "Philip", "title":"Duke"}';
  177. $this->setExpectedException('Everyman\Neo4j\Exception');
  178. $batch = $this->geoff->load($geoffString);
  179. }
  180. public function testLoad_IndexLinesInvalidRelationship_ThrowsException()
  181. {
  182. $geoffString = '{Marriages}->[LizNPhil] {"wife": "Elizabeth", "husband": "Philip"}';
  183. $this->setExpectedException('Everyman\Neo4j\Exception');
  184. $batch = $this->geoff->load($geoffString);
  185. }
  186. public function testLoad_IndexBracketMismatch_ThrowsException()
  187. {
  188. $geoffString = '(Liz) {"name": "Elizabeth", "title": "Queen of the Commonwealth Realms", "birth.date": "1926-04-21"}'.PHP_EOL
  189. . '(Phil) {"name": "Philip", "title": "Duke of Edinburgh", "birth.date": "1921-06-21"}'.PHP_EOL
  190. . '(Liz)-[LizNPhil:MARRIED]->(Phil) {"marriage.place": "Westminster Abbey", "marriage.date": "1947-11-20"}'.PHP_EOL
  191. . '{Marriages}->[LizNPhil) {"wife": "Elizabeth", "husband": "Philip"}';
  192. $this->setExpectedException('Everyman\Neo4j\Exception');
  193. $batch = $this->geoff->load($geoffString);
  194. }
  195. public function testLoad_InvalidLine_ThrowsException()
  196. {
  197. $geoffString = '(Liz) {"name": "Elizabeth", "title": "Queen of the Commonwealth Realms", "birth.date": "1926-04-21"}'.PHP_EOL
  198. . 'this line is total gibberish'.PHP_EOL
  199. . '{People}->(Liz) {"name": "Elizabeth"}';
  200. $this->setExpectedException('Everyman\Neo4j\Exception');
  201. $batch = $this->geoff->load($geoffString);
  202. }
  203. public function testLoad_UseTheSameBatch_ReturnsBatch()
  204. {
  205. $geoffString = '(Liz) {"name": "Elizabeth", "title": "Queen of the Commonwealth Realms", "birth.date": "1926-04-21"}'.PHP_EOL
  206. . '(Phil) {"name": "Philip", "title": "Duke of Edinburgh", "birth.date": "1921-06-21"}'.PHP_EOL
  207. . '(Chaz)';
  208. $initBatch = new Batch($this->client);
  209. $batch = $this->geoff->load($geoffString, $initBatch);
  210. self::assertSame($initBatch, $batch);
  211. $ops = $batch->getOperations();
  212. self::assertEquals(3, count($ops));
  213. $batch2 = $this->geoff->load($geoffString, $batch);
  214. self::assertSame($batch, $batch2);
  215. $ops = $batch->getOperations();
  216. self::assertEquals(6, count($ops));
  217. }
  218. public function testDump_PathsGiven_NoFileDescriptor_ReturnsString()
  219. {
  220. $nodeA = new Node($this->client);
  221. $nodeA->setId(123)->setProperties(array('foo' => 'bar','baz' => 'qux'));
  222. $nodeB = new Node($this->client);
  223. $nodeB->setId(456)->setProperties(array('somekey' => 'somevalue'));
  224. $nodeC = new Node($this->client);
  225. $nodeC->setId(789);
  226. $relA = new Relationship($this->client);
  227. $relA->setId(987)->setType('TEST')
  228. ->setStartNode($nodeA)->setEndNode($nodeB)
  229. ->setProperties(array('anotherkey' => 'anothervalue'));
  230. $relB = new Relationship($this->client);
  231. $relB->setId(654)->setType('TSET')
  232. ->setStartNode($nodeB)->setEndNode($nodeC);
  233. $path = new Path();
  234. $path->appendNode($nodeA);
  235. $path->appendNode($nodeB);
  236. $path->appendNode($nodeC);
  237. $path->appendRelationship($relA);
  238. $path->appendRelationship($relB);
  239. $expected =<<<GEOFF
  240. (123) {"foo":"bar","baz":"qux"}
  241. (456) {"somekey":"somevalue"}
  242. (789)
  243. (123)-[987:TEST]->(456) {"anotherkey":"anothervalue"}
  244. (456)-[654:TSET]->(789)
  245. GEOFF;
  246. $result = $this->geoff->dump($path);
  247. self::assertEquals($expected, $result);
  248. }
  249. public function testDump_PathsGiven_FileDescriptor_ReturnsDescriptor()
  250. {
  251. $nodeA = new Node($this->client);
  252. $nodeA->setId(123)->setProperties(array('foo' => 'bar','baz' => 'qux'));
  253. $nodeB = new Node($this->client);
  254. $nodeB->setId(456)->setProperties(array('somekey' => 'somevalue'));
  255. $relA = new Relationship($this->client);
  256. $relA->setId(987)->setType('TEST')
  257. ->setStartNode($nodeA)->setEndNode($nodeB)
  258. ->setProperties(array('anotherkey' => 'anothervalue'));
  259. $path = new Path();
  260. $path->appendNode($nodeA);
  261. $path->appendNode($nodeB);
  262. $path->appendRelationship($relA);
  263. $expected =<<<GEOFF
  264. (123) {"foo":"bar","baz":"qux"}
  265. (456) {"somekey":"somevalue"}
  266. (123)-[987:TEST]->(456) {"anotherkey":"anothervalue"}
  267. GEOFF;
  268. $handle = fopen('data:text/plain,', 'w+');
  269. $resultHandle = $this->geoff->dump($path, $handle);
  270. self::assertSame($handle, $resultHandle);
  271. self::assertEquals($expected, stream_get_contents($resultHandle, -1, 0));
  272. }
  273. public function testDump_NotAStreamOrString_ThrowsException()
  274. {
  275. $handle = 123;
  276. $path = new Path();
  277. $this->setExpectedException('Everyman\Neo4j\Exception');
  278. $batch = $this->geoff->dump($path, $handle);
  279. }
  280. public function testDump_NotAPath_ThrowsException()
  281. {
  282. $handle = "file";
  283. $notPath = "blah";
  284. $this->setExpectedException('Everyman\Neo4j\Exception');
  285. $batch = $this->geoff->dump($notPath);
  286. }
  287. }