‪TYPO3CMS  ‪main
YamlFileLoaderTest.php
Go to the documentation of this file.
1 <?php
2 
3 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 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
25 final class ‪YamlFileLoaderTest extends UnitTestCase
26 {
27  public static function ‪loadWithEnvVarDataProvider(): array
28  {
29  return [
30  'plain' => [
31  ['foo=heinz'],
32  'carl: \'%env(foo)%\'',
33  ['carl' => 'heinz'],
34  ],
35  'quoted var' => [
36  ['foo=heinz'],
37  "carl: '%env(''foo'')%'",
38  ['carl' => 'heinz'],
39  ],
40  'double quoted var' => [
41  ['foo=heinz'],
42  "carl: '%env(\"foo\")%'",
43  ['carl' => 'heinz'],
44  ],
45  'var in the middle' => [
46  ['foo=heinz'],
47  "carl: 'https://%env(foo)%/foo'",
48  ['carl' => 'https://heinz/foo'],
49  ],
50  'quoted var in the middle' => [
51  ['foo=heinz'],
52  "carl: 'https://%env(''foo'')%/foo'",
53  ['carl' => 'https://heinz/foo'],
54  ],
55  'double quoted var in the middle' => [
56  ['foo=heinz'],
57  "carl: 'https://%env(\"foo\")%/foo'",
58  ['carl' => 'https://heinz/foo'],
59  ],
60  'two env vars' => [
61  ['foo=karl', 'bar=heinz'],
62  'carl: \'%env(foo)%::%env(bar)%\'',
63  ['carl' => 'karl::heinz'],
64  ],
65  'three env vars' => [
66  ['foo=karl', 'bar=heinz', 'baz=bencer'],
67  'carl: \'%env(foo)%::%env(bar)%::%env(baz)%\'',
68  ['carl' => 'karl::heinz::bencer'],
69  ],
70  'three env vars with baz being undefined' => [
71  ['foo=karl', 'bar=heinz'],
72  'carl: \'%env(foo)%::%env(bar)%::%env(baz)%\'',
73  ['carl' => 'karl::heinz::%env(baz)%'],
74  ],
75  'three undefined env vars' => [
76  [],
77  'carl: \'%env(foo)%::%env(bar)%::%env(baz)%\'',
78  ['carl' => '%env(foo)%::%env(bar)%::%env(baz)%'],
79  ],
80  'nested env variables' => [
81  ['foo=bar', 'bar=heinz'],
82  'carl: \'%env(%env(foo)%)%\'',
83  ['carl' => 'heinz'],
84  ],
85  ];
86  }
87 
91  #[DataProvider('loadWithEnvVarDataProvider')]
92  #[Test]
93  public function ‪loadWithEnvVarPlaceholders(array $envs, string $yamlContent, array $expected): void
94  {
95  foreach ($envs as $env) {
96  putenv($env);
97  }
98  $fileName = 'Berta.yml';
99  $fileContents = $yamlContent;
100 
101  // Accessible mock to $subject since getFileContents calls GeneralUtility methods
102  $subject = $this->getAccessibleMock(YamlFileLoader::class, ['getFileContents', 'getStreamlinedFileName']);
103  $subject->expects(self::once())->method('getStreamlinedFileName')->with($fileName)->willReturn($fileName);
104  $subject->expects(self::once())->method('getFileContents')->with($fileName)->willReturn($fileContents);
105  ‪$output = $subject->load($fileName);
106  self::assertSame($expected, ‪$output);
107  putenv('foo=');
108  putenv('bar=');
109  putenv('baz=');
110  }
111 
115  #[Test]
117  {
118  $fileName = 'Berta.yml';
119  $fileContents = '
120 
121 firstset:
122  myinitialversion: 13
123 options:
124  - option1
125  - option2
126 betterthanbefore: \'%env(mynonexistingenv)%\'
127 ';
128 
129  $expected = [
130  'firstset' => [
131  'myinitialversion' => 13,
132  ],
133  'options' => [
134  'option1',
135  'option2',
136  ],
137  'betterthanbefore' => '%env(mynonexistingenv)%',
138  ];
139 
140  // Accessible mock to $subject since getFileContents calls GeneralUtility methods
141  $subject = $this->getAccessibleMock(YamlFileLoader::class, ['getFileContents', 'getStreamlinedFileName']);
142  $subject->expects(self::once())->method('getStreamlinedFileName')->with($fileName)->willReturn($fileName);
143  $subject->expects(self::once())->method('getFileContents')->with($fileName)->willReturn($fileContents);
144  ‪$output = $subject->load($fileName);
145  self::assertSame($expected, ‪$output);
146  }
147 
151  public static function ‪isPlaceholderDataProvider(): array
152  {
153  return [
154  'regular string' => [
155  'berta13',
156  false,
157  ],
158  'regular array' => [
159  ['berta13'],
160  false,
161  ],
162  'regular float' => [
163  13.131313,
164  false,
165  ],
166  'regular int' => [
167  13,
168  false,
169  ],
170  'invalid placeholder with only % at the beginning' => [
171  '%cool',
172  false,
173  ],
174  'invalid placeholder with only % at the end' => [
175  'cool%',
176  false,
177  ],
178  'invalid placeholder with two % but not at the end' => [
179  '%cool%again',
180  true,
181  ],
182  'invalid placeholder with two % but not at the beginning nor end' => [
183  'did%you%know',
184  true,
185  ],
186  'valid placeholder with just numbers' => [
187  '%13%',
188  true,
189  ],
190  'valid placeholder' => [
191  '%foo%baracks%',
192  true,
193  ],
194  ];
195  }
196 
197  #[DataProvider('isPlaceholderDataProvider')]
198  #[Test]
199  public function ‪containsPlaceholderTest(mixed $placeholderValue, bool $expected): void
200  {
201  $subject = $this->getAccessibleMock(YamlFileLoader::class, null);
202  ‪$output = $subject->_call('containsPlaceholder', $placeholderValue);
203  self::assertSame($expected, ‪$output);
204  }
205 }
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\isPlaceholderDataProvider
‪static isPlaceholderDataProvider()
Definition: YamlFileLoaderTest.php:151
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\containsPlaceholderTest
‪containsPlaceholderTest(mixed $placeholderValue, bool $expected)
Definition: YamlFileLoaderTest.php:199
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\loadWithEnvVarDataProvider
‪static loadWithEnvVarDataProvider()
Definition: YamlFileLoaderTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader
Definition: YamlFileLoaderTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest
Definition: YamlFileLoaderTest.php:26
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\loadWithEnvVarPlaceholders
‪loadWithEnvVarPlaceholders(array $envs, string $yamlContent, array $expected)
Definition: YamlFileLoaderTest.php:93
‪$output
‪$output
Definition: annotationChecker.php:114
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader
Definition: YamlFileLoader.php:47
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\loadWithEnvVarPlaceholdersDoesNotReplaceWithNonExistingValues
‪loadWithEnvVarPlaceholdersDoesNotReplaceWithNonExistingValues()
Definition: YamlFileLoaderTest.php:116