Completed
Pull Request — master (#128)
by
unknown
02:25
created

PathHelperTest::getContainerSf2Mock()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 19
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
        $routerMock = $this->getRouterMock();
32
        $assetHelperMock = $this->getAssetHelperMock();
33
        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
34
        $container
35
            ->expects($this->any())
36
            ->method('get')
37
            ->will($this->returnCallback(function ($param) use ($routerMock, $assetHelperMock) {
38
                if ($param == 'router') {
39
                    return $routerMock;
40
                } elseif ($param == 'assets.packages') {
41
                    return $assetHelperMock;
42
                }
43
                throw new \Exception();
44
            }));
45
46
        $container
47
            ->expects($this->any())
48
            ->method('has')
49
            ->with($this->equalTo('templating.helper.assets'))
50
            ->willReturn(false);
51
52
        return $container;
53
    }
54
55
    private function getContainerSf2Mock()
56
    {
57
        $routerMock = $this->getRouterMock();
58
        $assetHelperMock = $this->getAssetHelperMock();
59
        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
60
        $container
61
            ->expects($this->any())
62
            ->method('get')
63
            ->will($this->returnCallback(function ($param) use ($routerMock, $assetHelperMock) {
64
                if ($param == 'router') {
65
                    return $routerMock;
66
                } elseif ($param == 'templating.helper.assets') {
67
                    return $assetHelperMock;
68
                }
69
                throw new \Exception();
70
            }));
71
72
        $container
73
            ->expects($this->any())
74
            ->method('has')
75
            ->with($this->equalTo('templating.helper.assets'))
76
            ->willReturn(true);
77
78
        return $container;
79
    }
80
81
    private function getContainerMock()
82
    {
83
        $symfony_version = \Symfony\Component\HttpKernel\Kernel::VERSION;
84
        if (version_compare($symfony_version, '3.0.0') === -1) {
85
            $container = $this->getContainerSf2Mock();
86
        } else {
87
            $container = $this->getContainerSf3Mock();
88
        }
89
90
        return $container;
91
    }
92
93
    private function getRouterMock()
94
    {
95
        $router = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
96
        $router
97
            ->expects($this->any())
98
            ->method('generate')
99
            ->will($this->returnCallback(function ($arg) { return 'route_'.$arg; }));
100
101
        return $router;
102
    }
103
104
    private function getAssetHelperMock()
105
    {
106
        $helper = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper')
107
                       ->disableOriginalConstructor()
108
                       ->getMock();
109
110
        $helper
111
            ->expects($this->any())
112
            ->method('getUrl')
113
            ->will($this->returnCallback(function ($arg) { return 'url_'.$arg; }));
114
115
        return $helper;
116
    }
117
118
    public function getNewTestedClass()
119
    {
120
        $symfony_version = \Symfony\Component\HttpKernel\Kernel::VERSION;
121
122
        return new PathHelper($this->container, $symfony_version);
123
    }
124
125
    public function setUp()
126
    {
127
        $this->container = $this->getContainerMock();
128
    }
129
}
130