RequestMatcherTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\HttpFoundation\RequestMatcher;
  12. use Symfony\Component\HttpFoundation\Request;
  13. class RequestMatcherTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @dataProvider testMethodFixtures
  17. */
  18. public function testMethod($requestMethod, $matcherMethod, $isMatch)
  19. {
  20. $matcher = new RequestMatcher();
  21. $matcher->matchMethod($matcherMethod);
  22. $request = Request::create('', $requestMethod);
  23. $this->assertSame($isMatch, $matcher->matches($request));
  24. $matcher = new RequestMatcher(null, null, $matcherMethod);
  25. $request = Request::create('', $requestMethod);
  26. $this->assertSame($isMatch, $matcher->matches($request));
  27. }
  28. public function testMethodFixtures()
  29. {
  30. return array(
  31. array('get', 'get', true),
  32. array('get', array('get', 'post'), true),
  33. array('get', 'post', false),
  34. array('get', 'GET', true),
  35. array('get', array('GET', 'POST'), true),
  36. array('get', 'POST', false),
  37. );
  38. }
  39. /**
  40. * @dataProvider testHostFixture
  41. */
  42. public function testHost($pattern, $isMatch)
  43. {
  44. $matcher = new RequestMatcher();
  45. $request = Request::create('', 'get', array(), array(), array(), array('HTTP_HOST' => 'foo.example.com'));
  46. $matcher->matchHost($pattern);
  47. $this->assertSame($isMatch, $matcher->matches($request));
  48. $matcher= new RequestMatcher(null, $pattern);
  49. $this->assertSame($isMatch, $matcher->matches($request));
  50. }
  51. public function testHostFixture()
  52. {
  53. return array(
  54. array('.*\.example\.com', true),
  55. array('\.example\.com$', true),
  56. array('^.*\.example\.com$', true),
  57. array('.*\.sensio\.com', false),
  58. array('.*\.example\.COM', true),
  59. array('\.example\.COM$', true),
  60. array('^.*\.example\.COM$', true),
  61. array('.*\.sensio\.COM', false), );
  62. }
  63. public function testPath()
  64. {
  65. $matcher = new RequestMatcher();
  66. $request = Request::create('/admin/foo');
  67. $matcher->matchPath('/admin/.*');
  68. $this->assertTrue($matcher->matches($request));
  69. $matcher->matchPath('/admin');
  70. $this->assertTrue($matcher->matches($request));
  71. $matcher->matchPath('^/admin/.*$');
  72. $this->assertTrue($matcher->matches($request));
  73. $matcher->matchMethod('/blog/.*');
  74. $this->assertFalse($matcher->matches($request));
  75. }
  76. public function testPathWithLocaleIsNotSupported()
  77. {
  78. $matcher = new RequestMatcher();
  79. $request = Request::create('/en/login');
  80. $request->setLocale('en');
  81. $matcher->matchPath('^/{_locale}/login$');
  82. $this->assertFalse($matcher->matches($request));
  83. }
  84. public function testPathWithEncodedCharacters()
  85. {
  86. $matcher = new RequestMatcher();
  87. $request = Request::create('/admin/fo%20o');
  88. $matcher->matchPath('^/admin/fo o*$');
  89. $this->assertTrue($matcher->matches($request));
  90. }
  91. public function testAttributes()
  92. {
  93. $matcher = new RequestMatcher();
  94. $request = Request::create('/admin/foo');
  95. $request->attributes->set('foo', 'foo_bar');
  96. $matcher->matchAttribute('foo', 'foo_.*');
  97. $this->assertTrue($matcher->matches($request));
  98. $matcher->matchAttribute('foo', 'foo');
  99. $this->assertTrue($matcher->matches($request));
  100. $matcher->matchAttribute('foo', '^foo_bar$');
  101. $this->assertTrue($matcher->matches($request));
  102. $matcher->matchAttribute('foo', 'babar');
  103. $this->assertFalse($matcher->matches($request));
  104. }
  105. }