[TASK] Namespace classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Error / ResultTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Error;
3
4 /* *
5 * This script belongs to the Extbase framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23 /**
24 * Testcase for the Error Container object
25 *
26 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
27 */
28 class ResultTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
29
30 /**
31 * @var \TYPO3\CMS\Extbase\Error\Result
32 */
33 protected $result;
34
35 public function setUp() {
36 $this->result = new \TYPO3\CMS\Extbase\Error\Result();
37 }
38
39 /**
40 * @return array
41 */
42 public function dataTypes() {
43 return array(
44 array('Error', 'Errors'),
45 array('Warning', 'Warnings'),
46 array('Notice', 'Notices')
47 );
48 }
49
50 /**
51 * @param string $type
52 * @return PHPUnit_Framework_MockObject_MockObject
53 */
54 protected function getMockMessage($type) {
55 return $this->getMock('Tx_Extbase_Error_' . $type, array(), array(), '', FALSE);
56 }
57
58 /**
59 * @test
60 * @dataProvider dataTypes
61 * @author Sebastian Kurfürst <sebastian@typo3.org>
62 * @param string $dataTypeInSingular
63 * @param string $dataTypeInPlural
64 */
65 public function addedMessagesShouldBeRetrievableAgain($dataTypeInSingular, $dataTypeInPlural) {
66 $message = $this->getMockMessage($dataTypeInSingular);
67 $addMethodName = 'add' . $dataTypeInSingular;
68 $this->result->{$addMethodName}($message);
69 $getterMethodName = 'get' . $dataTypeInPlural;
70 $this->assertEquals(array($message), $this->result->{$getterMethodName}());
71 }
72
73 /**
74 * @test
75 * @dataProvider dataTypes
76 * @author Sebastian Kurfürst <sebastian@typo3.org>
77 * @param string $dataTypeInSingular
78 * @param string $dataTypeInPlural
79 */
80 public function getMessageShouldNotBeRecursive($dataTypeInSingular, $dataTypeInPlural) {
81 $message = $this->getMockMessage($dataTypeInSingular);
82 $addMethodName = 'add' . $dataTypeInSingular;
83 $this->result->forProperty('foo')->{$addMethodName}($message);
84 $getterMethodName = 'get' . $dataTypeInPlural;
85 $this->assertEquals(array(), $this->result->{$getterMethodName}());
86 }
87
88 /**
89 * @test
90 * @dataProvider dataTypes
91 * @author Sebastian Kurfürst <sebastian@typo3.org>
92 * @param string $dataTypeInSingular
93 * @param string $dataTypeInPlural
94 */
95 public function getFirstMessageShouldReturnFirstMessage($dataTypeInSingular, $dataTypeInPlural) {
96 $message1 = $this->getMockMessage($dataTypeInSingular);
97 $message2 = $this->getMockMessage($dataTypeInSingular);
98 $addMethodName = 'add' . $dataTypeInSingular;
99 $this->result->{$addMethodName}($message1);
100 $this->result->{$addMethodName}($message2);
101 $getterMethodName = 'getFirst' . $dataTypeInSingular;
102 $this->assertSame($message1, $this->result->{$getterMethodName}());
103 }
104
105 /**
106 * @test
107 * @author Sebastian Kurfürst <sebastian@typo3.org>
108 */
109 public function forPropertyShouldReturnSubResult() {
110 $container2 = $this->result->forProperty('foo.bar');
111 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Error\\Result', $container2);
112 $this->assertSame($container2, $this->result->forProperty('foo')->forProperty('bar'));
113 }
114
115 /**
116 * @test
117 * @author Sebastian Kurfürst <sebastian@typo3.org>
118 */
119 public function forPropertyWithEmptyStringShouldReturnSelf() {
120 $container2 = $this->result->forProperty('');
121 $this->assertSame($container2, $this->result);
122 }
123
124 /**
125 * @test
126 * @author Sebastian Kurfürst <sebastian@typo3.org>
127 */
128 public function forPropertyWithNullShouldReturnSelf() {
129 $container2 = $this->result->forProperty(NULL);
130 $this->assertSame($container2, $this->result);
131 }
132
133 /**
134 * @test
135 * @dataProvider dataTypes
136 * @author Sebastian Kurfürst <sebastian@typo3.org>
137 * @param string $dataTypeInSingular
138 * @param string $dataTypeInPlural
139 */
140 public function hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages($dataTypeInSingular, $dataTypeInPlural) {
141 $message = $this->getMockMessage($dataTypeInSingular);
142 $addMethodName = 'add' . $dataTypeInSingular;
143 $this->result->{$addMethodName}($message);
144 $methodName = 'has' . $dataTypeInPlural;
145 $this->assertTrue($this->result->{$methodName}());
146 }
147
148 /**
149 * @test
150 * @dataProvider dataTypes
151 * @author Sebastian Kurfürst <sebastian@typo3.org>
152 * @param string $dataTypeInSingular
153 * @param string $dataTypeInPlural
154 */
155 public function hasMessageshouldReturnTrueIfSubObjectHasErrors($dataTypeInSingular, $dataTypeInPlural) {
156 $addMethodName = 'add' . $dataTypeInSingular;
157 $methodName = 'has' . $dataTypeInPlural;
158 $message = $this->getMockMessage($dataTypeInSingular);
159 $this->result->forProperty('foo.bar')->{$addMethodName}($message);
160 $this->assertTrue($this->result->{$methodName}());
161 }
162
163 /**
164 * @test
165 * @dataProvider dataTypes
166 * @author Sebastian Kurfürst <sebastian@typo3.org>
167 * @param string $dataTypeInSingular
168 * @param string $dataTypeInPlural
169 */
170 public function hasMessagesShouldReturnFalseIfSubObjectHasNoErrors($dataTypeInSingular, $dataTypeInPlural) {
171 $methodName = 'has' . $dataTypeInPlural;
172 $this->result->forProperty('foo.baz');
173 $this->result->forProperty('foo.bar');
174 $this->assertFalse($this->result->{$methodName}());
175 }
176
177 /**
178 * @test
179 * @dataProvider dataTypes
180 * @author Sebastian Kurfürst <sebastian@typo3.org>
181 * @param string $dataTypeInSingular
182 * @param string $dataTypeInPlural
183 */
184 public function getFlattenedMessagesShouldReturnAllSubMessages($dataTypeInSingular, $dataTypeInPlural) {
185 $message1 = $this->getMockMessage($dataTypeInSingular);
186 $message2 = $this->getMockMessage($dataTypeInSingular);
187 $message3 = $this->getMockMessage($dataTypeInSingular);
188 $message4 = $this->getMockMessage($dataTypeInSingular);
189 $message5 = $this->getMockMessage($dataTypeInSingular);
190 $addMethodName = 'add' . $dataTypeInSingular;
191 $this->result->forProperty('foo.bar')->{$addMethodName}($message1);
192 $this->result->forProperty('foo.baz')->{$addMethodName}($message2);
193 $this->result->forProperty('foo')->{$addMethodName}($message3);
194 $this->result->{$addMethodName}($message4);
195 $this->result->{$addMethodName}($message5);
196 $getMethodName = 'getFlattened' . $dataTypeInPlural;
197 $expected = array(
198 '' => array($message4, $message5),
199 'foo' => array($message3),
200 'foo.bar' => array($message1),
201 'foo.baz' => array($message2)
202 );
203 $this->assertEquals($expected, $this->result->{$getMethodName}());
204 }
205
206 /**
207 * @test
208 * @dataProvider dataTypes
209 * @author Sebastian Kurfürst <sebastian@typo3.org>
210 * @param string $dataTypeInSingular
211 * @param string $dataTypeInPlural
212 */
213 public function getFlattenedMessagesShouldNotContainEmptyResults($dataTypeInSingular, $dataTypeInPlural) {
214 $message1 = $this->getMockMessage($dataTypeInSingular);
215 $message2 = $this->getMockMessage($dataTypeInSingular);
216 $addMethodName = 'add' . $dataTypeInSingular;
217 $this->result->forProperty('foo.bar')->{$addMethodName}($message1);
218 $this->result->forProperty('foo.baz')->{$addMethodName}($message2);
219 $getMethodName = 'getFlattened' . $dataTypeInPlural;
220 $expected = array(
221 'foo.bar' => array($message1),
222 'foo.baz' => array($message2)
223 );
224 $this->assertEquals($expected, $this->result->{$getMethodName}());
225 }
226
227 /**
228 * @test
229 * @author Sebastian Kurfürst <sebastian@typo3.org>
230 */
231 public function mergeShouldMergeTwoResults() {
232 $notice1 = $this->getMockMessage('Notice');
233 $notice2 = $this->getMockMessage('Notice');
234 $notice3 = $this->getMockMessage('Notice');
235 $warning1 = $this->getMockMessage('Warning');
236 $warning2 = $this->getMockMessage('Warning');
237 $warning3 = $this->getMockMessage('Warning');
238 $error1 = $this->getMockMessage('Error');
239 $error2 = $this->getMockMessage('Error');
240 $error3 = $this->getMockMessage('Error');
241 $otherResult = new \TYPO3\CMS\Extbase\Error\Result();
242 $otherResult->addNotice($notice1);
243 $otherResult->forProperty('foo.bar')->addNotice($notice2);
244 $this->result->forProperty('foo')->addNotice($notice3);
245 $otherResult->addWarning($warning1);
246 $this->result->addWarning($warning2);
247 $this->result->addWarning($warning3);
248 $otherResult->forProperty('foo')->addError($error1);
249 $otherResult->forProperty('foo')->addError($error2);
250 $otherResult->addError($error3);
251 $this->result->merge($otherResult);
252 $this->assertSame(array($notice1), $this->result->getNotices(), 'Notices are not merged correctly without recursion');
253 $this->assertSame(array($notice3), $this->result->forProperty('foo')->getNotices(), 'Original sub-notices are overridden.');
254 $this->assertSame(array($notice2), $this->result->forProperty('foo')->forProperty('bar')->getNotices(), 'Sub-notices are not copied.');
255 $this->assertSame(array($warning2, $warning3, $warning1), $this->result->getWarnings());
256 $this->assertSame(array($error3), $this->result->getErrors());
257 $this->assertSame(array($error1, $error2), $this->result->forProperty('foo')->getErrors());
258 }
259
260 }
261
262
263 ?>