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