ExpressionRequestMatcherTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\HttpFoundation\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  13. use Symfony\Component\HttpFoundation\ExpressionRequestMatcher;
  14. use Symfony\Component\HttpFoundation\Request;
  15. class ExpressionRequestMatcherTest extends TestCase
  16. {
  17. /**
  18. * @expectedException \LogicException
  19. */
  20. public function testWhenNoExpressionIsSet()
  21. {
  22. $expressionRequestMatcher = new ExpressionRequestMatcher();
  23. $expressionRequestMatcher->matches(new Request());
  24. }
  25. /**
  26. * @dataProvider provideExpressions
  27. */
  28. public function testMatchesWhenParentMatchesIsTrue($expression, $expected)
  29. {
  30. $request = Request::create('/foo');
  31. $expressionRequestMatcher = new ExpressionRequestMatcher();
  32. $expressionRequestMatcher->setExpression(new ExpressionLanguage(), $expression);
  33. $this->assertSame($expected, $expressionRequestMatcher->matches($request));
  34. }
  35. /**
  36. * @dataProvider provideExpressions
  37. */
  38. public function testMatchesWhenParentMatchesIsFalse($expression)
  39. {
  40. $request = Request::create('/foo');
  41. $request->attributes->set('foo', 'foo');
  42. $expressionRequestMatcher = new ExpressionRequestMatcher();
  43. $expressionRequestMatcher->matchAttribute('foo', 'bar');
  44. $expressionRequestMatcher->setExpression(new ExpressionLanguage(), $expression);
  45. $this->assertFalse($expressionRequestMatcher->matches($request));
  46. }
  47. public function provideExpressions()
  48. {
  49. return [
  50. ['request.getMethod() == method', true],
  51. ['request.getPathInfo() == path', true],
  52. ['request.getHost() == host', true],
  53. ['request.getClientIp() == ip', true],
  54. ['request.attributes.all() == attributes', true],
  55. ['request.getMethod() == method && request.getPathInfo() == path && request.getHost() == host && request.getClientIp() == ip && request.attributes.all() == attributes', true],
  56. ['request.getMethod() != method', false],
  57. ['request.getMethod() != method && request.getPathInfo() == path && request.getHost() == host && request.getClientIp() == ip && request.attributes.all() == attributes', false],
  58. ];
  59. }
  60. }