3589ec58bd819b57044fa991bc31526fd658b070
[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 /**
25 * Test case
26 */
27 class ResultTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
28
29 /**
30 * @var \TYPO3\CMS\Extbase\Error\Result
31 */
32 protected $result;
33
34 public function setUp() {
35 $this->result = new \TYPO3\CMS\Extbase\Error\Result();
36 }
37
38 /**
39 * @return array
40 */
41 public function dataTypes() {
42 return array(
43 array('Error', 'Errors'),
44 array('Warning', 'Warnings'),
45 array('Notice', 'Notices')
46 );
47 }
48
49 /**
50 * @param string $type
51 * @return \PHPUnit_Framework_MockObject_MockObject
52 */
53 protected function getMockMessage($type) {
54 return $this->getMock('Tx_Extbase_Error_' . $type, array(), array(), '', FALSE);
55 }
56
57 /**
58 * @test
59 * @dataProvider dataTypes
60 * @author Sebastian Kurfürst <sebastian@typo3.org>
61 * @param string $dataTypeInSingular
62 * @param string $dataTypeInPlural
63 */
64 public function addedMessagesShouldBeRetrievableAgain($dataTypeInSingular, $dataTypeInPlural) {
65 $message = $this->getMockMessage($dataTypeInSingular);
66 $addMethodName = 'add' . $dataTypeInSingular;
67 $this->result->{$addMethodName}($message);
68 $getterMethodName = 'get' . $dataTypeInPlural;
69 $this->assertEquals(array($message), $this->result->{$getterMethodName}());
70 }
71
72 /**
73 * @test
74 * @dataProvider dataTypes
75 * @author Sebastian Kurfürst <sebastian@typo3.org>
76 * @param string $dataTypeInSingular
77 * @param string $dataTypeInPlural
78 */
79 public function getMessageShouldNotBeRecursive($dataTypeInSingular, $dataTypeInPlural) {
80 $message = $this->getMockMessage($dataTypeInSingular);
81 $addMethodName = 'add' . $dataTypeInSingular;
82 $this->result->forProperty('foo')->{$addMethodName}($message);
83 $getterMethodName = 'get' . $dataTypeInPlural;
84 $this->assertEquals(array(), $this->result->{$getterMethodName}());
85 }
86
87 /**
88 * @test
89 * @dataProvider dataTypes
90 * @author Sebastian Kurfürst <sebastian@typo3.org>
91 * @param string $dataTypeInSingular
92 * @param string $dataTypeInPlural
93 */
94 public function getFirstMessageShouldReturnFirstMessage($dataTypeInSingular, $dataTypeInPlural) {
95 $message1 = $this->getMockMessage($dataTypeInSingular);
96 $message2 = $this->getMockMessage($dataTypeInSingular);
97 $addMethodName = 'add' . $dataTypeInSingular;
98 $this->result->{$addMethodName}($message1);
99 $this->result->{$addMethodName}($message2);
100 $getterMethodName = 'getFirst' . $dataTypeInSingular;
101 $this->assertSame($message1, $this->result->{$getterMethodName}());
102 }
103
104 /**
105 * @test
106 * @author Sebastian Kurfürst <sebastian@typo3.org>
107 */
108 public function forPropertyShouldReturnSubResult() {
109 $container2 = $this->result->forProperty('foo.bar');
110 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Error\\Result', $container2);
111 $this->assertSame($container2, $this->result->forProperty('foo')->forProperty('bar'));
112 }
113
114 /**
115 * @test
116 * @author Sebastian Kurfürst <sebastian@typo3.org>
117 */
118 public function forPropertyWithEmptyStringShouldReturnSelf() {
119 $container2 = $this->result->forProperty('');
120 $this->assertSame($container2, $this->result);
121 }
122
123 /**
124 * @test
125 * @author Sebastian Kurfürst <sebastian@typo3.org>
126 */
127 public function forPropertyWithNullShouldReturnSelf() {
128 $container2 = $this->result->forProperty(NULL);
129 $this->assertSame($container2, $this->result);
130 }
131
132 /**
133 * @test
134 * @dataProvider dataTypes
135 * @author Sebastian Kurfürst <sebastian@typo3.org>
136 * @param string $dataTypeInSingular
137 * @param string $dataTypeInPlural
138 */
139 public function hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages($dataTypeInSingular, $dataTypeInPlural) {
140 $message = $this->getMockMessage($dataTypeInSingular);
141 $addMethodName = 'add' . $dataTypeInSingular;
142 $this->result->{$addMethodName}($message);
143 $methodName = 'has' . $dataTypeInPlural;
144 $this->assertTrue($this->result->{$methodName}());
145 }
146
147 /**
148 * @test
149 * @dataProvider dataTypes
150 * @author Sebastian Kurfürst <sebastian@typo3.org>
151 * @param string $dataTypeInSingular
152 * @param string $dataTypeInPlural
153 */
154 public function hasMessagesShouldReturnTrueIfSubObjectHasErrors($dataTypeInSingular, $dataTypeInPlural) {
155 $addMethodName = 'add' . $dataTypeInSingular;
156 $methodName = 'has' . $dataTypeInPlural;
157 $message = $this->getMockMessage($dataTypeInSingular);
158 $this->result->forProperty('foo.bar')->{$addMethodName}($message);
159 $this->assertTrue($this->result->{$methodName}());
160 }
161
162 /**
163 * @test
164 * @dataProvider dataTypes
165 * @author Sebastian Kurfürst <sebastian@typo3.org>
166 * @param string $dataTypeInSingular
167 * @param string $dataTypeInPlural
168 */
169 public function hasMessagesShouldReturnFalseIfSubObjectHasNoErrors($dataTypeInSingular, $dataTypeInPlural) {
170 $methodName = 'has' . $dataTypeInPlural;
171 $this->result->forProperty('foo.baz');
172 $this->result->forProperty('foo.bar');
173 $this->assertFalse($this->result->{$methodName}());
174 }
175
176 /**
177 * @test
178 * @dataProvider dataTypes
179 * @author Sebastian Kurfürst <sebastian@typo3.org>
180 * @param string $dataTypeInSingular
181 * @param string $dataTypeInPlural
182 */
183 public function getFlattenedMessagesShouldReturnAllSubMessages($dataTypeInSingular, $dataTypeInPlural) {
184 $message1 = $this->getMockMessage($dataTypeInSingular);
185 $message2 = $this->getMockMessage($dataTypeInSingular);
186 $message3 = $this->getMockMessage($dataTypeInSingular);
187 $message4 = $this->getMockMessage($dataTypeInSingular);
188 $message5 = $this->getMockMessage($dataTypeInSingular);
189 $addMethodName = 'add' . $dataTypeInSingular;
190 $this->result->forProperty('foo.bar')->{$addMethodName}($message1);
191 $this->result->forProperty('foo.baz')->{$addMethodName}($message2);
192 $this->result->forProperty('foo')->{$addMethodName}($message3);
193 $this->result->{$addMethodName}($message4);
194 $this->result->{$addMethodName}($message5);
195 $getMethodName = 'getFlattened' . $dataTypeInPlural;
196 $expected = array(
197 '' => array($message4, $message5),
198 'foo' => array($message3),
199 'foo.bar' => array($message1),
200 'foo.baz' => array($message2)
201 );
202 $this->assertEquals($expected, $this->result->{$getMethodName}());
203 }
204
205 /**
206 * @test
207 * @dataProvider dataTypes
208 * @author Sebastian Kurfürst <sebastian@typo3.org>
209 * @param string $dataTypeInSingular
210 * @param string $dataTypeInPlural
211 */
212 public function getFlattenedMessagesShouldNotContainEmptyResults($dataTypeInSingular, $dataTypeInPlural) {
213 $message1 = $this->getMockMessage($dataTypeInSingular);
214 $message2 = $this->getMockMessage($dataTypeInSingular);
215 $addMethodName = 'add' . $dataTypeInSingular;
216 $this->result->forProperty('foo.bar')->{$addMethodName}($message1);
217 $this->result->forProperty('foo.baz')->{$addMethodName}($message2);
218 $getMethodName = 'getFlattened' . $dataTypeInPlural;
219 $expected = array(
220 'foo.bar' => array($message1),
221 'foo.baz' => array($message2)
222 );
223 $this->assertEquals($expected, $this->result->{$getMethodName}());
224 }
225
226 /**
227 * @test
228 * @author Sebastian Kurfürst <sebastian@typo3.org>
229 */
230 public function mergeShouldMergeTwoResults() {
231 $notice1 = $this->getMockMessage('Notice');
232 $notice2 = $this->getMockMessage('Notice');
233 $notice3 = $this->getMockMessage('Notice');
234 $warning1 = $this->getMockMessage('Warning');
235 $warning2 = $this->getMockMessage('Warning');
236 $warning3 = $this->getMockMessage('Warning');
237 $error1 = $this->getMockMessage('Error');
238 $error2 = $this->getMockMessage('Error');
239 $error3 = $this->getMockMessage('Error');
240 $otherResult = new \TYPO3\CMS\Extbase\Error\Result();
241 $otherResult->addNotice($notice1);
242 $otherResult->forProperty('foo.bar')->addNotice($notice2);
243 $this->result->forProperty('foo')->addNotice($notice3);
244 $otherResult->addWarning($warning1);
245 $this->result->addWarning($warning2);
246 $this->result->addWarning($warning3);
247 $otherResult->forProperty('foo')->addError($error1);
248 $otherResult->forProperty('foo')->addError($error2);
249 $otherResult->addError($error3);
250 $this->result->merge($otherResult);
251 $this->assertSame(array($notice1), $this->result->getNotices(), 'Notices are not merged correctly without recursion');
252 $this->assertSame(array($notice3), $this->result->forProperty('foo')->getNotices(), 'Original sub-notices are overridden.');
253 $this->assertSame(array($notice2), $this->result->forProperty('foo')->forProperty('bar')->getNotices(), 'Sub-notices are not copied.');
254 $this->assertSame(array($warning2, $warning3, $warning1), $this->result->getWarnings());
255 $this->assertSame(array($error3), $this->result->getErrors());
256 $this->assertSame(array($error1, $error2), $this->result->forProperty('foo')->getErrors());
257 }
258 }