FrontMatterTest::getPlainJson()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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