WebProfilerExtensionTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\WebProfilerBundle\Tests\DependencyInjection;
  11. use Symfony\Bundle\WebProfilerBundle\Tests\TestCase;
  12. use Symfony\Bundle\WebProfilerBundle\DependencyInjection\WebProfilerExtension;
  13. use Symfony\Component\DependencyInjection\Container;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Definition;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
  18. use Symfony\Component\DependencyInjection\Scope;
  19. class WebProfilerExtensionTest extends TestCase
  20. {
  21. private $kernel;
  22. /**
  23. * @var Symfony\Component\DependencyInjection\Container $container
  24. */
  25. private $container;
  26. public static function assertSaneContainer(Container $container, $message = '')
  27. {
  28. $errors = array();
  29. foreach ($container->getServiceIds() as $id) {
  30. try {
  31. $container->get($id);
  32. } catch (\Exception $e) {
  33. $errors[$id] = $e->getMessage();
  34. }
  35. }
  36. self::assertEquals(array(), $errors, $message);
  37. }
  38. protected function setUp()
  39. {
  40. parent::setUp();
  41. $this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\KernelInterface');
  42. $this->container = new ContainerBuilder();
  43. $this->container->addScope(new Scope('request'));
  44. $this->container->register('request', 'Symfony\\Component\\HttpFoundation\\Request')->setScope('request');
  45. $this->container->register('router', $this->getMockClass('Symfony\\Component\\Routing\\RouterInterface'));
  46. $this->container->register('templating.helper.assets', $this->getMockClass('Symfony\\Component\\Templating\\Helper\\AssetsHelper'));
  47. $this->container->register('templating.helper.router', $this->getMockClass('Symfony\\Bundle\\FrameworkBundle\\Templating\\Helper\\RouterHelper'))
  48. ->addArgument(new Reference('router'));
  49. $this->container->register('twig', 'Twig_Environment');
  50. $this->container->setParameter('kernel.bundles', array());
  51. $this->container->setParameter('kernel.cache_dir', __DIR__);
  52. $this->container->setParameter('kernel.debug', false);
  53. $this->container->setParameter('kernel.root_dir', __DIR__);
  54. $this->container->setParameter('profiler.class', array('Symfony\\Component\\HttpKernel\\Profiler\\Profiler'));
  55. $this->container->register('profiler', $this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\Profiler'))
  56. ->addArgument(new Definition($this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\ProfilerStorageInterface')));
  57. $this->container->setParameter('data_collector.templates', array());
  58. $this->container->set('kernel', $this->kernel);
  59. }
  60. protected function tearDown()
  61. {
  62. parent::tearDown();
  63. $this->container = null;
  64. $this->kernel = null;
  65. }
  66. /**
  67. * @dataProvider getDebugModes
  68. */
  69. public function testDefaultConfig($debug)
  70. {
  71. $this->container->setParameter('kernel.debug', $debug);
  72. $extension = new WebProfilerExtension();
  73. $extension->load(array(array()), $this->container);
  74. $this->assertFalse($this->container->get('web_profiler.debug_toolbar')->isEnabled());
  75. $this->assertSaneContainer($this->getDumpedContainer());
  76. }
  77. /**
  78. * @dataProvider getDebugModes
  79. */
  80. public function testToolbarConfig($enabled)
  81. {
  82. $extension = new WebProfilerExtension();
  83. $extension->load(array(array('toolbar' => $enabled)), $this->container);
  84. $this->assertSame($enabled, $this->container->get('web_profiler.debug_toolbar')->isEnabled());
  85. $this->assertSaneContainer($this->getDumpedContainer());
  86. }
  87. public function getDebugModes()
  88. {
  89. return array(
  90. array(true),
  91. array(false),
  92. );
  93. }
  94. private function getDumpedContainer()
  95. {
  96. static $i = 0;
  97. $class = 'WebProfilerExtensionTestContainer'.$i++;
  98. $this->container->compile();
  99. $dumper = new PhpDumper($this->container);
  100. eval('?>'.$dumper->dump(array('class' => $class)));
  101. $container = new $class();
  102. $container->enterScope('request');
  103. $container->set('kernel', $this->kernel);
  104. return $container;
  105. }
  106. }