/** * Test upsert with namespace */ public function testUpsertWithNamespace() { $str_ns = 'SomeNamepsace'; $obj_http = $this->initTestHttpClient('https://datastore.googleapis.com/v1/projects/DatasetTest:commit', ['json' => (object) ['mode' => 'NON_TRANSACTIONAL', 'mutations' => [(object) ['upsert' => (object) ['key' => (object) ['path' => [(object) ['kind' => 'Test', 'id' => '123456789']], 'partitionId' => (object) ['projectId' => self::TEST_PROJECT, 'namespaceId' => $str_ns]], 'properties' => (object) ['name' => (object) ['excludeFromIndexes' => false, 'stringValue' => 'Tom']]]]]]]); $obj_gateway = $this->initTestGateway($str_ns)->setHttpClient($obj_http); $obj_store = new \GDS\Store('Test', $obj_gateway); $obj_entity = new GDS\Entity(); $obj_entity->setKeyId('123456789'); $obj_entity->name = 'Tom'; $obj_store->upsert($obj_entity); $this->validateHttpClient($obj_http); }
/** * Create with 2+ Ancestors */ public function testAncestryFromEntity() { $obj_schema = (new \GDS\Schema('Child'))->addString('name', true); $obj_mapper = new \GDS\Mapper\ProtoBuf(); $obj_mapper->setSchema($obj_schema); $obj_gds_grandparent = new \GDS\Entity(); $obj_gds_grandparent->setKind('GrandParent'); $obj_gds_grandparent->setKeyId('123456781'); $obj_gds_grandparent->name = 'Grandfather'; $obj_gds_parent = new \GDS\Entity(); $obj_gds_parent->setKind('Parent'); $obj_gds_parent->setKeyId('123456782'); $obj_gds_parent->name = 'Dad'; $obj_gds_parent->setAncestry($obj_gds_grandparent); $obj_gds_child = new \GDS\Entity(); $obj_gds_child->setKind('Child'); $obj_gds_child->name = 'Son'; $obj_gds_child->setAncestry($obj_gds_parent); $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 entity */ public function testAncestryFromEntity() { $obj_schema = (new \GDS\Schema('Child'))->addString('name', true); $obj_mapper = new \GDS\Mapper\RESTv1(); $obj_mapper->setSchema($obj_schema); $obj_gds_grandparent = new \GDS\Entity(); $obj_gds_grandparent->setKind('GrandParent'); $obj_gds_grandparent->setKeyId('123456781'); $obj_gds_grandparent->name = 'Grandfather'; $obj_gds_parent = new \GDS\Entity(); $obj_gds_parent->setKind('Parent'); $obj_gds_parent->setKeyId('123456782'); $obj_gds_parent->name = 'Dad'; $obj_gds_parent->setAncestry($obj_gds_grandparent); $obj_gds_child = new \GDS\Entity(); $obj_gds_child->setKind('Child'); $obj_gds_child->name = 'Son'; $obj_gds_child->setAncestry($obj_gds_parent); $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); }
/** * Test transactional entity upsert */ public function testTxnUpsert() { // First begin the transaction $str_txn_ref = 'ghei34g498jhegijv0894hiwgerhiugjreiugh'; $obj_http = $this->initTestHttpClient('https://datastore.googleapis.com/v1/projects/DatasetTest:beginTransaction', [], ['transaction' => $str_txn_ref]); /** @var \GDS\Gateway\RESTv1 $obj_gateway */ $obj_gateway = $this->initTestGateway()->setHttpClient($obj_http); $obj_store = new \GDS\Store('Test', $obj_gateway); $obj_store->beginTransaction(); $this->validateHttpClient($obj_http); // Now set up the transactional upsert $obj_http = $this->initTestHttpClient('https://datastore.googleapis.com/v1/projects/DatasetTest:commit', ['json' => (object) ['mode' => 'TRANSACTIONAL', 'transaction' => $str_txn_ref, 'mutations' => [(object) ['upsert' => (object) ['key' => (object) ['path' => [(object) ['kind' => 'Test', 'id' => '123456789']], 'partitionId' => (object) ['projectId' => self::TEST_PROJECT]], 'properties' => (object) ['name' => (object) ['excludeFromIndexes' => false, 'stringValue' => 'Tom']]]]]]]); $obj_gateway->setHttpClient($obj_http); // Do the upsert $obj_entity = new GDS\Entity(); $obj_entity->setKeyId('123456789'); $obj_entity->name = 'Tom'; $obj_store->upsert($obj_entity); // Test the final output $this->validateHttpClient($obj_http); }