FirewallTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Symfony\Tests\Component\Security\Http;
  3. use Symfony\Component\Security\Http\Firewall;
  4. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  5. use Symfony\Component\HttpKernel\HttpKernelInterface;
  6. class FirewallTest extends \PHPUnit_Framework_TestCase
  7. {
  8. public function testOnKernelRequestRegistersExceptionListener()
  9. {
  10. $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  11. $listener = $this->getMock('Symfony\Component\Security\Http\Firewall\ExceptionListener', array(), array(), '', false);
  12. $listener
  13. ->expects($this->once())
  14. ->method('register')
  15. ->with($this->equalTo($dispatcher))
  16. ;
  17. $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
  18. $map = $this->getMock('Symfony\Component\Security\Http\FirewallMapInterface');
  19. $map
  20. ->expects($this->once())
  21. ->method('getListeners')
  22. ->with($this->equalTo($request))
  23. ->will($this->returnValue(array(array(), $listener)))
  24. ;
  25. $event = new GetResponseEvent($this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST);
  26. $firewall = new Firewall($map, $dispatcher);
  27. $firewall->onKernelRequest($event);
  28. }
  29. public function testOnKernelRequestStopsWhenThereIsAResponse()
  30. {
  31. $response = $this->getMock('Symfony\Component\HttpFoundation\Response');
  32. $first = $this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface');
  33. $first
  34. ->expects($this->once())
  35. ->method('handle')
  36. ;
  37. $second = $this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface');
  38. $second
  39. ->expects($this->never())
  40. ->method('handle')
  41. ;
  42. $map = $this->getMock('Symfony\Component\Security\Http\FirewallMapInterface');
  43. $map
  44. ->expects($this->once())
  45. ->method('getListeners')
  46. ->will($this->returnValue(array(array($first, $second), null)))
  47. ;
  48. $event = $this->getMock(
  49. 'Symfony\Component\HttpKernel\Event\GetResponseEvent',
  50. array('hasResponse'),
  51. array(
  52. $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
  53. $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false),
  54. HttpKernelInterface::MASTER_REQUEST
  55. )
  56. );
  57. $event
  58. ->expects($this->once())
  59. ->method('hasResponse')
  60. ->will($this->returnValue(true))
  61. ;
  62. $firewall = new Firewall($map, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
  63. $firewall->onKernelRequest($event);
  64. }
  65. public function testOnKernelRequestWithSubRequest()
  66. {
  67. $map = $this->getMock('Symfony\Component\Security\Http\FirewallMapInterface');
  68. $map
  69. ->expects($this->never())
  70. ->method('getListeners')
  71. ;
  72. $event = new GetResponseEvent(
  73. $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'),
  74. $this->getMock('Symfony\Component\HttpFoundation\Request'),
  75. HttpKernelInterface::SUB_REQUEST
  76. );
  77. $firewall = new Firewall($map, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
  78. $firewall->onKernelRequest($event);
  79. $this->assertFalse($event->hasResponse());
  80. }
  81. }