[BUGFIX] Add missing namespace parts
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Configuration / Loader / YamlFileLoaderTest.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Configuration\Loader;
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
18 use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader;
19
20 /**
21 * Test case for the yaml file loader class
22 */
23 class YamlFileLoaderTest extends \TYPO3\Components\TestingFramework\Core\Unit\UnitTestCase
24 {
25 /**
26 * Generic method to check if the load method returns an array from a YAML file
27 * @test
28 */
29 public function load()
30 {
31 $fileName = 'Berta.yml';
32 $fileContents = '
33 options:
34 - option1
35 - option2
36 betterthanbefore: 1
37 ';
38
39 $expected = [
40 'options' => [
41 'option1',
42 'option2'
43 ],
44 'betterthanbefore' => 1
45 ];
46
47 // Accessible mock to $subject since getFileContents calls GeneralUtility methods
48 $subject = $this->getAccessibleMock(YamlFileLoader::class, ['getFileContents']);
49 $subject->expects($this->once())->method('getFileContents')->with($fileName)->willReturn($fileContents);
50 $output = $subject->load($fileName);
51 $this->assertSame($expected, $output);
52 }
53
54 /**
55 * Method checking for imports that they have been processed properly
56 * @test
57 */
58 public function loadWithAnImport()
59 {
60 $fileName = 'Berta.yml';
61 $fileContents = '
62 imports:
63 - { resource: Secondfile.yml }
64
65 options:
66 - option1
67 - option2
68 betterthanbefore: 1
69 ';
70
71 $importFileName = 'Secondfile.yml';
72 $importFileContents = '
73 options:
74 - optionBefore
75 betterthanbefore: 2
76 ';
77
78 $expected = [
79 'options' => [
80 'optionBefore',
81 'option1',
82 'option2'
83 ],
84 'betterthanbefore' => 1
85 ];
86
87 // Accessible mock to $subject since getFileContents calls GeneralUtility methods
88 $subject = $this->getAccessibleMock(YamlFileLoader::class, ['getFileContents']);
89 $subject->expects($this->at(0))->method('getFileContents')->with($fileName)->willReturn($fileContents);
90 $subject->expects($this->at(1))->method('getFileContents')->with($importFileName)->willReturn($importFileContents);
91 $output = $subject->load($fileName);
92 $this->assertSame($expected, $output);
93 }
94
95 /**
96 * Method checking for placeholders
97 * @test
98 */
99 public function loadWithPlacholders()
100 {
101 $fileName = 'Berta.yml';
102 $fileContents = '
103
104 firstset:
105 myinitialversion: 13
106 options:
107 - option1
108 - option2
109 betterthanbefore: %firstset.myinitialversion%
110 ';
111
112 $expected = [
113 'firstset' => [
114 'myinitialversion' => 13
115 ],
116 'options' => [
117 'option1',
118 'option2'
119 ],
120 'betterthanbefore' => 13
121 ];
122
123 // Accessible mock to $subject since getFileContents calls GeneralUtility methods
124 $subject = $this->getAccessibleMock(YamlFileLoader::class, ['getFileContents']);
125 $subject->expects($this->once())->method('getFileContents')->with($fileName)->willReturn($fileContents);
126 $output = $subject->load($fileName);
127 $this->assertSame($expected, $output);
128 }
129
130 /**
131 * dataprovider for tests isPlaceholderTest
132 * @return array
133 */
134 public function isPlaceholderDataProvider()
135 {
136 return [
137 'regular string' => [
138 'berta13',
139 false
140 ],
141 'regular array' => [
142 ['berta13'],
143 false
144 ],
145 'regular float' => [
146 13.131313,
147 false
148 ],
149 'regular int' => [
150 13,
151 false
152 ],
153 'invalid placeholder with only % at the beginning' => [
154 '%cool',
155 false
156 ],
157 'invalid placeholder with only % at the end' => [
158 'cool%',
159 false
160 ],
161 'invalid placeholder with two % but not at the end' => [
162 '%cool%again',
163 false
164 ],
165 'invalid placeholder with two % but not at the beginning nor end' => [
166 'did%you%know',
167 false
168 ],
169 'valid placeholder with just numbers' => [
170 '%13%',
171 true
172 ],
173 'valid placeholder' => [
174 '%foo%baracks%',
175 true
176 ],
177 ];
178 }
179
180 /**
181 * @dataProvider isPlaceholderDataProvider
182 * @test
183 * @param mixed $placeholderValue
184 * @param bool $expected
185 * @skip
186 */
187 public function isPlaceholderTest($placeholderValue, bool $expected)
188 {
189 $subject = $this->getAccessibleMock(YamlFileLoader::class, ['dummy']);
190 $output = $subject->_call('isPlaceholder', $placeholderValue);
191 $this->assertSame($expected, $output);
192 }
193 }