/
YamlTest.php
419 lines (343 loc) · 10.4 KB
/
YamlTest.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
namespace Tests\Yaml;
use Exception;
use Statamic\Facades\YAML;
use Statamic\Yaml\ParseException;
use Statamic\Yaml\Yaml as StatamicYaml;
use Symfony\Component\Yaml\Yaml as SymfonyYaml;
use Tests\TestCase;
class YamlTest extends TestCase
{
/** @test */
public function it_dumps_yaml()
{
$array = [
'foo' => 'bar',
'two_words' => 'two words',
'multiline' => "first\nsecond",
'array' => ['one', 'two'],
];
$symfonyYaml = $this->mock(SymfonyYaml::class)
->shouldReceive('dump')
->with($array, 100, 2, SymfonyYaml::DUMP_MULTI_LINE_LITERAL_BLOCK)
->once()
->andReturn('some properly dumped yaml from symfony')
->getMock();
$this->app->instance(StatamicYaml::class, new StatamicYaml($symfonyYaml));
$this->assertEquals('some properly dumped yaml from symfony', YAML::dump($array));
}
/** @test */
public function it_dumps_with_front_matter_when_content_is_passed()
{
$expected = <<<'EOT'
---
foo: bar
---
some content
EOT;
$this->assertEquals($expected, YAML::dump(['foo' => 'bar'], 'some content'));
}
/** @test */
public function it_dumps_without_front_matter_when_content_is_an_array()
{
$expected = <<<'EOT'
foo: bar
content:
baz: qux
EOT;
$this->assertEquals($expected, YAML::dump(['foo' => 'bar'], ['baz' => 'qux']));
}
/** @test */
public function it_dumps_without_front_matter_when_content_is_null()
{
$expected = <<<'EOT'
foo: bar
EOT;
$this->assertEquals($expected, YAML::dump(['foo' => 'bar'], null));
$this->assertEquals($expected, YAML::dump(['foo' => 'bar']));
}
/** @test */
public function it_explicitly_dumps_front_matter()
{
$expected = <<<'EOT'
---
foo: bar
---
EOT;
$this->assertEquals($expected, YAML::dumpFrontMatter(['foo' => 'bar']));
}
/** @test */
public function it_explicitly_dumps_front_matter_with_content()
{
$expected = <<<'EOT'
---
foo: bar
---
some content
EOT;
$this->assertEquals($expected, YAML::dumpFrontMatter(['foo' => 'bar'], 'some content'));
}
/** @test */
public function it_explicitly_dumps_front_matter_including_content_when_its_an_array()
{
$expected = <<<'EOT'
---
foo: bar
content:
baz: qux
---
EOT;
$this->assertEquals($expected, YAML::dumpFrontMatter(['foo' => 'bar'], ['baz' => 'qux']));
}
/** @test */
public function it_explicitly_dumps_front_matter_without_content_when_its_null()
{
$expected = <<<'EOT'
---
foo: bar
---
EOT;
$this->assertEquals($expected, YAML::dumpFrontMatter(['foo' => 'bar'], null));
$this->assertEquals($expected, YAML::dumpFrontMatter(['foo' => 'bar']));
}
/**
* @test
*
* @see https://github.com/statamic/cms/issues/3612
**/
public function it_dumps_front_matter_properly_when_symfony_yaml_dumper_doesnt_end_with_a_line_break()
{
$array = [
'foo' => 'bar',
'baz' => "first line\nsecond line", // the multiline string *must* be last for this bug
];
// We mock symfony because the multiline character is different depending on the version installed.
// It will be | on early versions. It will be |- on later versions.
// The |- character means trim trailing newlines, and is the default when dumping multiline strings.
$symfonyYaml = $this->mock(SymfonyYaml::class)
->shouldReceive('dump')
->with($array, 100, 2, SymfonyYaml::DUMP_MULTI_LINE_LITERAL_BLOCK)
->once()
->andReturn($symfonyDumpedYaml = "foo: bar\nbaz: |-\n first line\n second line")
->getMock();
$this->app->instance(StatamicYaml::class, new StatamicYaml($symfonyYaml));
// Without the bug fix, the --- would come immediately after the "second line". Like this:
// baz: |-
// first line
// second line---
// content
$expected = <<<EOT
---
$symfonyDumpedYaml
---
content
EOT;
$this->assertEquals($expected, YAML::dumpFrontMatter($array, 'content'));
}
/** @test */
public function it_parses_a_string_of_yaml()
{
$this->assertEquals(['foo' => 'bar'], YAML::parse('foo: bar'));
}
/** @test */
public function it_parses_an_empty_string_of_yaml()
{
$this->assertEquals([], YAML::parse(''));
}
/** @test */
public function it_parses_with_content_and_front_matter()
{
$yaml = "---\nfoo: bar\n---\nsome content";
$this->assertEquals(['foo' => 'bar', 'content' => 'some content'], YAML::parse($yaml));
}
/** @test */
public function it_parses_with_content_and_front_matter_with_crlf()
{
$yaml = "---\r\nfoo: bar\r\n---\r\nsome content";
$this->assertEquals(['foo' => 'bar', 'content' => 'some content'], YAML::parse($yaml));
}
/** @test */
public function it_parses_with_content_when_its_in_the_front_matter()
{
$yaml = <<<'EOT'
---
foo: bar
content: some content
---
EOT;
$this->assertEquals(['foo' => 'bar', 'content' => 'some content'], YAML::parse($yaml));
}
/** @test */
public function it_throws_exception_when_there_is_a_content_var_and_a_content_area()
{
$yaml = <<<'EOT'
---
foo: bar
content: some content
---
some text
EOT;
$this->expectException(ParseException::class);
YAML::parse($yaml);
}
/** @test */
public function it_parses_a_file_when_no_argument_is_given()
{
$yaml = <<<'EOT'
---
foo: bar
---
some content
EOT;
$fp = tmpfile();
fwrite($fp, $yaml);
$path = stream_get_meta_data($fp)['uri'];
$this->assertEquals(
['foo' => 'bar', 'content' => 'some content'],
YAML::file($path)->parse()
);
}
/** @test */
public function when_parsing_and_content_is_just_whitespace_it_treats_it_as_null()
{
$yaml = <<<'EOT'
---
foo: bar
---
EOT;
$this->assertEquals(['foo' => 'bar'], YAML::parse($yaml));
}
/** @test */
public function it_throws_exception_when_parsing_without_an_argument_or_file()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Cannot parse YAML without a file or string.');
YAML::parse();
}
/** @test */
public function it_creates_parse_exception_pointing_to_temporary_file_when_no_file_is_provided()
{
$yaml = <<<'EOT'
---
foo: 'bar
baz: 'qux'
---
some content
EOT;
try {
YAML::parse($yaml);
} catch (Exception $e) {
$this->assertInstanceOf(ParseException::class, $e);
$this->assertEquals('Unexpected characters near "qux\'" at line 3 (near "baz: \'qux\'").', $e->getMessage());
$path = storage_path('statamic/tmp/yaml/'.md5("---\nfoo: 'bar\nbaz: 'qux'"));
$this->assertEquals($path, $e->getFile());
return;
}
$this->fail('Exception was not thrown.');
}
/** @test */
public function it_creates_parse_exception_pointing_to_actual_file_when_file_is_provided_and_it_exists()
{
$yaml = <<<'EOT'
---
foo: 'bar
baz: 'qux'
---
some content
EOT;
file_put_contents($path = __DIR__.'/test.yaml', $yaml);
try {
YAML::file($path)->parse($yaml);
} catch (Exception $e) {
$this->assertInstanceOf(ParseException::class, $e);
$this->assertEquals('Unexpected characters near "qux\'" at line 3 (near "baz: \'qux\'").', $e->getMessage());
$this->assertEquals($path, $e->getFile());
@unlink($path);
return;
}
$this->fail('Exception was not thrown.');
}
/** @test */
public function it_creates_parse_exception_pointing_to_temporary_file_with_similar_path_when_file_is_provided_but_doesnt_exist()
{
$yaml = <<<'EOT'
---
foo: 'bar
baz: 'qux'
---
some content
EOT;
try {
YAML::file('path/to/file.yaml')->parse($yaml);
} catch (Exception $e) {
$this->assertInstanceOf(ParseException::class, $e);
$this->assertEquals('Unexpected characters near "qux\'" at line 3 (near "baz: \'qux\'").', $e->getMessage());
$this->assertEquals(storage_path('statamic/tmp/yaml/path/to/file.yaml'), $e->getFile());
return;
}
$this->fail('Exception was not thrown.');
}
/** @test */
public function it_doesnt_maintain_files_across_uses()
{
YAML::file('path/to/file/previously/used.yaml')->parse('foo: bar');
$yaml = <<<'EOT'
---
foo: 'bar
baz: 'qux'
---
some content
EOT;
try {
YAML::parse($yaml);
} catch (Exception $e) {
$this->assertInstanceOf(ParseException::class, $e);
$this->assertEquals('Unexpected characters near "qux\'" at line 3 (near "baz: \'qux\'").', $e->getMessage());
$path = storage_path('statamic/tmp/yaml/'.md5("---\nfoo: 'bar\nbaz: 'qux'"));
$this->assertEquals($path, $e->getFile());
return;
}
$this->fail('Exception was not thrown.');
}
/** @test */
public function it_doesnt_maintain_files_across_uses_when_previous_call_had_no_yaml()
{
YAML::file('path/to/file/previously/used.yaml')->parse('');
$yaml = <<<'EOT'
---
foo: 'bar
baz: 'qux'
---
some content
EOT;
try {
YAML::parse($yaml);
} catch (Exception $e) {
$this->assertInstanceOf(ParseException::class, $e);
$this->assertEquals('Unexpected characters near "qux\'" at line 3 (near "baz: \'qux\'").', $e->getMessage());
$path = storage_path('statamic/tmp/yaml/'.md5("---\nfoo: 'bar\nbaz: 'qux'"));
$this->assertEquals($path, $e->getFile());
return;
}
$this->fail('Exception was not thrown.');
}
/** @test */
public function it_throws_an_exception_when_an_array_cannot_be_returned()
{
$string = <<<'EOT'
<<< HEAD
An example when this happens
===
Is in a merge conflict
>>> BRANCH
EOT;
try {
YAML::parse($string);
} catch (Exception $e) {
$this->assertInstanceOf(ParseException::class, $e);
$this->assertEquals('Unable to parse (near "<<< HEAD").', $e->getMessage());
return;
}
$this->fail('Exception was not thrown.');
}
}