ec830f5b2a250f8f7adc5979578263fb6a290d6b
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Mvc / Configuration / YamlSourceTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Mvc\Configuration;
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\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Form\Mvc\Configuration\Exception\NoSuchFileException;
19 use TYPO3\CMS\Form\Mvc\Configuration\Exception\ParseErrorException;
20 use TYPO3\CMS\Form\Mvc\Configuration\YamlSource;
21
22 /**
23 * Test case
24 */
25 class YamlSourceTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
26 {
27
28 /**
29 * @test
30 */
31 public function loadThrowsExceptionIfFileToLoadNotExists()
32 {
33 $this->expectException(NoSuchFileException::class);
34 $this->expectExceptionCode(1471473378);
35
36 $mockYamlSource = $this->getAccessibleMock(YamlSource::class, [
37 'dummy',
38 ], [], '', false);
39
40 $input = [
41 'EXT:form/Resources/Forms/_example.yaml'
42 ];
43
44 $mockYamlSource->_call('load', $input);
45 }
46
47 /**
48 * @test
49 */
50 public function loadThrowsExceptionIfFileToLoadIsNotValidYamlUseSymfonyParser()
51 {
52 if (!extension_loaded('yaml')) {
53 $this->expectException(ParseErrorException::class);
54 $this->expectExceptionCode(1480195405);
55
56 $mockYamlSource = $this->getAccessibleMock(YamlSource::class, [
57 'dummy',
58 ], [], '', false);
59
60 $input = [
61 'EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/Invalid.yaml'
62 ];
63
64 $mockYamlSource->_call('load', $input);
65 }
66 }
67
68 /**
69 * @test
70 */
71 public function loadThrowsExceptionIfFileToLoadIsNotValidYamlUsePhpExtensionParser()
72 {
73 if (!extension_loaded('yaml')) {
74 $this->markTestSkipped('Yaml PHP extension not available, skipping.');
75 }
76 $this->expectException(ParseErrorException::class);
77 $this->expectExceptionCode(1391894094);
78
79 $mockYamlSource = $this->getAccessibleMock(YamlSource::class, [
80 'dummy',
81 ], [], '', false);
82
83 $input = [
84 'EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/Invalid.yaml'
85 ];
86
87 $mockYamlSource->_call('load', $input);
88 }
89
90 /**
91 * @test
92 */
93 public function getHeaderFromFileReturnsHeaderPart()
94 {
95 $mockYamlSource = $this->getAccessibleMock(YamlSource::class, [
96 'dummy',
97 ], [], '', false);
98
99 $input = GeneralUtility::getFileAbsFileName('EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/Header.yaml');
100 $expected =
101 '# Header 1
102 # Header 2
103 ';
104
105 $this->assertSame($expected, $mockYamlSource->_call('getHeaderFromFile', $input));
106 }
107
108 /**
109 * @test
110 */
111 public function loadOverruleNonArrayValuesOverArrayValues()
112 {
113 $mockYamlSource = $this->getAccessibleMock(YamlSource::class, ['dummy'], [], '', false);
114
115 $input = [
116 'EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/OverruleNonArrayValuesOverArrayValues1.yaml',
117 'EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/OverruleNonArrayValuesOverArrayValues2.yaml'
118 ];
119
120 $expected = [
121 'Form' => [
122 'klaus01' => null,
123 'key03' => 'value2',
124 ],
125 ];
126
127 $this->assertSame($expected, $mockYamlSource->_call('load', $input));
128 }
129
130 /**
131 * @return array
132 */
133 public function mergeRecursiveWithOverruleCalculatesExpectedResultDataProvider()
134 {
135 return [
136 'Override array can reset string to array' => [
137 [
138 'first' => [
139 'second' => 'foo',
140 ],
141 ],
142 [
143 'first' => [
144 'second' => ['third' => 'bar'],
145 ],
146 ],
147 [
148 'first' => [
149 'second' => ['third' => 'bar'],
150 ],
151 ],
152 ],
153 'Override array does reset array to string' => [
154 [
155 'first' => [],
156 ],
157 [
158 'first' => 'foo',
159 ],
160 [
161 'first' => 'foo', // Note that ArrayUtility::mergeRecursiveWithOverrule returns [] here
162 ],
163 ],
164 'Override array does override null with string' => [
165 [
166 'first' => null,
167 ],
168 [
169 'first' => 'foo',
170 ],
171 [
172 'first' => 'foo',
173 ],
174 ],
175 'Override array does override null with empty string' => [
176 [
177 'first' => null,
178 ],
179 [
180 'first' => '',
181 ],
182 [
183 'first' => '',
184 ],
185 ],
186 'Override array does override string with null' => [
187 [
188 'first' => 'foo',
189 ],
190 [
191 'first' => null,
192 ],
193 [
194 'first' => null, // Note that ArrayUtility::mergeRecursiveWithOverrule returns 'foo' here
195 ],
196 ],
197 'Override array does override null with null' => [
198 [
199 'first' => null,
200 ],
201 [
202 'first' => null,
203 ],
204 [
205 'first' => null, // Note that ArrayUtility::mergeRecursiveWithOverrule returns '' here
206 ],
207 ],
208 'Override can add keys' => [
209 [
210 'first' => 'foo',
211 ],
212 [
213 'second' => 'bar',
214 ],
215 [
216 'first' => 'foo',
217 'second' => 'bar',
218 ],
219 ],
220 ];
221 }
222
223 /**
224 * Note the data provider is similar to the data provider for ArrayUtility::mergeRecursiveWithOverrule()
225 *
226 * @test
227 * @dataProvider mergeRecursiveWithOverruleCalculatesExpectedResultDataProvider
228 * @param array $input1 Input 1
229 * @param array $input2 Input 2
230 * @param array $expected expected array
231 */
232 public function mergeRecursiveWithOverruleCalculatesExpectedResult($input1, $input2, $expected)
233 {
234 $mockYamlSource = $this->getAccessibleMock(YamlSource::class, ['dummy'], [], '', false);
235 $mockYamlSource->_callRef('mergeRecursiveWithOverrule', $input1, $input2);
236 $this->assertSame($expected, $input1);
237 }
238 }