Skip to content

Commit

Permalink
Configuration for StandardSessionConnection data type (#1838)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chase Coalwell authored Jul 3, 2019
1 parent 295706a commit 9cf42d9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "enhancement",
"category": "DynamoDb",
"description": "Added support configuring data attribute type as 'binary', defaults to 'string'."
}
]
19 changes: 19 additions & 0 deletions src/DynamoDb/SessionConnectionConfigTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ trait SessionConnectionConfigTrait
/** @var string Name of the data attribute in table. Default: "data" */
protected $dataAttribute = 'data';

/** @var string Type of the data attribute in table. Default: "string" */
protected $dataAttributeType = 'string';

/** @var integer Lifetime of inactive sessions expiration */
protected $sessionLifetime;

Expand Down Expand Up @@ -114,6 +117,22 @@ public function setDataAttribute($dataAttribute)
$this->dataAttribute = $dataAttribute;
}

/**
* @return string
*/
public function getDataAttributeType()
{
return $this->dataAttributeType;
}

/**
* @param string $dataAttributeType
*/
public function setDataAttributeType($dataAttributeType)
{
$this->dataAttributeType = $dataAttributeType;
}

/**
* @return number
*/
Expand Down
8 changes: 7 additions & 1 deletion src/DynamoDb/StandardSessionConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ public function write($id, $data, $isChanged)
];
if ($isChanged) {
if ($data != '') {
$attributes[$this->getDataAttribute()] = ['Value' => ['S' => $data]];
$type = $this->getDataAttributeType();
if ($type == 'binary') {
$attributes[$this->getDataAttribute()] = ['Value' => ['B' => $data]];
} else {
$attributes[$this->getDataAttribute()] = ['Value' => ['S' => $data]];
}

} else {
$attributes[$this->getDataAttribute()] = ['Action' => 'DELETE'];
}
Expand Down
3 changes: 3 additions & 0 deletions tests/DynamoDb/SessionConnectionConfigTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function testStandardConfig()
$this->assertEquals('sessions', $scc->getTableName());
$this->assertEquals('id', $scc->getHashKey());
$this->assertEquals('data', $scc->getDataAttribute());
$this->assertEquals('string', $scc->getDataAttributeType());
$this->assertEquals((int) ini_get('session.gc_maxlifetime'), $scc->getSessionLifetime());
$this->assertEquals('expires', $scc->getSessionLifetimeAttribute());
$this->assertTrue($scc->isConsistentRead());
Expand All @@ -34,6 +35,7 @@ public function testCustomConfig()
'table_name' => 'sessions_custom',
'hash_key' => 'id_custom',
'data_attribute' => 'data_custom',
'data_attribute_type' => 'binary',
'session_lifetime' => 2019,
'session_lifetime_attribute' => 'expires_custom',
'consistent_read' => false,
Expand All @@ -48,6 +50,7 @@ public function testCustomConfig()
$this->assertEquals('sessions_custom', $scc->getTableName());
$this->assertEquals('id_custom', $scc->getHashKey());
$this->assertEquals('data_custom', $scc->getDataAttribute());
$this->assertEquals('binary', $scc->getDataAttributeType());
$this->assertEquals(2019, $scc->getSessionLifetime());
$this->assertEquals('expires_custom', $scc->getSessionLifetimeAttribute());
$this->assertFalse($scc->isConsistentRead());
Expand Down
25 changes: 23 additions & 2 deletions tests/DynamoDb/StandardSessionConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function testStandardConfig()
$this->assertEquals('sessions', $scc->getTableName());
$this->assertEquals('id', $scc->getHashKey());
$this->assertEquals('data', $scc->getDataAttribute());
$this->assertEquals('string', $scc->getDataAttributeType());
$this->assertEquals((int) ini_get('session.gc_maxlifetime'), $scc->getSessionLifetime());
$this->assertEquals('expires', $scc->getSessionLifetimeAttribute());
$this->assertTrue($scc->isConsistentRead());
Expand All @@ -38,6 +39,7 @@ public function testCustomConfig()
'table_name' => 'sessions_custom',
'hash_key' => 'id_custom',
'data_attribute' => 'data_custom',
'data_attribute_type' => 'binary',
'session_lifetime' => 2019,
'session_lifetime_attribute' => 'expires_custom',
'consistent_read' => false,
Expand All @@ -51,6 +53,7 @@ public function testCustomConfig()
$this->assertEquals('sessions_custom', $scc->getTableName());
$this->assertEquals('id_custom', $scc->getHashKey());
$this->assertEquals('data_custom', $scc->getDataAttribute());
$this->assertEquals('binary', $scc->getDataAttributeType());
$this->assertEquals(2019, $scc->getSessionLifetime());
$this->assertEquals('expires_custom', $scc->getSessionLifetimeAttribute());
$this->assertFalse($scc->isConsistentRead());
Expand All @@ -66,7 +69,8 @@ public function testReadRetrievesItemData()
$this->addMockResults($client, [
new Result(['Item' => [
'sessionid' => ['S' => 'session1'],
'otherkey' => ['S' => 'foo']
'otherkey' => ['S' => 'foo'],
'binarykey' => ['B' => 'bar']
]]),
]);

Expand All @@ -83,7 +87,7 @@ public function testReadRetrievesItemData()
$data = $connection->read('session1');

$this->assertEquals(
['sessionid' => 'session1', 'otherkey' => 'foo'],
['sessionid' => 'session1', 'otherkey' => 'foo', 'binarykey' => 'bar'],
$data
);
}
Expand All @@ -109,12 +113,29 @@ public function testWriteUpdatesItemData()
$this->assertArrayHasKey('expires', $updates);
$this->assertArrayHasKey('lock', $updates);
$this->assertArrayHasKey('data', $updates);
$this->assertArrayHasKey('S', $updates['data']['Value']);
}));
$connection = new StandardSessionConnection($client);
$return = $connection->write('s1', serialize(['foo' => 'bar']), true);
$this->assertTrue($return);
}

public function testWriteUpdatesItemDataAsBinary()
{
$client = $this->getTestSdk()->createDynamoDb();
$this->addMockResults($client, [new Result([])]);
$client->getHandlerList()->appendBuild(Middleware::tap(function ($command) {
$updates = $command['AttributeUpdates'];
$this->assertArrayHasKey('data', $updates);
$this->assertArrayHasKey('B', $updates['data']['Value']);
}));
$connection = new StandardSessionConnection($client, [
'data_attribute_type' => 'binary',
]);
$return = $connection->write('s1', serialize(['foo' => 'bar']), true);
$this->assertTrue($return);
}

public function testWriteReturnsFalseOnFailure()
{
$client = $this->getTestSdk()->createDynamoDb();
Expand Down

0 comments on commit 9cf42d9

Please sign in to comment.