Kaynağa Gözat

Added Session class

Taddeus Kroes 13 yıl önce
ebeveyn
işleme
129dbc4fdd
4 değiştirilmiş dosya ile 223 ekleme ve 0 silme
  1. 89 0
      session.php
  2. 31 0
      tests/SingletonTestCase.php
  3. 102 0
      tests/test_session.php
  4. 1 0
      webbasics.php

+ 89 - 0
session.php

@@ -0,0 +1,89 @@
+<?php
+/*
+ * 
+ * 
+ * @author Taddeus Kroes
+ * @date 05-10-2012
+ */
+
+namespace webbasics;
+
+require_once 'base.php';
+
+class Session {
+	private static $instance;
+	
+	static function getInstance() {
+		if (self::$instance === null)
+			self::$instance = new self;
+		
+		return self::$instance;
+	}
+	
+	/**
+	 * @codeCoverageIgnore
+	 */
+	private function __construct() {
+		session_start();
+	}
+	
+	function set($names, $value=null) {
+		if (is_array($names)) {
+			foreach ($names as $name => $value)
+				$this->set($name, $value);
+		} else {
+			$_SESSION[$names] = $value;
+		}
+	}
+	
+	function get($names) {
+		if (is_array($names)) {
+			$values = array();
+			
+			foreach ($names as $name)
+				$values[] = $_SESSION[$name];
+			
+			return $values;
+		}
+		
+		return $_SESSION[$names];
+	}
+	
+	function isRegistered($name) {
+		return isset($_SESSION[$name]);
+	}
+	
+	function areRegistered(array $names) {
+		foreach ($names as $name) {
+			if (!isset($_SESSION[$name]))
+				return false;
+		}
+		
+		return true;
+	}
+	
+	function regenerateId() {
+		session_regenerate_id();
+	}
+	
+	/**
+	 * @codeCoverageIgnore
+	 */
+	function close() {
+		session_write_close();
+	}
+	
+	function clear() {
+		$_SESSION = array();
+	}
+	
+	function destroy($clear=false) {
+		if ($clear)
+			$this->clear();
+		
+		session_destroy();
+		self::$instance = null;
+	}
+}
+
+?>

+ 31 - 0
tests/SingletonTestCase.php

@@ -0,0 +1,31 @@
+<?php
+
+abstract class SingletonTestCase extends PHPUnit_Framework_TestCase {
+	private $rclass;
+	
+	abstract function getClassName();
+	
+	function setUp() {
+		$this->rclass = new ReflectionClass($this->getClassName());
+	}
+	
+	function testConstructorPrivateness() {
+		$rmethod = new ReflectionMethod($this->getClassName(), '__construct');
+		$this->assertTrue($rmethod->isPrivate());
+	}
+	
+	function testInstanceVariable() {
+		$this->assertTrue($this->rclass->hasProperty('instance'));
+		$rprop = new ReflectionProperty($this->getClassName(), 'instance');
+		$this->assertTrue($rprop->isPrivate());
+		$this->assertTrue($rprop->isStatic());
+	}
+	
+	function testGetInstanceMethod() {
+		$this->assertTrue($this->rclass->hasMethod('getInstance'));
+		$rmethod = new ReflectionMethod($this->getClassName(), 'getInstance');
+		$this->assertTrue($rmethod->isPublic());
+		$this->assertTrue($rmethod->isStatic());
+		$this->assertEquals(0, $rmethod->getNumberOfParameters());
+	}
+}

+ 102 - 0
tests/test_session.php

@@ -0,0 +1,102 @@
+<?php
+
+require_once 'SingletonTestCase.php';
+require_once 'session.php';
+use webbasics\Session;
+
+// Turn on outbut buffering to circumvent "headers already sent" error
+ob_start();
+
+class SessionTest extends SingletonTestCase {
+	private $session;
+	
+	function getClassName() {
+		return 'webbasics\Session';
+	}
+	
+	function setUp() {
+		parent::setUp();
+		$this->session = Session::getInstance();
+	}
+	
+	function tearDown() {
+		$_SESSION = array();
+	}
+	
+	function testSessionStarted() {
+		$this->assertNotEquals('', session_id());
+	}
+	
+	function testSetSingle() {
+		$this->session->set('foo', 'bar');
+		$this->assertArrayHasKey('foo', $_SESSION);
+		$this->assertEquals('bar', $_SESSION['foo']);
+	}
+	
+	function testSetMultiple() {
+		$this->session->set(array('foo' => 'bar', 'bar' => 'baz'));
+		$this->assertArrayHasKey('foo', $_SESSION);
+		$this->assertEquals('bar', $_SESSION['foo']);
+		$this->assertArrayHasKey('bar', $_SESSION);
+		$this->assertEquals('baz', $_SESSION['bar']);
+	}
+	
+	function testGetSingle() {
+		$_SESSION['foo'] = 'bar';
+		$this->assertEquals('bar', $this->session->get('foo'));
+	}
+	
+	function testGetMultiple() {
+		$_SESSION['foo'] = 'bar';
+		$_SESSION['bar'] = 'baz';
+		$this->assertEquals(array('bar', 'baz'), $this->session->get(array('foo', 'bar')));
+	}
+	
+	function testIsRegistered() {
+		$_SESSION['foo'] = 'bar';
+		$this->assertTrue($this->session->isRegistered('foo'));
+		$this->assertFalse($this->session->isRegistered('bar'));
+	}
+	
+	/**
+	 * @depends testIsRegistered
+	 */
+	function testAreRegistered() {
+		$_SESSION['foo'] = 'bar';
+		$_SESSION['bar'] = 'baz';
+		$this->assertTrue($this->session->areRegistered(array('foo', 'bar')));
+		$this->assertFalse($this->session->areRegistered(array('foo', 'baz')));
+	}
+	
+	function testRegenerateId() {
+		$old_id = session_id();
+		$this->session->regenerateId();
+		$this->assertNotEquals($old_id, session_id());
+	}
+	
+	function testClear() {
+		$_SESSION['foo'] = 'bar';
+		$this->session->clear();
+		$this->assertEmpty($_SESSION);
+	}
+	
+	/**
+	 * @depends testRegenerateId
+	 */
+	function testDestroySimple() {
+		$this->session->destroy();
+		$this->assertEquals('', session_id());
+	}
+	
+	/**
+	 * @depends testClear
+	 */
+	function testDestroyClear() {
+		$_SESSION['foo'] = 'bar';
+		$this->session->destroy(true);
+		$this->assertEmpty($_SESSION);
+		$this->assertEquals('', session_id());
+	}
+}
+
+?>

+ 1 - 0
webbasics.php

@@ -14,6 +14,7 @@ require_once 'autoloader.php';
 require_once 'collection.php';
 require_once 'router.php';
 require_once 'template.php';
+require_once 'session.php';
 // @codeCoverageIgnoreEnd
 
 ?>