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 Webuni\FrontMatter\Document; |
16
|
|
|
use Webuni\FrontMatter\FrontMatter; |
17
|
|
|
use Webuni\FrontMatter\Processor\JsonProcessor; |
18
|
|
|
use Webuni\FrontMatter\Processor\JsonWithoutBracesProcessor; |
19
|
|
|
use Webuni\FrontMatter\Processor\NeonProcessor; |
20
|
|
|
use Webuni\FrontMatter\Processor\TomlProcessor; |
21
|
|
|
|
22
|
|
|
class FrontMatterTest extends \PHPUnit_Framework_TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @dataProvider getYaml |
26
|
|
|
*/ |
27
|
|
|
public function testYaml($string, $data, $content) |
28
|
|
|
{ |
29
|
|
|
$frontMatter = new FrontMatter(); |
30
|
|
|
$document = $frontMatter->parse($string); |
31
|
|
|
|
32
|
|
|
$this->assertDocument($data, $content, $document); |
33
|
|
|
$this->assertEquals($string, $frontMatter->dump($document)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @dataProvider getSeparator |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function testYamlWithCustomSeparator($string, $data, $content) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$frontMatter = new FrontMatter(null, '<!--', '-->'); |
42
|
|
|
$document = $frontMatter->parse($string); |
43
|
|
|
|
44
|
|
|
$this->assertDocument($data, $content, $document); |
45
|
|
|
$this->assertEquals($string, $frontMatter->dump($document)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @dataProvider getJson |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function testJson($string, $data, $content) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$frontMatter = new FrontMatter(new JsonProcessor()); |
54
|
|
|
$document = $frontMatter->parse($string); |
55
|
|
|
|
56
|
|
|
$this->assertDocument($data, $content, $document); |
57
|
|
|
$this->assertEquals($string, $frontMatter->dump($document)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @dataProvider getPlainJson |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function testPlainJson($string, $data, $content) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$frontMatter = new FrontMatter(new JsonWithoutBracesProcessor(), '{', '}'); |
66
|
|
|
$document = $frontMatter->parse($string); |
67
|
|
|
|
68
|
|
|
$this->assertDocument($data, $content, $document); |
69
|
|
|
$this->assertEquals($string, $frontMatter->dump($document)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @dataProvider getYaml |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function testNeon($string, $data, $content) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$frontMatter = new FrontMatter(new NeonProcessor()); |
78
|
|
|
$document = $frontMatter->parse($string); |
79
|
|
|
|
80
|
|
|
$this->assertDocument($data, $content, $document); |
81
|
|
|
$this->assertEquals($string, $frontMatter->dump($document)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @dataProvider getToml |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function testToml($string, $data, $content) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$frontMatter = new FrontMatter(new TomlProcessor()); |
90
|
|
|
$document = $frontMatter->parse($string); |
91
|
|
|
|
92
|
|
|
$this->assertDocument($data, $content, $document); |
93
|
|
|
|
94
|
|
|
$this->expectException(\BadMethodCallException::class); |
95
|
|
|
|
96
|
|
|
$this->assertEquals($string, $frontMatter->dump($document)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function getYaml() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
return [ |
102
|
|
|
['foo', [], 'foo'], |
103
|
|
|
["---\nfoo: bar\n---\n", ['foo' => 'bar'], ''], |
104
|
|
|
["---\nfoo: bar\n---\ntext", ['foo' => 'bar'], 'text'], |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
View Code Duplication |
public function getSeparator() |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
return [ |
111
|
|
|
['foo', [], 'foo'], |
112
|
|
|
["<!--\nfoo: bar\n-->\n", ['foo' => 'bar'], ''], |
113
|
|
|
["<!--\nfoo: bar\n-->\ntext", ['foo' => 'bar'], 'text'], |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getJson() |
118
|
|
|
{ |
119
|
|
|
return [ |
120
|
|
|
['foo', [], 'foo'], |
121
|
|
|
["---\n{\"foo\":\"bar\"}\n---\n", ['foo' => 'bar'], ''], |
122
|
|
|
["---\n{\"foo\":\"bar\"}\n---\ntext", ['foo' => 'bar'], 'text'], |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getPlainJson() |
127
|
|
|
{ |
128
|
|
|
return [ |
129
|
|
|
['foo', [], 'foo'], |
130
|
|
|
["{\n\"foo\":\"bar\"\n}\n", ['foo' => 'bar'], ''], |
131
|
|
|
["{\n\"foo\":\"bar\"\n}\ntext", ['foo' => 'bar'], 'text'], |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getToml() |
136
|
|
|
{ |
137
|
|
|
return [ |
138
|
|
|
['foo', [], 'foo'], |
139
|
|
|
["---\nfoo = 'bar'\n---\n", ['foo' => 'bar'], ''], |
140
|
|
|
["---\nfoo = 'bar'\n---\ntext", ['foo' => 'bar'], 'text'], |
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private function assertDocument($data, $content, Document $document) |
145
|
|
|
{ |
146
|
|
|
$this->assertEquals($data, $document->getData()); |
147
|
|
|
$this->assertEquals($content, $document->getContent()); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.