Skip to content
Permalink
Browse files

Storing flash messages during beforeRender event and restoring them

  • Loading branch information...
jeremyharris committed May 5, 2017
1 parent e85bfda commit 19954476fd14986bc550cc4ee641a7249eddf29f
Showing with 34 additions and 1 deletion.
  1. +34 −1 src/TestSuite/IntegrationTestCase.php
@@ -148,6 +148,21 @@ abstract class IntegrationTestCase extends TestCase
*/
protected $_csrfToken = false;
/**
* Boolean flag for whether or not the request should re-store
* flash messages
*
* @var bool
*/
protected $_rememberFlashMessages = false;
/**
* Stored flash messages before render
*
* @var null|array
*/
protected $_flashMessages;
/**
*
* @var null|string
@@ -187,6 +202,7 @@ public function tearDown()
$this->_appArgs = null;
$this->_securityToken = false;
$this->_csrfToken = false;
$this->_rememberFlashMessages = false;
$this->_useHttpServer = false;
}
@@ -242,6 +258,17 @@ public function enableCsrfToken()
$this->_csrfToken = true;
}
/**
* Calling this method will re-store flash messages into the test session
* after being removed by the FlashHelper
*
* @return void
*/
public function enableRememberFlashMessages()
{
$this->_rememberFlashMessages = true;
}
/**
* Configures the data for the *next* request.
*
@@ -425,6 +452,9 @@ protected function _sendRequest($url, $method, $data = [])
$request = $this->_buildRequest($url, $method, $data);
$response = $dispatcher->execute($request);
$this->_requestSession = $request['session'];
if ($this->_rememberFlashMessages) {
$this->_requestSession->write('Flash', $this->_flashMessages);
}
$this->_response = $response;
} catch (\PHPUnit\Exception $e) {
throw $e;
@@ -466,10 +496,13 @@ public function controllerSpy($event, $controller = null)
}
$this->_controller = $controller;
$events = $controller->eventManager();
$events->on('View.beforeRender', function ($event, $viewFile) {
$events->on('View.beforeRender', function ($event, $viewFile) use ($controller) {
if (!$this->_viewName) {
$this->_viewName = $viewFile;
}
if ($this->_rememberFlashMessages) {
$this->_flashMessages = $controller->request->session()->read('Flash');
}
});
$events->on('View.beforeLayout', function ($event, $viewFile) {
$this->_layoutName = $viewFile;

0 comments on commit 1995447

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