DataToTwigConvertorTest::testNothing()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
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\Twig;
14
15
use PHPUnit\Framework\TestCase;
16
use Symfony\Component\Yaml\Yaml;
17
use Webuni\FrontMatter\Twig\DataToTwigConvertor;
18
19
class DataToTwigConvertorTest extends TestCase
20
{
21
    private $data;
22
23
    public function __construct()
24
    {
25
        parent::__construct();
26
        $this->data = Yaml::parse(file_get_contents(__DIR__.'/data.yaml'), Yaml::PARSE_DATETIME);
27
    }
28
29
    public function testNothing(): void
30
    {
31
        $convertor = DataToTwigConvertor::nothing();
32
        $this->assertEquals('', $convertor->convert($this->data));
33
    }
34
35
    public function testVars(): void
36
    {
37
        $convertor = DataToTwigConvertor::vars();
38
        $twig = '{% set foo = "bar" %}
39
{% set number = 1234 %}
40
{% set pi = 3.14159 %}
41
{% set date = (1464307200|date_modify(\'0sec\')) %}
42
{% set empty = null %}
43
{% set multiline = "Multiple\nLine\nString\n" %}
44
{% set object = {key: "value", datetime: (1605185652|date_modify(\'0sec\')), values: {0: "one", 1: "two", }, } %}
45
';
46
        $this->assertEquals($twig, $convertor->convert($this->data));
47
    }
48
49
    public function testOptionalVars(): void
50
    {
51
        $convertor = DataToTwigConvertor::vars(false);
52
        $twig = '{% set foo = foo is defined ? foo : "bar" %}
53
{% set number = number is defined ? number : 1234 %}
54
{% set pi = pi is defined ? pi : 3.14159 %}
55
{% set date = date is defined ? date : (1464307200|date_modify(\'0sec\')) %}
56
{% set empty = empty is defined ? empty : null %}
57
{% set multiline = multiline is defined ? multiline : "Multiple\nLine\nString\n" %}
58
{% set object = object is defined ? object : {key: "value", datetime: (1605185652|date_modify(\'0sec\')), values: {0: "one", 1: "two", }, } %}
59
';
60
        $this->assertEquals($twig, $convertor->convert($this->data));
61
    }
62
63
    public function testVar(): void
64
    {
65
        $convertor = DataToTwigConvertor::var('parameters');
66
        $twig = '{% set parameters = {foo: "bar", number: 1234, pi: 3.14159, date: (1464307200|date_modify(\'0sec\')), empty: null, multiline: "Multiple\nLine\nString\n", object: {key: "value", datetime: (1605185652|date_modify(\'0sec\')), values: {0: "one", 1: "two", }, }, }%}'."\n";
67
        $this->assertEquals($twig, $convertor->convert($this->data));
68
    }
69
}
70