/**
  * 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);
 }
Example #2
0
 /**
  * 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);
 }
Example #3
0
<?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);
 /**
  * Insert One with Parent
  *
  * @expectedException        Exception
  * @expectedExceptionMessage Mismatch count of requested & returned Auto IDs
  */
 public function testUpsertEntityAncestorOneLevel()
 {
     $this->apiProxyMock->expectCall('datastore_v4', 'Commit', $this->getUpsertRequestWithBookAndAuthor(), new \google\appengine\datastore\v4\CommitResponse());
     $obj_will = new GDS\Entity();
     $obj_will->setKind('Author');
     $obj_will->setKeyName('WilliamShakespeare');
     $obj_store = $this->createBasicStore();
     $obj_book = $obj_store->createEntity(['nickname' => 'Romeo']);
     $obj_book->setAncestry($obj_will);
     $obj_store->upsert($obj_book);
     $this->apiProxyMock->verify();
 }
Example #5
0
 /**
  * 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());
 }
Example #6
0
 /**
  * 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);
 }
Example #7
0
 /**
  * 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);
 }