TemplateManagerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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\Profiler;
  11. use Symfony\Bundle\WebProfilerBundle\Tests\TestCase;
  12. use Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager;
  13. /**
  14. * Test for TemplateManager class.
  15. *
  16. * @author Artur Wielogórski <wodor@wodor.net>
  17. */
  18. class TemplateManagerTest extends TestCase
  19. {
  20. /**
  21. * @var \Twig_Environment
  22. */
  23. protected $twigEnvironment;
  24. /**
  25. * @var \Symfony\Component\HttpKernel\Profiler\Profiler
  26. */
  27. protected $profiler;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $profile;
  32. /**
  33. * @var \Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager
  34. */
  35. protected $templateManager;
  36. public function setUp()
  37. {
  38. parent::setUp();
  39. $profiler = $this->mockProfiler();
  40. $twigEnvironment = $this->mockTwigEnvironment();
  41. $templates = array(
  42. 'data_collector.foo'=>array('foo','FooBundle:Collector:foo'),
  43. 'data_collector.bar'=>array('bar','FooBundle:Collector:bar'),
  44. 'data_collector.baz'=>array('baz','FooBundle:Collector:baz')
  45. );
  46. $this->templateManager = new TemplateManager($profiler, $twigEnvironment, $templates);
  47. }
  48. /**
  49. * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  50. */
  51. public function testGetNameOfInvalidTemplate()
  52. {
  53. $profile = $this->mockProfile();
  54. $this->templateManager->getName($profile, 'notexistingpanel');
  55. }
  56. /**
  57. * if template exists in both profile and profiler then its name should be returned
  58. */
  59. public function testGetNameValidTemplate()
  60. {
  61. $this->profiler->expects($this->any())
  62. ->method('has')
  63. ->withAnyParameters()
  64. ->will($this->returnCallback(array($this, 'profilerHasCallback')));
  65. $profile = $this->mockProfile();
  66. $profile->expects($this->any())
  67. ->method('hasCollector')
  68. ->will($this->returnCallback(array($this, 'profileHasCollectorCallback')));
  69. $this->assertEquals('FooBundle:Collector:foo.html.twig', $this->templateManager->getName($profile, 'foo'));
  70. }
  71. /**
  72. * template should be loaded for 'foo' because other collectors are
  73. * missing in profile or in profiler
  74. */
  75. public function testGetTemplates()
  76. {
  77. $profile = $this->mockProfile();
  78. $profile->expects($this->any())
  79. ->method('hasCollector')
  80. ->will($this->returnCallback(array($this, 'profilerHasCallback')));
  81. $this->profiler->expects($this->any())
  82. ->method('has')
  83. ->withAnyParameters()
  84. ->will($this->returnCallback(array($this, 'profileHasCollectorCallback')));
  85. $result = $this->templateManager->getTemplates($profile);
  86. $this->assertArrayHasKey('foo',$result);
  87. $this->assertArrayNotHasKey('bar',$result);
  88. $this->assertArrayNotHasKey('baz',$result);
  89. }
  90. public function profilerHasCallback($panel)
  91. {
  92. switch ($panel) {
  93. case 'foo':
  94. case 'bar':
  95. return true;
  96. default:
  97. return false;
  98. }
  99. }
  100. public function profileHasCollectorCallback($panel)
  101. {
  102. switch ($panel) {
  103. case 'foo':
  104. case 'baz':
  105. return true;
  106. default:
  107. return false;
  108. }
  109. }
  110. protected function mockProfile()
  111. {
  112. $this->profile = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profile')
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. return $this->profile;
  116. }
  117. protected function mockTwigEnvironment()
  118. {
  119. $this->twigEnvironment = $this->getMockBuilder('Twig_Environment')->getMock();
  120. $this->twigEnvironment->expects($this->any())
  121. ->method('loadTemplate')
  122. ->will($this->returnValue('loadedTemplate'));
  123. $this->twigEnvironment->expects($this->any())
  124. ->method('getLoader')
  125. ->will($this->returnValue($this->getMock('\Twig_LoaderInterface')));
  126. return $this->twigEnvironment;
  127. }
  128. protected function mockProfiler()
  129. {
  130. $this->profiler = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
  131. ->disableOriginalConstructor()
  132. ->getMock();
  133. return $this->profiler;
  134. }
  135. }