/** * Validate getters and setters for Ancestry */ public function testAncestry() { $obj_entity1 = new GDS\Entity(); $obj_entity1->setKind('Testing')->setKeyId(123456); $obj_entity2 = new GDS\Entity(); $obj_entity2->setKind('Testing')->setKeyName('testing'); $obj_entity2->setAncestry($obj_entity1); $this->assertEquals($obj_entity2->getAncestry(), $obj_entity1); }
<?php /** * Create a single record in GDS * * @author Tom Walder <*****@*****.**> */ require_once 'boilerplate.php'; $obj_person_schema = (new GDS\Schema('Person'))->addString('name')->addString('description'); $obj_store = new GDS\Store($obj_person_schema, $obj_gateway); // Create the parent $obj_john = new \GDS\Entity(); $obj_john->name = 'John Smiths'; $obj_john->description = 'A parent'; $obj_john->setKeyName('*****@*****.**'); $obj_store->upsert($obj_john); $obj_stored_parent = $obj_store->fetchOne("SELECT * FROM Person WHERE __key__ = KEY(Person, '*****@*****.**')"); // Create a child $obj_jane = new \GDS\Entity(); $obj_jane->name = 'Jane Smiths'; $obj_jane->description = 'A child'; $obj_jane->setKeyName('*****@*****.**'); $obj_jane->setAncestry($obj_stored_parent); $obj_store->upsert($obj_jane);
/** * Create with 2+ Ancestors (from array) */ public function testAncestryFromArray() { $obj_schema = (new \GDS\Schema('Child'))->addString('name', true); $obj_mapper = new \GDS\Mapper\ProtoBuf(); $obj_mapper->setSchema($obj_schema); $obj_gds_child = new \GDS\Entity(); $obj_gds_child->setKind('Child'); $obj_gds_child->name = 'Son'; $obj_gds_child->setAncestry([['kind' => 'GrandParent', 'id' => '123456781'], ['kind' => 'Parent', 'id' => '123456782']]); $obj_target_ent = new \google\appengine\datastore\v4\Entity(); $obj_mapper->mapToGoogle($obj_gds_child, $obj_target_ent); /** @var \google\appengine\datastore\v4\Key\PathElement[] $arr_path */ $arr_path = $obj_target_ent->getKey()->getPathElementList(); $obj_path_first = $arr_path[0]; $obj_path_second = $arr_path[1]; $obj_path_last = $arr_path[2]; $this->assertEquals('GrandParent', $obj_path_first->getKind()); $this->assertEquals('123456781', $obj_path_first->getId()); $this->assertEquals('Parent', $obj_path_second->getKind()); $this->assertEquals('123456782', $obj_path_second->getId()); $this->assertEquals('Child', $obj_path_last->getKind()); }
/** * Tests 2 tiers of ancestry, based on array */ public function testAncestryFromArray() { $obj_schema = (new \GDS\Schema('Child'))->addString('name', true); $obj_mapper = new \GDS\Mapper\RESTv1(); $obj_mapper->setSchema($obj_schema); $obj_gds_child = new \GDS\Entity(); $obj_gds_child->setKind('Child'); $obj_gds_child->name = 'Son'; $obj_gds_child->setAncestry([['kind' => 'GrandParent', 'id' => '123456781'], ['kind' => 'Parent', 'id' => '123456782']]); $obj_rest_entity = $obj_mapper->mapToGoogle($obj_gds_child); $this->assertObjectHasAttribute('name', $obj_rest_entity->properties); $this->assertObjectHasAttribute('stringValue', $obj_rest_entity->properties->name); $this->assertEquals('Son', $obj_rest_entity->properties->name->stringValue); $this->assertObjectHasAttribute('key', $obj_rest_entity); $this->assertObjectHasAttribute('path', $obj_rest_entity->key); $this->assertTrue(is_array($obj_rest_entity->key->path)); $this->assertEquals(3, count($obj_rest_entity->key->path)); $obj_path_first = $obj_rest_entity->key->path[0]; $obj_path_second = $obj_rest_entity->key->path[1]; $obj_path_last = $obj_rest_entity->key->path[2]; $this->assertObjectHasAttribute('kind', $obj_path_first); $this->assertObjectHasAttribute('id', $obj_path_first); $this->assertObjectNotHasAttribute('name', $obj_path_first); $this->assertEquals('GrandParent', $obj_path_first->kind); $this->assertEquals('123456781', $obj_path_first->id); $this->assertObjectHasAttribute('kind', $obj_path_second); $this->assertObjectHasAttribute('id', $obj_path_second); $this->assertObjectNotHasAttribute('name', $obj_path_second); $this->assertEquals('Parent', $obj_path_second->kind); $this->assertEquals('123456782', $obj_path_second->id); $this->assertObjectHasAttribute('kind', $obj_path_last); $this->assertObjectNotHasAttribute('id', $obj_path_last); $this->assertObjectNotHasAttribute('name', $obj_path_last); $this->assertEquals('Child', $obj_path_last->kind); }