/** * Test fetch by key with namespace */ public function testFetchByKeyWithNamespace() { $str_ns = 'SpaceTheFinalFrontier'; $str_id = '1263751723'; $obj_http = $this->initTestHttpClient('https://datastore.googleapis.com/v1/projects/DatasetTest:lookup', ['json' => (object) ['keys' => [(object) ['path' => [(object) ['kind' => 'Test', 'id' => $str_id]], 'partitionId' => (object) ['projectId' => self::TEST_PROJECT, 'namespaceId' => $str_ns]]]]], ['found' => [(object) ['entity' => (object) ['key' => (object) ['path' => [(object) ['kind' => 'Test', 'id' => $str_id]]], 'properties' => (object) ['name' => (object) ['excludeFromIndexes' => false, 'stringValue' => 'Tom']]], 'version' => '123', 'cursor' => 'gfuh37f86gyu23']]]); $obj_gateway = $this->initTestGateway($str_ns)->setHttpClient($obj_http); $obj_store = new \GDS\Store('Test', $obj_gateway); $obj_entity = $obj_store->fetchById($str_id); $this->assertInstanceOf('\\GDS\\Entity', $obj_entity); $this->assertEquals($str_id, $obj_entity->getKeyId()); $this->assertEquals('Tom', $obj_entity->name); $this->validateHttpClient($obj_http); }
/** * Start transaction */ public function testBeginTransactionBasic() { $obj_request = new \google\appengine\datastore\v4\BeginTransactionRequest(); $obj_response = new \google\appengine\datastore\v4\BeginTransactionResponse(); $obj_response->setTransaction('test-txn-ref-123'); $this->apiProxyMock->expectCall('datastore_v4', 'BeginTransaction', $obj_request, $obj_response); $_SERVER['APPLICATION_ID'] = 'DatasetTest'; $obj_gateway = $this->getMockBuilder('\\GDS\\Gateway\\ProtoBuf')->setMethods(['withTransaction', 'fetchById'])->getMock(); $obj_gateway->expects($this->once())->method('withTransaction')->with($this->equalTo('test-txn-ref-123'))->willReturn($obj_gateway); $obj_store = new GDS\Store('Book', $obj_gateway); $obj_store->beginTransaction(); $obj_store->fetchById('123456'); $this->apiProxyMock->verify(); }
/** * Test FetchById with Namespace Request */ public function testNamespacedFetchByIdRequest() { $obj_gateway = new GDS\Gateway\GoogleAPIClient($this->setupTestClient(), 'Dataset', 'Spaced'); $obj_store = new \GDS\Store('Book', $obj_gateway); $this->expectRequest('https://www.googleapis.com/datastore/v1beta2/datasets/Dataset/lookup', '{"keys":[{"path":[{"id":1234567890,"kind":"Book"}],"partitionId":{"namespace":"Spaced"}}]}'); $obj_store->fetchById(1234567890); }
/** * Fetch with string list */ public function testFetchWithStringListResult() { $obj_response = new \google\appengine\datastore\v4\LookupResponse(); $obj_found = $obj_response->addFound(); $obj_entity = $obj_found->mutableEntity(); $obj_result_key = $obj_entity->mutableKey(); $obj_result_kpe = $obj_result_key->addPathElement(); $obj_result_kpe->setKind('Book'); $obj_result_kpe->setId(123456789); $obj_result_property = $obj_entity->addProperty(); $obj_result_property->setName('director'); $obj_val = $obj_result_property->mutableValue(); $obj_val->setStringValue('Robert Zemeckis'); $obj_result_property2 = $obj_entity->addProperty(); $obj_result_property2->setName('dedications'); $obj_val2 = $obj_result_property2->mutableValue(); $obj_val2->addListValue()->setStringValue('Marty McFly'); $obj_val2->addListValue()->setStringValue('Emmett Brown'); $this->apiProxyMock->expectCall('datastore_v4', 'Lookup', $this->getBasicBookByIdRequest(), $obj_response); $obj_gateway = new GDS\Gateway\ProtoBuf('Dataset'); $obj_store = new GDS\Store('Book', $obj_gateway); $obj_result = $obj_store->fetchById(123456789); $this->assertInstanceOf('\\GDS\\Entity', $obj_result); $this->assertEquals(2, count($obj_result->getData())); $this->assertEquals('Book', $obj_result->getKind()); $this->assertEquals(123456789, $obj_result->getKeyId()); $this->assertEquals('Robert Zemeckis', $obj_result->director); $this->assertEquals(['Marty McFly', 'Emmett Brown'], $obj_result->dedications); $this->apiProxyMock->verify(); }
/** * Fetch with custom entity class */ public function testFetchByIdWithResult() { $obj_request = new \google\appengine\datastore\v4\LookupRequest(); $obj_request->mutableReadOptions(); $obj_key = $obj_request->addKey(); $obj_partition = $obj_key->mutablePartitionId(); $obj_partition->setDatasetId('Dataset'); $obj_kpe = $obj_key->addPathElement(); $obj_kpe->setKind('Book'); $obj_kpe->setId(123456789); $obj_response = new \google\appengine\datastore\v4\LookupResponse(); $obj_found = $obj_response->addFound(); $obj_entity = $obj_found->mutableEntity(); $obj_result_key = $obj_entity->mutableKey(); $obj_result_kpe = $obj_result_key->addPathElement(); $obj_result_kpe->setKind('Book'); $obj_result_kpe->setId(123456789); $obj_result_property = $obj_entity->addProperty(); $obj_result_property->setName('author'); $obj_val = $obj_result_property->mutableValue(); // addDeprecatedValue(); $obj_val->setStringValue('William Shakespeare'); $this->apiProxyMock->expectCall('datastore_v4', 'Lookup', $obj_request, $obj_response); $obj_gateway = new \GDS\Gateway\ProtoBuf('Dataset'); $obj_store = new \GDS\Store('Book', $obj_gateway); $obj_store->setEntityClass('Book'); $obj_result = $obj_store->fetchById(123456789); $this->assertInstanceOf('\\GDS\\Entity', $obj_result); $this->assertInstanceOf('\\Book', $obj_result); $this->assertEquals(1, count($obj_result->getData())); $this->assertEquals('Book', $obj_result->getKind()); $this->assertEquals(123456789, $obj_result->getKeyId()); $this->assertEquals($obj_result->author, 'William Shakespeare'); $this->apiProxyMock->verify(); }
/** * Test fetch by single ID using schema */ public function testFetchByIdWithSchema() { $str_id = '1263751723'; $obj_http = $this->initTestHttpClient('https://datastore.googleapis.com/v1/projects/DatasetTest:lookup', ['json' => (object) ['keys' => [(object) ['path' => [(object) ['kind' => 'Test', 'id' => $str_id]], 'partitionId' => (object) ['projectId' => self::TEST_PROJECT]]]]], ['found' => [(object) ['entity' => (object) ['key' => (object) ['path' => [(object) ['kind' => 'Test', 'id' => $str_id]]], 'properties' => (object) ['name' => (object) ['excludeFromIndexes' => false, 'stringValue' => 'Tom'], 'age' => (object) ['excludeFromIndexes' => false, 'integerValue' => 37], 'dob' => (object) ['excludeFromIndexes' => false, 'timestampValue' => "2014-10-02T15:01:23.045123456Z"], 'likes' => (object) ['arrayValue' => (object) ['values' => [(object) ['excludeFromIndexes' => false, 'stringValue' => 'Beer'], (object) ['stringValue' => 'Cycling'], (object) ['stringValue' => 'PHP']]]], 'weight' => (object) ['excludeFromIndexes' => false, 'doubleValue' => 85.98999999999999], 'author' => (object) ['excludeFromIndexes' => false, 'booleanValue' => true], 'chickens' => (object) ['excludeFromIndexes' => false, 'nullValue' => null], 'lives' => (object) ['excludeFromIndexes' => false, 'geoPointValue' => (object) ['latitude' => 1.23, 'longitude' => 4.56]]]], 'version' => '123', 'cursor' => 'gfuh37f86gyu23']]]); $obj_gateway = $this->initTestGateway()->setHttpClient($obj_http); $obj_schema = (new \GDS\Schema('Test'))->addString('name')->addInteger('age')->addDatetime('dob')->addStringList('likes')->addFloat('weight')->addGeopoint('lives')->addBoolean('author'); $obj_store = new \GDS\Store($obj_schema, $obj_gateway); $obj_entity = $obj_store->fetchById($str_id); $this->assertInstanceOf('\\GDS\\Entity', $obj_entity); $this->assertEquals($str_id, $obj_entity->getKeyId()); $this->assertEquals('Tom', $obj_entity->name); $this->assertEquals(37, $obj_entity->age); $this->assertEquals('2014-10-02 15:01:23', $obj_entity->dob); $this->assertTrue(is_array($obj_entity->likes)); $this->assertEquals(['Beer', 'Cycling', 'PHP'], $obj_entity->likes); $this->assertEquals(85.98999999999999, $obj_entity->weight); $this->assertInstanceOf('\\GDS\\Property\\Geopoint', $obj_entity->lives); $this->assertEquals(1.23, $obj_entity->lives->getLatitude()); $this->assertEquals(4.56, $obj_entity->lives->getLongitude()); $this->assertTrue($obj_entity->author); $this->assertNull($obj_entity->chickens); $this->validateHttpClient($obj_http); }