TYPO3 CMS  TYPO3_6-2
ResultTest.php
Go to the documentation of this file.
1 <?php
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 
28 
32  protected $result;
33 
34  public function setUp() {
35  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
36  }
37 
41  public function dataTypes() {
42  return array(
43  array('Error', 'Errors'),
44  array('Warning', 'Warnings'),
45  array('Notice', 'Notices')
46  );
47  }
48 
53  protected function getMockMessage($type) {
54  return $this->getMock('Tx_Extbase_Error_' . $type, array(), array(), '', FALSE);
55  }
56 
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 
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 
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 
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 
119  $container2 = $this->result->forProperty('');
120  $this->assertSame($container2, $this->result);
121  }
122 
128  $container2 = $this->result->forProperty(NULL);
129  $this->assertSame($container2, $this->result);
130  }
131 
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 
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 
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 
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 
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 
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 }
getFlattenedMessagesShouldReturnAllSubMessages($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:183
getMessageShouldNotBeRecursive($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:79
getFlattenedMessagesShouldNotContainEmptyResults($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:212
addedMessagesShouldBeRetrievableAgain($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:64
hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:139
hasMessagesShouldReturnTrueIfSubObjectHasErrors($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:154
getFirstMessageShouldReturnFirstMessage($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:94
hasMessagesShouldReturnFalseIfSubObjectHasNoErrors($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:169