KernelTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Tests\Component\HttpKernel;
  11. use Symfony\Component\HttpKernel\Kernel;
  12. use Symfony\Component\HttpKernel\HttpKernelInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\DependencyInjection\Loader\LoaderInterface;
  15. class KernelTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testGetSafeName()
  18. {
  19. $kernel = new KernelForTest('dev', true, '-foo-');
  20. $this->assertEquals('foo', $kernel->getSafeName());
  21. }
  22. public function testHandleSetsTheRequest()
  23. {
  24. $masterRequest = Request::create('/');
  25. $subRequest = Request::create('/');
  26. $httpKernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
  27. ->disableOriginalConstructor()
  28. ->setMethods(array('handle'))
  29. ->getMock();
  30. $httpKernel->expects($this->at(0))
  31. ->method('handle')
  32. ->with($masterRequest);
  33. $httpKernel->expects($this->at(1))
  34. ->method('handle')
  35. ->with($subRequest);
  36. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container')
  37. ->disableOriginalConstructor()
  38. ->setMethods(array('get'))
  39. ->getMock();
  40. $container->expects($this->exactly(2))
  41. ->method('get')
  42. ->with('http_kernel')
  43. ->will($this->returnValue($httpKernel));
  44. $kernel = $this->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
  45. ->setConstructorArgs(array('dev', true, '-foo-'))
  46. ->setMethods(array('boot'))
  47. ->getMock();
  48. $kernel->setContainer($container);
  49. $testCase = $this;
  50. $bootCallback = function() use ($masterRequest, $kernel, $testCase) {
  51. $kernel->setBooted(true);
  52. $testCase->assertSame($masterRequest, $kernel->getRequest(), '->handle() sets the Request before booting');
  53. };
  54. $kernel->expects($this->once())
  55. ->method('boot')
  56. ->will($this->returnCallback($bootCallback));
  57. $kernel->handle($masterRequest);
  58. $kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
  59. $this->assertSame($masterRequest, $kernel->getRequest(), '->handle() restores the master Request after handling a sub-request');
  60. }
  61. }
  62. class KernelForTest extends Kernel
  63. {
  64. public function __construct($environment, $debug, $name)
  65. {
  66. parent::__construct($environment, $debug);
  67. $this->name = $name;
  68. }
  69. public function registerRootDir()
  70. {
  71. }
  72. public function registerBundles()
  73. {
  74. }
  75. public function registerBundleDirs()
  76. {
  77. }
  78. public function registerContainerConfiguration(LoaderInterface $loader)
  79. {
  80. }
  81. public function setBooted($booted)
  82. {
  83. $this->booted = $booted;
  84. }
  85. public function setContainer($container)
  86. {
  87. $this->container = $container;
  88. }
  89. }