AccessMapTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\Security\Http\AccessMap;
  13. class AccessMapTest extends TestCase
  14. {
  15. public function testReturnsFirstMatchedPattern()
  16. {
  17. $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
  18. $requestMatcher1 = $this->getRequestMatcher($request, false);
  19. $requestMatcher2 = $this->getRequestMatcher($request, true);
  20. $map = new AccessMap();
  21. $map->add($requestMatcher1, array('ROLE_ADMIN'), 'http');
  22. $map->add($requestMatcher2, array('ROLE_USER'), 'https');
  23. $this->assertSame(array(array('ROLE_USER'), 'https'), $map->getPatterns($request));
  24. }
  25. public function testReturnsEmptyPatternIfNoneMatched()
  26. {
  27. $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
  28. $requestMatcher = $this->getRequestMatcher($request, false);
  29. $map = new AccessMap();
  30. $map->add($requestMatcher, array('ROLE_USER'), 'https');
  31. $this->assertSame(array(null, null), $map->getPatterns($request));
  32. }
  33. private function getRequestMatcher($request, $matches)
  34. {
  35. $requestMatcher = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestMatcherInterface')->getMock();
  36. $requestMatcher->expects($this->once())
  37. ->method('matches')->with($request)
  38. ->will($this->returnValue($matches));
  39. return $requestMatcher;
  40. }
  41. }