‪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 
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22 
23 final class ‪YamlFileLoaderTest extends UnitTestCase
24 {
25  public static function ‪loadWithEnvVarDataProvider(): array
26  {
27  return [
28  'plain' => [
29  ['foo=heinz'],
30  'carl: \'%env(foo)%\'',
31  ['carl' => 'heinz'],
32  ],
33  'quoted var' => [
34  ['foo=heinz'],
35  "carl: '%env(''foo'')%'",
36  ['carl' => 'heinz'],
37  ],
38  'double quoted var' => [
39  ['foo=heinz'],
40  "carl: '%env(\"foo\")%'",
41  ['carl' => 'heinz'],
42  ],
43  'var in the middle' => [
44  ['foo=heinz'],
45  "carl: 'https://%env(foo)%/foo'",
46  ['carl' => 'https://heinz/foo'],
47  ],
48  'quoted var in the middle' => [
49  ['foo=heinz'],
50  "carl: 'https://%env(''foo'')%/foo'",
51  ['carl' => 'https://heinz/foo'],
52  ],
53  'double quoted var in the middle' => [
54  ['foo=heinz'],
55  "carl: 'https://%env(\"foo\")%/foo'",
56  ['carl' => 'https://heinz/foo'],
57  ],
58  'two env vars' => [
59  ['foo=karl', 'bar=heinz'],
60  'carl: \'%env(foo)%::%env(bar)%\'',
61  ['carl' => 'karl::heinz'],
62  ],
63  'three env vars' => [
64  ['foo=karl', 'bar=heinz', 'baz=bencer'],
65  'carl: \'%env(foo)%::%env(bar)%::%env(baz)%\'',
66  ['carl' => 'karl::heinz::bencer'],
67  ],
68  'three env vars with baz being undefined' => [
69  ['foo=karl', 'bar=heinz'],
70  'carl: \'%env(foo)%::%env(bar)%::%env(baz)%\'',
71  ['carl' => 'karl::heinz::%env(baz)%'],
72  ],
73  'three undefined env vars' => [
74  [],
75  'carl: \'%env(foo)%::%env(bar)%::%env(baz)%\'',
76  ['carl' => '%env(foo)%::%env(bar)%::%env(baz)%'],
77  ],
78  'nested env variables' => [
79  ['foo=bar', 'bar=heinz'],
80  'carl: \'%env(%env(foo)%)%\'',
81  ['carl' => 'heinz'],
82  ],
83  ];
84  }
85 
92  public function ‪loadWithEnvVarPlaceholders(array $envs, string $yamlContent, array $expected): void
93  {
94  foreach ($envs as $env) {
95  putenv($env);
96  }
97  $fileName = 'Berta.yml';
98  $fileContents = $yamlContent;
99 
100  // Accessible mock to $subject since getFileContents calls GeneralUtility methods
101  $subject = $this->getAccessibleMock(YamlFileLoader::class, ['getFileContents', 'getStreamlinedFileName']);
102  $subject->expects(self::once())->method('getStreamlinedFileName')->with($fileName)->willReturn($fileName);
103  $subject->expects(self::once())->method('getFileContents')->with($fileName)->willReturn($fileContents);
104  ‪$output = $subject->load($fileName);
105  self::assertSame($expected, ‪$output);
106  putenv('foo=');
107  putenv('bar=');
108  putenv('baz=');
109  }
110 
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 
201  public function ‪containsPlaceholderTest(mixed $placeholderValue, bool $expected): void
202  {
203  $subject = $this->getAccessibleMock(YamlFileLoader::class, null);
204  ‪$output = $subject->_call('containsPlaceholder', $placeholderValue);
205  self::assertSame($expected, ‪$output);
206  }
207 }
‪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:201
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\loadWithEnvVarDataProvider
‪static loadWithEnvVarDataProvider()
Definition: YamlFileLoaderTest.php:25
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader
Definition: YamlFileLoaderTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest
Definition: YamlFileLoaderTest.php:24
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\loadWithEnvVarPlaceholders
‪loadWithEnvVarPlaceholders(array $envs, string $yamlContent, array $expected)
Definition: YamlFileLoaderTest.php:92
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader
Definition: YamlFileLoader.php:47
‪TYPO3\CMS\Core\Tests\Unit\Configuration\Loader\YamlFileLoaderTest\loadWithEnvVarPlaceholdersDoesNotReplaceWithNonExistingValues
‪loadWithEnvVarPlaceholdersDoesNotReplaceWithNonExistingValues()
Definition: YamlFileLoaderTest.php:116