FirewallMapTest.php 4.5 KB

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