‪TYPO3CMS  9.5
ResultTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
17 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
18 
22 class ‪ResultTest extends UnitTestCase
23 {
27  protected ‪$result;
28 
29  protected function ‪setUp()
30  {
31  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
32  }
33 
37  public function ‪dataTypes()
38  {
39  return [
40  ['Error', 'Errors'],
41  ['Warning', 'Warnings'],
42  ['Notice', 'Notices']
43  ];
44  }
45 
50  protected function ‪getMockMessage($type)
51  {
52  return $this->createMock('TYPO3\\CMS\\Extbase\\Error\\' . $type);
53  }
54 
61  public function ‪addedMessagesShouldBeRetrievableAgain($dataTypeInSingular, $dataTypeInPlural)
62  {
63  $message = $this->‪getMockMessage($dataTypeInSingular);
64  $addMethodName = 'add' . $dataTypeInSingular;
65  $this->result->{$addMethodName}($message);
66  $getterMethodName = 'get' . $dataTypeInPlural;
67  $this->assertEquals([$message], $this->result->{$getterMethodName}());
68  }
69 
76  public function getMessageShouldNotBeRecursive($dataTypeInSingular, $dataTypeInPlural)
77  {
78  $message = $this->getMockMessage($dataTypeInSingular);
79  $addMethodName = 'add' . $dataTypeInSingular;
80  $this->result->forProperty('foo')->{$addMethodName}($message);
81  $getterMethodName = 'get' . $dataTypeInPlural;
82  $this->assertEquals([], $this->result->{$getterMethodName}());
83  }
84 
91  public function getFirstMessageShouldReturnFirstMessage($dataTypeInSingular, $dataTypeInPlural)
92  {
93  $message1 = $this->getMockMessage($dataTypeInSingular);
94  $message2 = $this->getMockMessage($dataTypeInSingular);
95  $addMethodName = 'add' . $dataTypeInSingular;
96  $this->result->{$addMethodName}($message1);
97  $this->result->{$addMethodName}($message2);
98  $getterMethodName = 'getFirst' . $dataTypeInSingular;
99  $this->assertSame($message1, $this->result->{$getterMethodName}());
100  }
101 
105  public function forPropertyShouldReturnSubResult()
106  {
107  $container2 = $this->result->forProperty('foo.bar');
108  $this->assertInstanceOf(\‪TYPO3\CMS\‪Extbase\Error\Result::class, $container2);
109  $this->assertSame($container2, $this->result->forProperty('foo')->forProperty('bar'));
110  }
111 
115  public function forPropertyWithEmptyStringShouldReturnSelf()
116  {
117  $container2 = $this->result->forProperty('');
118  $this->assertSame($container2, $this->result);
119  }
120 
124  public function forPropertyWithNullShouldReturnSelf()
125  {
126  $container2 = $this->result->forProperty(null);
127  $this->assertSame($container2, $this->result);
128  }
129 
136  public function hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages($dataTypeInSingular, $dataTypeInPlural)
137  {
138  $message = $this->getMockMessage($dataTypeInSingular);
139  $addMethodName = 'add' . $dataTypeInSingular;
140  $this->result->{$addMethodName}($message);
141  $methodName = 'has' . $dataTypeInPlural;
142  $this->assertTrue($this->result->{$methodName}());
143  }
144 
151  public function hasMessagesShouldReturnTrueIfSubObjectHasErrors($dataTypeInSingular, $dataTypeInPlural)
152  {
153  $addMethodName = 'add' . $dataTypeInSingular;
154  $methodName = 'has' . $dataTypeInPlural;
155  $message = $this->getMockMessage($dataTypeInSingular);
156  $this->result->forProperty('foo.bar')->{$addMethodName}($message);
157  $this->assertTrue($this->result->{$methodName}());
158  }
159 
166  public function hasMessagesShouldReturnFalseIfSubObjectHasNoErrors($dataTypeInSingular, $dataTypeInPlural)
167  {
168  $methodName = 'has' . $dataTypeInPlural;
169  $this->result->forProperty('foo.baz');
170  $this->result->forProperty('foo.bar');
171  $this->assertFalse($this->result->{$methodName}());
172  }
173 
180  public function getFlattenedMessagesShouldReturnAllSubMessages($dataTypeInSingular, $dataTypeInPlural)
181  {
182  $message1 = $this->getMockMessage($dataTypeInSingular);
183  $message2 = $this->getMockMessage($dataTypeInSingular);
184  $message3 = $this->getMockMessage($dataTypeInSingular);
185  $message4 = $this->getMockMessage($dataTypeInSingular);
186  $message5 = $this->getMockMessage($dataTypeInSingular);
187  $addMethodName = 'add' . $dataTypeInSingular;
188  $this->result->forProperty('foo.bar')->{$addMethodName}($message1);
189  $this->result->forProperty('foo.baz')->{$addMethodName}($message2);
190  $this->result->forProperty('foo')->{$addMethodName}($message3);
191  $this->result->{$addMethodName}($message4);
192  $this->result->{$addMethodName}($message5);
193  $getMethodName = 'getFlattened' . $dataTypeInPlural;
194  $expected = [
195  '' => [$message4, $message5],
196  'foo' => [$message3],
197  'foo.bar' => [$message1],
198  'foo.baz' => [$message2]
199  ];
200  $this->assertEquals($expected, $this->result->{$getMethodName}());
201  }
202 
209  public function getFlattenedMessagesShouldNotContainEmptyResults($dataTypeInSingular, $dataTypeInPlural)
210  {
211  $message1 = $this->getMockMessage($dataTypeInSingular);
212  $message2 = $this->getMockMessage($dataTypeInSingular);
213  $addMethodName = 'add' . $dataTypeInSingular;
214  $this->result->forProperty('foo.bar')->{$addMethodName}($message1);
215  $this->result->forProperty('foo.baz')->{$addMethodName}($message2);
216  $getMethodName = 'getFlattened' . $dataTypeInPlural;
217  $expected = [
218  'foo.bar' => [$message1],
219  'foo.baz' => [$message2]
220  ];
221  $this->assertEquals($expected, $this->result->{$getMethodName}());
222  }
223 
227  public function mergeShouldMergeTwoResults()
228  {
229  $notice1 = $this->getMockMessage('Notice');
230  $notice2 = $this->getMockMessage('Notice');
231  $notice3 = $this->getMockMessage('Notice');
232  $warning1 = $this->getMockMessage('Warning');
233  $warning2 = $this->getMockMessage('Warning');
234  $warning3 = $this->getMockMessage('Warning');
235  $error1 = $this->getMockMessage('Error');
236  $error2 = $this->getMockMessage('Error');
237  $error3 = $this->getMockMessage('Error');
238  $otherResult = new \TYPO3\CMS\Extbase\Error\Result();
239  $otherResult->addNotice($notice1);
240  $otherResult->forProperty('foo.bar')->addNotice($notice2);
241  $this->result->forProperty('foo')->addNotice($notice3);
242  $otherResult->addWarning($warning1);
243  $this->result->addWarning($warning2);
244  $this->result->addWarning($warning3);
245  $otherResult->forProperty('foo')->addError($error1);
246  $otherResult->forProperty('foo')->addError($error2);
247  $otherResult->addError($error3);
248  $this->result->merge($otherResult);
249  $this->assertSame([$notice1], $this->result->getNotices(), 'Notices are not merged correctly without recursion');
250  $this->assertSame([$notice3], $this->result->forProperty('foo')->getNotices(), 'Original sub-notices are overridden.');
251  $this->assertSame([$notice2], $this->result->forProperty('foo')->forProperty('bar')->getNotices(), 'Sub-notices are not copied.');
252  $this->assertSame([$warning2, $warning3, $warning1], $this->result->getWarnings());
253  $this->assertSame([$error3], $this->result->getErrors());
254  $this->assertSame([$error1, $error2], $this->result->forProperty('foo')->getErrors());
255  }
256 }
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\forPropertyWithEmptyStringShouldReturnSelf
‪forPropertyWithEmptyStringShouldReturnSelf()
Definition: ResultTest.php:114
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\forPropertyShouldReturnSubResult
‪forPropertyShouldReturnSubResult()
Definition: ResultTest.php:104
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\$result
‪TYPO3 CMS Extbase Error Result $result
Definition: ResultTest.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\dataTypes
‪array dataTypes()
Definition: ResultTest.php:36
‪TYPO3
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest
Definition: ResultTest.php:23
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getMockMessage
‪PHPUnit_Framework_MockObject_MockObject getMockMessage($type)
Definition: ResultTest.php:49
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getFlattenedMessagesShouldReturnAllSubMessages
‪getFlattenedMessagesShouldReturnAllSubMessages($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:179
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\hasMessagesShouldReturnFalseIfSubObjectHasNoErrors
‪hasMessagesShouldReturnFalseIfSubObjectHasNoErrors($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:165
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\forPropertyWithNullShouldReturnSelf
‪forPropertyWithNullShouldReturnSelf()
Definition: ResultTest.php:123
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getFlattenedMessagesShouldNotContainEmptyResults
‪getFlattenedMessagesShouldNotContainEmptyResults($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:208
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\setUp
‪setUp()
Definition: ResultTest.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages
‪hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:135
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\mergeShouldMergeTwoResults
‪mergeShouldMergeTwoResults()
Definition: ResultTest.php:226
‪TYPO3\CMS\Extbase\Tests\Unit\Error
Definition: ErrorTest.php:2
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getMessageShouldNotBeRecursive
‪getMessageShouldNotBeRecursive($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:75
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\addedMessagesShouldBeRetrievableAgain
‪addedMessagesShouldBeRetrievableAgain($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:60
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getFirstMessageShouldReturnFirstMessage
‪getFirstMessageShouldReturnFirstMessage($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:90
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\hasMessagesShouldReturnTrueIfSubObjectHasErrors
‪hasMessagesShouldReturnTrueIfSubObjectHasErrors($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:150