Skip to content

Commit

Permalink
Storing flash messages during beforeRender event and restoring them
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed May 5, 2017
1 parent e85bfda commit 1995447
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/TestSuite/IntegrationTestCase.php
Expand Up @@ -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
Expand Down Expand Up @@ -187,6 +202,7 @@ public function tearDown()
$this->_appArgs = null;
$this->_securityToken = false;
$this->_csrfToken = false;
$this->_rememberFlashMessages = false;
$this->_useHttpServer = false;
}

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1995447

Please sign in to comment.