FirewallTest.php 4.2 KB

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