AccessMapTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\AccessMap;
  12. class AccessMapTest extends \PHPUnit_Framework_TestCase
  13. {
  14. protected function setUp()
  15. {
  16. if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
  17. $this->markTestSkipped('The "HttpFoundation" component is not available');
  18. }
  19. }
  20. public function testReturnsFirstMatchedPattern()
  21. {
  22. $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
  23. $requestMatcher1 = $this->getRequestMatcher($request, false);
  24. $requestMatcher2 = $this->getRequestMatcher($request, true);
  25. $map = new AccessMap();
  26. $map->add($requestMatcher1, array('ROLE_ADMIN'), 'http');
  27. $map->add($requestMatcher2, array('ROLE_USER'), 'https');
  28. $this->assertSame(array(array('ROLE_USER'), 'https'), $map->getPatterns($request));
  29. }
  30. public function testReturnsEmptyPatternIfNoneMatched()
  31. {
  32. $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
  33. $requestMatcher = $this->getRequestMatcher($request, false);
  34. $map = new AccessMap();
  35. $map->add($requestMatcher, array('ROLE_USER'), 'https');
  36. $this->assertSame(array(null, null), $map->getPatterns($request));
  37. }
  38. private function getRequestMatcher($request, $matches)
  39. {
  40. $requestMatcher = $this->getMock('Symfony\Component\HttpFoundation\RequestMatcherInterface');
  41. $requestMatcher->expects($this->once())
  42. ->method('matches')->with($request)
  43. ->will($this->returnValue($matches));
  44. return $requestMatcher;
  45. }
  46. }