Completed
Pull Request — master (#128)
by
unknown
08:25 queued 06:20
created

PathHelperTest::getRouterMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Liip\MonitorBundle\Tests\Helper;
4
5
use Liip\MonitorBundle\Helper\PathHelper;
6
7
class PathHelperTest extends \PHPUnit_Framework_TestCase
8
{
9
    private $container = null;
10
11
    public function testGetRoutesJs()
12
    {
13
        $testedClass = $this->getNewTestedClass();
14
        $this->assertContains('api.test = "route_test";', $testedClass->getRoutesJs(array('test' => array())));
15
    }
16
17
    public function testGetScriptTags()
18
    {
19
        $testedClass = $this->getNewTestedClass();
20
        $this->assertContains('<script type="text/javascript" charset="utf-8" src="url_path"></script>', $testedClass->getScriptTags(array('path')));
21
    }
22
23
    public function testGetStyleTags()
24
    {
25
        $testedClass = $this->getNewTestedClass();
26
        $this->assertContains('<link rel="stylesheet" href="url_path" type="text/css">', $testedClass->getStyleTags(array('path')));
27
    }
28
29
    private function getContainerSf3Mock()
30
    {
31
        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
32
        $container
33
            ->expects($this->any())
34
            ->method('get')
35
            ->will($this->returnCallback(function ($param) {
36
                if ($param == 'router') {
37
                    return $this->getRouterMock();
38
                } elseif ($param == 'assets.packages') {
39
                    return $this->getAssetHelperMock();
40
                }
41
                throw new \Exception();
42
            }));
43
44
        $container
45
            ->expects($this->any())
46
            ->method('has')
47
            ->with($this->equalTo('templating.helper.assets'))
48
            ->willReturn(false);
49
50
        return $container;
51
    }
52
53
    private function getContainerSf2Mock()
54
    {
55
        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
56
        $container
57
            ->expects($this->any())
58
            ->method('get')
59
            ->will($this->returnCallback(function ($param) {
60
                if ($param == 'router') {
61
                    return $this->getRouterMock();
62
                } elseif ($param == 'templating.helper.assets') {
63
                    return $this->getAssetHelperMock();
64
                }
65
                throw new \Exception();
66
            }));
67
68
        $container
69
            ->expects($this->any())
70
            ->method('has')
71
            ->with($this->equalTo('templating.helper.assets'))
72
            ->willReturn(true);
73
74
        return $container;
75
    }
76
77
    private function getContainerMock()
78
    {
79
        $symfony_version = \Symfony\Component\HttpKernel\Kernel::VERSION;
80
        if (version_compare($symfony_version, '3.0.0') === -1) {
81
            $container = $this->getContainerSf2Mock();
82
        } else {
83
            $container = $this->getContainerSf3Mock();
84
        }
85
86
        return $container;
87
    }
88
89
    private function getRouterMock()
90
    {
91
        $router = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
92
        $router
93
            ->expects($this->any())
94
            ->method('generate')
95
            ->will($this->returnCallback(function ($arg) { return 'route_'.$arg; }));
96
97
        return $router;
98
    }
99
100
    private function getAssetHelperMock()
101
    {
102
        $helper = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper')
103
                       ->disableOriginalConstructor()
104
                       ->getMock();
105
106
        $helper
107
            ->expects($this->any())
108
            ->method('getUrl')
109
            ->will($this->returnCallback(function ($arg) { return 'url_'.$arg; }));
110
111
        return $helper;
112
    }
113
114
    public function getNewTestedClass()
115
    {
116
        $symfony_version = \Symfony\Component\HttpKernel\Kernel::VERSION;
117
        return new PathHelper($this->container, $symfony_version);
118
    }
119
120
    public function setUp()
121
    {
122
        $this->container = $this->getContainerMock();
123
    }
124
125
126
}
127