Browse Source

CookieHelper

euromark 11 years ago
parent
commit
204c429434

+ 45 - 0
src/View/Helper/CookieHelper.php

@@ -0,0 +1,45 @@
+<?php
+namespace Tools\View\Helper;
+
+use Cake\View\Helper;
+
+/**
+ * Cookie Helper.
+ */
+class CookieHelper extends Helper {
+
+/**
+ * Used to read a cookie values set in a controller for a key or return values for all keys.
+ *
+ * In your view: `$this->Cookie->read('Controller.sessKey');`
+ * Calling the method without a param will return all cookie vars
+ *
+ * @param string $name the name of the cookie key you want to read
+ * @return mixed values from the cookie vars
+ */
+	public function read($name = null) {
+		return $this->request->cookie($name);
+	}
+
+/**
+ * Used to check is a session key has been set
+ *
+ * In your view: `$this->Session->check('Controller.sessKey');`
+ *
+ * @param string $name Session key to check.
+ * @return bool
+ */
+	public function check($name) {
+		return $this->request->cookie($name) !== null;
+	}
+
+/**
+ * Event listeners.
+ *
+ * @return array
+ */
+	public function implementedEvents() {
+		return [];
+	}
+
+}

+ 74 - 0
tests/TestCase/View/Helper/CookieHelperTest.php

@@ -0,0 +1,74 @@
+<?php
+
+namespace Tools\TestCase\View\Helper;
+
+use Tools\View\Helper\CookieHelper;
+use Cake\TestSuite\TestCase;
+use Cake\View\View;
+use Cake\ORM\Entity;
+use Cake\ORM\Table;
+use Cake\ORM\TableRegistry;
+use Cake\Datasource\ConnectionManager;
+use Cake\Network\Request;
+
+class CookieHelperTest extends TestCase {
+
+	public $Cookie;
+
+	/**
+	 * @return void
+	 */
+	public function setUp() {
+		parent::setUp();
+
+		$this->Cookie = new CookieHelper(new View(null));
+		$this->Cookie->request = $this->getMock('Cake\Network\Request', ['cookie']);
+	}
+
+	public function tearDown() {
+		unset($this->Table);
+
+		parent::tearDown();
+	}
+
+	/**
+	 * CookieHelperTest::testObject()
+	 *
+	 * @return void
+	 */
+	public function testObject() {
+		$this->assertInstanceOf('Tools\View\Helper\CookieHelper', $this->Cookie);
+	}
+
+	/**
+	 * CookieHelperTest::testCheck()
+	 *
+	 * @return void
+	 */
+	public function testCheck() {
+		$this->Cookie->request->expects($this->at(0))
+			->method('cookie')
+			->will($this->returnValue(null));
+		$this->Cookie->request->expects($this->at(1))
+			->method('cookie')
+			->will($this->returnValue('val'));
+
+		$this->assertFalse($this->Cookie->check('Foo.key'));
+		$this->assertTrue($this->Cookie->check('Foo.key'));
+	}
+
+	/**
+	 * CookieHelperTest::testRead()
+	 *
+	 * @return void
+	 */
+	public function testRead() {
+		$this->Cookie->request->expects($this->once())
+			->method('cookie')
+			->will($this->returnValue('val'));
+
+		$output = $this->Cookie->read('Foo.key');
+		$this->assertTextEquals('val', $output);
+	}
+
+}

+ 29 - 2
tests/TestCase/View/Helper/TreeHelperTest.php

@@ -36,8 +36,6 @@ class TreeHelperTest extends TestCase {
 	public function setUp() {
 		parent::setUp();
 
-		Configure::write('debug', true);
-
 		$this->Tree = new TreeHelper(new View(null));
 		$this->Table = TableRegistry::get('AfterTrees');
 		$this->Table->addBehavior('Tree');
@@ -81,6 +79,11 @@ class TreeHelperTest extends TestCase {
 		$this->assertInstanceOf('Tools\View\Helper\TreeHelper', $this->Tree);
 	}
 
+	/**
+	 * TreeHelperTest::testGenerate()
+	 *
+	 * @return void
+	 */
 	public function testGenerate() {
 		$tree = $this->Table->find('threaded')->toArray();
 
@@ -166,6 +169,11 @@ TEXT;
 		$this->assertTextEquals($expected, $output);
 	}
 
+	/**
+	 * TreeHelperTest::testGenerateWithDepth()
+	 *
+	 * @return void
+	 */
 	public function testGenerateWithDepth() {
 		$tree = $this->Table->find('threaded')->toArray();
 
@@ -203,6 +211,11 @@ TEXT;
 		$this->assertTextEquals($expected, $output);
 	}
 
+	/**
+	 * TreeHelperTest::testGenerateWithSettings()
+	 *
+	 * @return void
+	 */
 	public function testGenerateWithSettings() {
 		$tree = $this->Table->find('threaded')->toArray();
 
@@ -240,6 +253,11 @@ TEXT;
 		$this->assertTextEquals($expected, $output);
 	}
 
+	/**
+	 * TreeHelperTest::testGenerateWithMaxDepth()
+	 *
+	 * @return void
+	 */
 	public function testGenerateWithMaxDepth() {
 		$tree = $this->Table->find('threaded')->toArray();
 
@@ -273,6 +291,11 @@ TEXT;
 		$this->assertTextEquals($expected, $output);
 	}
 
+	/**
+	 * TreeHelperTest::testGenerateWithAutoPath()
+	 *
+	 * @return void
+	 */
 	public function testGenerateWithAutoPath() {
 		$tree = $this->Table->find('threaded')->toArray();
 		//debug($tree);
@@ -359,6 +382,8 @@ TEXT;
 	 * - Three
 	 * - Four
 	 * -- Four-SubA
+	 *
+	 * @return void
 	 */
 	public function testGenerateWithAutoPathAndHideUnrelated() {
 		$this->skipIf(true, 'FIXME');
@@ -418,6 +443,8 @@ TEXT;
 	 * - Three
 	 * - Four
 	 * -- Four-SubA
+	 *
+	 * @return void
 	 */
 	public function testGenerateWithAutoPathAndHideUnrelatedAndSiblings() {
 		$this->skipIf(true, 'FIXME');