Improve SessionManager constructor and tests

Relates to https://github.com/shaarli/Shaarli/pull/1005

Changed:
- pass a copy of the ConfigManager instance instead of a reference
- move FakeConfigManager to a dedicated file
- update tests

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
This commit is contained in:
VirtualTam 2017-11-08 20:24:49 +01:00
parent b14d34d2c7
commit dd883aaf09
3 changed files with 25 additions and 24 deletions

View file

@ -12,12 +12,12 @@ class SessionManager
* Constructor * Constructor
* *
* @param array $session The $_SESSION array (reference) * @param array $session The $_SESSION array (reference)
* @param ConfigManager $conf ConfigManager instance (reference) * @param ConfigManager $conf ConfigManager instance
*/ */
public function __construct(& $session, & $conf) public function __construct(& $session, $conf)
{ {
$this->session = &$session; $this->session = &$session;
$this->conf = &$conf; $this->conf = $conf;
} }
/** /**

View file

@ -1,4 +1,6 @@
<?php <?php
require_once 'tests/utils/FakeConfigManager.php';
// Initialize reference data _before_ PHPUnit starts a session // Initialize reference data _before_ PHPUnit starts a session
require_once 'tests/utils/ReferenceSessionIdHashes.php'; require_once 'tests/utils/ReferenceSessionIdHashes.php';
ReferenceSessionIdHashes::genAllHashes(); ReferenceSessionIdHashes::genAllHashes();
@ -7,18 +9,6 @@
use \PHPUnit\Framework\TestCase; use \PHPUnit\Framework\TestCase;
/**
* Fake ConfigManager
*/
class FakeConfigManager
{
public static function get($key)
{
return $key;
}
}
/** /**
* Test coverage for SessionManager * Test coverage for SessionManager
*/ */
@ -27,12 +17,16 @@ class SessionManagerTest extends TestCase
// Session ID hashes // Session ID hashes
protected static $sidHashes = null; protected static $sidHashes = null;
// Fake ConfigManager
protected static $conf = null;
/** /**
* Assign reference data * Assign reference data
*/ */
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
self::$sidHashes = ReferenceSessionIdHashes::getHashes(); self::$sidHashes = ReferenceSessionIdHashes::getHashes();
self::$conf = new FakeConfigManager();
} }
/** /**
@ -41,8 +35,7 @@ public static function setUpBeforeClass()
public function testGenerateToken() public function testGenerateToken()
{ {
$session = []; $session = [];
$conf = new FakeConfigManager(); $sessionManager = new SessionManager($session, self::$conf);
$sessionManager = new SessionManager($session, $conf);
$token = $sessionManager->generateToken(); $token = $sessionManager->generateToken();
@ -61,9 +54,7 @@ public function testCheckToken()
$token => 1, $token => 1,
], ],
]; ];
$conf = new FakeConfigManager(); $sessionManager = new SessionManager($session, self::$conf);
$sessionManager = new SessionManager($session, $conf);
// check and destroy the token // check and destroy the token
$this->assertTrue($sessionManager->checkToken($token)); $this->assertTrue($sessionManager->checkToken($token));
@ -79,8 +70,7 @@ public function testCheckToken()
public function testGenerateAndCheckToken() public function testGenerateAndCheckToken()
{ {
$session = []; $session = [];
$conf = new FakeConfigManager(); $sessionManager = new SessionManager($session, self::$conf);
$sessionManager = new SessionManager($session, $conf);
$token = $sessionManager->generateToken(); $token = $sessionManager->generateToken();
@ -102,8 +92,7 @@ public function testGenerateAndCheckToken()
public function testCheckInvalidToken() public function testCheckInvalidToken()
{ {
$session = []; $session = [];
$conf = new FakeConfigManager(); $sessionManager = new SessionManager($session, self::$conf);
$sessionManager = new SessionManager($session, $conf);
$this->assertFalse($sessionManager->checkToken('4dccc3a45ad9d03e5542b90c37d8db6d10f2b38b')); $this->assertFalse($sessionManager->checkToken('4dccc3a45ad9d03e5542b90c37d8db6d10f2b38b'));
} }

View file

@ -0,0 +1,12 @@
<?php
/**
* Fake ConfigManager
*/
class FakeConfigManager
{
public static function get($key)
{
return $key;
}
}