diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..c20b8ae --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: php +php: + - '7.2' +before_script: + - mkdir elements && mkdir elements/cache && mkdir elements/cache/dev && mkdir elements/cache/prod + - composer install +script: phpunit --configuration phpunit.xml \ No newline at end of file diff --git a/BaseApps/FServiceApp/Routing/service.irt b/BaseApps/FServiceApp/Routing/service.merc similarity index 100% rename from BaseApps/FServiceApp/Routing/service.irt rename to BaseApps/FServiceApp/Routing/service.merc diff --git a/CHANGELOG.md b/CHANGELOG.md index 0345414..a14cf36 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,15 +5,32 @@ All notable changes to this project will be documented in this file.

-iumio Framework, an iumio component[https://www.iumio.com] +[iumio Framework, an iumio component] ======================================================== @ Let's create more simply ## [STABLE RELEASE] +## [1.0.2] STS - 2018-07-23 +- Fixed : + - HttpSession : Causes by Phpunit PrinterClass +- Added : + - Constant IUMIO_ROOT for root project directory path + - Session file on cache directory + + +## [1.0.1] STS - 2018-07-01 +- Fixed : + - HttpSession + ## [1.0.0] STS - 2018-06-01 +- Fixes : + - HttpSession [STABLE RELEASE]: https://github.com/iumio-team/iumio-framework/ [1.0.0]: https://github.com/iumio-team/iumio-framework/releases/tag/v1.0.0 +[1.0.1]: https://github.com/iumio-team/iumio-framework/releases/tag/v1.0.1 +[1.0.2]: https://github.com/iumio-team/iumio-framework/releases/tag/v1.0.2 +[iumio Framework, an iumio component]: https://www.iumio.com diff --git a/Core/Base/Http/HttpSession.php b/Core/Base/Http/HttpSession.php index 9da8d69..6a82cb8 100755 --- a/Core/Base/Http/HttpSession.php +++ b/Core/Base/Http/HttpSession.php @@ -13,6 +13,7 @@ use iumioFramework\Core\Exception\Server\Server500; use iumioFramework\Core\Requirement\Patterns\Singleton\SingletonClassicPattern; +use iumioFramework\Core\Server\Server; /** * HttpSession class. @@ -26,8 +27,20 @@ */ class HttpSession extends SingletonClassicPattern implements SessionInterfaceRequest { + /** + * @var null|array The session oject : Initialized to null firstly + */ protected $session = null; + /** + * @var null|string The session identifier : Initialized to null firstly + */ + private $id = null; + + /** + * @var null|string The session name : Initialized to null firstly + */ + private $name = null; /** * HttpSession constructor. @@ -38,28 +51,35 @@ public function __construct() $this->start(); } + /** Start the session + * @return void + * @throws Server500 + * @throws \Exception + */ public function start() { if (!$this->isStarted() && null === $this->session) { - $_SESSION = []; + $this->initSessionConfig(); + session_start(); $this->session = $_SESSION; } } - /** - * @return mixed + /** Get the session id + * @return mixed The session id */ public function getId() { - return (session_id()); + return ($this->id); } - /** - * @param string $id - * @return mixed + /** Set a new session id + * @param string $id The session id + * @return mixed Return the session id */ public function setId($id) { + $this->id = $id; return (session_id($id)); } @@ -68,7 +88,7 @@ public function setId($id) */ public function getName() { - return (session_name()); + return ($this->name); } /** @@ -77,6 +97,7 @@ public function getName() */ public function setName($name) { + $this->name = $name; return (session_name($name)); } @@ -100,27 +121,30 @@ public function migrate($destroy = false, $lifetime = null) } /** - * @return mixed + * Save the modification about sessions items + * @return bool true if iumio session is the same as PHP SESSION, false for an error + * ($_SESSION & $this->session not the same) */ - public function save() + public function save():bool { - $_SESSION = array_merge($this->session, $_SESSION); + $_SESSION = $this->session; return (0 === count(array_diff($this->session, $_SESSION)))? true : false; } /** - * @param string $name - * @return mixed + * Check if a session key exist + * @param string $name The session key + * @return bool If exist or not */ - public function has($name) + public function has($name):bool { return ((isset($this->session[$name]) && null != $this->session[$name])? true : false); } - /** - * @param string $name + /** Get a session key value + * @param string $name The session key * @param mixed|null $default - * @return mixed + * @return mixed The result (if null : not result) */ public function get($name, $default = null) { @@ -128,9 +152,10 @@ public function get($name, $default = null) } /** - * @param string $name - * @param mixed $value - * @return bool + * Edit a session key + * @param string $name Key name + * @param mixed $value session item value + * @return bool If session exist or not * @throws Server500 */ public function set($name, $value) @@ -146,59 +171,110 @@ public function set($name, $value) } /** - * @return mixed + * Return all session items + * @return null|array Null if session are not initialized or an array with all session item */ - public function all() + public function all():?array { return ($this->session); } - /** - * @param array $attributes - * @return mixed + /** Replace session items + * @param array $attributes Session item with key/value + * @return bool false if not session item has not edited or true if it has been edited + * @throws Server500 */ - public function replace(array $attributes) + public function replace(array $attributes):bool { - // TODO: Implement replace() method. + $status = false; + foreach ($this->session as $one => $value) { + if (null !== $this->get($one)) { + $this->set($one, $value); + $status = true; + } + } + if (true === $status) { + $this->save(); + } + + return ($status); } - /** - * @param string $name + /** Remove a session item + * @param string $name Item name * @return bool * @throws Server500 + * @throws \Exception */ - public function remove($name) + public function remove($name):bool { - if (isset($_SESSION[$name])) { - unset($_SESSION[$name]); - $this->start(); + if (isset($this->session[$name])) { + unset($this->session[$name]); + $this->save(); return (true); } else { throw new Server500(new \ArrayObject(array("explain" => - "iumio Session Error : Your session name is not defined", "solution" => - "Please check the session object with HttpSession::all instruction"))); + "Session Error : The session name [$name] is not defined", "solution" => + "Please check the session object with HttpSession::all instruction to remove". + " the correct session item"))); } } /** - * @return mixed + * Clear the session + * @return bool If session is clear properly or not + * @throws Server500 */ - public function clear() + public function clear():bool { - if ($this->isStarted()) { - return (session_destroy()); + if (true === $this->isStarted()) { + session_unset(); + $this->setToDefault(); + session_regenerate_id(true); + return (empty($_SESSION)? true : false); } - return (false); + throw new Server500(new \ArrayObject(array("explain" => "Cannot clear the session when is not started", + "solution" => "Please start a session instance before clear it"))); } + /** + * Set all value to default + */ + private function setToDefault():void + { + $this->session = null; + $this->id = null; + $this->name = null; + } + + /** Init the session configuration + * @throws Server500 + * @throws \Exception + * @return void + */ + private function initSessionConfig():void + { + if (!defined("IUMIO_ENV")) { + throw new Server500(new \ArrayObject(array("explain" => "Framework Environment is not defined", + "solution" => "Please initialize the framework environment"))); + } + if (false === Server::exist(IUMIO_ROOT."/elements/cache/".strtolower(IUMIO_ENV)."/sessions")) { + Server::create(IUMIO_ROOT."/elements/cache/". + strtolower(IUMIO_ENV)."/sessions", "directory"); + } + + $this->id = session_id(); + $this->name = session_name(); + session_save_path(IUMIO_ROOT.'/elements/cache/'.strtolower(IUMIO_ENV).'/sessions'); + ini_set('session.gc_probability', 1); + } + + /** Check if session is started * @return bool */ public function isStarted() { - if ('cli' !== php_sapi_name()) { - return ((PHP_SESSION_ACTIVE === session_status()) ? true : false); - } - return (false); + return ((PHP_SESSION_ACTIVE === session_status()) ? true : false); } } diff --git a/Core/Base/Http/MetaBagRequest.php b/Core/Base/Http/MetaBagRequest.php index 302b239..415af2f 100755 --- a/Core/Base/Http/MetaBagRequest.php +++ b/Core/Base/Http/MetaBagRequest.php @@ -17,7 +17,6 @@ * Adds metadata to the session. * * @author Fabien Potencier - * @author Dany RAFINA * @category Framework * @licence MIT License * @link https://framework.iumio.com diff --git a/Core/Base/Http/SessionInterfaceRequest.php b/Core/Base/Http/SessionInterfaceRequest.php index df16678..3367702 100755 --- a/Core/Base/Http/SessionInterfaceRequest.php +++ b/Core/Base/Http/SessionInterfaceRequest.php @@ -59,35 +59,6 @@ public function getName(); */ public function setName($name); - /** - * Invalidates the current session. - * - * Clears all session attributes and flashes and regenerates the - * session and deletes the old session from persistence. - * - * @param int $lifetime Sets the cookie lifetime for the session cookie. A null value - * will leave the system settings unchanged, 0 sets the cookie - * to expire with browser session. Time is in seconds, and is - * not a Unix timestamp. - * - * @return bool True if session invalidated, false if error - */ - public function invalidate($lifetime = null); - - /** - * Migrates the current session to a new session id while maintaining all - * session attributes. - * - * @param bool $destroy Whether to delete the old session or leave it to garbage collection - * @param int $lifetime Sets the cookie lifetime for the session cookie. A null value - * will leave the system settings unchanged, 0 sets the cookie - * to expire with browser session. Time is in seconds, and is - * not a Unix timestamp. - * - * @return bool True if session migrated, false if error - */ - public function migrate($destroy = false, $lifetime = null); - /** * Force the session to be saved and closed. * @@ -158,5 +129,4 @@ public function clear(); * @return bool */ public function isStarted(); - } diff --git a/Core/Masters/MasterCore.php b/Core/Masters/MasterCore.php index c248bfa..f30d000 100755 --- a/Core/Masters/MasterCore.php +++ b/Core/Masters/MasterCore.php @@ -24,7 +24,7 @@ use iumioFramework\Core\Requirement\FrameworkServices\GlobalCoreService; use iumioFramework\Core\Base\Database\DatabaseAccess as IDA; use iumioFramework\Core\Exception\Server\Server500; -use iumioFramework\Core\Base\Http\Session\HttpSession; +use iumioFramework\Core\Base\Http\HttpSession; use iumioFramework\Core\Base\Json\JsonListener as JL; /** @@ -68,7 +68,7 @@ final protected function get(string $component) return (FrameworkCore::getRuntimeParameters())->query; break; case 'session': - return (new HttpSession()); + return (HttpSession::getInstance()); break; case 'service': return (Services::getInstance()); diff --git a/Core/Requirement/Environment/FrameworkEnvironment.php b/Core/Requirement/Environment/FrameworkEnvironment.php old mode 100755 new mode 100644 index e231195..631dad0 --- a/Core/Requirement/Environment/FrameworkEnvironment.php +++ b/Core/Requirement/Environment/FrameworkEnvironment.php @@ -47,6 +47,7 @@ public static function definer(string $env):int $base = realpath(__DIR__ . "/../../../../../../")."/"; define('IUMIO_ENV', $env); + define('IUMIO_ROOT', $base); $current = self::getProtocol()."://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); $current_temp = substr($current, 0, strpos($current, self::getFileEnv($env))); diff --git a/Core/Requirement/FrameworkCore.php b/Core/Requirement/FrameworkCore.php old mode 100755 new mode 100644 index b690ffd..4c71a79 --- 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.1'; + public const CORE_VERSION = '1.0.2'; public const CORE_NAME = 'SUN'; public const CORE_STAGE = 'STS'; - public const CORE_BUILD = 201801; + public const CORE_BUILD = 201802; protected static $edition = array(); /** diff --git a/Core/Requirement/FrameworkServices/FrameworkTools.php b/Core/Requirement/FrameworkServices/FrameworkTools.php old mode 100755 new mode 100644 index f521669..2f611f6 --- a/Core/Requirement/FrameworkServices/FrameworkTools.php +++ b/Core/Requirement/FrameworkServices/FrameworkTools.php @@ -115,7 +115,7 @@ final public static function detectFirstInstallation():int header('Location: '.FEnv::get("host.current").'/setup/setup.php'); exit(1); } else { - throw new \RuntimeException("(Setup components does not exist in web directory => Please download". + throw new \RuntimeException("(Setup components does not exist in web directory => Please download ". "the setup components on iumio Framework Website to fix this error and put him in web directory)"); } } diff --git a/README.md b/README.md index bf46d4f..354118f 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,11 @@ iumio Framework @ Let's create more simply +- CODACY : [![Codacy Badge](https://api.codacy.com/project/badge/Grade/b1b1beef4a244d25a2586c2cdf3a1cc5)](https://www.codacy.com/app/iumio-team/iumio-framework?utm_source=github.com&utm_medium=referral&utm_content=iumio/framework&utm_campaign=Badge_Grade) +- TRAVIS : [![Build Status](https://travis-ci.org/iumio/framework.svg?branch=master)](https://travis-ci.org/iumio/framework) + Description ------------ diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..4485409 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + tests + + + + + + . + + Core/Exception/Server/views + + + + \ No newline at end of file diff --git a/tests/HttpSessionTest.php b/tests/HttpSessionTest.php index f1e209f..ace31e6 100644 --- a/tests/HttpSessionTest.php +++ b/tests/HttpSessionTest.php @@ -24,17 +24,169 @@ */ class HttpSessionTest extends TestCase { + public function setUp() + { + define('IUMIO_ENV', 'dev'); + $base = realpath(__DIR__ . "/../")."/"; + define('IUMIO_ROOT', $base); + } + /** + * Test if session is started + * @throws \Exception + */ + public function testIsStarted() + { + $instance = HttpSession::getInstance(); + $this->assertTrue($instance->isStarted()); + } + /** - * Test create an instance of HttpSession + * Test create session item * @throws \Exception */ - public function testCreateInstance() + public function testCreate() { $instance = HttpSession::getInstance(); $instance->set("test", "value1"); $instance->save(); $this->assertInstanceOf("iumioFramework\Core\Base\Http\HttpSession", $instance); + $this->assertNotNull($instance->get("test")); + $this->assertEquals($instance->get("test"), "value1"); $this->assertTrue(isset($_SESSION["test"])); $this->assertEquals($_SESSION["test"], "value1"); } + + /** + * Test remove a session item + * @throws \Exception + */ + public function testRemove() + { + $instance = HttpSession::getInstance(); + $instance->set("test", "value1"); + $instance->save(); + $this->assertInstanceOf("iumioFramework\Core\Base\Http\HttpSession", $instance); + $this->assertNotNull($instance->get("test")); + $this->assertEquals($instance->get("test"), "value1"); + $instance->remove("test"); + $instance->save(); + $this->assertNull($instance->get("test")); + $this->assertFalse(isset($_SESSION["test"])); + } + + /** + * Test if a session item exist + * @throws \Exception + */ + public function testHas() + { + $instance = HttpSession::getInstance(); + $instance->set("test", "value1"); + $instance->save(); + $this->assertTrue($instance->has("test")); + $instance->remove("test"); + $instance->save(); + $this->assertFalse($instance->has("test")); + } + + /** + * Test get all session item + * @throws \Exception + */ + public function testAll() + { + $arr = ["test" => "val1", "test2" => "val2", "test3" => "val3"]; + $instance = HttpSession::getInstance(); + foreach ($arr as $one => $value) { + $instance->set($one, $value); + } + $instance->save(); + $all = $instance->all(); + + foreach ($arr as $one => $value) { + $this->assertTrue(isset($all[$one])); + $this->assertEquals($value, $all[$one]); + $instance->remove($one); + } + + $instance->save(); + $this->assertEmpty($instance->all()); + } + + /** + * Test set id for a session + * @throws \Exception + */ + public function testId() + { + $instance = HttpSession::getInstance(); + $instance->setId("123"); + $this->assertEquals("123", session_id()); + $this->assertEquals("123", $instance->getId()); + } + + /** + * Test destroy a session + * @throws \Exception + */ + public function testClear() + { + $instance = HttpSession::getInstance(); + $arr = ["test" => "val1", "test2" => "val2", "test3" => "val3"]; + foreach ($arr as $one => $value) { + $instance->set($one, $value); + } + $instance->save(); + $this->assertTrue($instance->clear()); + } + + + /** + * Test set name for session + * @throws \Exception + */ + /*public function testName() + { + $instance = HttpSession::getInstance(); + $instance->setName("test"); + $this->assertEquals("test", session_name()); + $this->assertEquals("test", $instance->getName()); + }*/ + + + /** + * Test replace a session item + * @throws \Exception + */ + public function testReplace() + { + $arr = ["test" => "val1", "test2" => "val2", "test3" => "val3"]; + $instance = HttpSession::getInstance(); + foreach ($arr as $one => $value) { + $instance->set($one, $value); + } + $instance->save(); + $all = $instance->all(); + + foreach ($arr as $one => $value) { + $this->assertTrue(isset($all[$one])); + $this->assertEquals($value, $all[$one]); + } + + $arr = ["test" => "aze", "test2" => "azer", "test3" => "azert"]; + foreach ($arr as $one => $value) { + $instance->set($one, $value); + } + $instance->save(); + $all = $instance->all(); + + foreach ($arr as $one => $value) { + $this->assertTrue(isset($all[$one])); + $this->assertEquals($value, $all[$one]); + $instance->remove($one); + } + + $instance->save(); + $this->assertEmpty($instance->all()); + } }