‪TYPO3CMS  9.5
YamlFileLoaderTest.php
Go to the documentation of this file.
1 <?php
2 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 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
20 
24 class ‪YamlFileLoaderTest extends UnitTestCase
25 {
30  public function ‪load()
31  {
32  $fileName = 'Berta.yml';
33  $fileContents = '
34 options:
35  - option1
36  - option2
37 betterthanbefore: 1
38 ';
39 
40  $expected = [
41  'options' => [
42  'option1',
43  'option2'
44  ],
45  'betterthanbefore' => 1
46  ];
47 
48  // Accessible mock to $subject since getFileContents calls GeneralUtility methods
49  $subject = $this->getAccessibleMock(YamlFileLoader::class, ['getFileContents']);
50  $subject->expects($this->once())->method('getFileContents')->with($fileName)->willReturn($fileContents);
51  ‪$output = $subject->load($fileName);
52  $this->assertSame($expected, ‪$output);
53  }
54 
59  public function ‪loadWithAnImport()
60  {
61  $fileName = 'Berta.yml';
62  $fileContents = '
63 imports:
64  - { resource: Secondfile.yml }
65 
66 options:
67  - option1
68  - option2
69 betterthanbefore: 1
70 ';
71 
72  $importFileName = 'Secondfile.yml';
73  $importFileContents = '
74 options:
75  - optionBefore
76 betterthanbefore: 2
77 ';
78 
79  $expected = [
80  'options' => [
81  'optionBefore',
82  'option1',
83  'option2'
84  ],
85  'betterthanbefore' => 1
86  ];
87 
88  // Accessible mock to $subject since getFileContents calls GeneralUtility methods
89  $subject = $this->getAccessibleMock(YamlFileLoader::class, ['getFileContents']);
90  $subject->expects($this->at(0))->method('getFileContents')->with($fileName)->willReturn($fileContents);
91  $subject->expects($this->at(1))->method('getFileContents')->with($importFileName)->willReturn($importFileContents);
92  ‪$output = $subject->load($fileName);
93  $this->assertSame($expected, ‪$output);
94  }
95 
100  public function ‪loadWithPlaceholders(): void
101  {
102  $fileName = 'Berta.yml';
103  $fileContents = '
104 
105 firstset:
106  myinitialversion: 13
107 options:
108  - option1
109  - option2
110 betterthanbefore: \'%firstset.myinitialversion%\'
111 ';
112 
113  $expected = [
114  'firstset' => [
115  'myinitialversion' => 13
116  ],
117  'options' => [
118  'option1',
119  'option2'
120  ],
121  'betterthanbefore' => 13
122  ];
123 
124  // Accessible mock to $subject since getFileContents calls GeneralUtility methods
125  $subject = $this->getAccessibleMock(YamlFileLoader::class, ['getFileContents']);
126  $subject->expects($this->once())->method('getFileContents')->with($fileName)->willReturn($fileContents);
127  ‪$output = $subject->load($fileName);
128  $this->assertSame($expected, ‪$output);
129  }
130 
131  public function ‪loadWithEnvVarDataProvider(): array
132  {
133  return [
134  'plain' => [
135  ['foo=heinz'],
136  'carl: \'%env(foo)%\'',
137  ['carl' => 'heinz']
138  ],
139  'quoted var' => [
140  ['foo=heinz'],
141  "carl: '%env(''foo'')%'",
142  ['carl' => 'heinz']
143  ],
144  'double quoted var' => [
145  ['foo=heinz'],
146  "carl: '%env(\"foo\")%'",
147  ['carl' => 'heinz']
148  ],
149  'var in the middle' => [
150  ['foo=heinz'],
151  "carl: 'https://%env(foo)%/foo'",
152  ['carl' => 'https://heinz/foo']
153  ],
154  'quoted var in the middle' => [
155  ['foo=heinz'],
156  "carl: 'https://%env(''foo'')%/foo'",
157  ['carl' => 'https://heinz/foo']
158  ],
159  'double quoted var in the middle' => [
160  ['foo=heinz'],
161  "carl: 'https://%env(\"foo\")%/foo'",
162  ['carl' => 'https://heinz/foo']
163  ],
164  'two env vars' => [
165  ['foo=karl', 'bar=heinz'],
166  'carl: \'%env(foo)%::%env(bar)%\'',
167  ['carl' => 'karl::heinz']
168  ],
169  'three env vars' => [
170  ['foo=karl', 'bar=heinz', 'baz=bencer'],
171  'carl: \'%env(foo)%::%env(bar)%::%env(baz)%\'',
172  ['carl' => 'karl::heinz::bencer']
173  ],
174  'three env vars with baz being undefined' => [
175  ['foo=karl', 'bar=heinz'],
176  'carl: \'%env(foo)%::%env(bar)%::%env(baz)%\'',
177  ['carl' => 'karl::heinz::%env(baz)%']
178  ],
179  'three undefined env vars' => [
180  [],
181  'carl: \'%env(foo)%::%env(bar)%::%env(baz)%\'',
182  ['carl' => '%env(foo)%::%env(bar)%::%env(baz)%']
183  ],
184  ];
185  }
186 
196  public function ‪loadWithEnvVarPlaceholders(array $envs, string $yamlContent, array $expected): void
197  {
198  foreach ($envs as $env) {
199  putenv($env);
200  }
201  $fileName = 'Berta.yml';
202  $fileContents = $yamlContent;
203 
204  // Accessible mock to $subject since getFileContents calls GeneralUtility methods
205  $subject = $this->getAccessibleMock(YamlFileLoader::class, ['getFileContents']);
206  $subject->expects($this->once())->method('getFileContents')->with($fileName)->willReturn($fileContents);
207  ‪$output = $subject->load($fileName);
208  $this->assertSame($expected, ‪$output);
209  putenv('foo=');
210  putenv('bar=');
211  putenv('baz=');
212  }
213 
220  {
221  $fileName = 'Berta.yml';
222  $fileContents = '
223 
224 firstset:
225  myinitialversion: 13
226 options:
227  - option1
228  - option2
229 betterthanbefore: \'%env(mynonexistingenv)%\'
230 ';
231 
232  $expected = [
233  'firstset' => [
234  'myinitialversion' => 13
235  ],
236  'options' => [
237  'option1',
238  'option2'
239  ],
240  'betterthanbefore' => '%env(mynonexistingenv)%'
241  ];
242 
243  // Accessible mock to $subject since getFileContents calls GeneralUtility methods
244  $subject = $this->getAccessibleMock(YamlFileLoader::class, ['getFileContents']);
245  $subject->expects($this->once())->method('getFileContents')->with($fileName)->willReturn($fileContents);
246  ‪$output = $subject->load($fileName);
247  $this->assertSame($expected, ‪$output);
248  }
249 
255  {
256  return [
257  'regular string' => [
258  'berta13',
259  false
260  ],
261  'regular array' => [
262  ['berta13'],
263  false
264  ],
265  'regular float' => [
266  13.131313,
267  false
268  ],
269  'regular int' => [
270  13,
271  false
272  ],
273  'invalid placeholder with only % at the beginning' => [
274  '%cool',
275  false
276  ],
277  'invalid placeholder with only % at the end' => [
278  'cool%',
279  false
280  ],
281  'invalid placeholder with two % but not at the end' => [
282  '%cool%again',
283  false
284  ],
285  'invalid placeholder with two % but not at the beginning nor end' => [
286  'did%you%know',
287  false
288  ],
289  'valid placeholder with just numbers' => [
290  '%13%',
291  true
292  ],
293  'valid placeholder' => [
294  '%foo%baracks%',
295  true
296  ],
297  ];
298  }
299 
307  public function ‪isPlaceholderTest($placeholderValue, bool $expected)
308  {
309  $subject = $this->getAccessibleMock(YamlFileLoader::class, ['dummy']);
310  ‪$output = $subject->_call('isPlaceholder', $placeholderValue);
311  $this->assertSame($expected, ‪$output);
312  }
313 }
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader
Definition: YamlFileLoaderTest.php:3
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\loadWithAnImport
‪loadWithAnImport()
Definition: YamlFileLoaderTest.php:59
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest
Definition: YamlFileLoaderTest.php:25
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\loadWithEnvVarDataProvider
‪loadWithEnvVarDataProvider()
Definition: YamlFileLoaderTest.php:131
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\isPlaceholderDataProvider
‪array isPlaceholderDataProvider()
Definition: YamlFileLoaderTest.php:254
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\isPlaceholderTest
‪isPlaceholderTest($placeholderValue, bool $expected)
Definition: YamlFileLoaderTest.php:307
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\loadWithPlaceholders
‪loadWithPlaceholders()
Definition: YamlFileLoaderTest.php:100
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\loadWithEnvVarPlaceholders
‪loadWithEnvVarPlaceholders(array $envs, string $yamlContent, array $expected)
Definition: YamlFileLoaderTest.php:196
‪$output
‪$output
Definition: annotationChecker.php:113
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader
Definition: YamlFileLoader.php:41
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\loadWithEnvVarPlaceholdersDoesNotReplaceWithNonExistingValues
‪loadWithEnvVarPlaceholdersDoesNotReplaceWithNonExistingValues()
Definition: YamlFileLoaderTest.php:219
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\load
‪load()
Definition: YamlFileLoaderTest.php:30