From db2231ce5a2fdb4be91eb33995df37de053a1be2 Mon Sep 17 00:00:00 2001 From: Dany Rafina Date: Fri, 22 Jun 2018 15:22:07 +0200 Subject: [PATCH] - Fix HttpSession bug - Add units test for HttpSession - Up --- Core/Base/Http/HttpSession.php | 80 ++++++++-------------- Core/Base/Http/SessionInterfaceRequest.php | 22 ------ Core/Requirement/FrameworkCore.php | 4 +- composer.json | 5 ++ tests/HttpSessionTest.php | 40 +++++++++++ 5 files changed, 77 insertions(+), 74 deletions(-) create mode 100644 tests/HttpSessionTest.php diff --git a/Core/Base/Http/HttpSession.php b/Core/Base/Http/HttpSession.php index 9f32011..9da8d69 100755 --- a/Core/Base/Http/HttpSession.php +++ b/Core/Base/Http/HttpSession.php @@ -9,11 +9,10 @@ * file that was distributed with this source code. */ -namespace iumioFramework\Core\Base\Http\Session; +namespace iumioFramework\Core\Base\Http; -use iumioFramework\Core\Base\Http\SessionBagRequest; -use iumioFramework\Core\Base\Http\SessionInterfaceRequest; use iumioFramework\Core\Exception\Server\Server500; +use iumioFramework\Core\Requirement\Patterns\Singleton\SingletonClassicPattern; /** * HttpSession class. @@ -25,25 +24,26 @@ * @licence MIT License * @link https://framework.iumio.com */ -class HttpSession implements SessionInterfaceRequest +class HttpSession extends SingletonClassicPattern implements SessionInterfaceRequest { - protected $session; - /** - * @return mixed - */ + protected $session = null; + /** * HttpSession constructor. + * @throws */ public function __construct() { - //session_start(); $this->start(); } public function start() { - $this->session = $_SESSION; + if (!$this->isStarted() && null === $this->session) { + $_SESSION = []; + $this->session = $_SESSION; + } } /** @@ -51,7 +51,7 @@ public function start() */ public function getId() { - // TODO: Implement getId() method. + return (session_id()); } /** @@ -60,7 +60,7 @@ public function getId() */ public function setId($id) { - // TODO: Implement setId() method. + return (session_id($id)); } /** @@ -68,7 +68,7 @@ public function setId($id) */ public function getName() { - // TODO: Implement getName() method. + return (session_name()); } /** @@ -77,7 +77,7 @@ public function getName() */ public function setName($name) { - // TODO: Implement setName() method. + return (session_name($name)); } /** @@ -104,7 +104,8 @@ public function migrate($destroy = false, $lifetime = null) */ public function save() { - // TODO: Implement save() method. + $_SESSION = array_merge($this->session, $_SESSION); + return (0 === count(array_diff($this->session, $_SESSION)))? true : false; } /** @@ -113,7 +114,7 @@ public function save() */ public function has($name) { - // TODO: Implement has() method. + return ((isset($this->session[$name]) && null != $this->session[$name])? true : false); } /** @@ -123,7 +124,7 @@ public function has($name) */ public function get($name, $default = null) { - return (isset($this->session[$name])? $this->session[$name] : null); + return ((isset($this->session[$name]) && null != $this->session[$name])? $this->session[$name] : null); } /** @@ -135,9 +136,8 @@ public function get($name, $default = null) public function set($name, $value) { if (is_string($name)) { - $_SESSION[$name] = $value; - $this->start(); - return (true); + $this->session[$name] = $value; + return ((isset($this->session[$name]) && null != $this->session[$name])? true : false); } else { throw new Server500(new \ArrayObject(array("explain" => "Session Error : Your session name is not a string value", "solution" => @@ -185,40 +185,20 @@ public function remove($name) */ public function clear() { - return(session_destroy()); + if ($this->isStarted()) { + return (session_destroy()); + } + return (false); } - /** - * @return mixed + /** Check if session is started + * @return bool */ public function isStarted() { - // TODO: Implement isStarted() method. - } - - /** - * @param SessionBagRequest $bag - * @return mixed - */ - public function registerBag(SessionBagRequest $bag) - { - // TODO: Implement registerBag() method. - } - - /** - * @param string $name - * @return mixed - */ - public function getBag($name) - { - // TODO: Implement getBag() method. - } - - /** - * @return mixed - */ - public function getMetaBagRequest() - { - // TODO: Implement getMetaBagRequest() method. + if ('cli' !== php_sapi_name()) { + return ((PHP_SESSION_ACTIVE === session_status()) ? true : false); + } + return (false); } } diff --git a/Core/Base/Http/SessionInterfaceRequest.php b/Core/Base/Http/SessionInterfaceRequest.php index 8c9c9e1..df16678 100755 --- a/Core/Base/Http/SessionInterfaceRequest.php +++ b/Core/Base/Http/SessionInterfaceRequest.php @@ -159,26 +159,4 @@ public function clear(); */ public function isStarted(); - /** - * Registers a SessionBagRequest with the session. - * - * @param SessionBagRequest $bag - */ - public function registerBag(SessionBagRequest $bag); - - /** - * Gets a bag instance by name. - * - * @param string $name - * - * @return SessionBagRequest - */ - public function getBag($name); - - /** - * Gets session meta. - * - * @return MetaBagRequest - */ - public function getMetaBagRequest(); } diff --git a/Core/Requirement/FrameworkCore.php b/Core/Requirement/FrameworkCore.php index fbe64fd..b690ffd 100755 --- a/Core/Requirement/FrameworkCore.php +++ b/Core/Requirement/FrameworkCore.php @@ -46,10 +46,10 @@ abstract class FrameworkCore extends GlobalCoreService protected $environment; private static $runtime_parameters = null; - public const CORE_VERSION = '1.0.0'; + public const CORE_VERSION = '1.0.1'; public const CORE_NAME = 'SUN'; public const CORE_STAGE = 'STS'; - public const CORE_BUILD = 201800; + public const CORE_BUILD = 201801; protected static $edition = array(); /** diff --git a/composer.json b/composer.json index fda9093..4a27f40 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,11 @@ "role": "iumio Framework Co-Architect / iumio Co-Founder" } ], + "autoload-dev": { + "psr-4": { + "iumioFramework\\Tests\\": "tests/" + } + }, "autoload": { "psr-4": { "iumioFramework\\": "", diff --git a/tests/HttpSessionTest.php b/tests/HttpSessionTest.php new file mode 100644 index 0000000..f1e209f --- /dev/null +++ b/tests/HttpSessionTest.php @@ -0,0 +1,40 @@ + + * * + * * iumio Framework, an iumio component [https://iumio.com] + * * + * * To get more information about licence, please check the licence file + * + */ + + +namespace iumioFramework\Tests; + +use iumioFramework\Core\Base\Http\HttpSession; +use PHPUnit\Framework\TestCase; + +/** + * Class ServerTest + * @package iumioFramework\Tests + */ +class HttpSessionTest extends TestCase +{ + /** + * Test create an instance of HttpSession + * @throws \Exception + */ + public function testCreateInstance() + { + $instance = HttpSession::getInstance(); + $instance->set("test", "value1"); + $instance->save(); + $this->assertInstanceOf("iumioFramework\Core\Base\Http\HttpSession", $instance); + $this->assertTrue(isset($_SESSION["test"])); + $this->assertEquals($_SESSION["test"], "value1"); + } +}