-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ronan Dowling
committed
Sep 14, 2016
1 parent
3c6909f
commit 2411920
Showing
6 changed files
with
276 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace Pantheon\Terminus\Session; | ||
|
||
use Terminus\Caches\FileCache; | ||
use Terminus\Models\User; | ||
|
||
class Session { | ||
/** | ||
* @var FileCache | ||
*/ | ||
protected $cache; | ||
|
||
/** | ||
* @var object | ||
*/ | ||
protected $data; | ||
|
||
/** | ||
* Instantiates object, sets session data | ||
*/ | ||
public function __construct($fileCache) { | ||
$this->cache = $fileCache; | ||
$session = $this->cache->getData('session'); | ||
$this->data = $session; | ||
if (empty($session)) { | ||
$this->data = new \stdClass(); | ||
} | ||
} | ||
|
||
/** | ||
* Removes the session from the cache | ||
* | ||
* @return void | ||
*/ | ||
public function destroy() { | ||
$this->cache->remove('session'); | ||
} | ||
|
||
/** | ||
* Returns given data property or default if DNE. | ||
* | ||
* @param string $key Name of property to return | ||
* @param mixed $default Default return value in case property DNE | ||
* @return mixed | ||
*/ | ||
public function get($key, $default = false) { | ||
if (isset($this->data) && isset($this->data->$key)) { | ||
return $this->data->$key; | ||
} | ||
return $default; | ||
} | ||
|
||
/** | ||
* Sets a keyed value to be part of the data property object | ||
* | ||
* TODO: This is never used and arguably shouldn't be (since it doesn't save | ||
* data to the persistent session store) | ||
* This should be removed in favor of setData which is designed to | ||
* instantiate the entire session or changed to add persistent data. | ||
* | ||
* @param string $key Name of data property | ||
* @param mixed $value Value of property to set | ||
* @return Session | ||
*/ | ||
public function set($key, $value = null) { | ||
$this->data->$key = $value; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Saves session data to cache | ||
* | ||
* TODO: The difference between set and setData is confusing. This should be | ||
* refactored when Auth() is. | ||
* | ||
* @param array $data Session data to save | ||
* @return bool | ||
*/ | ||
public function setData($data) { | ||
if (empty($data)) { | ||
return false; | ||
} | ||
$this->cache->putData('session', $data); | ||
|
||
foreach ($data as $k => $v) { | ||
$this->set($k, $v); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Returns a user with the current session user id | ||
* @return \Terminus\Models\User [user] $session user | ||
*/ | ||
public function getUser() { | ||
$user_uuid = $this->get('user_uuid'); | ||
// TODO: Remove this direct instantiation to make this more testable | ||
$user = new User((object)array('id' => $user_uuid)); | ||
return $user; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Pantheon\Terminus\Session; | ||
|
||
|
||
/** | ||
* Interface SessionAwareInterface | ||
* @package Pantheon\Terminus\Session | ||
* | ||
* Provides an interface for direct injection of the session helper. | ||
*/ | ||
interface SessionAwareInterface { | ||
|
||
/*** | ||
* @param Session $session | ||
* @return void | ||
*/ | ||
public function setSession(Session $session); | ||
|
||
/** | ||
* @return Session | ||
*/ | ||
public function session(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Pantheon\Terminus\Session; | ||
|
||
|
||
/** | ||
* Provides the basic properties needed to fulfill the SessionAwareInterface. | ||
* | ||
* Class SessionAwareTrait | ||
* @package Pantheon\Terminus\Session | ||
*/ | ||
trait SessionAwareTrait { | ||
|
||
/** | ||
* @var Session | ||
*/ | ||
protected $session; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function setSession(Session $session) | ||
{ | ||
$this->session = $session; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function session() | ||
{ | ||
return $this->session; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace Pantheon\Terminus\UnitTests\Session; | ||
|
||
use Pantheon\Terminus\Session\Session; | ||
use Terminus\Caches\FileCache; | ||
|
||
/** | ||
* Testing class for Pantheon\Terminus\Session\Session | ||
*/ | ||
class SessionTest extends \PHPUnit_Framework_TestCase { | ||
|
||
protected $session; | ||
protected $filecache; | ||
|
||
protected function setUp() { | ||
$this->filecache = $this->getMockBuilder(FileCache::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->session = new Session($this->filecache); | ||
|
||
} | ||
|
||
/** | ||
* Test getting and setting data | ||
*/ | ||
public function testSetGet() { | ||
$this->session->set('foo', 'bar'); | ||
$this->session->set('abc', 123); | ||
$this->session->set('foo', 'baz'); | ||
|
||
$this->assertEquals('baz', $this->session->get('foo')); | ||
$this->assertEquals(123, $this->session->get('abc')); | ||
} | ||
|
||
|
||
/** | ||
* Test getting and setting data | ||
*/ | ||
public function testSetData() { | ||
$data = [ | ||
'foo' => 'bar', | ||
'abc' => 123 | ||
]; | ||
|
||
$this->filecache->expects($this->once()) | ||
->method('putData') | ||
->with('session', $data); | ||
|
||
$this->session->setData($data); | ||
|
||
foreach ($data as $key => $val) { | ||
$this->assertEquals($val, $this->session->get($key)); | ||
} | ||
} | ||
|
||
/** | ||
* Test getting and setting data | ||
*/ | ||
public function testGetUser() { | ||
$data = [ | ||
'foo' => 'bar', | ||
'abc' => 123 | ||
]; | ||
|
||
$this->filecache->expects($this->once()) | ||
->method('getData') | ||
->with('session') | ||
->willReturn(['user_uuid' => '123']); | ||
|
||
$this->session = new Session($this->filecache); | ||
|
||
// @TODO: Test mocking of new user (will require some sort of mockable factory rather than | ||
// the direct use of new User() in Session) | ||
$user = $this->session->getUser(); | ||
$this->assertInstanceOf('Terminus\Models\User', $user); | ||
} | ||
|
||
/** | ||
* Test destroying the session | ||
*/ | ||
public function testDestroy() { | ||
$data = [ | ||
'foo' => 'bar', | ||
'abc' => 123 | ||
]; | ||
|
||
$this->filecache->expects($this->once()) | ||
->method('remove') | ||
->with('session'); | ||
|
||
$this->session->destroy(); | ||
} | ||
|
||
} |