-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathTemplateManagerTest.php
149 lines (126 loc) · 4.03 KB
/
TemplateManagerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\WebProfilerBundle\Tests\Profiler;
use Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager;
use Symfony\Bundle\WebProfilerBundle\Tests\TestCase;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Twig\Environment;
/**
* Test for TemplateManager class.
*
* @author Artur Wielogórski <[email protected]>
*/
class TemplateManagerTest extends TestCase
{
/**
* @var Environment
*/
protected $twigEnvironment;
/**
* @var \Symfony\Component\HttpKernel\Profiler\Profiler
*/
protected $profiler;
/**
* @var \Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager
*/
protected $templateManager;
protected function setUp()
{
parent::setUp();
$profiler = $this->mockProfiler();
$twigEnvironment = $this->mockTwigEnvironment();
$templates = [
'data_collector.foo' => ['foo', 'FooBundle:Collector:foo'],
'data_collector.bar' => ['bar', 'FooBundle:Collector:bar'],
'data_collector.baz' => ['baz', 'FooBundle:Collector:baz'],
];
$this->templateManager = new TemplateManager($profiler, $twigEnvironment, $templates);
}
/**
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function testGetNameOfInvalidTemplate()
{
$this->templateManager->getName(new Profile('token'), 'notexistingpanel');
}
/**
* if template exists in both profile and profiler then its name should be returned.
*/
public function testGetNameValidTemplate()
{
$this->profiler->expects($this->any())
->method('has')
->withAnyParameters()
->willReturnCallback([$this, 'profilerHasCallback']);
$this->assertEquals('FooBundle:Collector:foo.html.twig', $this->templateManager->getName(new ProfileDummy(), 'foo'));
}
public function profilerHasCallback($panel)
{
switch ($panel) {
case 'foo':
case 'bar':
return true;
default:
return false;
}
}
public function profileHasCollectorCallback($panel)
{
switch ($panel) {
case 'foo':
case 'baz':
return true;
default:
return false;
}
}
protected function mockProfile()
{
return $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profile')->disableOriginalConstructor()->getMock();
}
protected function mockTwigEnvironment()
{
$this->twigEnvironment = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
$this->twigEnvironment->expects($this->any())
->method('loadTemplate')
->willReturn('loadedTemplate');
if (interface_exists('Twig\Loader\SourceContextLoaderInterface')) {
$loader = $this->getMockBuilder('Twig\Loader\SourceContextLoaderInterface')->getMock();
} else {
$loader = $this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock();
}
$this->twigEnvironment->expects($this->any())->method('getLoader')->willReturn($loader);
return $this->twigEnvironment;
}
protected function mockProfiler()
{
$this->profiler = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
->disableOriginalConstructor()
->getMock();
return $this->profiler;
}
}
class ProfileDummy extends Profile
{
public function __construct()
{
parent::__construct('token');
}
public function hasCollector($name)
{
switch ($name) {
case 'foo':
case 'bar':
return true;
default:
return false;
}
}
}