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 13, 2016
1 parent
d57ad1e
commit cab83b1
Showing
11 changed files
with
491 additions
and
3 deletions.
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
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,106 @@ | ||
<?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 = 'session', $default = false) { | ||
if (isset($this->data) && isset($this->data->$key)) { | ||
return $this->data->$key; | ||
} | ||
return $default; | ||
} | ||
|
||
/** | ||
* Retrieves session data | ||
* | ||
* @return object | ||
*/ | ||
public function getData() { | ||
return $this->data; | ||
} | ||
|
||
/** | ||
* Sets a keyed value to be part of the data property object | ||
* | ||
* @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 | ||
* | ||
* @param array $data Session data to save | ||
* @return bool | ||
*/ | ||
public function setData($data) { | ||
if (empty($data)) { | ||
return false; | ||
} | ||
$cache = new FileCache(); | ||
$cache->putData('session', $data); | ||
|
||
$this->data->set('data', $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'); | ||
$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,23 @@ | ||
Feature: Machine tokens command | ||
In order to manage my devices | ||
As a user | ||
I need to be able to view and delete my machine tokens. | ||
|
||
Background: I am logged in | ||
Given I am authenticated | ||
|
||
@vcr machine-tokens_list | ||
Scenario: List the machine tokens | ||
When I run "terminus machine-token:list" | ||
Then I should get: | ||
""" | ||
[[machine_token_id]] | ||
""" | ||
|
||
@vcr machine-tokens_delete | ||
Scenario: Delete machine token | ||
When I run "terminus machine-token:delete [[machine_token_id]] --yes" | ||
Then I should get: | ||
""" | ||
Deleted [[machine_token_device]]! | ||
""" |
51 changes: 51 additions & 0 deletions
51
tests/new_unit_tests/Commands/MachineToken/MachineTokenCommandTest.php
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,51 @@ | ||
<?php | ||
/** | ||
* @file | ||
* Contains Pantheon\Terminus\UnitTests\Commands\Auth\MachineTokenCommandTest | ||
*/ | ||
|
||
|
||
namespace Pantheon\Terminus\UnitTests\Commands\Auth; | ||
|
||
|
||
use Pantheon\Terminus\Session\Session; | ||
use Psr\Log\NullLogger; | ||
use Terminus\Collections\MachineTokens; | ||
use Terminus\Models\User; | ||
|
||
abstract class MachineTokenCommandTest extends \PHPUnit_Framework_TestCase { | ||
protected $session; | ||
protected $machine_tokens; | ||
protected $user; | ||
protected $logger; | ||
protected $command; | ||
|
||
/** | ||
* Sets up the fixture, for example, open a network connection. | ||
* This method is called before a test is executed. | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->machine_tokens = $this->getMockBuilder(MachineTokens::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->user = $this->getMockBuilder(User::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->user->machine_tokens = $this->machine_tokens; | ||
|
||
$this->session = $this->getMockBuilder(Session::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->session->method('getUser') | ||
->willReturn($this->user); | ||
|
||
$this->logger = $this->getMockBuilder(NullLogger::class) | ||
->setMethods(array('log')) | ||
->getMock(); | ||
|
||
|
||
} | ||
} |
Oops, something went wrong.