FirewallMapTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Symfony\Tests\Component\Security\Http;
  3. use Symfony\Component\Security\Http\FirewallMap;
  4. use Symfony\Component\HttpFoundation\Request;
  5. class FirewallMapTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testGetListeners()
  8. {
  9. $map = new FirewallMap();
  10. $request = new Request();
  11. $notMatchingMatcher = $this->getMock('Symfony\Component\HttpFoundation\RequestMatcher');
  12. $notMatchingMatcher
  13. ->expects($this->once())
  14. ->method('matches')
  15. ->with($this->equalTo($request))
  16. ->will($this->returnValue(false))
  17. ;
  18. $map->add($notMatchingMatcher, array($this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface')));
  19. $matchingMatcher = $this->getMock('Symfony\Component\HttpFoundation\RequestMatcher');
  20. $matchingMatcher
  21. ->expects($this->once())
  22. ->method('matches')
  23. ->with($this->equalTo($request))
  24. ->will($this->returnValue(true))
  25. ;
  26. $theListener = $this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface');
  27. $theException = $this->getMock('Symfony\Component\Security\Http\Firewall\ExceptionListener', array(), array(), '', false);
  28. $map->add($matchingMatcher, array($theListener), $theException);
  29. $tooLateMatcher = $this->getMock('Symfony\Component\HttpFoundation\RequestMatcher');
  30. $tooLateMatcher
  31. ->expects($this->never())
  32. ->method('matches')
  33. ;
  34. $map->add($tooLateMatcher, array($this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface')));
  35. list($listeners, $exception) = $map->getListeners($request);
  36. $this->assertEquals(array($theListener), $listeners);
  37. $this->assertEquals($theException, $exception);
  38. }
  39. public function testGetListenersWithAnEntryHavingNoRequestMatcher()
  40. {
  41. $map = new FirewallMap();
  42. $request = new Request();
  43. $notMatchingMatcher = $this->getMock('Symfony\Component\HttpFoundation\RequestMatcher');
  44. $notMatchingMatcher
  45. ->expects($this->once())
  46. ->method('matches')
  47. ->with($this->equalTo($request))
  48. ->will($this->returnValue(false))
  49. ;
  50. $map->add($notMatchingMatcher, array($this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface')));
  51. $theListener = $this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface');
  52. $theException = $this->getMock('Symfony\Component\Security\Http\Firewall\ExceptionListener', array(), array(), '', false);
  53. $map->add(null, array($theListener), $theException);
  54. $tooLateMatcher = $this->getMock('Symfony\Component\HttpFoundation\RequestMatcher');
  55. $tooLateMatcher
  56. ->expects($this->never())
  57. ->method('matches')
  58. ;
  59. $map->add($tooLateMatcher, array($this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface')));
  60. list($listeners, $exception) = $map->getListeners($request);
  61. $this->assertEquals(array($theListener), $listeners);
  62. $this->assertEquals($theException, $exception);
  63. }
  64. public function testGetListenersWithNoMatchingEntry()
  65. {
  66. $map = new FirewallMap();
  67. $request = new Request();
  68. $notMatchingMatcher = $this->getMock('Symfony\Component\HttpFoundation\RequestMatcher');
  69. $notMatchingMatcher
  70. ->expects($this->once())
  71. ->method('matches')
  72. ->with($this->equalTo($request))
  73. ->will($this->returnValue(false))
  74. ;
  75. $map->add($notMatchingMatcher, array($this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface')));
  76. list($listeners, $exception) = $map->getListeners($request);
  77. $this->assertEquals(array(), $listeners);
  78. $this->assertNull($exception);
  79. }
  80. }