‪TYPO3CMS  ‪main
ResultTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
22 use PHPUnit\Framework\MockObject\MockObject;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪ResultTest extends UnitTestCase
27 {
28  protected ‪Result ‪$result;
29 
30  protected function ‪setUp(): void
31  {
32  parent::setUp();
33  $this->result = new ‪Result();
34  }
35 
36  public static function ‪dataTypes(): array
37  {
38  return [
39  ['Error', 'Errors'],
40  ['Warning', 'Warnings'],
41  ['Notice', 'Notices'],
42  ];
43  }
44 
45  protected function ‪getMockMessage(string $type): MockObject
46  {
47  return $this->createMock('TYPO3\\CMS\\Extbase\\Error\\' . $type);
48  }
49 
50  #[DataProvider('dataTypes')]
51  #[Test]
52  public function ‪addedMessagesShouldBeRetrievableAgain(string $dataTypeInSingular, string $dataTypeInPlural): void
53  {
54  $message = $this->‪getMockMessage($dataTypeInSingular);
55  $addMethodName = 'add' . $dataTypeInSingular;
56  $this->result->{$addMethodName}($message);
57  $getterMethodName = 'get' . $dataTypeInPlural;
58  self::assertEquals([$message], $this->result->{$getterMethodName}());
59  }
60 
61  #[DataProvider('dataTypes')]
62  #[Test]
63  public function ‪getMessageShouldNotBeRecursive(string $dataTypeInSingular, string $dataTypeInPlural): void
64  {
65  $message = $this->getMockMessage($dataTypeInSingular);
66  $addMethodName = 'add' . $dataTypeInSingular;
67  $this->result->forProperty('foo')->{$addMethodName}($message);
68  $getterMethodName = 'get' . $dataTypeInPlural;
69  self::assertEquals([], $this->result->{$getterMethodName}());
70  }
71 
72  #[DataProvider('dataTypes')]
73  #[Test]
74  public function ‪getFirstMessageShouldReturnFirstMessage(string $dataTypeInSingular, string $dataTypeInPlural): void
75  {
76  $message1 = $this->getMockMessage($dataTypeInSingular);
77  $message2 = $this->getMockMessage($dataTypeInSingular);
78  $addMethodName = 'add' . $dataTypeInSingular;
79  $this->result->{$addMethodName}($message1);
80  $this->result->{$addMethodName}($message2);
81  $getterMethodName = 'getFirst' . $dataTypeInSingular;
82  self::assertSame($message1, $this->result->{$getterMethodName}());
83  }
84 
85  #[Test]
86  public function ‪forPropertyShouldReturnSubResult(): void
87  {
88  $container2 = $this->result->forProperty('foo.bar');
89  self::assertInstanceOf(Result::class, $container2);
90  self::assertSame($container2, $this->result->forProperty('foo')->forProperty('bar'));
91  }
92 
93  #[Test]
95  {
96  $container2 = $this->result->forProperty('');
97  self::assertSame($container2, $this->result);
98  }
99 
100  #[Test]
102  {
103  $container2 = $this->result->forProperty(null);
104  self::assertSame($container2, $this->result);
105  }
106 
107  #[DataProvider('dataTypes')]
108  #[Test]
109  public function ‪hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages(string $dataTypeInSingular, string $dataTypeInPlural): void
110  {
111  $message = $this->getMockMessage($dataTypeInSingular);
112  $addMethodName = 'add' . $dataTypeInSingular;
113  $this->result->{$addMethodName}($message);
114  $methodName = 'has' . $dataTypeInPlural;
115  self::assertTrue($this->result->{$methodName}());
116  }
117 
118  #[DataProvider('dataTypes')]
119  #[Test]
120  public function ‪hasMessagesShouldReturnTrueIfSubObjectHasErrors(string $dataTypeInSingular, string $dataTypeInPlural): void
121  {
122  $addMethodName = 'add' . $dataTypeInSingular;
123  $methodName = 'has' . $dataTypeInPlural;
124  $message = $this->getMockMessage($dataTypeInSingular);
125  $this->result->forProperty('foo.bar')->{$addMethodName}($message);
126  self::assertTrue($this->result->{$methodName}());
127  }
128 
129  #[DataProvider('dataTypes')]
130  #[Test]
131  public function ‪hasMessagesShouldReturnFalseIfSubObjectHasNoErrors(string $dataTypeInSingular, string $dataTypeInPlural): void
132  {
133  $methodName = 'has' . $dataTypeInPlural;
134  $this->result->forProperty('foo.baz');
135  $this->result->forProperty('foo.bar');
136  self::assertFalse($this->result->{$methodName}());
137  }
138 
139  #[DataProvider('dataTypes')]
140  #[Test]
141  public function ‪getFlattenedMessagesShouldReturnAllSubMessages(string $dataTypeInSingular, string $dataTypeInPlural): void
142  {
143  $message1 = $this->getMockMessage($dataTypeInSingular);
144  $message2 = $this->getMockMessage($dataTypeInSingular);
145  $message3 = $this->getMockMessage($dataTypeInSingular);
146  $message4 = $this->getMockMessage($dataTypeInSingular);
147  $message5 = $this->getMockMessage($dataTypeInSingular);
148  $addMethodName = 'add' . $dataTypeInSingular;
149  $this->result->forProperty('foo.bar')->{$addMethodName}($message1);
150  $this->result->forProperty('foo.baz')->{$addMethodName}($message2);
151  $this->result->forProperty('foo')->{$addMethodName}($message3);
152  $this->result->{$addMethodName}($message4);
153  $this->result->{$addMethodName}($message5);
154  $getMethodName = 'getFlattened' . $dataTypeInPlural;
155  $expected = [
156  '' => [$message4, $message5],
157  'foo' => [$message3],
158  'foo.bar' => [$message1],
159  'foo.baz' => [$message2],
160  ];
161  self::assertEquals($expected, $this->result->{$getMethodName}());
162  }
163 
164  #[DataProvider('dataTypes')]
165  #[Test]
166  public function ‪getFlattenedMessagesShouldNotContainEmptyResults(string $dataTypeInSingular, string $dataTypeInPlural): void
167  {
168  $message1 = $this->getMockMessage($dataTypeInSingular);
169  $message2 = $this->getMockMessage($dataTypeInSingular);
170  $addMethodName = 'add' . $dataTypeInSingular;
171  $this->result->forProperty('foo.bar')->{$addMethodName}($message1);
172  $this->result->forProperty('foo.baz')->{$addMethodName}($message2);
173  $getMethodName = 'getFlattened' . $dataTypeInPlural;
174  $expected = [
175  'foo.bar' => [$message1],
176  'foo.baz' => [$message2],
177  ];
178  self::assertEquals($expected, $this->result->{$getMethodName}());
179  }
180 
181  #[Test]
182  public function ‪mergeShouldMergeTwoResults(): void
183  {
184  $notice1 = $this->getMockMessage('Notice');
185  $notice2 = $this->getMockMessage('Notice');
186  $notice3 = $this->getMockMessage('Notice');
187  $warning1 = $this->getMockMessage('Warning');
188  $warning2 = $this->getMockMessage('Warning');
189  $warning3 = $this->getMockMessage('Warning');
190  $error1 = $this->getMockMessage('Error');
191  $error2 = $this->getMockMessage('Error');
192  $error3 = $this->getMockMessage('Error');
193  $otherResult = new ‪Result();
194  $otherResult->addNotice($notice1);
195  $otherResult->forProperty('foo.bar')->addNotice($notice2);
196  $this->result->forProperty('foo')->addNotice($notice3);
197  $otherResult->addWarning($warning1);
198  $this->result->addWarning($warning2);
199  $this->result->addWarning($warning3);
200  $otherResult->forProperty('foo')->addError($error1);
201  $otherResult->forProperty('foo')->addError($error2);
202  $otherResult->addError($error3);
203  $this->result->merge($otherResult);
204  self::assertSame([$notice1], $this->result->getNotices(), 'Notices are not merged correctly without recursion');
205  self::assertSame([$notice3], $this->result->forProperty('foo')->getNotices(), 'Original sub-notices are overridden.');
206  self::assertSame([$notice2], $this->result->forProperty('foo')->forProperty('bar')->getNotices(), 'Sub-notices are not copied.');
207  self::assertSame([$warning2, $warning3, $warning1], $this->result->getWarnings());
208  self::assertSame([$error3], $this->result->getErrors());
209  self::assertSame([$error1, $error2], $this->result->forProperty('foo')->getErrors());
210  }
211 
212  #[Test]
213  public function ‪getFirstReturnsFalseOnEmptyResult(): void
214  {
215  $subject = new ‪Result();
216  self::assertFalse($subject->getFirstError());
217  self::assertFalse($subject->getFirstNotice());
218  self::assertFalse($subject->getFirstWarning());
219  }
220 }
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\forPropertyWithEmptyStringShouldReturnSelf
‪forPropertyWithEmptyStringShouldReturnSelf()
Definition: ResultTest.php:94
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\dataTypes
‪static dataTypes()
Definition: ResultTest.php:36
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getFirstMessageShouldReturnFirstMessage
‪getFirstMessageShouldReturnFirstMessage(string $dataTypeInSingular, string $dataTypeInPlural)
Definition: ResultTest.php:74
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\forPropertyShouldReturnSubResult
‪forPropertyShouldReturnSubResult()
Definition: ResultTest.php:86
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\addedMessagesShouldBeRetrievableAgain
‪addedMessagesShouldBeRetrievableAgain(string $dataTypeInSingular, string $dataTypeInPlural)
Definition: ResultTest.php:52
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\hasMessagesShouldReturnTrueIfSubObjectHasErrors
‪hasMessagesShouldReturnTrueIfSubObjectHasErrors(string $dataTypeInSingular, string $dataTypeInPlural)
Definition: ResultTest.php:120
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest
Definition: ResultTest.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages
‪hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages(string $dataTypeInSingular, string $dataTypeInPlural)
Definition: ResultTest.php:109
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getFirstReturnsFalseOnEmptyResult
‪getFirstReturnsFalseOnEmptyResult()
Definition: ResultTest.php:213
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\$result
‪Result $result
Definition: ResultTest.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\forPropertyWithNullShouldReturnSelf
‪forPropertyWithNullShouldReturnSelf()
Definition: ResultTest.php:101
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getFlattenedMessagesShouldReturnAllSubMessages
‪getFlattenedMessagesShouldReturnAllSubMessages(string $dataTypeInSingular, string $dataTypeInPlural)
Definition: ResultTest.php:141
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\hasMessagesShouldReturnFalseIfSubObjectHasNoErrors
‪hasMessagesShouldReturnFalseIfSubObjectHasNoErrors(string $dataTypeInSingular, string $dataTypeInPlural)
Definition: ResultTest.php:131
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\setUp
‪setUp()
Definition: ResultTest.php:30
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\mergeShouldMergeTwoResults
‪mergeShouldMergeTwoResults()
Definition: ResultTest.php:182
‪TYPO3\CMS\Extbase\Tests\Unit\Error
Definition: ErrorTest.php:18
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getMockMessage
‪getMockMessage(string $type)
Definition: ResultTest.php:45
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getFlattenedMessagesShouldNotContainEmptyResults
‪getFlattenedMessagesShouldNotContainEmptyResults(string $dataTypeInSingular, string $dataTypeInPlural)
Definition: ResultTest.php:166
‪TYPO3\CMS\Extbase\Tests\Unit\Error\ResultTest\getMessageShouldNotBeRecursive
‪getMessageShouldNotBeRecursive(string $dataTypeInSingular, string $dataTypeInPlural)
Definition: ResultTest.php:63