Skip to content

Commit 81352e2

Browse files
committed
[TASK] Add tests to verify ConfigDumper working properly
1 parent 3efd4f7 commit 81352e2

File tree

2 files changed

+159
-4
lines changed

2 files changed

+159
-4
lines changed

src/ConfigDumper.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,9 @@ public function dumpToFile(array $config, string $file, string $comment = ''): b
4949
$fileContent .= Yaml::dump(['processors' => $config['processors']], 2) . chr(10);
5050
unset($config['processors']);
5151
}
52-
53-
if (!empty($config)) {
52+
if ($fileContent === '' || !empty($config)) {
5453
$fileContent .= Yaml::dump($config, 6);
5554
}
56-
5755
break;
5856
case 'php':
5957
default:
@@ -80,7 +78,7 @@ private function generateCommentBlock(string $comment, string $commentChar = '//
8078
array_map(function ($line) use ($commentChar) {
8179
return $commentChar . ' ' . $line;
8280
}, explode(chr(10), $comment))
83-
);
81+
) . chr(10);
8482
}
8583

8684
/**

tests/Unit/ConfigDumperTest.php

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace Helhum\TYPO3\ConfigHandling\Tests\Unit;
4+
5+
use Helhum\TYPO3\ConfigHandling\ConfigDumper;
6+
use Helhum\TYPO3\ConfigHandling\Processor\RemoveSettingsProcessor;
7+
use org\bovigo\vfs\vfsStream;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class ConfigDumperTest extends TestCase
11+
{
12+
/**
13+
* @test
14+
*/
15+
public function dumpingEmptyArrayProducesYamlWithEmptyArray(): void
16+
{
17+
vfsStream::setup();
18+
$root = vfsStream::url('root');
19+
$settingsFile = $root . '/settings.yaml';
20+
$configDumper = new ConfigDumper();
21+
$configDumper->dumpToFile(
22+
[],
23+
$settingsFile
24+
);
25+
self::assertSame(
26+
'{ }',
27+
file_get_contents($settingsFile),
28+
);
29+
}
30+
31+
/**
32+
* @test
33+
*/
34+
public function dumpingConfigWithOnlyProcessorConfigDoesProduceValidYaml(): void
35+
{
36+
vfsStream::setup();
37+
$root = vfsStream::url('root');
38+
$settingsFile = $root . '/settings.yaml';
39+
$configDumper = new ConfigDumper();
40+
$configDumper->dumpToFile(
41+
[
42+
'processors' => [
43+
'foo' => [
44+
'class' => RemoveSettingsProcessor::class,
45+
'paths' => [
46+
'"SYS"."sqlDebug"',
47+
],
48+
],
49+
],
50+
],
51+
$settingsFile
52+
);
53+
self::assertSame(
54+
'processors:' . chr(10) . ' foo: { class: Helhum\TYPO3\ConfigHandling\Processor\RemoveSettingsProcessor, paths: [\'"SYS"."sqlDebug"\'] }' . chr(10) . chr(10),
55+
file_get_contents($settingsFile),
56+
);
57+
}
58+
59+
/**
60+
* @test
61+
*/
62+
public function dumpingConfigWithOnlyImportConfigDoesProduceValidYaml(): void
63+
{
64+
vfsStream::setup();
65+
$root = vfsStream::url('root');
66+
$settingsFile = $root . '/settings.yaml';
67+
$configDumper = new ConfigDumper();
68+
$configDumper->dumpToFile(
69+
[
70+
'imports' => [
71+
[
72+
'resource' => 'foo.yaml',
73+
],
74+
],
75+
],
76+
$settingsFile
77+
);
78+
self::assertSame(
79+
'imports:' . chr(10) . ' - { resource: foo.yaml }' . chr(10) . chr(10),
80+
file_get_contents($settingsFile),
81+
);
82+
}
83+
84+
/**
85+
* @test
86+
*/
87+
public function dumpingConfigWithImportAndProcessorsProduceNiceOutput(): void
88+
{
89+
vfsStream::setup();
90+
$root = vfsStream::url('root');
91+
$settingsFile = $root . '/settings.yaml';
92+
$configDumper = new ConfigDumper();
93+
$configDumper->dumpToFile(
94+
[
95+
'processors' => [
96+
'foo' => [
97+
'class' => RemoveSettingsProcessor::class,
98+
'paths' => [
99+
'"SYS"."sqlDebug"',
100+
],
101+
],
102+
],
103+
'imports' => [
104+
[
105+
'resource' => 'foo.yaml',
106+
],
107+
],
108+
'foo' => 'bar'
109+
],
110+
$settingsFile
111+
);
112+
self::assertSame(
113+
'imports:' . chr(10) . ' - { resource: foo.yaml }' . chr(10) .
114+
'processors:' . chr(10) . ' foo: { class: Helhum\TYPO3\ConfigHandling\Processor\RemoveSettingsProcessor, paths: [\'"SYS"."sqlDebug"\'] }' . chr(10) . chr(10) .
115+
'foo: bar' . chr(10),
116+
file_get_contents($settingsFile),
117+
);
118+
}
119+
120+
/**
121+
* @test
122+
*/
123+
public function commentBlockIsAddedProperly(): void
124+
{
125+
vfsStream::setup();
126+
$root = vfsStream::url('root');
127+
$settingsFile = $root . '/settings.yaml';
128+
$configDumper = new ConfigDumper();
129+
$configDumper->dumpToFile(
130+
[
131+
'processors' => [
132+
'foo' => [
133+
'class' => RemoveSettingsProcessor::class,
134+
'paths' => [
135+
'"SYS"."sqlDebug"',
136+
],
137+
],
138+
],
139+
'imports' => [
140+
[
141+
'resource' => 'foo.yaml',
142+
],
143+
],
144+
'foo' => 'bar'
145+
],
146+
$settingsFile,
147+
'MY COMMENT'
148+
);
149+
self::assertSame(
150+
'# MY COMMENT' . chr(10) .
151+
'imports:' . chr(10) . ' - { resource: foo.yaml }' . chr(10) .
152+
'processors:' . chr(10) . ' foo: { class: Helhum\TYPO3\ConfigHandling\Processor\RemoveSettingsProcessor, paths: [\'"SYS"."sqlDebug"\'] }' . chr(10) . chr(10) .
153+
'foo: bar' . chr(10),
154+
file_get_contents($settingsFile),
155+
);
156+
}
157+
}

0 commit comments

Comments
 (0)