pictcode / lib / Cake / Test / Case / Controller / Component / FlashComponentTest.php @ 635eef61
履歴 | 表示 | アノテート | ダウンロード (4.748 KB)
1 | 635eef61 | spyder1211 | <?php
|
---|---|---|---|
2 | /**
|
||
3 | * FlashComponentTest file
|
||
4 | *
|
||
5 | * Series of tests for flash component.
|
||
6 | *
|
||
7 | * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
||
8 | * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||
9 | *
|
||
10 | * Licensed under The MIT License
|
||
11 | * For full copyright and license information, please see the LICENSE.txt
|
||
12 | * Redistributions of files must retain the above copyright notice
|
||
13 | *
|
||
14 | * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||
15 | * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
||
16 | * @package Cake.Test.Case.Controller.Component
|
||
17 | * @since CakePHP(tm) v 2.7.0-dev
|
||
18 | * @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||
19 | */
|
||
20 | |||
21 | App::uses('FlashComponent', 'Controller/Component'); |
||
22 | App::uses('ComponentCollection', 'Controller'); |
||
23 | |||
24 | /**
|
||
25 | * FlashComponentTest class
|
||
26 | *
|
||
27 | * @package Cake.Test.Case.Controller.Component
|
||
28 | */
|
||
29 | class FlashComponentTest extends CakeTestCase { |
||
30 | |||
31 | /**
|
||
32 | * setUp method
|
||
33 | *
|
||
34 | * @return void
|
||
35 | */
|
||
36 | public function setUp() { |
||
37 | parent::setUp();
|
||
38 | $this->Components = new ComponentCollection(); |
||
39 | $this->Flash = new FlashComponent($this->Components); |
||
40 | } |
||
41 | |||
42 | /**
|
||
43 | * tearDown method
|
||
44 | *
|
||
45 | * @return void
|
||
46 | */
|
||
47 | public function tearDown() { |
||
48 | parent::tearDown();
|
||
49 | CakeSession::destroy();
|
||
50 | } |
||
51 | |||
52 | /**
|
||
53 | * testSet method
|
||
54 | *
|
||
55 | * @return void
|
||
56 | */
|
||
57 | public function testSet() { |
||
58 | $this->assertNull(CakeSession::read('Message.flash')); |
||
59 | |||
60 | $this->Flash->set('This is a test message'); |
||
61 | $expected = array( |
||
62 | 'message' => 'This is a test message', |
||
63 | 'key' => 'flash', |
||
64 | 'element' => 'Flash/default', |
||
65 | 'params' => array() |
||
66 | ); |
||
67 | $result = CakeSession::read('Message.flash'); |
||
68 | $this->assertEquals($expected, $result); |
||
69 | |||
70 | $this->Flash->set('This is a test message', array( |
||
71 | 'element' => 'test', |
||
72 | 'params' => array('foo' => 'bar') |
||
73 | )); |
||
74 | $expected = array( |
||
75 | 'message' => 'This is a test message', |
||
76 | 'key' => 'flash', |
||
77 | 'element' => 'Flash/test', |
||
78 | 'params' => array('foo' => 'bar') |
||
79 | ); |
||
80 | $result = CakeSession::read('Message.flash'); |
||
81 | $this->assertEquals($expected, $result); |
||
82 | |||
83 | $this->Flash->set('This is a test message', array('element' => 'MyPlugin.alert')); |
||
84 | $expected = array( |
||
85 | 'message' => 'This is a test message', |
||
86 | 'key' => 'flash', |
||
87 | 'element' => 'MyPlugin.Flash/alert', |
||
88 | 'params' => array() |
||
89 | ); |
||
90 | $result = CakeSession::read('Message.flash'); |
||
91 | $this->assertEquals($expected, $result); |
||
92 | |||
93 | $this->Flash->set('This is a test message', array('key' => 'foobar')); |
||
94 | $expected = array( |
||
95 | 'message' => 'This is a test message', |
||
96 | 'key' => 'foobar', |
||
97 | 'element' => 'Flash/default', |
||
98 | 'params' => array() |
||
99 | ); |
||
100 | $result = CakeSession::read('Message.foobar'); |
||
101 | $this->assertEquals($expected, $result); |
||
102 | } |
||
103 | |||
104 | /**
|
||
105 | * testSetWithException method
|
||
106 | *
|
||
107 | * @return void
|
||
108 | */
|
||
109 | public function testSetWithException() { |
||
110 | $this->assertNull(CakeSession::read('Message.flash')); |
||
111 | |||
112 | $this->Flash->set(new Exception('This is a test message', 404)); |
||
113 | $expected = array( |
||
114 | 'message' => 'This is a test message', |
||
115 | 'key' => 'flash', |
||
116 | 'element' => 'Flash/default', |
||
117 | 'params' => array('code' => 404) |
||
118 | ); |
||
119 | $result = CakeSession::read('Message.flash'); |
||
120 | $this->assertEquals($expected, $result); |
||
121 | } |
||
122 | |||
123 | /**
|
||
124 | * testSetWithComponentConfiguration method
|
||
125 | *
|
||
126 | * @return void
|
||
127 | */
|
||
128 | public function testSetWithComponentConfiguration() { |
||
129 | $this->assertNull(CakeSession::read('Message.flash')); |
||
130 | |||
131 | $FlashWithSettings = $this->Components->load('Flash', array('element' => 'test')); |
||
132 | $FlashWithSettings->set('This is a test message'); |
||
133 | $expected = array( |
||
134 | 'message' => 'This is a test message', |
||
135 | 'key' => 'flash', |
||
136 | 'element' => 'Flash/test', |
||
137 | 'params' => array() |
||
138 | ); |
||
139 | $result = CakeSession::read('Message.flash'); |
||
140 | $this->assertEquals($expected, $result); |
||
141 | } |
||
142 | |||
143 | /**
|
||
144 | * Test magic call method.
|
||
145 | *
|
||
146 | * @return void
|
||
147 | */
|
||
148 | public function testCall() { |
||
149 | $this->assertNull(CakeSession::read('Message.flash')); |
||
150 | |||
151 | $this->Flash->success('It worked'); |
||
152 | $expected = array( |
||
153 | 'message' => 'It worked', |
||
154 | 'key' => 'flash', |
||
155 | 'element' => 'Flash/success', |
||
156 | 'params' => array() |
||
157 | ); |
||
158 | $result = CakeSession::read('Message.flash'); |
||
159 | $this->assertEquals($expected, $result); |
||
160 | |||
161 | $this->Flash->alert('It worked', array('plugin' => 'MyPlugin')); |
||
162 | $expected = array( |
||
163 | 'message' => 'It worked', |
||
164 | 'key' => 'flash', |
||
165 | 'element' => 'MyPlugin.Flash/alert', |
||
166 | 'params' => array() |
||
167 | ); |
||
168 | $result = CakeSession::read('Message.flash'); |
||
169 | $this->assertEquals($expected, $result); |
||
170 | |||
171 | $this->Flash->error('It did not work', array('element' => 'error_thing')); |
||
172 | $expected = array( |
||
173 | 'message' => 'It did not work', |
||
174 | 'key' => 'flash', |
||
175 | 'element' => 'Flash/error', |
||
176 | 'params' => array() |
||
177 | ); |
||
178 | $result = CakeSession::read('Message.flash'); |
||
179 | $this->assertEquals($expected, $result, 'Element is ignored in magic call.'); |
||
180 | } |
||
181 | } |