Browse Source

Add assertions for flash/session/cookies.

Add additional assertions and accessors for view variables.
mark_story 11 years ago
parent
commit
99884e0f12

+ 80 - 0
src/TestSuite/IntegrationTestCase.php

@@ -84,6 +84,13 @@ class IntegrationTestCase extends TestCase {
 	protected $_layoutName;
 
 /**
+ * The session instance from the last request
+ *
+ * @var \Cake\Network\Session
+ */
+	protected $_requestSession;
+
+/**
  * Clear the state used for requests.
  *
  * @return void
@@ -97,6 +104,7 @@ class IntegrationTestCase extends TestCase {
 		$this->_controller = null;
 		$this->_viewName = null;
 		$this->_layoutName = null;
+		$this->_requestSession = null;
 	}
 
 /**
@@ -243,6 +251,7 @@ class IntegrationTestCase extends TestCase {
 		);
 		try {
 			$dispatcher->dispatch($request, $response);
+			$this->_requestSession = $request->session();
 			$this->_response = $response;
 		} catch (\PHPUnit_Exception $e) {
 			throw $e;
@@ -466,4 +475,75 @@ class IntegrationTestCase extends TestCase {
 		$this->assertContains($content, $this->_layoutName, $message);
 	}
 
+/**
+ * Fetch a view variable by name.
+ *
+ * If the view variable does not exist null will be returned.
+ *
+ * @param string $name The view variable to get.
+ * @return mixed The view variable if set.
+ */
+	public function viewVariable($name) {
+		if (empty($this->_controller->viewVars)) {
+			$this->fail('There are no view variables, perhaps you need to run a request?');
+		}
+		if (isset($this->_controller->viewVars[$name])) {
+			return $this->_controller->viewVars[$name];
+		}
+		return null;
+	}
+
+/**
+ * Assert that a flash message was set.
+ *
+ * @param string $type The flash type to check.
+ * @param string $content The flash message content to check.
+ * @param string $key The flash namespace the message is in.
+ * @param string $message The failure message that will be appended to the generated message.
+ */
+	public function assertFlash($type, $content, $key = 'flash', $message = '') {
+		if (empty($this->_requestSession)) {
+			$this->fail('There is no stored session data. Perhaps you need to run a request?');
+		}
+		$key = "Flash.{$key}";
+		if (!$this->_requestSession->check($key)) {
+			$this->fail("The {$key} key is not set in the session. " . $message);
+		}
+		$val = $this->_requestSession->read($key);
+		if (strpos($val['element'], $type) === false) {
+			$this->fail("The {$key} does not have a matching element/type of {$type}. " . $message);
+		}
+		$this->assertEquals($content, $val['message'], 'Flash message differs. ' . $message);
+	}
+
+/**
+ * Assert session contents
+ *
+ * @param string $expected The expected contents.
+ * @param string $path The session data path. Uses Hash::get() compatible notation
+ * @param string $message The failure message that will be appended to the generated message.
+ */
+	public function assertSession($expected, $path, $message = '') {
+		if (empty($this->_requestSession)) {
+			$this->fail('There is no stored session data. Perhaps you need to run a request?');
+		}
+		$result = $this->_requestSession->read($path);
+		$this->assertEquals($expected, $result, 'Session content differs. ' . $message);
+	}
+
+/**
+ * Assert cookie values
+ *
+ * @param string $expected The expected contents.
+ * @param string $name The cookie name.
+ * @param string $message The failure message that will be appended to the generated message.
+ */
+	public function assertCookie($expected, $name, $message = '') {
+		if (empty($this->_response)) {
+			$this->fail('Not response set, cannot assert cookies.');
+		}
+		$result = $this->_response->cookie($name);
+		$this->assertEquals($expected, $result['value'], 'Cookie data differs. ' . $message);
+	}
+
 }

+ 14 - 0
tests/TestCase/TestSuite/IntegrationTestCaseTest.php

@@ -88,6 +88,20 @@ class IntegrationTestCaseTest extends IntegrationTestCase {
 
 		$this->assertTemplate('index');
 		$this->assertLayout('default');
+		$this->assertEquals('value', $this->viewVariable('test'));
+	}
+
+/**
+ * Test flash and cookie assertions
+ *
+ * @return void
+ */
+	public function testFlashSessionAndCookieAsserts() {
+		$this->post('/posts/index');
+
+		$this->assertFlash('error', 'An error message');
+		$this->assertSession('An error message', 'Flash.flash.message');
+		$this->assertCookie(1, 'remember_me');
 	}
 
 /**

+ 6 - 0
tests/test_app/TestApp/Controller/PostsController.php

@@ -28,6 +28,7 @@ class PostsController extends AppController {
  * @var array
  */
 	public $components = array(
+		'Flash',
 		'RequestHandler',
 	);
 
@@ -37,6 +38,11 @@ class PostsController extends AppController {
  * @return void
  */
 	public function index() {
+		$this->Flash->error('An error message');
+		$this->response->cookie([
+			'name' => 'remember_me',
+			'value' => 1
+		]);
 		$this->set('test', 'value');
 	}
 }