‪TYPO3CMS  10.4
ResultTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
20 
24 class ‪ResultTest extends UnitTestCase
25 {
29  protected ‪$result;
30 
31  protected function ‪setUp(): void
32  {
33  parent::setUp();
34  $this->result = new ‪Result();
35  }
36 
40  public function ‪dataTypes()
41  {
42  return [
43  ['Error', 'Errors'],
44  ['Warning', 'Warnings'],
45  ['Notice', 'Notices']
46  ];
47  }
48 
53  protected function ‪getMockMessage($type)
54  {
55  return $this->createMock('TYPO3\\CMS\\Extbase\\Error\\' . $type);
56  }
57 
64  public function ‪addedMessagesShouldBeRetrievableAgain($dataTypeInSingular, $dataTypeInPlural)
65  {
66  $message = $this->‪getMockMessage($dataTypeInSingular);
67  $addMethodName = 'add' . $dataTypeInSingular;
68  $this->result->{$addMethodName}($message);
69  $getterMethodName = 'get' . $dataTypeInPlural;
70  self::assertEquals([$message], $this->result->{$getterMethodName}());
71  }
72 
79  public function getMessageShouldNotBeRecursive($dataTypeInSingular, $dataTypeInPlural)
80  {
81  $message = $this->getMockMessage($dataTypeInSingular);
82  $addMethodName = 'add' . $dataTypeInSingular;
83  $this->result->forProperty('foo')->{$addMethodName}($message);
84  $getterMethodName = 'get' . $dataTypeInPlural;
85  self::assertEquals([], $this->result->{$getterMethodName}());
86  }
87 
94  public function getFirstMessageShouldReturnFirstMessage($dataTypeInSingular, $dataTypeInPlural)
95  {
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  self::assertSame($message1, $this->result->{$getterMethodName}());
103  }
104 
108  public function forPropertyShouldReturnSubResult()
109  {
110  $container2 = $this->result->forProperty('foo.bar');
111  self::assertInstanceOf(Result::class, $container2);
112  self::assertSame($container2, $this->result->forProperty('foo')->forProperty('bar'));
113  }
114 
118  public function forPropertyWithEmptyStringShouldReturnSelf()
119  {
120  $container2 = $this->result->forProperty('');
121  self::assertSame($container2, $this->result);
122  }
123 
127  public function forPropertyWithNullShouldReturnSelf()
128  {
129  $container2 = $this->result->forProperty(null);
130  self::assertSame($container2, $this->result);
131  }
132 
139  public function hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages($dataTypeInSingular, $dataTypeInPlural)
140  {
141  $message = $this->getMockMessage($dataTypeInSingular);
142  $addMethodName = 'add' . $dataTypeInSingular;
143  $this->result->{$addMethodName}($message);
144  $methodName = 'has' . $dataTypeInPlural;
145  self::assertTrue($this->result->{$methodName}());
146  }
147 
154  public function hasMessagesShouldReturnTrueIfSubObjectHasErrors($dataTypeInSingular, $dataTypeInPlural)
155  {
156  $addMethodName = 'add' . $dataTypeInSingular;
157  $methodName = 'has' . $dataTypeInPlural;
158  $message = $this->getMockMessage($dataTypeInSingular);
159  $this->result->forProperty('foo.bar')->{$addMethodName}($message);
160  self::assertTrue($this->result->{$methodName}());
161  }
162 
169  public function hasMessagesShouldReturnFalseIfSubObjectHasNoErrors($dataTypeInSingular, $dataTypeInPlural)
170  {
171  $methodName = 'has' . $dataTypeInPlural;
172  $this->result->forProperty('foo.baz');
173  $this->result->forProperty('foo.bar');
174  self::assertFalse($this->result->{$methodName}());
175  }
176 
183  public function getFlattenedMessagesShouldReturnAllSubMessages($dataTypeInSingular, $dataTypeInPlural)
184  {
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 = [
198  '' => [$message4, $message5],
199  'foo' => [$message3],
200  'foo.bar' => [$message1],
201  'foo.baz' => [$message2]
202  ];
203  self::assertEquals($expected, $this->result->{$getMethodName}());
204  }
205 
212  public function getFlattenedMessagesShouldNotContainEmptyResults($dataTypeInSingular, $dataTypeInPlural)
213  {
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 = [
221  'foo.bar' => [$message1],
222  'foo.baz' => [$message2]
223  ];
224  self::assertEquals($expected, $this->result->{$getMethodName}());
225  }
226 
230  public function mergeShouldMergeTwoResults()
231  {
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 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  self::assertSame([$notice1], $this->result->getNotices(), 'Notices are not merged correctly without recursion');
253  self::assertSame([$notice3], $this->result->forProperty('foo')->getNotices(), 'Original sub-notices are overridden.');
254  self::assertSame([$notice2], $this->result->forProperty('foo')->forProperty('bar')->getNotices(), 'Sub-notices are not copied.');
255  self::assertSame([$warning2, $warning3, $warning1], $this->result->getWarnings());
256  self::assertSame([$error3], $this->result->getErrors());
257  self::assertSame([$error1, $error2], $this->result->forProperty('foo')->getErrors());
258  }
259 
263  public function getFirstReturnsFalseOnEmptyResult()
264  {
265  $subject = new Result();
266  self::assertFalse($subject->getFirstError());
267  self::assertFalse($subject->getFirstNotice());
268  self::assertFalse($subject->getFirstWarning());
269  }
270 }
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getMockMessage
‪PHPUnit Framework MockObject MockObject getMockMessage($type)
Definition: ResultTest.php:52
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\forPropertyWithEmptyStringShouldReturnSelf
‪forPropertyWithEmptyStringShouldReturnSelf()
Definition: ResultTest.php:117
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\forPropertyShouldReturnSubResult
‪forPropertyShouldReturnSubResult()
Definition: ResultTest.php:107
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\$result
‪TYPO3 CMS Extbase Error Result $result
Definition: ResultTest.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\dataTypes
‪array dataTypes()
Definition: ResultTest.php:39
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest
Definition: ResultTest.php:25
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getFirstReturnsFalseOnEmptyResult
‪getFirstReturnsFalseOnEmptyResult()
Definition: ResultTest.php:262
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getFlattenedMessagesShouldReturnAllSubMessages
‪getFlattenedMessagesShouldReturnAllSubMessages($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:182
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\hasMessagesShouldReturnFalseIfSubObjectHasNoErrors
‪hasMessagesShouldReturnFalseIfSubObjectHasNoErrors($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:168
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\forPropertyWithNullShouldReturnSelf
‪forPropertyWithNullShouldReturnSelf()
Definition: ResultTest.php:126
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getFlattenedMessagesShouldNotContainEmptyResults
‪getFlattenedMessagesShouldNotContainEmptyResults($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:211
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\setUp
‪setUp()
Definition: ResultTest.php:30
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages
‪hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:138
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\mergeShouldMergeTwoResults
‪mergeShouldMergeTwoResults()
Definition: ResultTest.php:229
‪TYPO3\CMS\Extbase\Tests\Unit\Error
Definition: ErrorTest.php:16
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getMessageShouldNotBeRecursive
‪getMessageShouldNotBeRecursive($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:78
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\addedMessagesShouldBeRetrievableAgain
‪addedMessagesShouldBeRetrievableAgain($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:63
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getFirstMessageShouldReturnFirstMessage
‪getFirstMessageShouldReturnFirstMessage($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:93
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\hasMessagesShouldReturnTrueIfSubObjectHasErrors
‪hasMessagesShouldReturnTrueIfSubObjectHasErrors($dataTypeInSingular, $dataTypeInPlural)
Definition: ResultTest.php:153