Google\Cloud\Datastore\DatastoreSessionHandler::open PHP Method

open() public method

Start a session, by creating a transaction for the later write.
public open ( string $savePath, string $sessionName ) : boolean
$savePath string The value of `session.save_path` setting will be used here. It will use this value as the Datastore namespaceId.
$sessionName string The value of `session.name` setting will be used here. It will use this value as the Datastore kind.
return boolean
    public function open($savePath, $sessionName)
    {
        $this->kind = $sessionName;
        if (preg_match(self::NAMESPACE_ALLOWED_PATTERN, $savePath) !== 1 || preg_match(self::NAMESPACE_RESERVED_PATTERN, $savePath) === 1) {
            throw new InvalidArgumentException(sprintf('The given save_path "%s" not allowed', $savePath));
        }
        $this->namespaceId = $savePath;
        $this->transaction = $this->datastore->transaction();
        return true;
    }

Usage Example

 /**
  * @expectedException PHPUnit_Framework_Error_Warning
  */
 public function testGcWithException()
 {
     $key1 = new Key('projectid');
     $key1->pathElement(self::KIND, 'sessionid1');
     $key2 = new Key('projectid');
     $key2->pathElement(self::KIND, 'sessionid2');
     $entity1 = new Entity($key1);
     $entity2 = new Entity($key2);
     $query = $this->prophesize(Query::class);
     $query->kind(self::KIND)->shouldBeCalledTimes(1)->willReturn($query->reveal());
     $that = $this;
     $query->filter(Argument::type('string'), Argument::type('string'), Argument::type('int'))->shouldBeCalledTimes(1)->will(function ($args) use($that, $query) {
         $that->assertEquals('t', $args[0]);
         $that->assertEquals('<', $args[1]);
         $that->assertInternalType('int', $args[2]);
         $diff = time() - $args[2];
         // 2 seconds grace period should be enough
         $that->assertTrue($diff <= 102);
         $that->assertTrue($diff >= 100);
         return $query->reveal();
     });
     $query->order('t')->shouldBeCalledTimes(1)->willReturn($query->reveal());
     $query->keysOnly()->shouldBeCalledTimes(1)->willReturn($query->reveal());
     $query->limit(1000)->shouldBeCalledTimes(1)->willReturn($query->reveal());
     $this->datastore->transaction()->shouldBeCalledTimes(1)->willReturn($this->transaction->reveal());
     $this->datastore->query()->shouldBeCalledTimes(1)->willReturn($query->reveal());
     $this->datastore->runQuery(Argument::type(Query::class), Argument::type('array'))->shouldBeCalledTimes(1)->will(function ($args) use($that, $query, $entity1, $entity2) {
         $that->assertEquals($query->reveal(), $args[0]);
         $that->assertEquals(['namespaceId' => self::NAMESPACE_ID], $args[1]);
         return [$entity1, $entity2];
     });
     $this->datastore->deleteBatch([$key1, $key2])->shouldBeCalledTimes(1)->willThrow(new Exception());
     $datastoreSessionHandler = new DatastoreSessionHandler($this->datastore->reveal(), 1000);
     $datastoreSessionHandler->open(self::NAMESPACE_ID, self::KIND);
     $ret = $datastoreSessionHandler->gc(100);
     $this->assertEquals(false, $ret);
 }