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