FirewallMapTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Request;
  13. use Symfony\Component\Security\Http\FirewallMap;
  14. class FirewallMapTest extends TestCase
  15. {
  16. public function testGetListeners()
  17. {
  18. $map = new FirewallMap();
  19. $request = new Request();
  20. $notMatchingMatcher = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestMatcher')->getMock();
  21. $notMatchingMatcher
  22. ->expects($this->once())
  23. ->method('matches')
  24. ->with($this->equalTo($request))
  25. ->will($this->returnValue(false))
  26. ;
  27. $map->add($notMatchingMatcher, array($this->getMockBuilder('Symfony\Component\Security\Http\Firewall\ListenerInterface')->getMock()));
  28. $matchingMatcher = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestMatcher')->getMock();
  29. $matchingMatcher
  30. ->expects($this->once())
  31. ->method('matches')
  32. ->with($this->equalTo($request))
  33. ->will($this->returnValue(true))
  34. ;
  35. $theListener = $this->getMockBuilder('Symfony\Component\Security\Http\Firewall\ListenerInterface')->getMock();
  36. $theException = $this->getMockBuilder('Symfony\Component\Security\Http\Firewall\ExceptionListener')->disableOriginalConstructor()->getMock();
  37. $map->add($matchingMatcher, array($theListener), $theException);
  38. $tooLateMatcher = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestMatcher')->getMock();
  39. $tooLateMatcher
  40. ->expects($this->never())
  41. ->method('matches')
  42. ;
  43. $map->add($tooLateMatcher, array($this->getMockBuilder('Symfony\Component\Security\Http\Firewall\ListenerInterface')->getMock()));
  44. list($listeners, $exception) = $map->getListeners($request);
  45. $this->assertEquals(array($theListener), $listeners);
  46. $this->assertEquals($theException, $exception);
  47. }
  48. public function testGetListenersWithAnEntryHavingNoRequestMatcher()
  49. {
  50. $map = new FirewallMap();
  51. $request = new Request();
  52. $notMatchingMatcher = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestMatcher')->getMock();
  53. $notMatchingMatcher
  54. ->expects($this->once())
  55. ->method('matches')
  56. ->with($this->equalTo($request))
  57. ->will($this->returnValue(false))
  58. ;
  59. $map->add($notMatchingMatcher, array($this->getMockBuilder('Symfony\Component\Security\Http\Firewall\ListenerInterface')->getMock()));
  60. $theListener = $this->getMockBuilder('Symfony\Component\Security\Http\Firewall\ListenerInterface')->getMock();
  61. $theException = $this->getMockBuilder('Symfony\Component\Security\Http\Firewall\ExceptionListener')->disableOriginalConstructor()->getMock();
  62. $map->add(null, array($theListener), $theException);
  63. $tooLateMatcher = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestMatcher')->getMock();
  64. $tooLateMatcher
  65. ->expects($this->never())
  66. ->method('matches')
  67. ;
  68. $map->add($tooLateMatcher, array($this->getMockBuilder('Symfony\Component\Security\Http\Firewall\ListenerInterface')->getMock()));
  69. list($listeners, $exception) = $map->getListeners($request);
  70. $this->assertEquals(array($theListener), $listeners);
  71. $this->assertEquals($theException, $exception);
  72. }
  73. public function testGetListenersWithNoMatchingEntry()
  74. {
  75. $map = new FirewallMap();
  76. $request = new Request();
  77. $notMatchingMatcher = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestMatcher')->getMock();
  78. $notMatchingMatcher
  79. ->expects($this->once())
  80. ->method('matches')
  81. ->with($this->equalTo($request))
  82. ->will($this->returnValue(false))
  83. ;
  84. $map->add($notMatchingMatcher, array($this->getMockBuilder('Symfony\Component\Security\Http\Firewall\ListenerInterface')->getMock()));
  85. list($listeners, $exception) = $map->getListeners($request);
  86. $this->assertEquals(array(), $listeners);
  87. $this->assertNull($exception);
  88. }
  89. }