Completed
Push — master ( 0926c2...16eb90 )
by WEBEWEB
01:52
created

JavascriptTwigExtensionTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 82
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2020 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\CoreBundle\Tests\Twig\Extension;
13
14
use Twig\Node\Node;
15
use Twig\TwigFilter;
16
use Twig\TwigFunction;
17
use WBW\Bundle\CoreBundle\Tests\AbstractTestCase;
18
use WBW\Bundle\CoreBundle\Twig\Extension\JavascriptTwigExtension;
19
20
/**
21
 * Javascript Twig extension test.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\CoreBundle\Tests\Twig\Extension
25
 */
26
class JavascriptTwigExtensionTest extends AbstractTestCase {
27
28
    /**
29
     * Tests the getFilters() method.
30
     *
31
     * @return void
32
     */
33
    public function testGetFilters(): void {
34
35
        $obj = new JavascriptTwigExtension($this->twigEnvironment);
36
37
        $res = $obj->getFilters();
38
        $this->assertCount(1, $res);
39
40
        $this->assertInstanceOf(TwigFilter::class, $res[0]);
41
        $this->assertEquals("jsGtag", $res[0]->getName());
42
        $this->assertEquals([$obj, "jsGtag"], $res[0]->getCallable());
43
        $this->assertEquals(["html"], $res[0]->getSafe(new Node()));
44
    }
45
46
    /**
47
     * Tests the getFunctions() method.
48
     *
49
     * @return void
50
     */
51
    public function testGetFunctions(): void {
52
53
        $obj = new JavascriptTwigExtension($this->twigEnvironment);
54
55
        $res = $obj->getFunctions();
56
        $this->assertCount(1, $res);
57
58
        $this->assertInstanceOf(TwigFunction::class, $res[0]);
59
        $this->assertEquals("jsGtag", $res[0]->getName());
60
        $this->assertEquals([$obj, "jsGtag"], $res[0]->getCallable());
61
        $this->assertEquals(["html"], $res[0]->getSafe(new Node()));
62
    }
63
64
    /**
65
     * Tests the jsGtag() method.
66
     *
67
     * @returns void
68
     */
69
    public function testJsGtag(): void {
70
71
        $obj = new JavascriptTwigExtension($this->twigEnvironment);
72
73
        $res = <<< EOTXT
74
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-123456789-0"></script>
75
<script>
76
    window.dataLayer = window.dataLayer || [];
77
78
    function gtag() {
79
        dataLayer.push(arguments);
80
    }
81
82
    gtag('js', new Date());
83
    gtag('config', 'UA-123456789-0');
84
</script>
85
86
EOTXT;
87
88
        $this->assertEquals($res, $obj->jsGtag("UA-123456789-0"));
89
90
        $this->assertEquals("", $obj->jsGtag(null));
91
        $this->assertEquals("", $obj->jsGtag(""));
92
    }
93
94
    /**
95
     * Tests the __construct() method.
96
     *
97
     * @return void
98
     */
99
    public function test__construct(): void {
100
101
        $this->assertEquals("wbw.core.twig.extension.javascript", JavascriptTwigExtension::SERVICE_NAME);
102
103
        $obj = new JavascriptTwigExtension($this->twigEnvironment);
104
105
        $this->assertSame($this->twigEnvironment, $obj->getTwigEnvironment());
106
    }
107
}