|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpFoundation\Tests; |
| 13 | + |
| 14 | +use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
| 15 | +use Symfony\Component\HttpFoundation\ExpressionRequestMatcher; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | + |
| 18 | +class ExpressionRequestMatcherTest extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @expectedException \LogicException |
| 22 | + */ |
| 23 | + public function testWhenNoExpressionIsSet() |
| 24 | + { |
| 25 | + $expressionRequestMatcher = new ExpressionRequestMatcher(); |
| 26 | + $expressionRequestMatcher->matches(new Request()); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @dataProvider provideExpressions |
| 31 | + */ |
| 32 | + public function testMatchesWhenParentMatchesIsTrue($expression, $expected) |
| 33 | + { |
| 34 | + $request = Request::create('/foo'); |
| 35 | + $expressionRequestMatcher = new ExpressionRequestMatcher(); |
| 36 | + |
| 37 | + $expressionRequestMatcher->setExpression(new ExpressionLanguage(), $expression); |
| 38 | + $this->assertSame($expected, $expressionRequestMatcher->matches($request)); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @dataProvider provideExpressions |
| 43 | + */ |
| 44 | + public function testMatchesWhenParentMatchesIsFalse($expression) |
| 45 | + { |
| 46 | + $request = Request::create('/foo'); |
| 47 | + $request->attributes->set('foo', 'foo'); |
| 48 | + $expressionRequestMatcher = new ExpressionRequestMatcher(); |
| 49 | + $expressionRequestMatcher->matchAttribute('foo', 'bar'); |
| 50 | + |
| 51 | + $expressionRequestMatcher->setExpression(new ExpressionLanguage(), $expression); |
| 52 | + $this->assertFalse($expressionRequestMatcher->matches($request)); |
| 53 | + } |
| 54 | + |
| 55 | + public function provideExpressions() |
| 56 | + { |
| 57 | + return array( |
| 58 | + array('request.getMethod() == method', true), |
| 59 | + array('request.getPathInfo() == path', true), |
| 60 | + array('request.getHost() == host', true), |
| 61 | + array('request.getClientIp() == ip', true), |
| 62 | + array('request.attributes.all() == attributes', true), |
| 63 | + array('request.getMethod() == method && request.getPathInfo() == path && request.getHost() == host && request.getClientIp() == ip && request.attributes.all() == attributes', true), |
| 64 | + array('request.getMethod() != method', false), |
| 65 | + array('request.getMethod() != method && request.getPathInfo() == path && request.getHost() == host && request.getClientIp() == ip && request.attributes.all() == attributes', false), |
| 66 | + ); |
| 67 | + } |
| 68 | +} |
0 commit comments