FlashComponentTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\Controller\Component;
  16. use Cake\Controller\ComponentRegistry;
  17. use Cake\Controller\Component\FlashComponent;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\Configure;
  20. use Cake\Event\Event;
  21. use Cake\Network\Request;
  22. use Cake\Network\Session;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * FlashComponentTest class
  26. */
  27. class FlashComponentTest extends TestCase
  28. {
  29. /**
  30. * setUp method
  31. *
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. parent::setUp();
  37. Configure::write('App.namespace', 'TestApp');
  38. $this->Controller = new Controller(new Request(['session' => new Session()]));
  39. $this->ComponentRegistry = new ComponentRegistry($this->Controller);
  40. $this->Flash = new FlashComponent($this->ComponentRegistry);
  41. $this->Session = new Session();
  42. }
  43. /**
  44. * tearDown method
  45. *
  46. * @return void
  47. */
  48. public function tearDown()
  49. {
  50. parent::tearDown();
  51. $this->Session->destroy();
  52. }
  53. /**
  54. * testSet method
  55. *
  56. * @return void
  57. * @covers \Cake\Controller\Component\FlashComponent::set
  58. */
  59. public function testSet()
  60. {
  61. $this->assertNull($this->Session->read('Flash.flash'));
  62. $this->Flash->set('This is a test message');
  63. $expected = [
  64. 'message' => 'This is a test message',
  65. 'key' => 'flash',
  66. 'element' => 'Flash/default',
  67. 'params' => []
  68. ];
  69. $result = $this->Session->read('Flash.flash');
  70. $this->assertEquals($expected, $result);
  71. $this->Flash->set('This is a test message', ['element' => 'test', 'params' => ['foo' => 'bar']]);
  72. $expected = [
  73. 'message' => 'This is a test message',
  74. 'key' => 'flash',
  75. 'element' => 'Flash/test',
  76. 'params' => ['foo' => 'bar']
  77. ];
  78. $result = $this->Session->read('Flash.flash');
  79. $this->assertEquals($expected, $result);
  80. $this->Flash->set('This is a test message', ['element' => 'MyPlugin.alert']);
  81. $expected = [
  82. 'message' => 'This is a test message',
  83. 'key' => 'flash',
  84. 'element' => 'MyPlugin.Flash/alert',
  85. 'params' => []
  86. ];
  87. $result = $this->Session->read('Flash.flash');
  88. $this->assertEquals($expected, $result);
  89. $this->Flash->set('This is a test message', ['key' => 'foobar']);
  90. $expected = [
  91. 'message' => 'This is a test message',
  92. 'key' => 'foobar',
  93. 'element' => 'Flash/default',
  94. 'params' => []
  95. ];
  96. $result = $this->Session->read('Flash.foobar');
  97. $this->assertEquals($expected, $result);
  98. }
  99. /**
  100. * testSetWithException method
  101. *
  102. * @return void
  103. * @covers \Cake\Controller\Component\FlashComponent::set
  104. */
  105. public function testSetWithException()
  106. {
  107. $this->assertNull($this->Session->read('Flash.flash'));
  108. $this->Flash->set(new \Exception('This is a test message', 404));
  109. $expected = [
  110. 'message' => 'This is a test message',
  111. 'key' => 'flash',
  112. 'element' => 'Flash/default',
  113. 'params' => ['code' => 404]
  114. ];
  115. $result = $this->Session->read('Flash.flash');
  116. $this->assertEquals($expected, $result);
  117. }
  118. /**
  119. * testSetWithComponentConfiguration method
  120. *
  121. * @return void
  122. */
  123. public function testSetWithComponentConfiguration()
  124. {
  125. $this->assertNull($this->Session->read('Flash.flash'));
  126. $this->Controller->loadComponent('Flash', ['element' => 'test']);
  127. $this->Controller->Flash->set('This is a test message');
  128. $expected = [
  129. 'message' => 'This is a test message',
  130. 'key' => 'flash',
  131. 'element' => 'Flash/test',
  132. 'params' => []
  133. ];
  134. $result = $this->Session->read('Flash.flash');
  135. $this->assertEquals($expected, $result);
  136. }
  137. /**
  138. * Test magic call method.
  139. *
  140. * @covers \Cake\Controller\Component\FlashComponent::__call
  141. * @return void
  142. */
  143. public function testCall()
  144. {
  145. $this->assertNull($this->Session->read('Flash.flash'));
  146. $this->Flash->success('It worked');
  147. $expected = [
  148. 'message' => 'It worked',
  149. 'key' => 'flash',
  150. 'element' => 'Flash/success',
  151. 'params' => []
  152. ];
  153. $result = $this->Session->read('Flash.flash');
  154. $this->assertEquals($expected, $result);
  155. $this->Flash->error('It did not work', ['element' => 'error_thing']);
  156. $expected = [
  157. 'message' => 'It did not work',
  158. 'key' => 'flash',
  159. 'element' => 'Flash/error',
  160. 'params' => []
  161. ];
  162. $result = $this->Session->read('Flash.flash');
  163. $this->assertEquals($expected, $result, 'Element is ignored in magic call.');
  164. $this->Flash->success('It worked', ['plugin' => 'MyPlugin']);
  165. $expected = [
  166. 'message' => 'It worked',
  167. 'key' => 'flash',
  168. 'element' => 'MyPlugin.Flash/success',
  169. 'params' => []
  170. ];
  171. $result = $this->Session->read('Flash.flash');
  172. $this->assertEquals($expected, $result);
  173. }
  174. }