ExpressionRequestMatcherTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 array(
  50. array('request.getMethod() == method', true),
  51. array('request.getPathInfo() == path', true),
  52. array('request.getHost() == host', true),
  53. array('request.getClientIp() == ip', true),
  54. array('request.attributes.all() == attributes', true),
  55. array('request.getMethod() == method && request.getPathInfo() == path && request.getHost() == host && request.getClientIp() == ip && request.attributes.all() == attributes', true),
  56. array('request.getMethod() != method', false),
  57. array('request.getMethod() != method && request.getPathInfo() == path && request.getHost() == host && request.getClientIp() == ip && request.attributes.all() == attributes', false),
  58. );
  59. }
  60. }