FlashComponentTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. [
  65. 'message' => 'This is a test message',
  66. 'key' => 'flash',
  67. 'element' => 'Flash/default',
  68. 'params' => []
  69. ]
  70. ];
  71. $result = $this->Session->read('Flash.flash');
  72. $this->assertEquals($expected, $result);
  73. $this->Flash->set('This is a test message', ['element' => 'test', 'params' => ['foo' => 'bar']]);
  74. $expected[] = [
  75. 'message' => 'This is a test message',
  76. 'key' => 'flash',
  77. 'element' => 'Flash/test',
  78. 'params' => ['foo' => 'bar']
  79. ];
  80. $result = $this->Session->read('Flash.flash');
  81. $this->assertEquals($expected, $result);
  82. $this->Flash->set('This is a test message', ['element' => 'MyPlugin.alert']);
  83. $expected[] = [
  84. 'message' => 'This is a test message',
  85. 'key' => 'flash',
  86. 'element' => 'MyPlugin.Flash/alert',
  87. 'params' => []
  88. ];
  89. $result = $this->Session->read('Flash.flash');
  90. $this->assertEquals($expected, $result);
  91. $this->Flash->set('This is a test message', ['key' => 'foobar']);
  92. $expected = [
  93. [
  94. 'message' => 'This is a test message',
  95. 'key' => 'foobar',
  96. 'element' => 'Flash/default',
  97. 'params' => []
  98. ]
  99. ];
  100. $result = $this->Session->read('Flash.foobar');
  101. $this->assertEquals($expected, $result);
  102. }
  103. /**
  104. * test setting messages with using the clear option
  105. *
  106. * @return void
  107. * @covers \Cake\Controller\Component\FlashComponent::set
  108. */
  109. public function testSetWithClear()
  110. {
  111. $this->assertNull($this->Session->read('Flash.flash'));
  112. $this->Flash->set('This is a test message');
  113. $expected = [
  114. [
  115. 'message' => 'This is a test message',
  116. 'key' => 'flash',
  117. 'element' => 'Flash/default',
  118. 'params' => []
  119. ]
  120. ];
  121. $result = $this->Session->read('Flash.flash');
  122. $this->assertEquals($expected, $result);
  123. $this->Flash->set('This is another test message', ['clear' => true]);
  124. $expected = [
  125. [
  126. 'message' => 'This is another test message',
  127. 'key' => 'flash',
  128. 'element' => 'Flash/default',
  129. 'params' => []
  130. ]
  131. ];
  132. $result = $this->Session->read('Flash.flash');
  133. $this->assertEquals($expected, $result);
  134. }
  135. /**
  136. * testSetWithException method
  137. *
  138. * @return void
  139. * @covers \Cake\Controller\Component\FlashComponent::set
  140. */
  141. public function testSetWithException()
  142. {
  143. $this->assertNull($this->Session->read('Flash.flash'));
  144. $this->Flash->set(new \Exception('This is a test message', 404));
  145. $expected = [
  146. [
  147. 'message' => 'This is a test message',
  148. 'key' => 'flash',
  149. 'element' => 'Flash/default',
  150. 'params' => ['code' => 404]
  151. ]
  152. ];
  153. $result = $this->Session->read('Flash.flash');
  154. $this->assertEquals($expected, $result);
  155. }
  156. /**
  157. * testSetWithComponentConfiguration method
  158. *
  159. * @return void
  160. */
  161. public function testSetWithComponentConfiguration()
  162. {
  163. $this->assertNull($this->Session->read('Flash.flash'));
  164. $this->Controller->loadComponent('Flash', ['element' => 'test']);
  165. $this->Controller->Flash->set('This is a test message');
  166. $expected = [
  167. [
  168. 'message' => 'This is a test message',
  169. 'key' => 'flash',
  170. 'element' => 'Flash/test',
  171. 'params' => []
  172. ]
  173. ];
  174. $result = $this->Session->read('Flash.flash');
  175. $this->assertEquals($expected, $result);
  176. }
  177. /**
  178. * Test magic call method.
  179. *
  180. * @covers \Cake\Controller\Component\FlashComponent::__call
  181. * @return void
  182. */
  183. public function testCall()
  184. {
  185. $this->assertNull($this->Session->read('Flash.flash'));
  186. $this->Flash->success('It worked');
  187. $expected = [
  188. [
  189. 'message' => 'It worked',
  190. 'key' => 'flash',
  191. 'element' => 'Flash/success',
  192. 'params' => []
  193. ]
  194. ];
  195. $result = $this->Session->read('Flash.flash');
  196. $this->assertEquals($expected, $result);
  197. $this->Flash->error('It did not work', ['element' => 'error_thing']);
  198. $expected[] = [
  199. 'message' => 'It did not work',
  200. 'key' => 'flash',
  201. 'element' => 'Flash/error',
  202. 'params' => []
  203. ];
  204. $result = $this->Session->read('Flash.flash');
  205. $this->assertEquals($expected, $result, 'Element is ignored in magic call.');
  206. $this->Flash->success('It worked', ['plugin' => 'MyPlugin']);
  207. $expected[] = [
  208. 'message' => 'It worked',
  209. 'key' => 'flash',
  210. 'element' => 'MyPlugin.Flash/success',
  211. 'params' => []
  212. ];
  213. $result = $this->Session->read('Flash.flash');
  214. $this->assertEquals($expected, $result);
  215. }
  216. /**
  217. * Test a magic call with the "clear" flag to true
  218. *
  219. * @return void
  220. * @covers \Cake\Controller\Component\FlashComponent::set
  221. */
  222. public function testCallWithClear()
  223. {
  224. $this->assertNull($this->Session->read('Flash.flash'));
  225. $this->Flash->success('It worked');
  226. $expected = [
  227. [
  228. 'message' => 'It worked',
  229. 'key' => 'flash',
  230. 'element' => 'Flash/success',
  231. 'params' => []
  232. ]
  233. ];
  234. $result = $this->Session->read('Flash.flash');
  235. $this->assertEquals($expected, $result);
  236. $this->Flash->success('It worked too', ['clear' => true]);
  237. $expected = [
  238. [
  239. 'message' => 'It worked too',
  240. 'key' => 'flash',
  241. 'element' => 'Flash/success',
  242. 'params' => []
  243. ]
  244. ];
  245. $result = $this->Session->read('Flash.flash');
  246. $this->assertEquals($expected, $result);
  247. }
  248. }