1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This is part of the webuni/front-matter package. |
5
|
|
|
* |
6
|
|
|
* (c) Martin Hasoň <[email protected]> |
7
|
|
|
* (c) Webuni s.r.o. <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Webuni\FrontMatter\Tests; |
14
|
|
|
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use Webuni\FrontMatter\Document; |
17
|
|
|
use Webuni\FrontMatter\FrontMatter; |
18
|
|
|
use Webuni\FrontMatter\Processor\JsonProcessor; |
19
|
|
|
use Webuni\FrontMatter\Processor\JsonWithoutBracesProcessor; |
20
|
|
|
use Webuni\FrontMatter\Processor\NeonProcessor; |
21
|
|
|
use Webuni\FrontMatter\Processor\TomlProcessor; |
22
|
|
|
|
23
|
|
|
class FrontMatterTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @dataProvider getYaml |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
public function testYaml($string, $data, $content, bool $hasFrontMatter) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$frontMatter = new FrontMatter(); |
31
|
|
|
$document = $frontMatter->parse($string); |
32
|
|
|
|
33
|
|
|
$this->assertSame($hasFrontMatter, $frontMatter->exists($string)); |
34
|
|
|
$this->assertDocument($data, $content, $document); |
35
|
|
|
$this->assertEquals($string, $frontMatter->dump($document)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @dataProvider getSeparator |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function testYamlWithCustomSeparator($string, $data, $content, bool $hasFrontMatter) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$frontMatter = new FrontMatter(null, '<!--', '-->'); |
44
|
|
|
$document = $frontMatter->parse($string); |
45
|
|
|
|
46
|
|
|
$this->assertSame($hasFrontMatter, $frontMatter->exists($string)); |
47
|
|
|
$this->assertDocument($data, $content, $document); |
48
|
|
|
$this->assertEquals($string, $frontMatter->dump($document)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @dataProvider getJson |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function testJson($string, $data, $content, bool $hasFrontMatter) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$frontMatter = new FrontMatter(new JsonProcessor()); |
57
|
|
|
$document = $frontMatter->parse($string); |
58
|
|
|
|
59
|
|
|
$this->assertSame($hasFrontMatter, $frontMatter->exists($string)); |
60
|
|
|
$this->assertDocument($data, $content, $document); |
61
|
|
|
$this->assertEquals($string, $frontMatter->dump($document)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @dataProvider getPlainJson |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function testPlainJson($string, $data, $content, bool $hasFrontMatter) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$frontMatter = new FrontMatter(new JsonWithoutBracesProcessor(), '{', '}'); |
70
|
|
|
$document = $frontMatter->parse($string); |
71
|
|
|
|
72
|
|
|
$this->assertSame($hasFrontMatter, $frontMatter->exists($string)); |
73
|
|
|
$this->assertDocument($data, $content, $document); |
74
|
|
|
$this->assertEquals($string, $frontMatter->dump($document)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @dataProvider getYaml |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function testNeon($string, $data, $content, bool $hasFrontMatter) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$frontMatter = new FrontMatter(new NeonProcessor()); |
83
|
|
|
$document = $frontMatter->parse($string); |
84
|
|
|
|
85
|
|
|
$this->assertSame($hasFrontMatter, $frontMatter->exists($string)); |
86
|
|
|
$this->assertDocument($data, $content, $document); |
87
|
|
|
$this->assertEquals($string, $frontMatter->dump($document)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @dataProvider getToml |
92
|
|
|
*/ |
93
|
|
|
public function testToml($string, $data, $content, bool $hasFrontMatter) |
94
|
|
|
{ |
95
|
|
|
$frontMatter = new FrontMatter(new TomlProcessor()); |
96
|
|
|
$document = $frontMatter->parse($string); |
97
|
|
|
|
98
|
|
|
$this->assertSame($hasFrontMatter, $frontMatter->exists($string)); |
99
|
|
|
$this->assertDocument($data, $content, $document); |
100
|
|
|
|
101
|
|
|
$this->expectException(\BadMethodCallException::class); |
102
|
|
|
$this->expectExceptionMessage('Dump for Toml is not implemented.'); |
103
|
|
|
|
104
|
|
|
$this->assertEquals($string, $frontMatter->dump($document)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function getYaml() |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
return [ |
110
|
|
|
['foo', [], 'foo', false], |
111
|
|
|
["---\nfoo: bar\n---\n", ['foo' => 'bar'], '', true], |
112
|
|
|
["---\nfoo: bar\n---\ntext", ['foo' => 'bar'], 'text', true], |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function getSeparator() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
return [ |
119
|
|
|
['foo', [], 'foo', false], |
120
|
|
|
["<!--\nfoo: bar\n-->\n", ['foo' => 'bar'], '', true], |
121
|
|
|
["<!--\nfoo: bar\n-->\ntext", ['foo' => 'bar'], 'text', true], |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
View Code Duplication |
public function getJson() |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
return [ |
128
|
|
|
['foo', [], 'foo', false], |
129
|
|
|
["---\n{\"foo\":\"bar\"}\n---\n", ['foo' => 'bar'], '', true], |
130
|
|
|
["---\n{\"foo\":\"bar\"}\n---\ntext", ['foo' => 'bar'], 'text', true], |
131
|
|
|
]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function getPlainJson() |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
return [ |
137
|
|
|
['foo', [], 'foo', false], |
138
|
|
|
["{\n\"foo\":\"bar\"\n}\n", ['foo' => 'bar'], '', true], |
139
|
|
|
["{\n\"foo\":\"bar\"\n}\ntext", ['foo' => 'bar'], 'text', true], |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
View Code Duplication |
public function getToml() |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
return [ |
146
|
|
|
['foo', [], 'foo', false], |
147
|
|
|
["---\nfoo = 'bar'\n---\n", ['foo' => 'bar'], '', true], |
148
|
|
|
["---\nfoo = 'bar'\n---\ntext", ['foo' => 'bar'], 'text', true], |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function assertDocument($data, $content, Document $document) |
153
|
|
|
{ |
154
|
|
|
$this->assertEquals($data, $document->getData()); |
155
|
|
|
$this->assertEquals($content, $document->getContent()); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.