FirewallTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Component\Security\Tests\Http;
  11. use Symfony\Component\Security\Http\Firewall;
  12. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  13. use Symfony\Component\HttpKernel\HttpKernelInterface;
  14. class FirewallTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected function setUp()
  17. {
  18. if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
  19. $this->markTestSkipped('The "EventDispatcher" component is not available');
  20. }
  21. if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
  22. $this->markTestSkipped('The "HttpFoundation" component is not available');
  23. }
  24. if (!class_exists('Symfony\Component\HttpKernel\HttpKernel')) {
  25. $this->markTestSkipped('The "HttpKernel" component is not available');
  26. }
  27. }
  28. public function testOnKernelRequestRegistersExceptionListener()
  29. {
  30. $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  31. $listener = $this->getMock('Symfony\Component\Security\Http\Firewall\ExceptionListener', array(), array(), '', false);
  32. $listener
  33. ->expects($this->once())
  34. ->method('register')
  35. ->with($this->equalTo($dispatcher))
  36. ;
  37. $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
  38. $map = $this->getMock('Symfony\Component\Security\Http\FirewallMapInterface');
  39. $map
  40. ->expects($this->once())
  41. ->method('getListeners')
  42. ->with($this->equalTo($request))
  43. ->will($this->returnValue(array(array(), $listener)))
  44. ;
  45. $event = new GetResponseEvent($this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST);
  46. $firewall = new Firewall($map, $dispatcher);
  47. $firewall->onKernelRequest($event);
  48. }
  49. public function testOnKernelRequestStopsWhenThereIsAResponse()
  50. {
  51. $response = $this->getMock('Symfony\Component\HttpFoundation\Response');
  52. $first = $this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface');
  53. $first
  54. ->expects($this->once())
  55. ->method('handle')
  56. ;
  57. $second = $this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface');
  58. $second
  59. ->expects($this->never())
  60. ->method('handle')
  61. ;
  62. $map = $this->getMock('Symfony\Component\Security\Http\FirewallMapInterface');
  63. $map
  64. ->expects($this->once())
  65. ->method('getListeners')
  66. ->will($this->returnValue(array(array($first, $second), null)))
  67. ;
  68. $event = $this->getMock(
  69. 'Symfony\Component\HttpKernel\Event\GetResponseEvent',
  70. array('hasResponse'),
  71. array(
  72. $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
  73. $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false),
  74. HttpKernelInterface::MASTER_REQUEST
  75. )
  76. );
  77. $event
  78. ->expects($this->once())
  79. ->method('hasResponse')
  80. ->will($this->returnValue(true))
  81. ;
  82. $firewall = new Firewall($map, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
  83. $firewall->onKernelRequest($event);
  84. }
  85. public function testOnKernelRequestWithSubRequest()
  86. {
  87. $map = $this->getMock('Symfony\Component\Security\Http\FirewallMapInterface');
  88. $map
  89. ->expects($this->never())
  90. ->method('getListeners')
  91. ;
  92. $event = new GetResponseEvent(
  93. $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
  94. $this->getMock('Symfony\Component\HttpFoundation\Request'),
  95. HttpKernelInterface::SUB_REQUEST
  96. );
  97. $firewall = new Firewall($map, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
  98. $firewall->onKernelRequest($event);
  99. $this->assertFalse($event->hasResponse());
  100. }
  101. }