FlashComponentTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. Configure::write('App.namespace', 'TestApp');
  36. $controller = new Controller(new Request(['session' => new Session()]));
  37. $this->ComponentRegistry = new ComponentRegistry($controller);
  38. $this->Flash = new FlashComponent($this->ComponentRegistry);
  39. $this->Session = new Session();
  40. }
  41. /**
  42. * tearDown method
  43. *
  44. * @return void
  45. */
  46. public function tearDown() {
  47. parent::tearDown();
  48. $this->Session->destroy();
  49. }
  50. /**
  51. * testSet method
  52. *
  53. * @return void
  54. * @covers \Cake\Controller\Component\FlashComponent::set
  55. */
  56. public function testSet() {
  57. $this->assertNull($this->Session->read('Flash.flash'));
  58. $this->Flash->set('This is a test message');
  59. $expected = [
  60. 'message' => 'This is a test message',
  61. 'key' => 'flash',
  62. 'element' => null,
  63. 'params' => []
  64. ];
  65. $result = $this->Session->read('Flash.flash');
  66. $this->assertEquals($expected, $result);
  67. $this->Flash->set('This is a test message', ['element' => 'test', 'params' => ['foo' => 'bar']]);
  68. $expected = [
  69. 'message' => 'This is a test message',
  70. 'key' => 'flash',
  71. 'element' => 'Flash/test',
  72. 'params' => ['foo' => 'bar']
  73. ];
  74. $result = $this->Session->read('Flash.flash');
  75. $this->assertEquals($expected, $result);
  76. $this->Flash->set('This is a test message', ['element' => 'MyPlugin.alert']);
  77. $expected = [
  78. 'message' => 'This is a test message',
  79. 'key' => 'flash',
  80. 'element' => 'MyPlugin.Flash/alert',
  81. 'params' => []
  82. ];
  83. $result = $this->Session->read('Flash.flash');
  84. $this->assertEquals($expected, $result);
  85. $this->Flash->set('This is a test message', ['key' => 'foobar']);
  86. $expected = [
  87. 'message' => 'This is a test message',
  88. 'key' => 'foobar',
  89. 'element' => null,
  90. 'params' => []
  91. ];
  92. $result = $this->Session->read('Flash.foobar');
  93. $this->assertEquals($expected, $result);
  94. }
  95. /**
  96. * testSetWithException method
  97. *
  98. * @return void
  99. * @covers \Cake\Controller\Component\FlashComponent::set
  100. */
  101. public function testSetWithException() {
  102. $this->assertNull($this->Session->read('Flash.flash'));
  103. $this->Flash->set(new \Exception('This is a test message'));
  104. $expected = [
  105. 'message' => 'This is a test message',
  106. 'key' => 'flash',
  107. 'element' => null,
  108. 'params' => []
  109. ];
  110. $result = $this->Session->read('Flash.flash');
  111. $this->assertEquals($expected, $result);
  112. }
  113. }