TagcommanderExtensionTest   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
lcom 0
cbo 4
dl 0
loc 66
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A testConstruct() 0 63 1
1
<?php
2
3
/**
4
* This file is part of the Meup TagCommander Bundle.
5
*
6
* (c) 1001pharmacies <http://github.com/1001pharmacies/tagcommander-bundle>
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 Meup\Bundle\TagcommanderBundle\Twig;
13
14
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
15
use Symfony\Component\EventDispatcher\EventDispatcher;
16
use Symfony\Component\Serializer\Serializer;
17
use Symfony\Component\Serializer\Encoder\JsonEncoder;
18
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
19
20
/**
21
 *
22
 */
23
class TagcommanderExtensionTest extends \PHPUnit_Framework_TestCase
24
{
25
    public function testConstruct()
26
    {
27
        $datalayer  = new ParameterBag(array('foo' => 'bar'));
28
        $dispatcher = new EventDispatcher();
29
30
        $tc_vars    = 'toto_var';
31
        $extension  = new TagcommanderExtension($datalayer, $dispatcher, $tc_vars);
32
        $this->assertEquals('tagcommander_extension', $extension->getName());
33
34
35
        $tc_container_config = array(
36
            'name'        => 'ab-test',
37
            'script'      => 'my-ab-test-container.js',
38
            'version'     => 1,
39
            'alternative' => 'none',
40
        );
41
        $extension->addContainer($tc_container_config);
42
43
        $tc_event_config = array(
44
            'name'     => 'generic',
45
            'function' => 'tc_event_1',
46
        );
47
        $extension->addEvent($tc_event_config);
48
49
        $this->assertEquals('tagcommander_extension', $extension->getName());
50
51
52
53
        $functions = array_reduce(
54
            $extension->getFunctions(),
55
            function($functions, $twig_function) {
56
                $functions[] = $twig_function->getName();
57
                return $functions;
58
            }
59
        );
60
        $this->assertTrue(in_array('tc_vars', $functions));
61
        $this->assertTrue(in_array('tc_container', $functions));
62
        $this->assertTrue(in_array('tc_event', $functions));
63
64
65
66
        /* test tc_vars */
67
        $doc = new \DOMDocument();
68
        $doc->loadHTML(
69
            $extension->tcVars(array('lorem'=>'ipsum'))
70
        );
71
        $str = trim($doc->getElementsByTagName('script')->item(0)->nodeValue, ' ;');
72
        list(, $values) = sscanf($str, 'var %s = %s');
73
        $values = json_decode($values);
74
75
        $this->assertEquals('ipsum', $values->lorem);
76
77
78
        $container  = '<script type="text/javascript" src="my-ab-test-container.js?1" async></script>';
79
        $container .= sprintf(
80
            '<noscript><iframe src="none" width="1" height="1" rel="noindex,nofollow" sandbox="%s"></iframe></noscript>',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
81
            'allow-same-origin allow-scripts'
82
        );
83
84
        /* test tc_event */
85
        $this->assertEquals("tc_event_1('click', {\"foo\":\"bar\"});", $extension->tcEvent('click'));
86
        $this->assertEquals($container, $extension->tcContainer('ab-test'));
87
    }
88
}
89