|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
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 Symfony\Bundle\WebProfilerBundle\Tests\DependencyInjection; |
|
13 |
|
14 use Symfony\Bundle\WebProfilerBundle\Tests\TestCase; |
|
15 |
|
16 use Symfony\Bundle\WebProfilerBundle\DependencyInjection\WebProfilerExtension; |
|
17 use Symfony\Component\Config\FileLocator; |
|
18 use Symfony\Component\DependencyInjection\Container; |
|
19 use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
20 use Symfony\Component\DependencyInjection\Definition; |
|
21 use Symfony\Component\DependencyInjection\Parameter; |
|
22 use Symfony\Component\DependencyInjection\Reference; |
|
23 use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; |
|
24 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
25 use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
26 use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
|
27 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
|
28 use Symfony\Component\DependencyInjection\Scope; |
|
29 |
|
30 class WebProfilerExtensionTest extends TestCase |
|
31 { |
|
32 private $kernel; |
|
33 /** |
|
34 * @var Symfony\Component\DependencyInjection\Container $container |
|
35 */ |
|
36 private $container; |
|
37 |
|
38 static public function assertSaneContainer(Container $container, $message = '') |
|
39 { |
|
40 $errors = array(); |
|
41 foreach ($container->getServiceIds() as $id) { |
|
42 try { |
|
43 $container->get($id); |
|
44 } catch (\Exception $e) { |
|
45 $errors[$id] = $e->getMessage(); |
|
46 } |
|
47 } |
|
48 |
|
49 self::assertEquals(array(), $errors, $message); |
|
50 } |
|
51 |
|
52 protected function setUp() |
|
53 { |
|
54 parent::setUp(); |
|
55 |
|
56 $this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\KernelInterface'); |
|
57 |
|
58 $this->container = new ContainerBuilder(); |
|
59 $this->container->addScope(new Scope('request')); |
|
60 $this->container->register('request', 'Symfony\\Component\\HttpFoundation\\Request')->setScope('request'); |
|
61 $this->container->register('templating.helper.assets', $this->getMockClass('Symfony\\Component\\Templating\\Helper\\AssetsHelper')); |
|
62 $this->container->register('templating.helper.router', $this->getMockClass('Symfony\\Bundle\\FrameworkBundle\\Templating\\Helper\\RouterHelper')) |
|
63 ->addArgument(new Definition($this->getMockClass('Symfony\\Component\\Routing\\RouterInterface'))); |
|
64 $this->container->register('twig', 'Twig_Environment'); |
|
65 $this->container->register('templating.engine.twig', $this->getMockClass('Symfony\\Bundle\\TwigBundle\\TwigEngine')) |
|
66 ->addArgument(new Definition($this->getMockClass('Twig_Environment'))) |
|
67 ->addArgument(new Definition($this->getMockClass('Symfony\\Component\\Templating\\TemplateNameParserInterface'))) |
|
68 ->addArgument(new Definition($this->getMockClass('Symfony\\Bundle\\FrameworkBundle\\Templating\\GlobalVariables'), array(new Definition($this->getMockClass('Symfony\\Component\\DependencyInjection\\Container'))))); |
|
69 $this->container->setParameter('kernel.bundles', array()); |
|
70 $this->container->setParameter('kernel.cache_dir', __DIR__); |
|
71 $this->container->setParameter('kernel.debug', false); |
|
72 $this->container->setParameter('kernel.root_dir', __DIR__); |
|
73 $this->container->set('kernel', $this->kernel); |
|
74 } |
|
75 |
|
76 protected function tearDown() |
|
77 { |
|
78 parent::tearDown(); |
|
79 |
|
80 $this->container = null; |
|
81 $this->kernel = null; |
|
82 } |
|
83 |
|
84 /** |
|
85 * @dataProvider getDebugModes |
|
86 */ |
|
87 public function testDefaultConfig($debug) |
|
88 { |
|
89 $this->container->setParameter('kernel.debug', $debug); |
|
90 |
|
91 $extension = new WebProfilerExtension(); |
|
92 $extension->load(array(array()), $this->container); |
|
93 |
|
94 $this->assertFalse($this->container->get('web_profiler.debug_toolbar')->isEnabled()); |
|
95 |
|
96 $this->assertSaneContainer($this->getDumpedContainer()); |
|
97 } |
|
98 |
|
99 /** |
|
100 * @dataProvider getDebugModes |
|
101 */ |
|
102 public function testToolbarConfig($enabled, $verbose) |
|
103 { |
|
104 $extension = new WebProfilerExtension(); |
|
105 $extension->load(array(array('toolbar' => $enabled, 'verbose' => $verbose)), $this->container); |
|
106 |
|
107 $this->assertSame($enabled, $this->container->get('web_profiler.debug_toolbar')->isEnabled()); |
|
108 $this->assertSame($enabled && $verbose, $this->container->get('web_profiler.debug_toolbar')->isVerbose()); |
|
109 |
|
110 $this->assertSaneContainer($this->getDumpedContainer()); |
|
111 } |
|
112 |
|
113 public function getDebugModes() |
|
114 { |
|
115 return array( |
|
116 array(true, true), |
|
117 array(true, false), |
|
118 array(false, false), |
|
119 array(false, true), |
|
120 ); |
|
121 } |
|
122 |
|
123 private function getDumpedContainer() |
|
124 { |
|
125 static $i = 0; |
|
126 $class = 'WebProfilerExtensionTestContainer'.$i++; |
|
127 |
|
128 $this->container->compile(); |
|
129 |
|
130 $dumper = new PhpDumper($this->container); |
|
131 eval('?>'.$dumper->dump(array('class' => $class))); |
|
132 |
|
133 $container = new $class(); |
|
134 $container->enterScope('request'); |
|
135 $container->set('kernel', $this->kernel); |
|
136 |
|
137 return $container; |
|
138 } |
|
139 } |