Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

ExpressionRequestMatcherTest.php 2.5 KiB

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