Skip to content
Permalink
Browse files

Add assertions for flash/session/cookies.

Add additional assertions and accessors for view variables.
  • Loading branch information...
markstory committed Sep 10, 2014
1 parent e35b808 commit 99884e0f121074f883a4235fc82fc5e02fff4f54
@@ -83,6 +83,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.
*
@@ -97,6 +104,7 @@ public function tearDown() {
$this->_controller = null;
$this->_viewName = null;
$this->_layoutName = null;
$this->_requestSession = null;
}
/**
@@ -243,6 +251,7 @@ protected function _sendRequest($url, $method, $data = null) {
);
try {
$dispatcher->dispatch($request, $response);
$this->_requestSession = $request->session();
$this->_response = $response;
} catch (\PHPUnit_Exception $e) {
throw $e;
@@ -466,4 +475,75 @@ public function assertLayout($content, $message = '') {
$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);
}
}
@@ -88,6 +88,20 @@ public function testRequestSetsProperties() {
$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');
}
/**
@@ -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');
}
}

0 comments on commit 99884e0

Please sign in to comment.
You can’t perform that action at this time.