FlashComponentTest.php 3.3 KB

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