Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for #10616 #10626

Merged
merged 3 commits into from May 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 $_retainFlashMessages = 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->_retainFlashMessages = 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 enableRetainFlashMessages()
{
$this->_retainFlashMessages = 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->_retainFlashMessages) {
$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->_retainFlashMessages) {
$this->_flashMessages = $controller->request->session()->read('Flash');
}
});
$events->on('View.beforeLayout', function ($event, $viewFile) {
$this->_layoutName = $viewFile;
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/TestSuite/IntegrationTestCaseTest.php
Expand Up @@ -405,6 +405,20 @@ public function testFlashSessionAndCookieAssertsHttpServer()
$this->assertCookie(1, 'remember_me');
}

/**
* Test flash assertions stored with enableRememberFlashMessages() after they
* are rendered
*
* @return void
*/
public function testFlashAssertionsAfterRender()
{
$this->enableRetainFlashMessages();
$this->get('/posts/index/with_flash');

$this->assertSession('An error message', 'Flash.flash.0.message');
}

/**
* Tests the failure message for assertCookieNotSet
*
Expand Down
4 changes: 3 additions & 1 deletion tests/test_app/TestApp/Controller/PostsController.php
Expand Up @@ -48,16 +48,18 @@ public function beforeFilter(Event $event)
/**
* Index method.
*
* @param string $layout
* @return void
*/
public function index()
public function index($layout = 'default')
{
$this->Flash->error('An error message');
$this->response->cookie([
'name' => 'remember_me',
'value' => 1
]);
$this->set('test', 'value');
$this->viewBuilder()->setLayout($layout);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/test_app/TestApp/Template/Element/Flash/error.ctp
@@ -0,0 +1 @@
<div class="message"><?= h($message); ?></div>
43 changes: 43 additions & 0 deletions tests/test_app/TestApp/Template/Layout/with_flash.ctp
@@ -0,0 +1,43 @@
<?php
$this->loadHelper('Html');

$cakeDescription = 'CakePHP: the rapid development php framework';
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?= $this->Html->charset(); ?>
<title>
<?= $cakeDescription ?>:
<?= $this->fetch('title'); ?>
</title>
<?php
echo $this->Html->meta('icon');

echo $this->Html->css('cake.generic');

echo $this->fetch('script');
?>
</head>
<body>
<div id="container">
<div id="header">
<h1><?= $this->Html->link($cakeDescription, 'http://cakephp.org'); ?></h1>
</div>
<div id="content">

<?= $this->Flash->render(); ?>
<?= $this->fetch('content'); ?>

</div>
<div id="footer">
<?= $this->Html->link(
$this->Html->image('cake.power.gif', ['alt' => $cakeDescription, 'border' => '0']),
'http://www.cakephp.org/',
['target' => '_blank', 'escape' => false]
);
?>
</div>
</div>
</body>
</html>