diff --git a/coverage/AbstractSession.php.html b/coverage/AbstractSession.php.html index e7b166a..72e51e4 100644 --- a/coverage/AbstractSession.php.html +++ b/coverage/AbstractSession.php.html @@ -57,7 +57,7 @@
100.00%
-
19 / 19
+
20 / 20
CRAP
@@ -66,7 +66,7 @@
100.00%
-
64 / 64
+
67 / 67
@@ -86,8 +86,8 @@
100.00%
-
23 / 23
- 37 +
24 / 24
+ 39
100.00% covered (success) @@ -95,11 +95,11 @@
100.00%
-
64 / 64
+
67 / 67
-  __construct +  __construct
100.00% covered (success) @@ -120,7 +120,7 @@ -  setID +  setID
100.00% covered (success) @@ -137,11 +137,11 @@
100.00%
-
6 / 6
+
7 / 7
-  idExists +  idExists
100.00% covered (success) @@ -162,7 +162,7 @@ -  destroySafely +  destroySafely
100.00% covered (success) @@ -179,11 +179,11 @@
100.00%
-
4 / 4
+
5 / 5
-  isActive +  getLastActiveSession
100.00% covered (success) @@ -204,7 +204,28 @@ -  getLastActiveSession +  jsonSerialize +
+
+ 100.00% covered (success) +
+
+ +
100.00%
+
1 / 1
+ 2 +
+
+ 100.00% covered (success) +
+
+ +
100.00%
+
1 / 1
+ + + +  isActive
100.00% covered (success) @@ -225,7 +246,7 @@ -  start +  start
100.00% covered (success) @@ -246,7 +267,7 @@ -  identityCheck +  identityCheck
100.00% covered (success) @@ -267,7 +288,7 @@ -  getFingerprint +  getFingerprint
100.00% covered (success) @@ -288,7 +309,7 @@ -  handleIdentityCheckFailure +  handleIdentityCheckFailure
100.00% covered (success) @@ -309,7 +330,7 @@ -  close +  close
100.00% covered (success) @@ -330,7 +351,7 @@ -  destroy +  destroy
100.00% covered (success) @@ -351,7 +372,7 @@ -  gc +  gc
100.00% covered (success) @@ -372,7 +393,7 @@ -  open +  open
100.00% covered (success) @@ -393,7 +414,7 @@ -  offsetExists +  offsetExists
100.00% covered (success) @@ -414,7 +435,7 @@ -  sessionRequiredWarning +  sessionRequiredWarning
100.00% covered (success) @@ -435,7 +456,7 @@ -  __get +  __get
100.00% covered (success) @@ -456,7 +477,7 @@ -  __set +  __set
100.00% covered (success) @@ -477,7 +498,7 @@ -  offsetGet +  offsetGet
100.00% covered (success) @@ -498,7 +519,7 @@ -  offsetSet +  offsetSet
100.00% covered (success) @@ -519,7 +540,7 @@ -  offsetUnset +  offsetUnset
100.00% covered (success) @@ -540,7 +561,7 @@ -  __destruct +  __destruct
100.00% covered (success) @@ -561,7 +582,7 @@ -  shouldBeSaved +  shouldBeSaved
100.00% covered (success) @@ -588,395 +609,408 @@ <?php -     namespace AloFramework\Session; + namespace AloFramework\Session; -     use AloFramework\Common\Alo; -     use AloFramework\Config\Configurable; -     use AloFramework\Config\ConfigurableTrait; -     use AloFramework\Log\Log; -     use ArrayAccess; -     use Psr\Log\LoggerInterface; -     use SessionHandlerInterface; - -     /** -      * Abstract session operations -      * @author Art <a.molcanovas@gmail.com> -      * @property Config $config -      */ -     abstract class AbstractSession implements SessionHandlerInterface, Configurable, ArrayAccess { - -         use ConfigurableTrait; + use AloFramework\Common\Alo; + use AloFramework\Config\Configurable; + use AloFramework\Config\ConfigurableTrait; + use AloFramework\Log\Log; + use ArrayAccess; + use JsonSerializable; + use Psr\Log\LoggerInterface; + use SessionHandlerInterface; + + /** +  * Abstract session operations +  * @author Art <a.molcanovas@gmail.com> +  * @property Config $config +  */ + abstract class AbstractSession +     implements SessionHandlerInterface, Configurable, ArrayAccess, JsonSerializable { -         /** -          * The last started session object. This is reset back to null after the session is destroyed or closed. -          * @var self -          */ -         private static $activeSession; -         /** -          * Logger instance -          * @var LoggerInterface -          */ -         protected $log; - -         /** -          * Constructor -          * @author Art <a.molcanovas@gmail.com> -          * -          * @param Config          $cfg    Your custom configuration -          * @param LoggerInterface $logger A logger object. If omitted, AloFramework\Log will be used. -          */ -         function __construct(Config $cfg = null, LoggerInterface $logger = null) { -             $this->config = Alo::ifnull($cfg, new Config()); -             $this->log    = Alo::ifnull($logger, new Log()); -             $this->setID(); -         } - -         /** -          * Sets the session ID variable & the cookie -          * -          * @author Art <a.molcanovas@gmail.com> -          * @return string The generated ID -          */ -         protected function setID() { -             $c = Alo::nullget($_COOKIE[$this->config->cookie]); -             //@codeCoverageIgnoreStart -             if ($c && strlen($c) == strlen(hash($this->config->sessionAlgo, 1))) { -                 $sid = $c; -             } else { -                 //@codeCoverageIgnoreEnd -                 do { -                     $sid = Alo::getUniqid($this->config->sessionAlgo, 'session' . Alo::getFingerprint('md5')); -                 } while ($this->idExists($sid)); -             } - -             session_id($sid); +     use ConfigurableTrait; + +     /** +      * The last started session object. This is reset back to null after the session is destroyed or closed. +      * @var self +      */ +     private static $activeSession; +     /** +      * Logger instance +      * @var LoggerInterface +      */ +     protected $log; + +     /** +      * Constructor +      * @author Art <a.molcanovas@gmail.com> +      * +      * @param Config $cfg Your custom configuration +      * @param LoggerInterface $logger A logger object. If omitted, AloFramework\Log will be used. +      */ +     function __construct(Config $cfg = null, LoggerInterface $logger = null) { +         $this->config = Alo::ifnull($cfg, new Config()); +         $this->log    = Alo::ifnull($logger, new Log()); +         $this->setID(); +     } + +     /** +      * Sets the session ID variable & the cookie +      * +      * @author Art <a.molcanovas@gmail.com> +      * @return string The generated ID +      */ +     protected function setID() { +         $c = Alo::nullget($_COOKIE[$this->config->cookie]); +         //@codeCoverageIgnoreStart +         if ($c && strlen($c) == strlen(hash($this->config->sessionAlgo, 1))) { +             $sid = $c; +         } else { +             //@codeCoverageIgnoreEnd +             do { +                 $sid = Alo::getUniqid($this->config->sessionAlgo, 'session' . Alo::getFingerprint('md5')); +             } while ($this->idExists($sid)); +         } -             $this->log->debug('Session ID set to ' . $sid); +         session_id($sid); -             return $sid; -         } - -         /** -          * Check if the given session ID exists -          * @author Art <a.molcanovas@gmail.com> -          * -          * @param string $sessionID The session ID -          * -          * @return bool -          */ -         protected abstract function idExists($sessionID); - -         /** -          * Only calls session_destroy() if a session is active -          * -          * @author Art <a.molcanovas@gmail.com> -          * @return bool -          */ -         static function destroySafely() { -             if (self::isActive()) { -                 session_destroy(); - -                 return true; -             } else { -                 return false; -             } -         } - -         /** -          * Checks whether a session is currently active -          * @author Art <a.molcanovas@gmail.com> -          * @return bool -          */ -         static function isActive() { -             return session_status() === PHP_SESSION_ACTIVE; -         } - -         /** -          * Returns the last started session object. This is reset back to null after the session is destroyed or closed. -          * @author Art <a.molcanovas@gmail.com> -          * @return self -          * @since  1.2 -          */ -         static function getLastActiveSession() { -             return self::$activeSession; -         } - -         /** -          * Starts the session -          * @author Art <a.molcanovas@gmail.com> -          * @return self -          */ -         function start() { -             $this->log->debug('Starting session with ' . __CLASS__); -             if (self::isActive()) { -                 //Can't test this via PHPUnit -                 //@codeCoverageIgnoreStart -                 session_write_close(); -                 //@codeCoverageIgnoreEnd -                 trigger_error('A session has already been started - it has now been destroyed to start the new one', -                               E_USER_WARNING); -                 //@codeCoverageIgnoreStart -             } -             //@codeCoverageIgnoreEnd - -             self::$activeSession = &$this; -             session_set_cookie_params($this->config->timeout, '/', null, $this->config->secure, true); -             session_name($this->config->cookie); - -             session_set_save_handler($this, false); -             session_start(); -             $this->identityCheck(); - -             return $this; -         } - -         /** -          * Checks if the session hasn't been hijacked -          * -          * @author Art <a.molcanovas@gmail.com> -          * @return boolean TRUE if the check has passed, FALSE if not and the session has been terminated. -          */ -         private function identityCheck() { -             $fingerprint = self::getFingerprint(); - -             if (!Alo::nullget($_SESSION[$this->config->fingerprint])) { -                 $_SESSION[$this->config->fingerprint] = $fingerprint; -             } elseif ($fingerprint !== $_SESSION[$this->config->fingerprint]) { -                 //@codeCoverageIgnoreStart -                 $this->handleIdentityCheckFailure(session_id()); - -                 return false; -                 //@codeCoverageIgnoreEnd -             } -             $this->log->debug('Identity check passed for session ID ' . session_id()); - -             return true; -         } - -         /** -          * Generates a session token -          * -          * @author Art <a.molcanovas@gmail.com> -          * @return string -          */ -         private static function getFingerprint() { -             return md5('AloSession' . Alo::getFingerprint('md5')); -         } +         $this->log->debug('Session ID set to ' . $sid); + +         return $sid; +     } + +     /** +      * Check if the given session ID exists +      * @author Art <a.molcanovas@gmail.com> +      * +      * @param string $sessionID The session ID +      * +      * @return bool +      */ +     protected abstract function idExists($sessionID); + +     /** +      * Only calls session_destroy() if a session is active +      * +      * @author Art <a.molcanovas@gmail.com> +      * @return bool +      */ +     static function destroySafely() { +         if (self::isActive()) { +             session_destroy(); + +             return true; +         } else { +             return false; +         } +     } + +     /** +      * Returns the last started session object. This is reset back to null after the session is destroyed or closed. +      * @author Art <a.molcanovas@gmail.com> +      * @return self +      * @since  1.2 +      */ +     static function getLastActiveSession() { +         return self::$activeSession; +     } + +     /** +      * Specify data which should be serialized to JSON +      * @author Art <a.molcanovas@gmail.com> +      * @link   http://php.net/manual/en/jsonserializable.jsonserialize.php +      * @return array +      * @since  1.2.1 +      */ +     function jsonSerialize() { +         return self::isActive() ? $_SESSION : []; +     } + +     /** +      * Checks whether a session is currently active +      * @author Art <a.molcanovas@gmail.com> +      * @return bool +      */ +     static function isActive() { +         return session_status() === PHP_SESSION_ACTIVE; +     } + +     /** +      * Starts the session +      * @author Art <a.molcanovas@gmail.com> +      * @return self +      */ +     function start() { +         $this->log->debug('Starting session with ' . __CLASS__); +         if (self::isActive()) { +             //Can't test this via PHPUnit +             //@codeCoverageIgnoreStart +             session_write_close(); +             //@codeCoverageIgnoreEnd +             trigger_error('A session has already been started - it has now been destroyed to start the new one', +                           E_USER_WARNING); +             //@codeCoverageIgnoreStart +         } +         //@codeCoverageIgnoreEnd + +         self::$activeSession = &$this; +         session_set_cookie_params($this->config->timeout, '/', null, $this->config->secure, true); +         session_name($this->config->cookie); + +         session_set_save_handler($this, false); +         session_start(); +         $this->identityCheck(); + +         return $this; +     } + +     /** +      * Checks if the session hasn't been hijacked +      * +      * @author Art <a.molcanovas@gmail.com> +      * @return boolean TRUE if the check has passed, FALSE if not and the session has been terminated. +      */ +     private function identityCheck() { +         $fingerprint = self::getFingerprint(); + +         if (!Alo::nullget($_SESSION[$this->config->fingerprint])) { +             $_SESSION[$this->config->fingerprint] = $fingerprint; +         } elseif ($fingerprint !== $_SESSION[$this->config->fingerprint]) { +             //@codeCoverageIgnoreStart +             $this->handleIdentityCheckFailure(session_id()); + +             return false; +             //@codeCoverageIgnoreEnd +         } +         $this->log->debug('Identity check passed for session ID ' . session_id()); -         /** -          * What to do when an identity check fails -          * @author Art <a.molcanovas@gmail.com> -          * -          * @param string $sessionID The session ID that failed -          */ -         protected function handleIdentityCheckFailure($sessionID) { -             $this->log->notice('Session identity check failed for session ID ' . $sessionID); -             session_destroy(); -         } - -         /** -          * Close the session -          * @author Art <a.molcanovas@gmail.com> -          * @link   http://php.net/manual/en/sessionhandlerinterface.close.php -          * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned -          *              internally to PHP for processing. -          * @codeCoverageIgnore -          */ -         function close() { -             self::$activeSession = null; - -             return true; -         } - -         /** -          * Destroy a session -          * @author Art <a.molcanovas@gmail.com> -          * @link   http://php.net/manual/en/sessionhandlerinterface.destroy.php -          * -          * @param string $sessionID The session ID being destroyed. -          * -          * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned -          * internally to PHP for processing. -          */ -         function destroy($sessionID) { -             $this->log->info('Destroyed session ' . $sessionID); -             self::$activeSession = null; - -             return setcookie($this->config->cookie, '', time() - 3, null, null, $this->config->secure, true); -         } - -         /** -          * Cleanup old sessions. -          * @author Art <a.molcanovas@gmail.com> -          * @link   http://php.net/manual/en/sessionhandlerinterface.gc.php -          * -          * @param int $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed. -          * -          * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned -          * internally to PHP for processing. -          * @codeCoverageIgnore -          */ -         function gc($maxlifetime) { -             return true; -         } - -         /** -          * Initialize session -          * @author Art <a.molcanovas@gmail.com> -          * @link   http://php.net/manual/en/sessionhandlerinterface.open.php -          * -          * @param string $savePath  The path where to store/retrieve the session. -          * @param string $sessionID The session id. -          * -          * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned -          *              internally to PHP for processing. -          * @codeCoverageIgnore -          */ -         function open($savePath, $sessionID) { -             return true; -         } - -         /** -          * Whether a offset exists -          * @author Art <a.molcanovas@gmail.com> -          * @link   http://php.net/manual/en/arrayaccess.offsetexists.php -          * -          * @param string $offset The key -          * -          * @return boolean -          */ -         function offsetExists($offset) { -             if (!self::isActive()) { -                 self::sessionRequiredWarning(__METHOD__); +         return true; +     } + +     /** +      * Generates a session token +      * +      * @author Art <a.molcanovas@gmail.com> +      * @return string +      */ +     private static function getFingerprint() { +         return md5('AloSession' . Alo::getFingerprint('md5')); +     } + +     /** +      * What to do when an identity check fails +      * @author Art <a.molcanovas@gmail.com> +      * +      * @param string $sessionID The session ID that failed +      */ +     protected function handleIdentityCheckFailure($sessionID) { +         $this->log->notice('Session identity check failed for session ID ' . $sessionID); +         session_destroy(); +     } + +     /** +      * Close the session +      * @author Art <a.molcanovas@gmail.com> +      * @link   http://php.net/manual/en/sessionhandlerinterface.close.php +      * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned +      *              internally to PHP for processing. +      * @codeCoverageIgnore +      */ +     function close() { +         self::$activeSession = null; + +         return true; +     } + +     /** +      * Destroy a session +      * @author Art <a.molcanovas@gmail.com> +      * @link   http://php.net/manual/en/sessionhandlerinterface.destroy.php +      * +      * @param string $sessionID The session ID being destroyed. +      * +      * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned +      * internally to PHP for processing. +      */ +     function destroy($sessionID) { +         $this->log->info('Destroyed session ' . $sessionID); +         self::$activeSession = null; + +         return setcookie($this->config->cookie, '', time() - 3, null, null, $this->config->secure, true); +     } + +     /** +      * Cleanup old sessions. +      * @author Art <a.molcanovas@gmail.com> +      * @link   http://php.net/manual/en/sessionhandlerinterface.gc.php +      * +      * @param int $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed. +      * +      * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned +      * internally to PHP for processing. +      * @codeCoverageIgnore +      */ +     function gc($maxlifetime) { +         return true; +     } + +     /** +      * Initialize session +      * @author Art <a.molcanovas@gmail.com> +      * @link   http://php.net/manual/en/sessionhandlerinterface.open.php +      * +      * @param string $savePath The path where to store/retrieve the session. +      * @param string $sessionID The session id. +      * +      * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned +      *              internally to PHP for processing. +      * @codeCoverageIgnore +      */ +     function open($savePath, $sessionID) { +         return true; +     } -                 //@codeCoverageIgnoreStart -                 return false; -                 //@codeCoverageIgnoreEnd -             } - -             return isset($_SESSION[$offset]); -         } - -         /** -          * Trigger an error when an operation requires an active session, but one isn't active -          * @author Art <a.molcanovas@gmail.com> -          * -          * @param string $method The method used -          */ -         private static function sessionRequiredWarning($method) { -             trigger_error($method . ' failed: the session must be started first', E_USER_WARNING); -             //@codeCoverageIgnoreStart -         } -         //@codeCoverageIgnoreEnd - -         /** -          * Magic getter -          * @author Art <a.molcanovas@gmail.com> -          * -          * @param string $key The key to get -          * -          * @return mixed -          * @uses   AbstractSession::offsetGet() -          */ -         function __get($key) { -             return $this->offsetGet($key); -         } +     /** +      * Whether a offset exists +      * @author Art <a.molcanovas@gmail.com> +      * @link   http://php.net/manual/en/arrayaccess.offsetexists.php +      * +      * @param string $offset The key +      * +      * @return boolean +      */ +     function offsetExists($offset) { +         if (!self::isActive()) { +             self::sessionRequiredWarning(__METHOD__); + +             //@codeCoverageIgnoreStart +             return false; +             //@codeCoverageIgnoreEnd +         } + +         return isset($_SESSION[$offset]); +     } + +     /** +      * Trigger an error when an operation requires an active session, but one isn't active +      * @author Art <a.molcanovas@gmail.com> +      * +      * @param string $method The method used +      */ +     private static function sessionRequiredWarning($method) { +         trigger_error($method . ' failed: the session must be started first', E_USER_WARNING); +         //@codeCoverageIgnoreStart +     } +     //@codeCoverageIgnoreEnd -         /** -          * Magic setter -          * @author Art <a.molcanovas@gmail.com> -          * -          * @param string $key   Key to set -          * @param mixed  $value Value to set -          * -          * @uses   AbstractSession::offsetSet() -          */ -         function __set($key, $value) { -             $this->offsetSet($key, $value); -         } +     /** +      * Magic getter +      * @author Art <a.molcanovas@gmail.com> +      * +      * @param string $key The key to get +      * +      * @return mixed +      * @uses   AbstractSession::offsetGet() +      */ +     function __get($key) { +         return $this->offsetGet($key); +     } -         /** -          * Offset to retrieve -          * @author Art <a.molcanovas@gmail.com> -          * @link   http://php.net/manual/en/arrayaccess.offsetget.php -          * -          * @param string $offset The key -          * -          * @return mixed -          */ -         function offsetGet($offset) { -             if (!self::isActive()) { -                 self::sessionRequiredWarning(__METHOD__); +     /** +      * Magic setter +      * @author Art <a.molcanovas@gmail.com> +      * +      * @param string $key Key to set +      * @param mixed $value Value to set +      * +      * @uses   AbstractSession::offsetSet() +      */ +     function __set($key, $value) { +         $this->offsetSet($key, $value); +     } -                 //@codeCoverageIgnoreStart -                 return null; -                 //@codeCoverageIgnoreEnd -             } - -             return Alo::get($_SESSION[$offset]); -         } - -         /** -          * Offset to set -          * @author Art <a.molcanovas@gmail.com> -          * @link   http://php.net/manual/en/arrayaccess.offsetset.php -          * -          * @param string $offset The key -          * @param mixed  $value  Value to set -          * -          * @return void -          */ -         function offsetSet($offset, $value) { -             if (!self::isActive()) { -                 self::sessionRequiredWarning(__METHOD__); -                 //@codeCoverageIgnoreStart -             } else { -                 //@codeCoverageIgnoreEnd -                 $_SESSION[$offset] = $value; -             } -         } - -         /** -          * Offset to unset -          * @author Art <a.molcanovas@gmail.com> -          * @link   http://php.net/manual/en/arrayaccess.offsetunset.php -          * -          * @param string $offset The key -          * -          * @return void -          */ -         function offsetUnset($offset) { -             if (!self::isActive()) { -                 self::sessionRequiredWarning(__METHOD__); -                 //@codeCoverageIgnoreStart -             } else { -                 //@codeCoverageIgnoreEnd -                 unset($_SESSION[$offset]); -             } -         } - -         /** -          * Saves session data -          * @author Art <a.molcanovas@gmail.com> -          */ -         function __destruct() { -             if (self::isActive()) { -                 session_write_close(); -             } -         } - -         /** -          * Checks if the session should be saved/written -          * @author Art <a.molcanovas@gmail.com> -          * @return bool -          * @since  1.1 -          */ -         protected function shouldBeSaved() { -             $isCli = Alo::isCliRequest(); - -             return !$isCli || ($isCli && $this->config->saveCLI); -         } -     } +     /** +      * Offset to retrieve +      * @author Art <a.molcanovas@gmail.com> +      * @link   http://php.net/manual/en/arrayaccess.offsetget.php +      * +      * @param string $offset The key +      * +      * @return mixed +      */ +     function offsetGet($offset) { +         if (!self::isActive()) { +             self::sessionRequiredWarning(__METHOD__); + +             //@codeCoverageIgnoreStart +             return null; +             //@codeCoverageIgnoreEnd +         } + +         return Alo::get($_SESSION[$offset]); +     } + +     /** +      * Offset to set +      * @author Art <a.molcanovas@gmail.com> +      * @link   http://php.net/manual/en/arrayaccess.offsetset.php +      * +      * @param string $offset The key +      * @param mixed $value Value to set +      * +      * @return void +      */ +     function offsetSet($offset, $value) { +         if (!self::isActive()) { +             self::sessionRequiredWarning(__METHOD__); +             //@codeCoverageIgnoreStart +         } else { +             //@codeCoverageIgnoreEnd +             $_SESSION[$offset] = $value; +         } +     } + +     /** +      * Offset to unset +      * @author Art <a.molcanovas@gmail.com> +      * @link   http://php.net/manual/en/arrayaccess.offsetunset.php +      * +      * @param string $offset The key +      * +      * @return void +      */ +     function offsetUnset($offset) { +         if (!self::isActive()) { +             self::sessionRequiredWarning(__METHOD__); +             //@codeCoverageIgnoreStart +         } else { +             //@codeCoverageIgnoreEnd +             unset($_SESSION[$offset]); +         } +     } + +     /** +      * Saves session data +      * @author Art <a.molcanovas@gmail.com> +      */ +     function __destruct() { +         if (self::isActive()) { +             session_write_close(); +         } +     } + +     /** +      * Checks if the session should be saved/written +      * @author Art <a.molcanovas@gmail.com> +      * @return bool +      * @since  1.1 +      */ +     protected function shouldBeSaved() { +         $isCli = Alo::isCliRequest(); + +         return !$isCli || ($isCli && $this->config->saveCLI); +     } + } @@ -989,7 +1023,7 @@

Legend

Dead Code

- Generated by PHP_CodeCoverage 3.0.2 using PHP 5.6.15 and PHPUnit 5.0.9 at Sat Nov 14 14:52:38 GMT 2015. + Generated by PHP_CodeCoverage 3.1.1 using PHP 5.6.18 and PHPUnit 5.2.3 at Mon Feb 8 19:54:38 GMT 2016.

diff --git a/coverage/Config.php.html b/coverage/Config.php.html index afb7312..44b67d5 100644 --- a/coverage/Config.php.html +++ b/coverage/Config.php.html @@ -167,7 +167,13 @@      * @property int    $gc               Garbage collection probability. If set to 100 (default) there is a 1/100      *                                    (i.e. 1% chance) that a garbage collection event will occur on session start.      * @property bool   $saveCLI          Whether to save sessions in CLI mode. Defaults to false. -      * @property string $tokenKey         The session key to identify flashdata. You must not set any session values + + + + +      * @property string $tokenKey         The session key to identify token data. You must not set any session values + +      *                                    using this key.      */     class Config extends AbstractConfig { @@ -230,7 +236,13 @@         const CFG_SAVE_CLI = 'saveCLI';         /** -          * Key to identify flash data + + + + +          * Key to identify token data + +          * @var string          * @since 1.2          */ @@ -249,16 +261,56 @@          * @param array $cfg Your custom config overrides          */         function __construct(array $cfg = []) { -             self::setDefaultConfig(); -             parent::__construct(self::$defaults, $cfg); -         } + + + + +             self::setDefaultConfig(); + + + + + +             parent::__construct(self::$defaults, $cfg); + + + + + +         } +         /**          * Sets the default configuration array          * @author Art <a.molcanovas@gmail.com>          */         private static function setDefaultConfig() { -             if (!self::$defaults) { + + + + +             if (!self::$defaults) { +                 self::$defaults = [self::CFG_TIMEOUT          => 300,                                    self::CFG_COOKIE_NAME      => 'AloSession',                                    self::CFG_FINGERPRINT_NAME => '_fp_', @@ -267,10 +319,40 @@                                    self::CFG_TABLE            => 'alo_session',                                    self::CFG_SECURE           => true,                                    self::CFG_GC               => 100, -                                    self::CFG_SAVE_CLI => false, -                                    self::CFG_TOKEN    => '_tk_']; + + + + +                                    self::CFG_SAVE_CLI         => false, + + + + + +                                    self::CFG_TOKEN            => '_tk_']; +             } -         } + + + + +         } +     } @@ -284,7 +366,10 @@

Legend

Dead Code

- Generated by PHP_CodeCoverage 3.0.2 using PHP 5.6.15 and PHPUnit 5.0.9 at Sat Nov 14 14:52:38 GMT 2015. + Generated by PHP_CodeCoverage + 3.1.1 using PHP 5.6.18 and PHPUnit + 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. +

diff --git a/coverage/MySQLNoEventSession.php.html b/coverage/MySQLNoEventSession.php.html index 96f2f62..534533d 100644 --- a/coverage/MySQLNoEventSession.php.html +++ b/coverage/MySQLNoEventSession.php.html @@ -199,7 +199,10 @@

Legend

Dead Code

- Generated by PHP_CodeCoverage 3.0.2 using PHP 5.6.15 and PHPUnit 5.0.9 at Sat Nov 14 14:52:38 GMT 2015. + Generated by PHP_CodeCoverage + 3.1.1 using PHP 5.6.18 and PHPUnit + 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. +

diff --git a/coverage/MySQLSession.php.html b/coverage/MySQLSession.php.html index 27d4ad2..87b0f64 100644 --- a/coverage/MySQLSession.php.html +++ b/coverage/MySQLSession.php.html @@ -66,7 +66,9 @@
100.00%
-
19 / 19
+ +
18 / 18
+ @@ -95,7 +97,9 @@
100.00%
-
19 / 19
+ +
18 / 18
+ @@ -141,7 +145,7 @@ -  idExists +  read
100.00% covered (success) @@ -158,11 +162,14 @@
100.00%
-
3 / 3
+ +
5 / 5
+ -  read +  write +
100.00% covered (success) @@ -171,7 +178,7 @@
100.00%
1 / 1
- 4 + 3
100.00% covered (success) @@ -179,11 +186,14 @@
100.00%
-
5 / 5
+ +
4 / 4
+ -  write +  idExists +
100.00% covered (success) @@ -192,7 +202,7 @@
100.00%
1 / 1
- 3 + 4
100.00% covered (success) @@ -200,7 +210,9 @@
100.00%
-
4 / 4
+ +
2 / 2
+ @@ -210,168 +222,1217 @@ <?php -     namespace AloFramework\Session; + + + + + namespace AloFramework\Session; + -     use AloFramework\Session\SessionException as SEx; -     use PDO; -     use PDOException; -     use Psr\Log\LoggerInterface; + + + + + use AloFramework\Session\SessionException as SEx; + + + + + + use PDO; + + + + + + use PDOException; + + + + + + use Psr\Log\LoggerInterface; + -     /** -      * MySQL-based session handler -      * @author Art <a.molcanovas@gmail.com> -      */ -     class MySQLSession extends AbstractSession { + + + + + /** + + + + + +  * MySQL-based session handler + + + + + +  * @author Art <a.molcanovas@gmail.com> + + + + + +  */ + + + + + + class MySQLSession extends AbstractSession { + + -         /** -          * The PDO instance -          * @var PDO -          */ -         protected $client; + + + + +     /** + + + + + +      * The PDO instance + + + + + +      * @var PDO + + + + + +      */ + + + + + +     protected $client; + -         /** -          * Constructor -          * @author Art <a.molcanovas@gmail.com> -          * -          * @param PDO             $pdo    PDO instance to use -          * @param Config          $cfg    Your custom configuration -          * @param LoggerInterface $logger A logger object. If omitted, AloFramework\Log will be used. -          */ -         function __construct(PDO $pdo, Config $cfg = null, LoggerInterface $logger = null) { -             $this->client = $pdo; + + + + +     /** + + + + + +      * Constructor + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * + + + + + +      * @param PDO $pdo PDO instance to use + + + + + + +      * @param Config $cfg Your custom configuration + + + + + + +      * @param LoggerInterface $logger A logger object. If omitted, AloFramework\Log will be used. + + + + + + +      */ + + + + + +     function __construct(PDO $pdo, Config $cfg = null, LoggerInterface $logger = null) { + + + + + +         $this->client = $pdo; + -             //Parent constructor must be called after $this->client is set -             parent::__construct($cfg, $logger); -         } + + + + +         //Parent constructor must be called after $this->client is set + + + + + + +         parent::__construct($cfg, $logger); + + + + + +     } + -         /** -          * Destroy a session -          * @author Art <a.molcanovas@gmail.com> -          * @link   http://php.net/manual/en/sessionhandlerinterface.destroy.php -          * -          * @param string $sessionID The session ID being destroyed. -          * -          * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned -          * internally to PHP for processing. -          */ -         function destroy($sessionID) { -             parent::destroy($sessionID); -             try { -                 $sql  = $this->client->prepare('DELETE FROM `' . $this->config->table . '` WHERE `id`=? LIMIT 1'); -                 $exec = $sql->execute([$sessionID]); + + + + +     /** + + + + + +      * Destroy a session + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * @link   http://php.net/manual/en/sessionhandlerinterface.destroy.php + + + + + + +      * + + + + + +      * @param string $sessionID The session ID being destroyed. + + + + + + +      * + + + + + +      * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned + + + + + + +      * internally to PHP for processing. + + + + + + +      */ + + + + + +     function destroy($sessionID) { + + + + + +         parent::destroy($sessionID); + + + + + +         try { + + + + + +             $sql  = $this->client->prepare('DELETE FROM `' . $this->config->table . '` WHERE `id`=? LIMIT 1'); + + + + + +             $exec = $sql->execute([$sessionID]); + -                 return $exec; -                 //@codeCoverageIgnoreStart -             } catch (PDOException $e) { -                 $this->log->error('Failed to remove session ' . $sessionID . ' from database: ' . $e->getMessage()); + + + + +             return $exec; + + + + + +             //@codeCoverageIgnoreStart + + + + + +         } catch (PDOException $e) { + + + + + +             $this->log->error('Failed to remove session ' . $sessionID . ' from database: ' . $e->getMessage()); + -                 return false; -             } -             //@codeCoverageIgnoreEnd -         } + + + + +             return false; + + + + + +         } + + + + + +         //@codeCoverageIgnoreEnd + + + + + +     } + -         /** -          * Check if the given session ID exists -          * @author Art <a.molcanovas@gmail.com> -          * -          * @param string $sessionID The session ID -          * -          * @return bool -          * @throws SEx On PDOException or general query failure -          */ -         protected function idExists($sessionID) { -             try { -                 $sql = -                     $this->client->prepare('SELECT COUNT(*) FROM `' . $this->config->table . '` WHERE `id`=? LIMIT 0'); + + + + +     /** + + + + + +      * Read session data + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * @link   http://php.net/manual/en/sessionhandlerinterface.read.php + + + + + + +      * + + + + + +      * @param string $sessionID The session id to read data for. + + + + + + +      * + + + + + +      * @return string Returns an encoded string of the read data. If nothing was read, it must return an empty + + + + + + +      *                string. Note this value is returned internally to PHP for processing. + + + + + + +      */ + + + + + +     function read($sessionID) { + + + + + +         try { + + + + + +             $sql = $this->client->prepare('SELECT `data` FROM `' . $this->config->table . '` WHERE `id`=? LIMIT 1'); + -                 //@codeCoverageIgnoreStart -                 if (!$sql->execute([$sessionID])) { -                     throw new SEx('Failed to check if the session ID ' . $sessionID . -                                   ' exists: $sql->execute() returned ' . 'false', Sex::E_SECURITY_ERROR); -                 } else { -                     //@codeCoverageIgnoreEnd -                     $exec = $sql->fetchAll(PDO::FETCH_NUM); - -                     return empty($exec) ? false : $exec[0] != 0; -                 } -                 //@codeCoverageIgnoreStart -             } catch (PDOException $e) { -                 throw new SEx('Failed to check if the session ID ' . $sessionID . ' exists: ' . $e->getMessage(), -                               SEx::E_PDO_FORWARD, -                               $e); -             } -             //@codeCoverageIgnoreEnd -         } - -         /** -          * Read session data -          * @author Art <a.molcanovas@gmail.com> -          * @link   http://php.net/manual/en/sessionhandlerinterface.read.php -          * -          * @param string $sessionID The session id to read data for. -          * -          * @return string Returns an encoded string of the read data. If nothing was read, it must return an empty -          *                string. Note this value is returned internally to PHP for processing. -          */ -         function read($sessionID) { -             try { -                 $sql = $this->client->prepare('SELECT `data` FROM `' . $this->config->table . '` WHERE `id`=? LIMIT 1'); - -                 if ($sql->execute([$sessionID])) { -                     $exec = $sql->fetchAll(PDO::FETCH_COLUMN, 0); - -                     //@codeCoverageIgnoreStart -                     if (!empty($exec)) { -                         return $exec[0]; -                     } -                     //@codeCoverageIgnoreEnd -                 } + + + + +             if ($sql->execute([$sessionID])) { + + + + + +                 $exec = $sql->fetchAll(PDO::FETCH_COLUMN, 0); + + + + + + + + + + + +                 //@codeCoverageIgnoreStart + + + + + +                 if (!empty($exec)) { + + + + + +                     return $exec[0]; + + + + + +                 } + + + + + +                 //@codeCoverageIgnoreEnd + + + + + +             } + + + + + +             //@codeCoverageIgnoreStart + + + + + +         } catch (PDOException $e) { + + + + + +             $this->log->error('Error while fetching session data for ' . $sessionID . ': ' . $e->getMessage()); + + + + + + + + + + + +             return serialize(''); + + + + + +         } + + + + + + + + + + + +         //@codeCoverageIgnoreEnd + + + + + + + + + + + +         return ''; + + + + + +     } + + + + + + + + + + + +     /** + + + + + +      * Write session data + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * @link   http://php.net/manual/en/sessionhandlerinterface.write.php + + + + + + +      * + + + + + +      * @param string $sessionID The session id. + + + + + + +      * @param string $sessionData The encoded session data. This data is the result of the PHP internally + + + + + + +      *                             encoding the $_SESSION superglobal to a serialized string and passing it as + + + + + + +      *                             this parameter. Please note sessions use an alternative serialization method. + + + + + + +      * + + + + + +      * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned + + + + + + +      *              internally to PHP for processing. + + + + + + +      */ + + + + + +     function write($sessionID, $sessionData) { + + + + + +         if ($this->shouldBeSaved()) { + + + + + +             try { + + + + + +                 $sql  = + + + + + +                     $this->client->prepare('REPLACE INTO `' . $this->config->table . '`(`id`,`data`) VALUES(?,?)'); + + + + + +                 $exec = $sql->execute([$sessionID, $sessionData]); + + + + + + + + + + + +                 return $exec; +                 //@codeCoverageIgnoreStart             } catch (PDOException $e) { -                 $this->log->error('Error while fetching session data for ' . $sessionID . ': ' . $e->getMessage()); + + + + +                 $this->log->error('Failed to write session data for ' . $sessionID . ': ' . $e->getMessage()); + -                 return ''; + + + + +                 return false; +             } - -             //@codeCoverageIgnoreEnd - -             return ''; -         } - -         /** -          * Write session data -          * @author Art <a.molcanovas@gmail.com> -          * @link   http://php.net/manual/en/sessionhandlerinterface.write.php -          * -          * @param string $sessionID    The session id. -          * @param string $sessionData  The encoded session data. This data is the result of the PHP internally -          *                             encoding the $_SESSION superglobal to a serialized string and passing it as -          *                             this parameter. Please note sessions use an alternative serialization method. -          * -          * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned -          *              internally to PHP for processing. -          */ -         function write($sessionID, $sessionData) { -             if ($this->shouldBeSaved()) { -                 try { -                     $sql  = -                         $this->client->prepare('REPLACE INTO `' . $this->config->table . '`(`id`,`data`) VALUES(?,?)'); -                     $exec = $sql->execute([$sessionID, $sessionData]); - -                     return $exec; -                     //@codeCoverageIgnoreStart -                 } catch (PDOException $e) { -                     $this->log->error('Failed to write session data for ' . $sessionID . ': ' . $e->getMessage()); - -                     return false; -                 } -             } - -             return false; -         } -         //@codeCoverageIgnoreEnd + + + + +         } + + + + + + + + + + + +         return true; + + + + + +     } + + + + + + + + + + + +     /** + + + + + +      * Check if the given session ID exists + + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * + + + + + +      * @param string $sessionID The session ID + + + + + + +      * + + + + + +      * @return bool + + + + + +      * @throws SEx On PDOException or general query failure + + + + + + +      */ + + + + + +     protected function idExists($sessionID) { + + + + + +         try { + + + + + +             $sql = + + + + + +                 $this->client->prepare('SELECT COUNT(*) FROM `' . $this->config->table . '` WHERE `id`=? LIMIT 0'); + + + + + + + + + + + +             //@codeCoverageIgnoreStart + + + + + +             if (!$sql->execute([$sessionID])) { + + + + + +                 throw new SEx('Failed to check if the session ID ' . $sessionID . + + + + + +                               ' exists: $sql->execute() returned ' . 'false', Sex::E_SECURITY_ERROR); + + + + + + +             } else { + + + + + +                 //@codeCoverageIgnoreEnd + + + + + +                 $exec = $sql->fetchAll(PDO::FETCH_NUM); + + + + + + + + + + + +                 return empty($exec) ? false : $exec[0] != 0; + + + + + +             } + + + + + +             //@codeCoverageIgnoreStart + + + + + +         } catch (PDOException $e) { + + + + + +             throw new SEx('Failed to check if the session ID ' . $sessionID . ' exists: ' . $e->getMessage(), + + + + + +                           SEx::E_PDO_FORWARD, + + + + + +                           $e); + + + + + +         } + + + + + +         //@codeCoverageIgnoreEnd + + + + + +     } + + + + + +     //@codeCoverageIgnoreEnd + + -     } + + + + + } + @@ -384,7 +1445,10 @@

Legend

Dead Code

- Generated by PHP_CodeCoverage 3.0.2 using PHP 5.6.15 and PHPUnit 5.0.9 at Sat Nov 14 14:52:38 GMT 2015. + Generated by PHP_CodeCoverage + 3.1.1 using PHP 5.6.18 and PHPUnit + 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. +

diff --git a/coverage/RedisSession.php.html b/coverage/RedisSession.php.html index e9d1072..42ded66 100644 --- a/coverage/RedisSession.php.html +++ b/coverage/RedisSession.php.html @@ -66,7 +66,9 @@
100.00%
-
12 / 12
+ +
13 / 13
+ @@ -95,7 +97,9 @@
100.00%
-
12 / 12
+ +
13 / 13
+ @@ -141,7 +145,7 @@ -  idExists +  read
100.00% covered (success) @@ -150,7 +154,7 @@
100.00%
1 / 1
- 1 + 2
100.00% covered (success) @@ -158,11 +162,14 @@
100.00%
-
1 / 1
+ +
2 / 2
+ -  read +  write +
100.00% covered (success) @@ -179,11 +186,14 @@
100.00%
-
2 / 2
+ +
4 / 4
+ -  write +  idExists +
100.00% covered (success) @@ -192,7 +202,7 @@
100.00%
1 / 1
- 2 + 1
100.00% covered (success) @@ -200,7 +210,9 @@
100.00%
-
3 / 3
+ +
1 / 1
+ @@ -210,117 +222,839 @@ <?php -     namespace AloFramework\Session; + + + + + namespace AloFramework\Session; + -     use AloFramework\Session\SessionException as SEx; -     use Psr\Log\LoggerInterface; -     use Redis; + + + + + use AloFramework\Session\SessionException as SEx; + + + + + + use Psr\Log\LoggerInterface; + + + + + + use Redis; + -     /** -      * Redis-based session handler -      * @author Art <a.molcanovas@gmail.com> -      */ -     class RedisSession extends AbstractSession { + + + + + /** + + + + + +  * Redis-based session handler + + + + + +  * @author Art <a.molcanovas@gmail.com> + + + + + +  */ + + + + + + class RedisSession extends AbstractSession { + + -         /** -          * The Redis client -          * @var Redis -          */ -         protected $client; + + + + +     /** + + + + + +      * The Redis client + + + + + +      * @var Redis + + + + + +      */ + + + + + +     protected $client; + -         /** -          * Constructor -          * @author Art <a.molcanovas@gmail.com> -          * -          * @param Redis           $redis  The Redis instance with an active connection. If omitted, a new one will be -          *                                created and an attempt to connect to localhost with default settings will -          *                                be made. -          * @param Config          $cfg    Your custom configuration -          * @param LoggerInterface $logger A logger object. If omitted, AloFramework\Log will be used. -          * -          * @throws SEx When $redis isn't supplied and we're unable to connect to localhost. -          */ -         function __construct(Redis $redis = null, Config $cfg = null, LoggerInterface $logger = null) { -             //@codeCoverageIgnoreStart -             if (!$redis) { -                 $redis = new Redis(); -                 if (!$redis->connect('127.0.0.1')) { -                     throw new SEx('Unable to connect to Redis @ 127.0.0.1'); -                 } -             } -             //@codeCoverageIgnoreEnd + + + + +     /** + + + + + +      * Constructor + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * + + + + + +      * @param Redis $redis The Redis instance with an active connection. If omitted, a new one will be + + + + + + +      *                                created and an attempt to connect to localhost with default settings will + + + + + + +      *                                be made. + + + + + + +      * @param Config $cfg Your custom configuration + + + + + + +      * @param LoggerInterface $logger A logger object. If omitted, AloFramework\Log will be used. + + + + + + +      * + + + + + +      * @throws SEx When $redis isn't supplied and we're unable to connect to localhost. + + + + + + +      */ + + + + + +     function __construct(Redis $redis = null, Config $cfg = null, LoggerInterface $logger = null) { + + + + + +         //@codeCoverageIgnoreStart + + + + + +         if (!$redis) { + + + + + +             $redis = new Redis(); + + + + + +             if (!$redis->connect('127.0.0.1')) { + + + + + +                 throw new SEx('Unable to connect to Redis @ 127.0.0.1'); + + + + + +             } + + + + + +         } + + + + + +         //@codeCoverageIgnoreEnd + -             $this->client = $redis; + + + + +         $this->client = $redis; + -             //Parent constructor must be called after $this->client is set -             parent::__construct($cfg, $logger); -         } + + + + +         //Parent constructor must be called after $this->client is set + + + + + + +         parent::__construct($cfg, $logger); + + + + + +     } + -         /** -          * Destroy a session -          * @author Art <a.molcanovas@gmail.com> -          * @link   http://php.net/manual/en/sessionhandlerinterface.destroy.php -          * -          * @param string $sessionID The session ID being destroyed. -          * -          * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned -          * internally to PHP for processing. -          */ -         function destroy($sessionID) { -             $parent = parent::destroy($sessionID); -             $this->client->delete($this->config->prefix . $sessionID); + + + + +     /** + + + + + +      * Destroy a session + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * @link   http://php.net/manual/en/sessionhandlerinterface.destroy.php + + + + + + +      * + + + + + +      * @param string $sessionID The session ID being destroyed. + + + + + + +      * + + + + + +      * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned + + + + + + +      * internally to PHP for processing. + + + + + + +      */ + + + + + +     function destroy($sessionID) { + + + + + +         $parent = parent::destroy($sessionID); + + + + + +         $this->client->delete($this->config->prefix . $sessionID); + -             return $parent; -         } + + + + +         return $parent; + + + + + +     } + -         /** -          * Check if the given session ID exists -          * @author Art <a.molcanovas@gmail.com> -          * -          * @param string $sessionID The session ID -          * -          * @return bool -          */ -         protected function idExists($sessionID) { -             return $this->client->exists($sessionID); -         } - -         /** -          * Read session data -          * @author Art <a.molcanovas@gmail.com> -          * @link   http://php.net/manual/en/sessionhandlerinterface.read.php -          * -          * @param string $sessionID The session id to read data for. -          * -          * @return string Returns an encoded string of the read data. If nothing was read, it must return an empty -          *                string. Note this value is returned internally to PHP for processing. -          */ -         function read($sessionID) { -             $get = $this->client->get($this->config->prefix . $sessionID); - -             return is_string($get) ? $get : ''; -         } - -         /** -          * Write session data -          * @author Art <a.molcanovas@gmail.com> -          * @link   http://php.net/manual/en/sessionhandlerinterface.write.php -          * -          * @param string $sessionID    The session id. -          * @param string $sessionData  The encoded session data. This data is the result of the PHP internally -          *                             encoding the $_SESSION superglobal to a serialized string and passing it as -          *                             this parameter. Please note sessions use an alternative serialization method. -          * -          * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned -          *              internally to PHP for processing. -          */ -         function write($sessionID, $sessionData) { -             return $this->shouldBeSaved() && $this->client->setex($this->config->prefix . $sessionID, -                                                                   $this->config->timeout, -                                                                   $sessionData); -         } - -     } + + + + +     /** + + + + + +      * Read session data + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * @link   http://php.net/manual/en/sessionhandlerinterface.read.php + + + + + + +      * + + + + + +      * @param string $sessionID The session id to read data for. + + + + + + +      * + + + + + +      * @return string Returns an encoded string of the read data. If nothing was read, it must return an empty + + + + + + +      *                string. Note this value is returned internally to PHP for processing. + + + + + + +      */ + + + + + +     function read($sessionID) { + + + + + +         $get = $this->client->get($this->config->prefix . $sessionID); + + + + + + + + + + + +         return is_string($get) ? $get : ''; + + + + + +     } + + + + + + + + + + + +     /** + + + + + +      * Write session data + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * @link   http://php.net/manual/en/sessionhandlerinterface.write.php + + + + + + +      * + + + + + +      * @param string $sessionID The session id. + + + + + + +      * @param string $sessionData The encoded session data. This data is the result of the PHP internally + + + + + + +      *                             encoding the $_SESSION superglobal to a serialized string and passing it as + + + + + + +      *                             this parameter. Please note sessions use an alternative serialization method. + + + + + + +      * + + + + + +      * @return bool The return value (usually TRUE on success, FALSE on failure). Note this value is returned + + + + + + +      *              internally to PHP for processing. + + + + + + +      */ + + + + + +     function write($sessionID, $sessionData) { + + + + + +         if ($this->shouldBeSaved()) { + + + + + +             return $this->client->setex($this->config->prefix . $sessionID, + + + + + +                                         $this->config->timeout, + + + + + +                                         $sessionData); + + + + + +         } + + + + + + + + + + + +         // @codeCoverageIgnoreStart + + + + + +         return true; + + + + + +         // @codeCoverageIgnoreEnd + + + + + +     } + + + + + + + + + + + +     /** + + + + + +      * Check if the given session ID exists + + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * + + + + + +      * @param string $sessionID The session ID + + + + + + +      * + + + + + +      * @return bool + + + + + +      */ + + + + + +     protected function idExists($sessionID) { + + + + + +         return $this->client->exists($sessionID); + + + + + +     } + + + + + + + + + + + + } + @@ -333,7 +1067,10 @@

Legend

Dead Code

- Generated by PHP_CodeCoverage 3.0.2 using PHP 5.6.15 and PHPUnit 5.0.9 at Sat Nov 14 14:52:38 GMT 2015. + Generated by PHP_CodeCoverage + 3.1.1 using PHP 5.6.18 and PHPUnit + 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. +

diff --git a/coverage/SessionException.php.html b/coverage/SessionException.php.html index f040b8c..7d293cb 100644 --- a/coverage/SessionException.php.html +++ b/coverage/SessionException.php.html @@ -143,7 +143,10 @@

Legend

Dead Code

- Generated by PHP_CodeCoverage 3.0.2 using PHP 5.6.15 and PHPUnit 5.0.9 at Sat Nov 14 14:52:38 GMT 2015. + Generated by PHP_CodeCoverage + 3.1.1 using PHP 5.6.18 and PHPUnit + 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. +

diff --git a/coverage/Token.php.html b/coverage/Token.php.html index 4d88751..2e85215 100644 --- a/coverage/Token.php.html +++ b/coverage/Token.php.html @@ -57,7 +57,9 @@
100.00%
-
9 / 9
+ +
10 / 10
+ CRAP
@@ -66,7 +68,9 @@
100.00%
-
45 / 45
+ +
47 / 47
+ @@ -86,8 +90,10 @@
100.00%
-
9 / 9
- 23 + +
10 / 10
+ + 24
100.00% covered (success) @@ -95,11 +101,14 @@
100.00%
-
45 / 45
+ +
47 / 47
+ -  __construct +  __construct
100.00% covered (success) @@ -120,7 +129,7 @@ -  get +  jsonSerialize
100.00% covered (success) @@ -129,6 +138,39 @@
100.00%
1 / 1
+ 1 + +
+
+ 100.00% covered (success) +
+
+ + +
100.00%
+ + +
2 / 2
+ + + + +  get + +
+
+ 100.00% covered (success) +
+
+ + +
100.00%
+ + +
1 / 1
+ 2
@@ -141,7 +183,8 @@ -  sessionRequiredWarning +  sessionRequiredWarning +
100.00% covered (success) @@ -162,7 +205,8 @@ -  create +  create +
100.00% covered (success) @@ -183,7 +227,8 @@ -  removeAllTokens +  removeAllTokens +
100.00% covered (success) @@ -204,7 +249,7 @@ -  getAndRemove +  getAndRemove
100.00% covered (success) @@ -225,7 +270,7 @@ -  remove +  remove
100.00% covered (success) @@ -246,7 +291,7 @@ -  getName +  getName
100.00% covered (success) @@ -267,7 +312,7 @@ -  getTokenKey +  getTokenKey
100.00% covered (success) @@ -294,196 +339,1569 @@ <?php -     namespace AloFramework\Session; + + + + + namespace AloFramework\Session; + -     use AloFramework\Common\Alo; -     use AloFramework\Session\AbstractSession as Sess; -     use InvalidArgumentException; - -     /** -      * CSRF token management -      * @author Art <a.molcanovas@gmail.com> -      * @since  1.2 -      */ -     class Token { - -         /** -          * Token key in the session array -          * @var string -          */ -         private $tokenKey; - -         /** -          * Token name -          * @var string -          */ -         private $name; - -         /** -          * Constructor -          * @author Art <a.molcanovas@gmail.com> -          * -          * @param string      $name Token name/ID -          * @param Sess|Config $cfg  The instance of the currently active session. If omitted, -          *                          AbstractSession::getLastActiveSession() will be used -          * -          * @throws InvalidArgumentException if $cfg isn't an instance of Config or AbstractSession -          */ -         function __construct($name, $cfg = null) { -             if ($cfg instanceof Config) { -                 $this->tokenKey = $cfg->tokenKey; -             } elseif ($cfg instanceof Sess) { -                 $this->tokenKey = $cfg->getConfig(Config::CFG_TOKEN); -             } elseif ($cfg === null && $lastSession = Sess::getLastActiveSession()) { -                 $this->tokenKey = $lastSession->getConfig(Config::CFG_TOKEN); -             } else { -                 throw new InvalidArgumentException('$cfg must be an instance of ' . __NAMESPACE__ . '\\Config or ' . -                                                    __NAMESPACE__ . '\\AbstractSession'); -             } - -             $this->name = $name; -         } - -         /** -          * Returns a previously generated token -          * @author Art <a.molcanovas@gmail.com> -          * -          * @return string|null -          */ -         function get() { -             if (!Sess::isActive()) { -                 self::sessionRequiredWarning(__METHOD__); - -                 // @codeCoverageIgnoreStart -                 return null; -                 // @codeCoverageIgnoreEnd -             } - -             return Alo::nullget($_SESSION[$this->tokenKey][$this->name]); -         } - -         /** -          * Trigger an error when an operation requires an active session, but one isn't active -          * @author Art <a.molcanovas@gmail.com> -          * -          * @param string $method The method used -          */ -         private static function sessionRequiredWarning($method) { -             trigger_error($method . ' failed: the session must be started first', E_USER_WARNING); -             //@codeCoverageIgnoreStart -         } -         //@codeCoverageIgnoreEnd + + + + + use AloFramework\Common\Alo; + + + + + + use AloFramework\Session\AbstractSession as Sess; + + + + + + use InvalidArgumentException; + + + + + + use JsonSerializable; + + + + + + + + + + + + /** + + + + + +  * CSRF token management + + + + + +  * @author Art <a.molcanovas@gmail.com> + + + + + +  * @since  1.2.1 is json serializable<br/> + + + + + + +  *         1.2 + + + + + +  */ + + + + + + class Token implements JsonSerializable { + + + + + + + + + + + +     /** + + + + + +      * Token key in the session array + + + + + + +      * @var string + + + + + +      */ + + + + + +     private $tokenKey; + + + + + + + + + + + +     /** + + + + + +      * Token name + + + + + +      * @var string + + + + + +      */ + + + + + +     private $name; + + + + + + + + + + + +     /** + + + + + +      * Constructor + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * + + + + + +      * @param string $name Token name/ID + + + + + + +      * @param Sess|Config $cfg The instance of the currently active session. If omitted, + + + + + + +      *                          AbstractSession::getLastActiveSession() will be used + + + + + + +      * + + + + + +      * @throws InvalidArgumentException if $cfg isn't an instance of Config or AbstractSession + + + + + + +      */ + + + + + +     function __construct($name, $cfg = null) { + + + + + +         if ($cfg instanceof Config) { + + + + + +             $this->tokenKey = $cfg->tokenKey; + + + + + +         } elseif ($cfg instanceof Sess) { + + + + + +             $this->tokenKey = $cfg->getConfig(Config::CFG_TOKEN); + + + + + +         } elseif ($cfg === null && $lastSession = Sess::getLastActiveSession()) { + + + + + +             $this->tokenKey = $lastSession->getConfig(Config::CFG_TOKEN); + + + + + +         } else { + + + + + +             throw new InvalidArgumentException('$cfg must be an instance of ' . __NAMESPACE__ . '\\Config or ' . + + + + + +                                                __NAMESPACE__ . '\\AbstractSession'); + + + + + + +         } + + + + + + + + + + + +         $this->name = $name; + + + + + +     } + + + + + + + + + + + +     /** + + + + + +      * Returns a json-serializable version of this object + + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * @return array + + + + + +      */ + + + + + +     function jsonSerialize() { + + + + + +         return ['name' => $this->name, + + + + + +                 'key' => $this->tokenKey]; + + + + + +     } + + + + + + + + + + + +     /** + + + + + +      * Returns a previously generated token + + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * + + + + + +      * @return string|null + + + + + + +      */ + + + + + +     function get() { + + + + + +         if (!Sess::isActive()) { + + + + + +             self::sessionRequiredWarning(__METHOD__); + + + + + + + + + + + +             // @codeCoverageIgnoreStart + + + + + +             return null; + + + + + +             // @codeCoverageIgnoreEnd + + + + + +         } + + + + + + + + + + + +         return Alo::nullget($_SESSION[$this->tokenKey][$this->name]); + + + + + +     } + -         /** -          * Creates and returns a token -          * @author Art <a.molcanovas@gmail.com> -          * -          * @param string $hash Hash algorithm to use for the token -          * -          * @return string|null -          */ -         function create($hash = 'sha256') { -             if (!Sess::isActive()) { -                 self::sessionRequiredWarning(__METHOD__); + + + + +     /** + + + + + +      * Trigger an error when an operation requires an active session, but one isn't active + + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * + + + + + +      * @param string $method The method used + + + + + + +      */ + + + + + +     private static function sessionRequiredWarning($method) { + + + + + +         trigger_error($method . ' failed: the session must be started first', E_USER_WARNING); + + + + + +         //@codeCoverageIgnoreStart + + + + + +     } + + + + + +     //@codeCoverageIgnoreEnd + + -                 // @codeCoverageIgnoreStart -                 return null; -                 // @codeCoverageIgnoreEnd -             } - -             $entry = &$_SESSION[$this->tokenKey]; - -             if (!Alo::get($entry) || !is_array($entry)) { -                 $entry = []; -             } - -             $entry[$this->name] = Alo::getUniqid($hash, __METHOD__); - -             return $entry[$this->name]; -         } - -         /** -          * Removes all the stored tokens -          * @author Art <a.molcanovas@gmail.com> -          * @return bool -          */ -         function removeAllTokens() { -             if (!Sess::isActive()) { -                 self::sessionRequiredWarning(__METHOD__); -                 //@codeCoverageIgnoreStart -             } -             //@codeCoverageIgnoreEnd + + + + +     /** + + + + + +      * Creates and returns a token + + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * + + + + + +      * @param string $hash Hash algorithm to use for the token + + + + + + +      * + + + + + +      * @return string|null + + + + + + +      */ + + + + + +     function create($hash = 'sha256') { + + + + + +         if (!Sess::isActive()) { + + + + + +             self::sessionRequiredWarning(__METHOD__); + + + + + + + + + + + +             // @codeCoverageIgnoreStart + + + + + +             return null; + + + + + +             // @codeCoverageIgnoreEnd + + + + + +         } + + + + + + + + + + + +         $entry = &$_SESSION[$this->tokenKey]; + + + + + + + + + + + +         if (!Alo::get($entry) || !is_array($entry)) { + + + + + +             $entry = []; + + + + + +         } + + + + + + + + + + + +         $entry[$this->name] = Alo::getUniqid($hash, __METHOD__); + + + + + + + + + + + +         return $entry[$this->name]; + + + + + +     } + -             if (isset($_SESSION[$this->tokenKey])) { -                 unset($_SESSION[$this->tokenKey]); - -                 return true; -             } - -             return false; -         } - -         /** -          * Gets a token and removes it from the session -          * @author Art <a.molcanovas@gmail.com> -          * -          * @return mixed|null The token -          */ -         function getAndRemove() { -             if (!Sess::isActive()) { -                 self::sessionRequiredWarning(__METHOD__); - -                 // @codeCoverageIgnoreStart -                 return null; -                 // @codeCoverageIgnoreEnd -             } - -             $tok = Alo::nullget($_SESSION[$this->tokenKey][$this->name]); -             $this->remove(); - -             return $tok; -         } - -         /** -          * Removes a token -          * @author Art <a.molcanovas@gmail.com> -          * -          * @return bool -          */ -         function remove() { -             if (!Sess::isActive()) { -                 self::sessionRequiredWarning(__METHOD__); -             } elseif (isset($_SESSION[$this->tokenKey][$this->name])) { -                 unset($_SESSION[$this->tokenKey][$this->name]); + + + + +     /** + + + + + +      * Removes all the stored tokens + + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * @return bool + + + + + +      */ + + + + + +     function removeAllTokens() { + + + + + +         if (!Sess::isActive()) { + + + + + +             self::sessionRequiredWarning(__METHOD__); + + + + + +             //@codeCoverageIgnoreStart + + + + + +         } + + + + + +         //@codeCoverageIgnoreEnd + + + + + + + + + + + +         if (isset($_SESSION[$this->tokenKey])) { + + + + + +             unset($_SESSION[$this->tokenKey]); + + + + + + + + + + + +             return true; + + + + + +         } + + + + + + + + + + + +         return false; + + + + + +     } + + + + + + + + + + + +     /** + + + + + +      * Gets a token and removes it from the session + + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * + + + + + +      * @return mixed|null The token + + + + + + +      */ + + + + + +     function getAndRemove() { + + + + + +         if (!Sess::isActive()) { + + + + + +             self::sessionRequiredWarning(__METHOD__); + + + + + + + + + + + +             // @codeCoverageIgnoreStart + + + + + +             return null; + + + + + +             // @codeCoverageIgnoreEnd + + + + + +         } + + + + + + + + + + + +         $tok = Alo::nullget($_SESSION[$this->tokenKey][$this->name]); + + + + + +         $this->remove(); + + + + + + + + + + + +         return $tok; + + + + + +     } + -                 if (empty($_SESSION[$this->tokenKey])) { -                     unset($_SESSION[$this->tokenKey]); -                 } - -                 return true; -             } - -             return false; -         } - -         /** -          * Returns the token name -          * @author Art <a.molcanovas@gmail.com> -          * @return string -          */ -         protected function getName() { -             return $this->name; + + + + +     /** + + + + + +      * Removes a token + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * + + + + + +      * @return bool + + + + + +      */ + + + + + +     function remove() { + + + + + +         if (!Sess::isActive()) { + + + + + +             self::sessionRequiredWarning(__METHOD__); + + + + + +         } elseif (isset($_SESSION[$this->tokenKey][$this->name])) { + + + + + +             unset($_SESSION[$this->tokenKey][$this->name]); + + + + + + + + + + + +             if (empty($_SESSION[$this->tokenKey])) { + + + + + +                 unset($_SESSION[$this->tokenKey]); + + + + + +             } + + + + + + + + + + + +             return true; +         } -         /** -          * Returns the token key -          * @author Art <a.molcanovas@gmail.com> -          * @return string -          */ -         protected function getTokenKey() { -             return $this->tokenKey; -         } -     } + + + + +         return false; + + + + + +     } + + + + + + + + + + + +     /** + + + + + +      * Returns the token name + + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * @return string + + + + + +      */ + + + + + +     protected function getName() { + + + + + +         return $this->name; + + + + + +     } + + + + + + + + + + + +     /** + + + + + +      * Returns the token key + + + + + + +      * @author Art <a.molcanovas@gmail.com> + + + + + + +      * @return string + + + + + +      */ + + + + + +     protected function getTokenKey() { + + + + + +         return $this->tokenKey; + + + + + +     } + + + + + + } + @@ -496,7 +1914,10 @@

Legend

Dead Code

- Generated by PHP_CodeCoverage 3.0.2 using PHP 5.6.15 and PHPUnit 5.0.9 at Sat Nov 14 14:52:38 GMT 2015. + Generated by PHP_CodeCoverage + 3.1.1 using PHP 5.6.18 and PHPUnit + 5.2.3 at Mon Feb 8 19:54:38 GMT 2016. +

diff --git a/coverage/dashboard.html b/coverage/dashboard.html index e4ecd84..8509101 100644 --- a/coverage/dashboard.html +++ b/coverage/dashboard.html @@ -136,7 +136,10 @@

Project Risks

@@ -175,7 +178,7 @@

Project Risks

.yAxis.tickFormat(d3.format('d')); d3.select('#methodCoverageDistribution svg') - .datum(getCoverageDistributionData([0,0,0,0,0,0,0,0,0,0,0,46], "Method Coverage")) + .datum(getCoverageDistributionData([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48], "Method Coverage")) .transition().duration(500).call(chart); nv.utils.windowResize(chart.update); @@ -225,7 +228,27 @@

Project Risks

chart.yAxis.axisLabel('Cyclomatic Complexity'); d3.select('#classComplexity svg') - .datum(getComplexityData([[100,37,"AbstractSession<\/a>"],[100,3,"Config<\/a>"],[100,2,"MySQLNoEventSession<\/a>"],[100,14,"MySQLSession<\/a>"],[100,9,"RedisSession<\/a>"],[100,0,"SessionException<\/a>"],[100,23,"Token<\/a>"]], 'Class Complexity')) + .datum(getComplexityData([[100, + 39, + "AbstractSession<\/a>"], + [100, + 3, + "Config<\/a>"], + [100, + 2, + "MySQLNoEventSession<\/a>"], + [100, + 14, + "MySQLSession<\/a>"], + [100, + 9, + "RedisSession<\/a>"], + [100, + 0, + "SessionException<\/a>"], + [100, + 24, + "Token<\/a>"]], 'Class Complexity')) .transition() .duration(500) .call(chart); @@ -249,7 +272,150 @@

Project Risks

chart.yAxis.axisLabel('Method Complexity'); d3.select('#methodComplexity svg') - .datum(getComplexityData([[100,1,"
AbstractSession::__construct<\/a>"],[100,4,"AbstractSession::setID<\/a>"],[100,1,"AbstractSession::idExists<\/a>"],[100,2,"AbstractSession::destroySafely<\/a>"],[100,1,"AbstractSession::isActive<\/a>"],[100,1,"AbstractSession::getLastActiveSession<\/a>"],[100,2,"AbstractSession::start<\/a>"],[100,3,"AbstractSession::identityCheck<\/a>"],[100,1,"AbstractSession::getFingerprint<\/a>"],[100,1,"AbstractSession::handleIdentityCheckFailure<\/a>"],[100,1,"AbstractSession::close<\/a>"],[100,1,"AbstractSession::destroy<\/a>"],[100,1,"AbstractSession::gc<\/a>"],[100,1,"AbstractSession::open<\/a>"],[100,2,"AbstractSession::offsetExists<\/a>"],[100,1,"AbstractSession::sessionRequiredWarning<\/a>"],[100,1,"AbstractSession::__get<\/a>"],[100,1,"AbstractSession::__set<\/a>"],[100,2,"AbstractSession::offsetGet<\/a>"],[100,2,"AbstractSession::offsetSet<\/a>"],[100,2,"AbstractSession::offsetUnset<\/a>"],[100,2,"AbstractSession::__destruct<\/a>"],[100,3,"AbstractSession::shouldBeSaved<\/a>"],[100,1,"Config::__construct<\/a>"],[100,2,"Config::setDefaultConfig<\/a>"],[100,1,"MySQLNoEventSession::gc<\/a>"],[100,1,"MySQLNoEventSession::start<\/a>"],[100,1,"MySQLSession::__construct<\/a>"],[100,2,"MySQLSession::destroy<\/a>"],[100,4,"MySQLSession::idExists<\/a>"],[100,4,"MySQLSession::read<\/a>"],[100,3,"MySQLSession::write<\/a>"],[100,3,"RedisSession::__construct<\/a>"],[100,1,"RedisSession::destroy<\/a>"],[100,1,"RedisSession::idExists<\/a>"],[100,2,"RedisSession::read<\/a>"],[100,2,"RedisSession::write<\/a>"],[100,5,"Token::__construct<\/a>"],[100,2,"Token::get<\/a>"],[100,1,"Token::sessionRequiredWarning<\/a>"],[100,4,"Token::create<\/a>"],[100,3,"Token::removeAllTokens<\/a>"],[100,2,"Token::getAndRemove<\/a>"],[100,4,"Token::remove<\/a>"],[100,1,"Token::getName<\/a>"],[100,1,"Token::getTokenKey<\/a>"]], 'Method Complexity')) + .datum(getComplexityData([[100, + 1, + "AbstractSession::__construct<\/a>"], + [100, + 4, + "AbstractSession::setID<\/a>"], + [100, + 1, + "AbstractSession::idExists<\/a>"], + [100, + 2, + "AbstractSession::destroySafely<\/a>"], + [100, + 1, + "AbstractSession::getLastActiveSession<\/a>"], + [100, + 2, + "AbstractSession::jsonSerialize<\/a>"], + [100, + 1, + "AbstractSession::isActive<\/a>"], + [100, + 2, + "AbstractSession::start<\/a>"], + [100, + 3, + "AbstractSession::identityCheck<\/a>"], + [100, + 1, + "AbstractSession::getFingerprint<\/a>"], + [100, + 1, + "AbstractSession::handleIdentityCheckFailure<\/a>"], + [100, + 1, + "AbstractSession::close<\/a>"], + [100, + 1, + "AbstractSession::destroy<\/a>"], + [100, + 1, + "AbstractSession::gc<\/a>"], + [100, + 1, + "AbstractSession::open<\/a>"], + [100, + 2, + "AbstractSession::offsetExists<\/a>"], + [100, + 1, + "AbstractSession::sessionRequiredWarning<\/a>"], + [100, + 1, + "AbstractSession::__get<\/a>"], + [100, + 1, + "AbstractSession::__set<\/a>"], + [100, + 2, + "AbstractSession::offsetGet<\/a>"], + [100, + 2, + "AbstractSession::offsetSet<\/a>"], + [100, + 2, + "AbstractSession::offsetUnset<\/a>"], + [100, + 2, + "AbstractSession::__destruct<\/a>"], + [100, + 3, + "AbstractSession::shouldBeSaved<\/a>"], + [100, + 1, + "Config::__construct<\/a>"], + [100, + 2, + "Config::setDefaultConfig<\/a>"], + [100, + 1, + "MySQLNoEventSession::gc<\/a>"], + [100, + 1, + "MySQLNoEventSession::start<\/a>"], + [100, + 1, + "MySQLSession::__construct<\/a>"], + [100, + 2, + "MySQLSession::destroy<\/a>"], + [100, + 4, + "MySQLSession::read<\/a>"], + [100, + 3, + "MySQLSession::write<\/a>"], + [100, + 4, + "MySQLSession::idExists<\/a>"], + [100, + 3, + "RedisSession::__construct<\/a>"], + [100, + 1, + "RedisSession::destroy<\/a>"], + [100, + 2, + "RedisSession::read<\/a>"], + [100, + 2, + "RedisSession::write<\/a>"], + [100, + 1, + "RedisSession::idExists<\/a>"], + [100, + 5, + "Token::__construct<\/a>"], + [100, + 1, + "Token::jsonSerialize<\/a>"], + [100, + 2, + "Token::get<\/a>"], + [100, + 1, + "Token::sessionRequiredWarning<\/a>"], + [100, + 4, + "Token::create<\/a>"], + [100, + 3, + "Token::removeAllTokens<\/a>"], + [100, + 2, + "Token::getAndRemove<\/a>"], + [100, + 4, + "Token::remove<\/a>"], + [100, + 1, + "Token::getName<\/a>"], + [100, + 1, + "Token::getTokenKey<\/a>"]], 'Method Complexity')) .transition() .duration(500) .call(chart); diff --git a/coverage/index.html b/coverage/index.html index 9b9f776..1f25bad 100644 --- a/coverage/index.html +++ b/coverage/index.html @@ -49,7 +49,9 @@
100.00%
-
164 / 164
+ +
169 / 169
+
100.00% covered (success) @@ -57,7 +59,9 @@
100.00%
-
42 / 42
+ +
44 / 44
+
100.00% covered (success) @@ -77,7 +81,9 @@
100.00%
-
64 / 64
+ +
67 / 67
+
100.00% covered (success) @@ -85,7 +91,9 @@
100.00%
-
19 / 19
+ +
20 / 20
+
100.00% covered (success) @@ -161,7 +169,9 @@
100.00%
-
19 / 19
+ +
18 / 18
+
100.00% covered (success) @@ -189,7 +199,9 @@
100.00%
-
12 / 12
+ +
13 / 13
+
100.00% covered (success) @@ -245,7 +257,9 @@
100.00%
-
45 / 45
+ +
47 / 47
+
100.00% covered (success) @@ -253,7 +267,9 @@
100.00%
-
9 / 9
+ +
10 / 10
+
diff --git a/src/RedisSession.php b/src/RedisSession.php index 893754b..9794021 100644 --- a/src/RedisSession.php +++ b/src/RedisSession.php @@ -99,7 +99,9 @@ function write($sessionID, $sessionData) { $sessionData); } + // @codeCoverageIgnoreStart return true; + // @codeCoverageIgnoreEnd } /** diff --git a/tests/AbstractSessionTest.php b/tests/AbstractSessionTest.php index 86bf8b9..dc217da 100644 --- a/tests/AbstractSessionTest.php +++ b/tests/AbstractSessionTest.php @@ -1,203 +1,211 @@ cfg = new Cfg([Cfg::CFG_SECURE => false, - Cfg::CFG_SAVE_CLI => true]); - } + /** @var Redis */ + private $redis; - function checkSouldBeSaved() { - $red = new \Redis(); - $red->connect('127.0.0.1'); + function __construct($name = null, array $data = [], $dataName = '') { + parent::__construct($name, $data, $dataName); + $this->cfg = new Cfg([Cfg::CFG_SECURE => false, + Cfg::CFG_SAVE_CLI => true]); + $this->redis = new Redis(); + $this->redis->connect('127.0.0.1'); + } - $sess = new ProtectedChecks($red, $this->cfg); - $this->assertTrue($sess->shouldBeSavedCheck()); - $sess->addConfig(Cfg::CFG_SAVE_CLI, false); - $this->assertFalse($sess->shouldBeSavedCheck()); - } + function checkSouldBeSaved() { + $sess = new ProtectedChecks($this->redis, $this->cfg); + $this->assertTrue($sess->shouldBeSavedCheck()); + $sess->addConfig(Cfg::CFG_SAVE_CLI, false); + $this->assertFalse($sess->shouldBeSavedCheck()); + } - function testgetLastActiveSession() { - $red = new \Redis(); - $red->connect('127.0.0.1'); - - $this->assertNull(Sess::getLastActiveSession()); - - (new Sess($red,$this->cfg))->start(); - $this->assertTrue(Sess::getLastActiveSession() instanceof Sess); - session_write_close(); - $this->assertNull(Sess::getLastActiveSession()); - - (new Sess($red,$this->cfg))->start(); - $this->assertTrue(Sess::getLastActiveSession() instanceof Sess); - session_destroy(); - $this->assertNull(Sess::getLastActiveSession()); - } + function testgetLastActiveSession() { + $this->assertNull(Sess::getLastActiveSession()); - function testDestruct() { - $red = new \Redis(); - $red->connect('127.0.0.1'); - $sess = new Sess($red, $this->cfg); + (new Sess($this->redis, $this->cfg))->start(); + $this->assertTrue(Sess::getLastActiveSession() instanceof Sess); + session_write_close(); + $this->assertNull(Sess::getLastActiveSession()); - $this->assertFalse(Sess::isActive()); - $sess->start(); - $this->assertTrue(Sess::isActive()); + (new Sess($this->redis, $this->cfg))->start(); + $this->assertTrue(Sess::getLastActiveSession() instanceof Sess); + session_destroy(); + $this->assertNull(Sess::getLastActiveSession()); + } - $key = $this->cfg->prefix . session_id(); - $this->assertFalse($red->exists($key)); + function testDestruct() { + $sess = new Sess($this->redis, $this->cfg); - $_SESSION[__METHOD__] = 'foo'; - $sess->__destruct(); - $this->assertEquals('s:3:"foo"', self::sessionUnserialize($red->get($key))[__METHOD__]); - } + $this->assertFalse(Sess::isActive()); + $sess->start(); + $this->assertTrue(Sess::isActive()); - protected static function sessionUnserialize($str) { - $spl = explode(';', $str); + $key = $this->cfg->prefix . session_id(); + $this->assertFalse($this->redis->exists($key)); - foreach ($spl as $k => $s) { - if (trim($s)) { - $s = explode('|', $s); - $spl[$s[0]] = $s[1]; - } - unset($spl[$k]); - } + $_SESSION[__METHOD__] = 'foo'; + $sess->__destruct(); + $this->assertEquals('s:3:"foo"', self::sessionUnserialize($this->redis->get($key))[__METHOD__]); + } - return $spl; - } + protected static function sessionUnserialize($str) { + $spl = explode(';', $str); - function testDestroySafely() { - $this->assertFalse(Sess::isActive()); - $this->assertFalse(Sess::destroySafely()); - (new Sess(null, $this->cfg))->start(); - $this->assertTrue(Sess::isActive()); - $this->assertTrue(Sess::destroySafely()); - $this->assertFalse(Sess::isActive()); + foreach ($spl as $k => $s) { + if (trim($s)) { + $s = explode('|', $s); + $spl[$s[0]] = $s[1]; + } + unset($spl[$k]); } - /** - * @expectedException \PHPUnit_Framework_Error - */ - function testStartDuplicate() { - $red = new \Redis(); - $red->connect('127.0.0.1'); + return $spl; + } - (new Sess($red, $this->cfg))->start(); - (new Sess($red, $this->cfg))->start(); - } + function testJsonSerializeNoSession() { + $sess = new Sess($this->redis, $this->cfg); + $this->assertEquals(json_encode([]), json_encode($sess)); + } - /** - * @expectedException \PHPUnit_Framework_Error - */ - function testSessionRequiredWarningIsset() { - $s = new Sess(null, $this->cfg); + function testJsonSerializeSession() { + $sess = new Sess($this->redis, $this->cfg); + $sess->start(); + $finger = (new Config())->get(Config::CFG_FINGERPRINT_NAME); + $_SESSION[__METHOD__] = 'bar'; + $pattern = '~{"' . $finger . '":"[a-z0-9]{32}","' . __METHOD__ . '":"bar"}~i'; + $str = json_encode($sess); - isset($s['foo']); - } + $this->assertEquals(1, preg_match($pattern, $str)); + } - /** - * @expectedException \PHPUnit_Framework_Error - */ - function testSessionRequiredWarningUnset() { - $s = new Sess(null, $this->cfg); + function testDestroySafely() { + $this->assertFalse(Sess::isActive()); + $this->assertFalse(Sess::destroySafely()); + (new Sess(null, $this->cfg))->start(); + $this->assertTrue(Sess::isActive()); + $this->assertTrue(Sess::destroySafely()); + $this->assertFalse(Sess::isActive()); + } - unset($s['foo']); - } + /** + * @expectedException \PHPUnit_Framework_Error + */ + function testStartDuplicate() { + (new Sess($this->redis, $this->cfg))->start(); + (new Sess($this->redis, $this->cfg))->start(); + } - /** - * @expectedException \PHPUnit_Framework_Error - */ - function testSessionRequiredWarningOffsetGet() { - $s = new Sess(null, $this->cfg); + /** + * @expectedException \PHPUnit_Framework_Error + */ + function testSessionRequiredWarningIsset() { + $s = new Sess(null, $this->cfg); - $s['foo']; - } + /** @noinspection PhpExpressionResultUnusedInspection */ + isset($s['foo']); + } - /** - * @expectedException \PHPUnit_Framework_Error - */ - function testSessionRequiredWarningMagicGet() { - $s = new Sess(null, $this->cfg); + /** + * @expectedException \PHPUnit_Framework_Error + */ + function testSessionRequiredWarningUnset() { + $s = new Sess(null, $this->cfg); - $s->foo; - } + unset($s['foo']); + } - /** - * @expectedException \PHPUnit_Framework_Error - */ - function testSessionRequiredWarningOffsetSet() { - $s = new Sess(null, $this->cfg); + /** + * @expectedException \PHPUnit_Framework_Error + */ + function testSessionRequiredWarningOffsetGet() { + $s = new Sess(null, $this->cfg); - $s['foo'] = 'bar'; - } + $s['foo']; + } - /** - * @expectedException \PHPUnit_Framework_Error - */ - function testSessionRequiredWarningMagicSet() { - $s = new Sess(null, $this->cfg); + /** + * @expectedException \PHPUnit_Framework_Error + */ + function testSessionRequiredWarningMagicGet() { + $s = new Sess(null, $this->cfg); - $s->foo = 'bar'; - } + $s->foo; + } - function testBadIdentityCheck() { - /** @var ProtectedChecks $sess */ - $red = new \Redis(); - $red->connect('127.0.0.1'); + /** + * @expectedException \PHPUnit_Framework_Error + */ + function testSessionRequiredWarningOffsetSet() { + $s = new Sess(null, $this->cfg); - $this->assertFalse(Sess::isActive()); - $sess = (new ProtectedChecks($red, $this->cfg))->start(); - $sid = session_id(); - $prefix = $this->cfg->prefix . $sid; - $_SESSION[__METHOD__] = 1; - $sess->write($sid, $_SESSION); + $s['foo'] = 'bar'; + } - $this->assertTrue(Sess::isActive()); - $this->assertTrue($red->exists($prefix)); + /** + * @expectedException \PHPUnit_Framework_Error + */ + function testSessionRequiredWarningMagicSet() { + $s = new Sess(null, $this->cfg); - $sess->idCheck($sid); + $s->foo = 'bar'; + } - $this->assertFalse(Sess::isActive()); - $this->assertFalse($red->exists($prefix)); - } + function testBadIdentityCheck() { + /** @var ProtectedChecks $sess */ - function testMagic() { - $red = new \Redis(); - $red->connect('127.0.0.1'); + $this->assertFalse(Sess::isActive()); + $sess = (new ProtectedChecks($this->redis, $this->cfg))->start(); + $sid = session_id(); + $prefix = $this->cfg->prefix . $sid; + $_SESSION[__METHOD__] = 1; + $sess->write($sid, $_SESSION); - $sess = (new Sess($red, $this->cfg))->start(); - $sess['one'] = 'foo'; - $sess->two = 'bar'; + $this->assertTrue(Sess::isActive()); + $this->assertTrue($this->redis->exists($prefix)); - $this->assertTrue(isset($sess['one'])); - $this->assertTrue(isset($_SESSION['one'])); + $sess->idCheck($sid); - $this->assertEquals('foo', $sess->{'one'}); - $this->assertEquals('bar', $sess['two']); - $this->assertEquals('foo', $_SESSION['one']); - $this->assertEquals('bar', $_SESSION['two']); + $this->assertFalse(Sess::isActive()); + $this->assertFalse($this->redis->exists($prefix)); + } - unset($sess['two']); - $this->assertFalse(isset($_SESSION['two'])); - } + function testMagic() { + $sess = (new Sess($this->redis, $this->cfg))->start(); + $sess['one'] = 'foo'; + $sess->two = 'bar'; + + $this->assertTrue(isset($sess['one'])); + $this->assertTrue(isset($_SESSION['one'])); + + $this->assertEquals('foo', $sess->{'one'}); + $this->assertEquals('bar', $sess['two']); + $this->assertEquals('foo', $_SESSION['one']); + $this->assertEquals('bar', $_SESSION['two']); + + unset($sess['two']); + $this->assertFalse(isset($_SESSION['two'])); } +} - class ProtectedChecks extends Sess { +class ProtectedChecks extends Sess { - function idCheck($sessionID) { - parent::handleIdentityCheckFailure($sessionID); - } + function idCheck($sessionID) { + parent::handleIdentityCheckFailure($sessionID); + } - function shouldBeSavedCheck() { - return $this->shouldBeSaved(); - } + function shouldBeSavedCheck() { + return $this->shouldBeSaved(); } +} diff --git a/tests/TokenTest.php b/tests/TokenTest.php index 7b05676..1e814aa 100644 --- a/tests/TokenTest.php +++ b/tests/TokenTest.php @@ -1,179 +1,188 @@ redis = new Redis(); - $this->redis->connect('127.0.0.1'); - $this->cfg = new Config(); - $this->key = $this->cfg->tokenKey; - } +namespace AloFramework\Session\Tests; + +use AloFramework\Session\Config; +use AloFramework\Session\RedisSession as Sess; +use AloFramework\Session\Token; +use PHPUnit_Framework_TestCase; +use Redis; + +class TokenTest extends PHPUnit_Framework_TestCase { + + /** + * @var Redis + */ + private $redis; + + /** + * @var string + */ + private $key; + + /** @var Config */ + private $cfg; + + function __construct($name = null, array $data = [], $dataName = '') { + parent::__construct($name, $data, $dataName); + $this->redis = new Redis(); + $this->redis->connect('127.0.0.1'); + $this->cfg = new Config(); + $this->key = $this->cfg->tokenKey; + } - function testConstructWithConfigObject() { - $tok = new TokenProtectedMethods(__METHOD__, $this->cfg); + function testConstructWithConfigObject() { + $tok = new TokenProtectedMethods(__METHOD__, $this->cfg); - $this->assertEquals($this->key, $tok->getKeyTest()); - $this->assertEquals(__METHOD__, $tok->getNameTest()); - } + $this->assertEquals($this->key, $tok->getKeyTest()); + $this->assertEquals(__METHOD__, $tok->getNameTest()); + } - function testConstructWithSessionObject() { - $tok = new TokenProtectedMethods(__METHOD__, $this->sess()); - $this->assertEquals($this->key, $tok->getKeyTest()); - } + function testConstructWithSessionObject() { + $tok = new TokenProtectedMethods(__METHOD__, $this->sess()); + $this->assertEquals($this->key, $tok->getKeyTest()); + } - function testConstructNull() { - $this->sess(); - $tok = new TokenProtectedMethods(__METHOD__); - $this->assertEquals($this->key, $tok->getKeyTest()); - } + private function sess() { + Sess::destroySafely(); - function testConstructNullNoSession() { - Sess::destroySafely(); - $this->setExpectedException('\InvalidArgumentException'); - new TokenProtectedMethods(__METHOD__); - } + return (new Sess($this->redis))->start(); + } - /** @dataProvider methodSessionWarningsProvider */ - function testMethodSessionWarnings($method) { - Sess::destroySafely(); - $this->setExpectedException('\PHPUnit_Framework_Error'); - $tok = new Token('foo', $this->cfg); + function testConstructNull() { + $this->sess(); + $tok = new TokenProtectedMethods(__METHOD__); + $this->assertEquals($this->key, $tok->getKeyTest()); + } - call_user_func([$tok, $method]); - } + function testJsonEncode() { + $token = new Token(__METHOD__, $this->cfg); + $this->assertEquals(json_encode([ + 'name' => __METHOD__, + 'key' => $this->cfg->tokenKey + ]), + json_encode($token)); + } - function testCreate() { - $this->sess(); - $tok = new Token(__METHOD__, $this->cfg); - $this->assertFalse(array_key_exists($this->key, $_SESSION)); + function testConstructNullNoSession() { + Sess::destroySafely(); + $this->setExpectedException('\InvalidArgumentException'); + new TokenProtectedMethods(__METHOD__); + } - $token = $tok->create('sha512'); - $this->assertEquals(strlen(hash('sha512', 1)), strlen($token)); - $this->assertTrue(array_key_exists($this->key, $_SESSION)); + /** @dataProvider methodSessionWarningsProvider */ + function testMethodSessionWarnings($method) { + Sess::destroySafely(); + $this->setExpectedException('\PHPUnit_Framework_Error'); + $tok = new Token('foo', $this->cfg); - $this->assertTrue(array_key_exists(__METHOD__, $_SESSION[$this->key])); - $this->assertEquals($token, $_SESSION[$this->key][__METHOD__]); - } + call_user_func([$tok, $method]); + } - function testGet() { - $this->sess(); - $tok = new Token(__METHOD__, $this->cfg); - $this->assertTrue($tok->get() === null); + function testCreate() { + $this->sess(); + $tok = new Token(__METHOD__, $this->cfg); + $this->assertFalse(array_key_exists($this->key, $_SESSION)); - $create = $tok->create(); + $token = $tok->create('sha512'); + $this->assertEquals(strlen(hash('sha512', 1)), strlen($token)); + $this->assertTrue(array_key_exists($this->key, $_SESSION)); - $this->assertEquals($create, $tok->get()); - } + $this->assertTrue(array_key_exists(__METHOD__, $_SESSION[$this->key])); + $this->assertEquals($token, $_SESSION[$this->key][__METHOD__]); + } - function testRemove() { - $this->sess(); - $tok = new Token(__METHOD__, $this->cfg); + function testGet() { + $this->sess(); + $tok = new Token(__METHOD__, $this->cfg); + $this->assertTrue($tok->get() === null); - $this->assertFalse($tok->remove()); - $tok->create(); - $this->assertTrue($tok->remove()); - } + $create = $tok->create(); + + $this->assertEquals($create, $tok->get()); + } - function testRemoveMultiple() { - $this->sess(); - $tok1 = new Token(__METHOD__ . '1', $this->cfg); - $tok2 = new Token(__METHOD__ . '2', $this->cfg); + function testRemove() { + $this->sess(); + $tok = new Token(__METHOD__, $this->cfg); - $this->assertFalse(array_key_exists($this->key, $_SESSION)); + $this->assertFalse($tok->remove()); + $tok->create(); + $this->assertTrue($tok->remove()); + } - $tok1->create(); - $tok2->create(); + function testRemoveMultiple() { + $this->sess(); + $tok1 = new Token(__METHOD__ . '1', $this->cfg); + $tok2 = new Token(__METHOD__ . '2', $this->cfg); - $this->assertTrue(array_key_exists($this->key, $_SESSION)); - $this->assertTrue(array_key_exists(__METHOD__ . '1', $_SESSION[$this->key])); - $this->assertTrue(array_key_exists(__METHOD__ . '2', $_SESSION[$this->key])); + $this->assertFalse(array_key_exists($this->key, $_SESSION)); - $tok1->remove(); - $this->assertTrue(array_key_exists($this->key, $_SESSION)); - $this->assertFalse(array_key_exists(__METHOD__ . '1', $_SESSION[$this->key])); - $this->assertTrue(array_key_exists(__METHOD__ . '2', $_SESSION[$this->key])); + $tok1->create(); + $tok2->create(); - $tok2->remove(); - $this->assertFalse(array_key_exists($this->key, $_SESSION)); - } + $this->assertTrue(array_key_exists($this->key, $_SESSION)); + $this->assertTrue(array_key_exists(__METHOD__ . '1', $_SESSION[$this->key])); + $this->assertTrue(array_key_exists(__METHOD__ . '2', $_SESSION[$this->key])); - function testRemoveAllTokens() { - $this->sess(); - /** @var Token[] $toks */ - $toks = []; + $tok1->remove(); + $this->assertTrue(array_key_exists($this->key, $_SESSION)); + $this->assertFalse(array_key_exists(__METHOD__ . '1', $_SESSION[$this->key])); + $this->assertTrue(array_key_exists(__METHOD__ . '2', $_SESSION[$this->key])); - $this->assertFalse(array_key_exists($this->key, $_SESSION)); + $tok2->remove(); + $this->assertFalse(array_key_exists($this->key, $_SESSION)); + } - for ($i = 0; $i < 5; $i++) { - $tok = new Token(__METHOD__ . $i, $this->cfg); - $tok->create('md5'); - $toks[] = $tok; - } + function testRemoveAllTokens() { + $this->sess(); + /** @var Token[] $toks */ + $toks = []; - foreach ($toks as $tok) { - $this->assertEquals(32, strlen($tok->get())); - } + $this->assertFalse(array_key_exists($this->key, $_SESSION)); - $this->assertTrue($toks[0]->removeAllTokens()); - $this->assertFalse($toks[0]->removeAllTokens()); - $this->assertFalse(array_key_exists($this->key, $_SESSION)); + for ($i = 0; $i < 5; $i++) { + $tok = new Token(__METHOD__ . $i, $this->cfg); + $tok->create('md5'); + $toks[] = $tok; } - function testGetAndRemove() { - $this->sess(); - $tok = new Token(__METHOD__, $this->cfg); - $this->assertTrue($tok->get() === null); + foreach ($toks as $tok) { + $this->assertEquals(32, strlen($tok->get())); + } - $create = $tok->create(); - $this->assertEquals($create, $tok->getAndRemove()); + $this->assertTrue($toks[0]->removeAllTokens()); + $this->assertFalse($toks[0]->removeAllTokens()); + $this->assertFalse(array_key_exists($this->key, $_SESSION)); + } - $this->assertTrue($tok->get() === null); - } + function testGetAndRemove() { + $this->sess(); + $tok = new Token(__METHOD__, $this->cfg); + $this->assertTrue($tok->get() === null); - function methodSessionWarningsProvider() { - return [['get'], - ['create'], - ['remove'], - ['removeAllTokens'], - ['getAndRemove']]; - } + $create = $tok->create(); + $this->assertEquals($create, $tok->getAndRemove()); - private function sess() { - Sess::destroySafely(); + $this->assertTrue($tok->get() === null); + } - return (new Sess($this->redis))->start(); - } + function methodSessionWarningsProvider() { + return [['get'], + ['create'], + ['remove'], + ['removeAllTokens'], + ['getAndRemove']]; } +} - class TokenProtectedMethods extends Token { +class TokenProtectedMethods extends Token { - function getNameTest() { - return $this->getName(); - } + function getNameTest() { + return $this->getName(); + } - function getKeyTest() { - return $this->getTokenKey(); - } + function getKeyTest() { + return $this->getTokenKey(); } +}