Skip to content

Commit

Permalink
MNT Add more robust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Feb 13, 2023
1 parent f898f78 commit 487831b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/docs export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.github export-ignore
41 changes: 40 additions & 1 deletion tests/DynamoDbSessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,54 @@

namespace SilverStripe\DynamoDb\Tests;

use ReflectionProperty;
use SilverStripe\Core\Environment;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\DynamoDb\Model\DynamoDbSession;

class DynamoDbSessionTest extends SapphireTest
{

public function testGetReturnsNullWhenNotConfigured()
{
// Ensure the session table is null for this test
$origTableName = Environment::getEnv('AWS_DYNAMODB_SESSION_TABLE');
Environment::setEnv('AWS_DYNAMODB_SESSION_TABLE', null);

$this->assertNull(DynamoDbSession::get());

Environment::setEnv('AWS_DYNAMODB_SESSION_TABLE', $origTableName);
}

public function testGetReturnsSessionWhenConfigured()
{
// Ensure the session table is not null for this test
$origTableName = Environment::getEnv('AWS_DYNAMODB_SESSION_TABLE');
$origRegionName = Environment::getEnv('AWS_REGION_NAME');
$origDynamoDBEndpoint = Environment::getEnv('AWS_DYNAMODB_ENDPOINT');
$origAccessKey = Environment::getEnv('AWS_ACCESS_KEY');
$origSecretKey = Environment::getEnv('AWS_SECRET_KEY');
Environment::setEnv('AWS_DYNAMODB_SESSION_TABLE', 'sessiontable');
Environment::setEnv('AWS_REGION_NAME', 'ap-southeast-2');
Environment::setEnv('AWS_DYNAMODB_ENDPOINT', 'http://www.example.com/');
Environment::setEnv('AWS_ACCESS_KEY', 'arbitrary-value');
Environment::setEnv('AWS_SECRET_KEY', 'arbitrary-value');
$session = DynamoDbSession::get();

// Check there is a session
$this->assertInstanceOf(DynamoDbSession::class, $session);

$reflectionTable = new ReflectionProperty($session, 'table');
$reflectionTable->setAccessible(true);

// Check the session set the correct table name
$this->assertSame('sessiontable', $reflectionTable->getValue($session));

// Set env vars back to whatever they were before
Environment::setEnv('AWS_DYNAMODB_SESSION_TABLE', $origTableName);
Environment::setEnv('AWS_REGION_NAME', $origRegionName);
Environment::setEnv('AWS_DYNAMODB_ENDPOINT', $origDynamoDBEndpoint);
Environment::setEnv('AWS_ACCESS_KEY', $origAccessKey);
Environment::setEnv('AWS_SECRET_KEY', $origSecretKey);
}

public function testGetSessionHandler()
Expand Down

0 comments on commit 487831b

Please sign in to comment.