-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNaclTest.php
250 lines (205 loc) · 6.47 KB
/
NaclTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
declare(strict_types=1);
namespace Nuglif\Nacl;
class NaclTest extends \PHPUnit\Framework\TestCase
{
private Parser $parser;
public static function setUpBeforeClass(): void
{
define('TEST_CONST', 'value');
putenv('TEST=valid');
}
public static function getJsonFiles(): iterable
{
$files = glob(__DIR__ . '/json/*.json');
return array_map(fn($f) => [$f], $files);
}
/**
* @dataProvider getJsonFiles
* @test
*/
public function naclIsJsonCompatible(string $jsonFile): void
{
$this->assertSame(
json_decode(file_get_contents($jsonFile), true, 512, JSON_THROW_ON_ERROR),
Nacl::parseFile($jsonFile)
);
}
public static function getNaclFiles(): iterable
{
$files = glob(__DIR__ . '/nacl/*.conf');
return array_map(function ($f) {
@unlink($f . '.out');
return [$f, str_replace('.conf', '.json', $f) ];
}, $files);
}
/**
* @dataProvider getNaclFiles
* @test
*/
public function testNacl(string $naclFile, string $jsonFile): void
{
$this->parser = Nacl::createParser();
$this->parser->registerMacro(new Macros\Callback('testMacroWithOptions', fn($p, $a = []) => [
'param' => $p,
'options' => $a,
]));
$this->parser->registerMacro(new Macros\Callback('testMacro', fn($p, $a = []) => $p));
$this->parser->registerMacro(new Macros\Callback('json_encode', fn($p) => json_encode($p, JSON_THROW_ON_ERROR)));
$this->parser->setVariable('BAR', 'bar');
$this->parser->setVariable('MY_VAR', 'my var value');
$result = $this->parser->parseFile($naclFile);
try {
$this->assertEquals(
file_exists($jsonFile) ? json_decode(file_get_contents($jsonFile), true, 512, JSON_THROW_ON_ERROR) : [],
$result
);
} catch (\Exception $e) {
file_put_contents($naclFile . '.out', json_encode($result, JSON_PRETTY_PRINT));
throw $e;
}
}
/**
* @test
*/
public function testUnterminatedString(): void
{
$this->expectException(LexingException::class);
$this->expectExceptionMessage('Unterminated string');
Nacl::parse('foo="bar');
}
/**
* @test
*/
public function testUnterminatedHeredoc(): void
{
$this->expectException(LexingException::class);
$this->expectExceptionMessage('Unterminated HEREDOC');
Nacl::parse("foo=<<<TEST\n");
}
/**
* @test
*/
public function testUnterminatedMultilineComment(): void
{
$this->expectException(LexingException::class);
$this->expectExceptionMessage('Unterminated multiline comment');
Nacl::parse('/*');
}
/**
* @test
*/
public function testParseErrorMessageWithTokenName(): void
{
$this->expectException(ParsingException::class);
$this->expectExceptionMessage('Syntax error, unexpected \'10\' (T_NUM)');
Nacl::parse('foo bar baz {}10;');
}
/**
* @test
*/
public function testParseErrorMesageWithoutTokenName(): void
{
$this->expectException(ParsingException::class);
$this->expectExceptionMessage('Syntax error, unexpected \';\'');
Nacl::parse('+;');
}
/**
* @test
*/
public function parsingUnexistingFileThrowInvalidArgumentException(): void
{
$this->expectException(\InvalidArgumentException::class);
Nacl::parseFile('error');
}
/**
* @test
*/
public function scalarValueMacroInsideObjectNodeThrowParsingException(): void
{
$this->expectException(ParsingException::class);
$this->expectExceptionMessage('Macro without assignation key must return an object');
Nacl::parse('foo bar; .env unexistingenv');
}
/**
* @test
*/
public function contentAfterScalarRootValueThrowsParsingExcpetion(): void
{
$this->expectException(ParsingException::class);
$this->expectExceptionMessage('Syntax error, unexpected \'foo\' (T_NAME)');
Nacl::parse('.env unexistingenv; foo bar');
}
/**
* @test
*/
public function testRegisterMacro(): void
{
Nacl::registerMacro($macro = new Macros\Callback('strtoupper', fn($p) => strtoupper($p)));
$this->assertSame(['foo' => 'BAR'], Nacl::parse('foo .strtoupper bar'));
$this->assertSame(['foo' => 'BAR'], Nacl::parse('${BAR} = .strtoupper bar; foo ${BAR};'));
}
/**
* @test
*/
public function testLazyMacroExecution(): void
{
$this->parser = Nacl::createParser();
$this->parser->registerMacro($macro = new Macros\Callback('error', function ($p) {
throw new \Exception($p);
}));
$this->assertSame(['foo' => 'bar'], $this->parser->parse('foo .error "FAIL"; foo bar;'));
}
/**
* @test
*/
public function refWithoutString(): void
{
$this->expectException(ReferenceException::class);
$this->expectExceptionMessage('.ref expects parameter to be string, array given.');
Nacl::parse('ref .ref {}');
}
/**
* @test
*/
public function refWithUnexistingStringAndDefaultValue(): void
{
$result = Nacl::parse('foo .ref (default: bar) "/app/foo"');
$this->assertSame([ 'foo' => 'bar' ], $result);
}
/**
* @test
*/
public function circularDependencyDetection(): void
{
$this->expectException(ReferenceException::class);
$this->expectExceptionMessage('Circular dependence detected.');
Nacl::parse('foo .ref "./bar"; bar .ref "foo"');
}
/**
* @test
*/
public function undefinedRef(): void
{
$this->expectException(ReferenceException::class);
$this->expectExceptionMessage('Undefined property: bar.');
Nacl::parse('foo .ref "bar";');
}
/**
* @test
*/
public function fileMacroWithUnexistingFile(): void
{
$this->expectException(ParsingException::class);
$this->expectExceptionMessage('Unable to read file \'unknown.txt\'');
Nacl::parse('foo .file "unknown.txt"');
}
/**
* @test
*/
public function fileMacroWithUnexistingFileAndDefaultValue(): void
{
$result = Nacl::parse('foo .file (default: bar) "unknown.txt"');
$this->assertSame([ 'foo' => 'bar' ], $result);
}
}