TYPO3 CMS  TYPO3_7-6
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  protected function setUp()
35  {
36  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
37  }
38 
42  public function dataTypes()
43  {
44  return [
45  ['Error', 'Errors'],
46  ['Warning', 'Warnings'],
47  ['Notice', 'Notices']
48  ];
49  }
50 
55  protected function getMockMessage($type)
56  {
57  return $this->getMock('TYPO3\\CMS\\Extbase\\Error\\' . $type, [], [], '', false);
58  }
59 
66  public function addedMessagesShouldBeRetrievableAgain($dataTypeInSingular, $dataTypeInPlural)
67  {
68  $message = $this->getMockMessage($dataTypeInSingular);
69  $addMethodName = 'add' . $dataTypeInSingular;
70  $this->result->{$addMethodName}($message);
71  $getterMethodName = 'get' . $dataTypeInPlural;
72  $this->assertEquals([$message], $this->result->{$getterMethodName}());
73  }
74 
81  public function getMessageShouldNotBeRecursive($dataTypeInSingular, $dataTypeInPlural)
82  {
83  $message = $this->getMockMessage($dataTypeInSingular);
84  $addMethodName = 'add' . $dataTypeInSingular;
85  $this->result->forProperty('foo')->{$addMethodName}($message);
86  $getterMethodName = 'get' . $dataTypeInPlural;
87  $this->assertEquals([], $this->result->{$getterMethodName}());
88  }
89 
96  public function getFirstMessageShouldReturnFirstMessage($dataTypeInSingular, $dataTypeInPlural)
97  {
98  $message1 = $this->getMockMessage($dataTypeInSingular);
99  $message2 = $this->getMockMessage($dataTypeInSingular);
100  $addMethodName = 'add' . $dataTypeInSingular;
101  $this->result->{$addMethodName}($message1);
102  $this->result->{$addMethodName}($message2);
103  $getterMethodName = 'getFirst' . $dataTypeInSingular;
104  $this->assertSame($message1, $this->result->{$getterMethodName}());
105  }
106 
111  {
112  $container2 = $this->result->forProperty('foo.bar');
113  $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Result::class, $container2);
114  $this->assertSame($container2, $this->result->forProperty('foo')->forProperty('bar'));
115  }
116 
121  {
122  $container2 = $this->result->forProperty('');
123  $this->assertSame($container2, $this->result);
124  }
125 
130  {
131  $container2 = $this->result->forProperty(null);
132  $this->assertSame($container2, $this->result);
133  }
134 
141  public function hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages($dataTypeInSingular, $dataTypeInPlural)
142  {
143  $message = $this->getMockMessage($dataTypeInSingular);
144  $addMethodName = 'add' . $dataTypeInSingular;
145  $this->result->{$addMethodName}($message);
146  $methodName = 'has' . $dataTypeInPlural;
147  $this->assertTrue($this->result->{$methodName}());
148  }
149 
156  public function hasMessagesShouldReturnTrueIfSubObjectHasErrors($dataTypeInSingular, $dataTypeInPlural)
157  {
158  $addMethodName = 'add' . $dataTypeInSingular;
159  $methodName = 'has' . $dataTypeInPlural;
160  $message = $this->getMockMessage($dataTypeInSingular);
161  $this->result->forProperty('foo.bar')->{$addMethodName}($message);
162  $this->assertTrue($this->result->{$methodName}());
163  }
164 
171  public function hasMessagesShouldReturnFalseIfSubObjectHasNoErrors($dataTypeInSingular, $dataTypeInPlural)
172  {
173  $methodName = 'has' . $dataTypeInPlural;
174  $this->result->forProperty('foo.baz');
175  $this->result->forProperty('foo.bar');
176  $this->assertFalse($this->result->{$methodName}());
177  }
178 
185  public function getFlattenedMessagesShouldReturnAllSubMessages($dataTypeInSingular, $dataTypeInPlural)
186  {
187  $message1 = $this->getMockMessage($dataTypeInSingular);
188  $message2 = $this->getMockMessage($dataTypeInSingular);
189  $message3 = $this->getMockMessage($dataTypeInSingular);
190  $message4 = $this->getMockMessage($dataTypeInSingular);
191  $message5 = $this->getMockMessage($dataTypeInSingular);
192  $addMethodName = 'add' . $dataTypeInSingular;
193  $this->result->forProperty('foo.bar')->{$addMethodName}($message1);
194  $this->result->forProperty('foo.baz')->{$addMethodName}($message2);
195  $this->result->forProperty('foo')->{$addMethodName}($message3);
196  $this->result->{$addMethodName}($message4);
197  $this->result->{$addMethodName}($message5);
198  $getMethodName = 'getFlattened' . $dataTypeInPlural;
199  $expected = [
200  '' => [$message4, $message5],
201  'foo' => [$message3],
202  'foo.bar' => [$message1],
203  'foo.baz' => [$message2]
204  ];
205  $this->assertEquals($expected, $this->result->{$getMethodName}());
206  }
207 
214  public function getFlattenedMessagesShouldNotContainEmptyResults($dataTypeInSingular, $dataTypeInPlural)
215  {
216  $message1 = $this->getMockMessage($dataTypeInSingular);
217  $message2 = $this->getMockMessage($dataTypeInSingular);
218  $addMethodName = 'add' . $dataTypeInSingular;
219  $this->result->forProperty('foo.bar')->{$addMethodName}($message1);
220  $this->result->forProperty('foo.baz')->{$addMethodName}($message2);
221  $getMethodName = 'getFlattened' . $dataTypeInPlural;
222  $expected = [
223  'foo.bar' => [$message1],
224  'foo.baz' => [$message2]
225  ];
226  $this->assertEquals($expected, $this->result->{$getMethodName}());
227  }
228 
232  public function mergeShouldMergeTwoResults()
233  {
234  $notice1 = $this->getMockMessage('Notice');
235  $notice2 = $this->getMockMessage('Notice');
236  $notice3 = $this->getMockMessage('Notice');
237  $warning1 = $this->getMockMessage('Warning');
238  $warning2 = $this->getMockMessage('Warning');
239  $warning3 = $this->getMockMessage('Warning');
240  $error1 = $this->getMockMessage('Error');
241  $error2 = $this->getMockMessage('Error');
242  $error3 = $this->getMockMessage('Error');
243  $otherResult = new \TYPO3\CMS\Extbase\Error\Result();
244  $otherResult->addNotice($notice1);
245  $otherResult->forProperty('foo.bar')->addNotice($notice2);
246  $this->result->forProperty('foo')->addNotice($notice3);
247  $otherResult->addWarning($warning1);
248  $this->result->addWarning($warning2);
249  $this->result->addWarning($warning3);
250  $otherResult->forProperty('foo')->addError($error1);
251  $otherResult->forProperty('foo')->addError($error2);
252  $otherResult->addError($error3);
253  $this->result->merge($otherResult);
254  $this->assertSame([$notice1], $this->result->getNotices(), 'Notices are not merged correctly without recursion');
255  $this->assertSame([$notice3], $this->result->forProperty('foo')->getNotices(), 'Original sub-notices are overridden.');
256  $this->assertSame([$notice2], $this->result->forProperty('foo')->forProperty('bar')->getNotices(), 'Sub-notices are not copied.');
257  $this->assertSame([$warning2, $warning3, $warning1], $this->result->getWarnings());
258  $this->assertSame([$error3], $this->result->getErrors());
259  $this->assertSame([$error1, $error2], $this->result->forProperty('foo')->getErrors());
260  }
261 }
getFlattenedMessagesShouldReturnAllSubMessages($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:185
getMessageShouldNotBeRecursive($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:81
getFlattenedMessagesShouldNotContainEmptyResults($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:214
addedMessagesShouldBeRetrievableAgain($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:66
hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:141
hasMessagesShouldReturnTrueIfSubObjectHasErrors($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:156
getFirstMessageShouldReturnFirstMessage($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:96
hasMessagesShouldReturnFalseIfSubObjectHasNoErrors($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:171