Browse Source

Added test for asserting flash after they're rendered

Jeremy Harris 9 years ago
parent
commit
a33cb7f372

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

@@ -406,6 +406,20 @@ class IntegrationTestCaseTest extends IntegrationTestCase
     }
 
     /**
+     * Test flash assertions stored with enableRememberFlashMessages() after they
+     * are rendered
+     *
+     * @return void
+     */
+    public function testFlashAssertionsAfterRender()
+    {
+        $this->enableRememberFlashMessages();
+        $this->get('/posts/index/with_flash');
+
+        $this->assertSession('An error message', 'Flash.flash.0.message');
+    }
+
+    /**
      * Tests the failure message for assertCookieNotSet
      *
      * @expectedException \PHPUnit\Framework\AssertionFailedError

+ 3 - 1
tests/test_app/TestApp/Controller/PostsController.php

@@ -48,9 +48,10 @@ class PostsController extends AppController
     /**
      * Index method.
      *
+     * @param string $layout
      * @return void
      */
-    public function index()
+    public function index($layout = 'default')
     {
         $this->Flash->error('An error message');
         $this->response->cookie([
@@ -58,6 +59,7 @@ class PostsController extends AppController
             'value' => 1
         ]);
         $this->set('test', 'value');
+        $this->viewBuilder()->setLayout($layout);
     }
 
     /**

+ 1 - 0
tests/test_app/TestApp/Template/Element/Flash/error.ctp

@@ -0,0 +1 @@
+<div class="message"><?= h($message); ?></div>

+ 43 - 0
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>