From 1248fbd87bf9335a7ed41d1474a0bbccb19d77a7 Mon Sep 17 00:00:00 2001 From: Groups Date: Mon, 8 Jan 2018 05:34:50 -0800 Subject: [PATCH] indexing further tested --- src/Pho/Kernel/Init.php | 13 ++-- .../Kernel/Services/Index/IndexInterface.php | 59 +++++++++++++++++-- .../Kernel/Traits/Node/PersistentTrait.php | 5 ++ tests/Pho/Kernel/BootTest.php | 18 +++++- .../Kernel/DefaultObjectsAreStandardTest.php | 2 +- tests/Pho/Kernel/Services/Index/Neo4jTest.php | 51 ++++++++++++++-- tests/Pho/Kernel/TestCase.php | 3 + 7 files changed, 131 insertions(+), 20 deletions(-) diff --git a/src/Pho/Kernel/Init.php b/src/Pho/Kernel/Init.php index 07030d7..ddaa9fa 100644 --- a/src/Pho/Kernel/Init.php +++ b/src/Pho/Kernel/Init.php @@ -175,13 +175,14 @@ protected function seedRoot(? Foundation\AbstractActor $founder = null): void } else { $this->logger()->info("Creating a new graph from scratch"); + if(!is_null($founder)) { + $founder->persist(); + } + else { + throw new Exceptions\FounderMustBeSetException(); + } $this["founder"] = $this->share(function($c) use ($founder) { - if(!is_null($founder)) { - $founder->persist(); - return $founder; - } - $founder_class = $c["config"]->default_objects->founder; - return new $founder_class($c); // will turn into admin by Network + return $founder; }); $this["graph"] = $this->share(function($c) { $graph_class = $c["config"]->default_objects->graph; diff --git a/src/Pho/Kernel/Services/Index/IndexInterface.php b/src/Pho/Kernel/Services/Index/IndexInterface.php index 445bd5b..9902426 100644 --- a/src/Pho/Kernel/Services/Index/IndexInterface.php +++ b/src/Pho/Kernel/Services/Index/IndexInterface.php @@ -25,19 +25,68 @@ interface IndexInterface { /** - * Direct access to the index service to query data. + * Searches through the index with given key and its value. + * + * @param string $query Cypher query + * @param array $param Query params. Optional. + * + * @return mixed Result set + */ + public function query(string $query, array $params = []); //: mixed; + + /** + * Direct access to the the index client * - * @param string $query Cypher query. - * @param array $params Query parameters. + * This class does also provide direct read-only access to the + * client, for debugging purposes. * - * @return mixed + * @return mixed Client in its native form */ - public function query(string $query, array $params = []); //: void; + public function client(); //: mixed + /** + * Indexes an entity + * + * Given in array form, indexes the entity (be it an + * edge or node) making it searchable. + * + * @param array $entity In array form. + * + * @return void + * + * @throws Exception if the entity header is not recognized. + */ public function index(array $entity): void; + /** + * Notifies the index that a node was deleted + * + * Removes the entity from the index, cleaning + * up resources and making index results more efficient. + * + * @param string $id ID in string format. + * + * @return void + */ public function nodeDeleted(string $id): void; + /** + * Notifies the index that an edge was deleted + * + * Removes the entity from the index, cleaning + * up resources and making index results more efficient. + * + * @param string $id ID in string format. + * + * @return void + */ public function edgeDeleted(string $id): void; + /** + * Cleans up the index database + * + * @return void + */ + public function flush(): void; + } diff --git a/src/Pho/Kernel/Traits/Node/PersistentTrait.php b/src/Pho/Kernel/Traits/Node/PersistentTrait.php index bf67b72..670eaa6 100644 --- a/src/Pho/Kernel/Traits/Node/PersistentTrait.php +++ b/src/Pho/Kernel/Traits/Node/PersistentTrait.php @@ -28,6 +28,11 @@ protected function persistable(): bool return (static::T_PERSISTENT && $this->kernel->live()); } + /** + * Persists the node in the database. + * + * @return void + */ public function persist(): void { if(!$this->persistable()) diff --git a/tests/Pho/Kernel/BootTest.php b/tests/Pho/Kernel/BootTest.php index 428e1c2..19aa663 100644 --- a/tests/Pho/Kernel/BootTest.php +++ b/tests/Pho/Kernel/BootTest.php @@ -11,6 +11,8 @@ namespace Pho\Kernel; +use Pho\Kernel\Standards\Space; + class BootTest extends TestCase { @@ -23,10 +25,21 @@ public function testSimple() { $this->assertInstanceOf(Kernel::class, $this->kernel); } + /** + * @expectedException \Pho\Kernel\Exceptions\FounderMustBeSetException + * + * @return void + */ + public function testFirstBootWithNoFounder() { + $this->redis->flushdb(); + $this->kernel->boot(); + } + public function testBoot() { $this->redis->flushdb(); $this->assertFalse($this->kernel->live()); - $this->kernel->boot(); + $founder = new \PhoNetworksAutogenerated\User($this->kernel, new Space($this->kernel), "123456"); + $this->kernel->boot($founder); $redis_configs = $this->redis->keys("configs:*"); $this->assertContains("configs:graph_id", $redis_configs); $graph_recreated = $this->kernel->gs()->node($this->redis->get("configs:graph_id")); @@ -38,7 +51,8 @@ public function testBoot() { public function testReboot() { $this->redis->flushdb(); - $this->kernel->boot(); + $founder = new \PhoNetworksAutogenerated\User($this->kernel, new Space($this->kernel), "123456"); + $this->kernel->boot($founder); $founder_id = $this->kernel->founder()->id()->toString(); $graph_id = $this->kernel->graph()->id()->toString(); $founder_context = $this->kernel->founder()->context()->id()->toString(); diff --git a/tests/Pho/Kernel/DefaultObjectsAreStandardTest.php b/tests/Pho/Kernel/DefaultObjectsAreStandardTest.php index 9292b34..150b078 100644 --- a/tests/Pho/Kernel/DefaultObjectsAreStandardTest.php +++ b/tests/Pho/Kernel/DefaultObjectsAreStandardTest.php @@ -20,7 +20,7 @@ public function testSimple() { $this->kernel->logger()->info("DB flushed and restarted"); $this->assertInstanceOf(Kernel::class, $this->kernel); $this->kernel->logger()->info("instance check for kernel completed."); - $this->assertInstanceOf(Standards\Founder::class, $this->kernel->founder()); + $this->assertInstanceOf(\PhoNetworksAutogenerated\User::class, $this->kernel->founder()); $this->kernel->logger()->info("instance check for founder completed."); $u = new \PhoNetworksAutogenerated\User($this->kernel, $this->graph, "123456"); $this->kernel->logger()->info("The User object is created"); diff --git a/tests/Pho/Kernel/Services/Index/Neo4jTest.php b/tests/Pho/Kernel/Services/Index/Neo4jTest.php index 0bb3df8..a9363f1 100644 --- a/tests/Pho/Kernel/Services/Index/Neo4jTest.php +++ b/tests/Pho/Kernel/Services/Index/Neo4jTest.php @@ -18,17 +18,31 @@ class Neo4jTest extends TestCase private $client; + protected function _countNumberOfNodes(): int + { + $res = $this->kernel->index()->query("MATCH(n) RETURN n"); + return count($res->results()); + } + + protected function _countNumberOfEdges(): int + { + $res = $this->kernel->index()->query("MATCH ()-[r]->() RETURN r"); + return count($res->results()); + } + public function testKernelService() { $this->assertInstanceOf(\Pho\Kernel\Services\Index\Adapters\Neo4j::class, $this->kernel->index()); } public function testBasic() { $this->flushDBandRestart(); + //eval(\Psy\sh()); $u = new \PhoNetworksAutogenerated\User($this->kernel, $this->graph, "123456"); $about = "for testing purposes only"; $u->setAbout($about); $res = $this->kernel->index()->query("MATCH(n {About: {about}}) RETURN n", ["about" => $about]); - $this->assertEquals($res->firstRecord()->values()[0]->value("Birthday"), $u->getBirthday()); + //$this->assertEquals($res->firstRecord()->values()[0]->value("Birthday"), $u->getBirthday()); + $this->assertEquals($res->results()[0]["Birthday"], $u->getBirthday()); $this->created[] = $u->id(); } @@ -38,15 +52,40 @@ public function testWithEdge() { $content = "my first post"; $post = $u->post($content); $res = $this->kernel->index()->query("MATCH(n {Status: {content}}) RETURN n", ["content" => $content]); - $this->assertEquals($res->firstRecord()->values()[0]->value("CreateTime"), $post->getCreateTime()); + $this->assertEquals($res->results()[0]["CreateTime"], $post->getCreateTime()); $res = $this->kernel->index()->query("MATCH(t {udid: {tail}})-[e]->(h {udid: {head}}) RETURN e", ["tail" => (string) $u->id(), "head" => (string) $post->id()]); - $this->assertEquals($res->firstRecord()->values()[0]->value("udid"), (string) $post->edges()->in()->current()->id()); + $this->assertEquals($res->results()[0]["udid"], (string) $post->edges()->in()->current()->id()); $this->created[] = $u->id(); $this->created[] = $post->id(); $this->created[] = $post->edges()->in()->current()->id(); } - - - + public function testFromScratch() { + $this->flushDBandRestart(); + $initial_number_of_nodes = 3; // virtualgraph, graph, founder + $initial_number_of_edges = 0; + $this->assertEquals($initial_number_of_nodes, $this->_countNumberOfNodes()); + $this->assertEquals($initial_number_of_edges, $this->_countNumberOfEdges()); + $user1 = new \PhoNetworksAutogenerated\User($this->kernel, $this->graph, "123456"); + $user2 = new \PhoNetworksAutogenerated\User($this->kernel, $this->graph, "123456"); + $user3 = new \PhoNetworksAutogenerated\User($this->kernel, $this->graph, "123456"); + $this->assertEquals(($initial_number_of_nodes + 3), $this->_countNumberOfNodes()); // including the Graph itself // where is the founder? + $user1->follow($user2); + $user2->follow($user1); + $user2->follow($user3); + $this->assertEquals(($initial_number_of_edges + 3), $this->_countNumberOfEdges()); + $this->kernel->founder()->follow($user1); + $this->assertEquals(($initial_number_of_edges + 4), $this->_countNumberOfEdges()); + $content = "my first post"; + $post = $user1->post($content); + $this->assertEquals(($initial_number_of_nodes + 4), $this->_countNumberOfNodes()); + $this->assertEquals(($initial_number_of_edges + 5), $this->_countNumberOfEdges()); + $this->kernel->founder()->like($post); + $user3->like($post); + $this->assertEquals(($initial_number_of_nodes + 4), $this->_countNumberOfNodes()); + $this->assertEquals(($initial_number_of_edges + 7), $this->_countNumberOfEdges()); + //eval(\Psy\sh()); + + + } } \ No newline at end of file diff --git a/tests/Pho/Kernel/TestCase.php b/tests/Pho/Kernel/TestCase.php index a75c584..1c306db 100644 --- a/tests/Pho/Kernel/TestCase.php +++ b/tests/Pho/Kernel/TestCase.php @@ -22,6 +22,7 @@ include("tests/assets/compiled/UserOut/Consume.php"); use Pho\Kernel\Kernel; +use Pho\Kernel\Standards\Space; class TestCase extends \PHPUnit\Framework\TestCase { @@ -49,6 +50,8 @@ protected function startKernel($founder=null): void { $this->configs = $this->getKernelConfig(); $this->kernel = new Kernel($this->configs); + if(is_null($founder)) + $founder = new \PhoNetworksAutogenerated\User($this->kernel, new Space($this->kernel), "123456"); $this->kernel->boot($founder); $this->graph = $this->kernel->graph(); $this->kernel->logger()->info("Test started for: %s", get_class($this));