Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration for StandardSessionConnection data type #1838

Merged
merged 4 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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