FlashHelperTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Controller\Controller;
  17. use Cake\Core\App;
  18. use Cake\Core\Plugin;
  19. use Cake\Network\Request;
  20. use Cake\Network\Session;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\View\Helper\FlashHelper;
  23. use Cake\View\View;
  24. /**
  25. * FlashHelperTest class
  26. *
  27. */
  28. class FlashHelperTest extends TestCase {
  29. /**
  30. * setUp method
  31. *
  32. * @return void
  33. */
  34. public function setUp() {
  35. parent::setUp();
  36. $this->View = new View();
  37. $session = new Session();
  38. $this->View->request = new Request(['session' => $session]);
  39. $this->Flash = new FlashHelper($this->View);
  40. $session->write(array(
  41. 'Flash' => array(
  42. 'flash' => array(
  43. 'key' => 'flash',
  44. 'message' => 'This is a calling',
  45. 'element' => 'Flash/default',
  46. 'params' => array()
  47. ),
  48. 'notification' => array(
  49. 'key' => 'notification',
  50. 'message' => 'This is a test of the emergency broadcasting system',
  51. 'element' => 'flash_helper',
  52. 'params' => array(
  53. 'title' => 'Notice!',
  54. 'name' => 'Alert!'
  55. )
  56. ),
  57. 'classy' => array(
  58. 'key' => 'classy',
  59. 'message' => 'Recorded',
  60. 'element' => 'flash_classy',
  61. 'params' => array()
  62. )
  63. )
  64. ));
  65. }
  66. /**
  67. * tearDown method
  68. *
  69. * @return void
  70. */
  71. public function tearDown() {
  72. parent::tearDown();
  73. unset($this->View, $this->Flash);
  74. }
  75. /**
  76. * testFlash method
  77. *
  78. * @return void
  79. */
  80. public function testFlash() {
  81. $result = $this->Flash->render();
  82. $expected = '<div class="message">This is a calling</div>';
  83. $this->assertContains($expected, $result);
  84. $expected = '<div id="classy-message">Recorded</div>';
  85. $result = $this->Flash->render('classy');
  86. $this->assertEquals($expected, $result);
  87. $result = $this->Flash->render('notification');
  88. $expected = [
  89. 'div' => ['id' => 'notificationLayout'],
  90. '<h1', 'Alert!', '/h1',
  91. '<h3', 'Notice!', '/h3',
  92. '<p', 'This is a test of the emergency broadcasting system', '/p',
  93. '/div'
  94. ];
  95. $this->assertHtml($expected, $result);
  96. $this->assertNull($this->Flash->render('non-existent'));
  97. }
  98. /**
  99. * testFlashThrowsException
  100. *
  101. * @expectedException \UnexpectedValueException
  102. */
  103. public function testFlashThrowsException() {
  104. $this->View->request->session()->write('Flash.foo', 'bar');
  105. $this->Flash->render('foo');
  106. }
  107. /**
  108. * test setting the element from the attrs.
  109. *
  110. * @return void
  111. */
  112. public function testFlashElementInAttrs() {
  113. $result = $this->Flash->render('notification', array(
  114. 'element' => 'flash_helper',
  115. 'params' => array('title' => 'Notice!', 'name' => 'Alert!')
  116. ));
  117. $expected = [
  118. 'div' => ['id' => 'notificationLayout'],
  119. '<h1', 'Alert!', '/h1',
  120. '<h3', 'Notice!', '/h3',
  121. '<p', 'This is a test of the emergency broadcasting system', '/p',
  122. '/div'
  123. ];
  124. $this->assertHtml($expected, $result);
  125. }
  126. /**
  127. * test using elements in plugins.
  128. *
  129. * @return void
  130. */
  131. public function testFlashWithPluginElement() {
  132. Plugin::load('TestPlugin');
  133. $result = $this->Flash->render('flash', array('element' => 'TestPlugin.Flash/plugin_element'));
  134. $expected = 'this is the plugin element';
  135. $this->assertEquals($expected, $result);
  136. }
  137. }