[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Error / ResultTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Error;
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
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25
26 /**
27 * Test case
28 */
29 class ResultTest extends UnitTestCase
30 {
31 /**
32 * @var \TYPO3\CMS\Extbase\Error\Result
33 */
34 protected $result;
35
36 protected function setUp()
37 {
38 $this->result = new \TYPO3\CMS\Extbase\Error\Result();
39 }
40
41 /**
42 * @return array
43 */
44 public function dataTypes()
45 {
46 return [
47 ['Error', 'Errors'],
48 ['Warning', 'Warnings'],
49 ['Notice', 'Notices']
50 ];
51 }
52
53 /**
54 * @param string $type
55 * @return \PHPUnit_Framework_MockObject_MockObject
56 */
57 protected function getMockMessage($type)
58 {
59 return $this->createMock('TYPO3\\CMS\\Extbase\\Error\\' . $type);
60 }
61
62 /**
63 * @test
64 * @dataProvider dataTypes
65 * @param string $dataTypeInSingular
66 * @param string $dataTypeInPlural
67 */
68 public function addedMessagesShouldBeRetrievableAgain($dataTypeInSingular, $dataTypeInPlural)
69 {
70 $message = $this->getMockMessage($dataTypeInSingular);
71 $addMethodName = 'add' . $dataTypeInSingular;
72 $this->result->{$addMethodName}($message);
73 $getterMethodName = 'get' . $dataTypeInPlural;
74 $this->assertEquals([$message], $this->result->{$getterMethodName}());
75 }
76
77 /**
78 * @test
79 * @dataProvider dataTypes
80 * @param string $dataTypeInSingular
81 * @param string $dataTypeInPlural
82 */
83 public function getMessageShouldNotBeRecursive($dataTypeInSingular, $dataTypeInPlural)
84 {
85 $message = $this->getMockMessage($dataTypeInSingular);
86 $addMethodName = 'add' . $dataTypeInSingular;
87 $this->result->forProperty('foo')->{$addMethodName}($message);
88 $getterMethodName = 'get' . $dataTypeInPlural;
89 $this->assertEquals([], $this->result->{$getterMethodName}());
90 }
91
92 /**
93 * @test
94 * @dataProvider dataTypes
95 * @param string $dataTypeInSingular
96 * @param string $dataTypeInPlural
97 */
98 public function getFirstMessageShouldReturnFirstMessage($dataTypeInSingular, $dataTypeInPlural)
99 {
100 $message1 = $this->getMockMessage($dataTypeInSingular);
101 $message2 = $this->getMockMessage($dataTypeInSingular);
102 $addMethodName = 'add' . $dataTypeInSingular;
103 $this->result->{$addMethodName}($message1);
104 $this->result->{$addMethodName}($message2);
105 $getterMethodName = 'getFirst' . $dataTypeInSingular;
106 $this->assertSame($message1, $this->result->{$getterMethodName}());
107 }
108
109 /**
110 * @test
111 */
112 public function forPropertyShouldReturnSubResult()
113 {
114 $container2 = $this->result->forProperty('foo.bar');
115 $this->assertInstanceOf(\TYPO3\CMS\Extbase\Error\Result::class, $container2);
116 $this->assertSame($container2, $this->result->forProperty('foo')->forProperty('bar'));
117 }
118
119 /**
120 * @test
121 */
122 public function forPropertyWithEmptyStringShouldReturnSelf()
123 {
124 $container2 = $this->result->forProperty('');
125 $this->assertSame($container2, $this->result);
126 }
127
128 /**
129 * @test
130 */
131 public function forPropertyWithNullShouldReturnSelf()
132 {
133 $container2 = $this->result->forProperty(null);
134 $this->assertSame($container2, $this->result);
135 }
136
137 /**
138 * @test
139 * @dataProvider dataTypes
140 * @param string $dataTypeInSingular
141 * @param string $dataTypeInPlural
142 */
143 public function hasMessagesShouldReturnTrueIfTopLevelObjectHasMessages($dataTypeInSingular, $dataTypeInPlural)
144 {
145 $message = $this->getMockMessage($dataTypeInSingular);
146 $addMethodName = 'add' . $dataTypeInSingular;
147 $this->result->{$addMethodName}($message);
148 $methodName = 'has' . $dataTypeInPlural;
149 $this->assertTrue($this->result->{$methodName}());
150 }
151
152 /**
153 * @test
154 * @dataProvider dataTypes
155 * @param string $dataTypeInSingular
156 * @param string $dataTypeInPlural
157 */
158 public function hasMessagesShouldReturnTrueIfSubObjectHasErrors($dataTypeInSingular, $dataTypeInPlural)
159 {
160 $addMethodName = 'add' . $dataTypeInSingular;
161 $methodName = 'has' . $dataTypeInPlural;
162 $message = $this->getMockMessage($dataTypeInSingular);
163 $this->result->forProperty('foo.bar')->{$addMethodName}($message);
164 $this->assertTrue($this->result->{$methodName}());
165 }
166
167 /**
168 * @test
169 * @dataProvider dataTypes
170 * @param string $dataTypeInSingular
171 * @param string $dataTypeInPlural
172 */
173 public function hasMessagesShouldReturnFalseIfSubObjectHasNoErrors($dataTypeInSingular, $dataTypeInPlural)
174 {
175 $methodName = 'has' . $dataTypeInPlural;
176 $this->result->forProperty('foo.baz');
177 $this->result->forProperty('foo.bar');
178 $this->assertFalse($this->result->{$methodName}());
179 }
180
181 /**
182 * @test
183 * @dataProvider dataTypes
184 * @param string $dataTypeInSingular
185 * @param string $dataTypeInPlural
186 */
187 public function getFlattenedMessagesShouldReturnAllSubMessages($dataTypeInSingular, $dataTypeInPlural)
188 {
189 $message1 = $this->getMockMessage($dataTypeInSingular);
190 $message2 = $this->getMockMessage($dataTypeInSingular);
191 $message3 = $this->getMockMessage($dataTypeInSingular);
192 $message4 = $this->getMockMessage($dataTypeInSingular);
193 $message5 = $this->getMockMessage($dataTypeInSingular);
194 $addMethodName = 'add' . $dataTypeInSingular;
195 $this->result->forProperty('foo.bar')->{$addMethodName}($message1);
196 $this->result->forProperty('foo.baz')->{$addMethodName}($message2);
197 $this->result->forProperty('foo')->{$addMethodName}($message3);
198 $this->result->{$addMethodName}($message4);
199 $this->result->{$addMethodName}($message5);
200 $getMethodName = 'getFlattened' . $dataTypeInPlural;
201 $expected = [
202 '' => [$message4, $message5],
203 'foo' => [$message3],
204 'foo.bar' => [$message1],
205 'foo.baz' => [$message2]
206 ];
207 $this->assertEquals($expected, $this->result->{$getMethodName}());
208 }
209
210 /**
211 * @test
212 * @dataProvider dataTypes
213 * @param string $dataTypeInSingular
214 * @param string $dataTypeInPlural
215 */
216 public function getFlattenedMessagesShouldNotContainEmptyResults($dataTypeInSingular, $dataTypeInPlural)
217 {
218 $message1 = $this->getMockMessage($dataTypeInSingular);
219 $message2 = $this->getMockMessage($dataTypeInSingular);
220 $addMethodName = 'add' . $dataTypeInSingular;
221 $this->result->forProperty('foo.bar')->{$addMethodName}($message1);
222 $this->result->forProperty('foo.baz')->{$addMethodName}($message2);
223 $getMethodName = 'getFlattened' . $dataTypeInPlural;
224 $expected = [
225 'foo.bar' => [$message1],
226 'foo.baz' => [$message2]
227 ];
228 $this->assertEquals($expected, $this->result->{$getMethodName}());
229 }
230
231 /**
232 * @test
233 */
234 public function mergeShouldMergeTwoResults()
235 {
236 $notice1 = $this->getMockMessage('Notice');
237 $notice2 = $this->getMockMessage('Notice');
238 $notice3 = $this->getMockMessage('Notice');
239 $warning1 = $this->getMockMessage('Warning');
240 $warning2 = $this->getMockMessage('Warning');
241 $warning3 = $this->getMockMessage('Warning');
242 $error1 = $this->getMockMessage('Error');
243 $error2 = $this->getMockMessage('Error');
244 $error3 = $this->getMockMessage('Error');
245 $otherResult = new \TYPO3\CMS\Extbase\Error\Result();
246 $otherResult->addNotice($notice1);
247 $otherResult->forProperty('foo.bar')->addNotice($notice2);
248 $this->result->forProperty('foo')->addNotice($notice3);
249 $otherResult->addWarning($warning1);
250 $this->result->addWarning($warning2);
251 $this->result->addWarning($warning3);
252 $otherResult->forProperty('foo')->addError($error1);
253 $otherResult->forProperty('foo')->addError($error2);
254 $otherResult->addError($error3);
255 $this->result->merge($otherResult);
256 $this->assertSame([$notice1], $this->result->getNotices(), 'Notices are not merged correctly without recursion');
257 $this->assertSame([$notice3], $this->result->forProperty('foo')->getNotices(), 'Original sub-notices are overridden.');
258 $this->assertSame([$notice2], $this->result->forProperty('foo')->forProperty('bar')->getNotices(), 'Sub-notices are not copied.');
259 $this->assertSame([$warning2, $warning3, $warning1], $this->result->getWarnings());
260 $this->assertSame([$error3], $this->result->getErrors());
261 $this->assertSame([$error1, $error2], $this->result->forProperty('foo')->getErrors());
262 }
263 }