1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\HttpFoundation\Tests;
13:
14: use Symfony\Component\HttpFoundation\RequestMatcher;
15: use Symfony\Component\HttpFoundation\Request;
16:
17: class RequestMatcherTest extends \PHPUnit_Framework_TestCase
18: {
19: 20: 21:
22: public function testMethod($requestMethod, $matcherMethod, $isMatch)
23: {
24: $matcher = new RequestMatcher();
25: $matcher->matchMethod($matcherMethod);
26: $request = Request::create('', $requestMethod);
27: $this->assertSame($isMatch, $matcher->matches($request));
28:
29: $matcher = new RequestMatcher(null, null, $matcherMethod);
30: $request = Request::create('', $requestMethod);
31: $this->assertSame($isMatch, $matcher->matches($request));
32: }
33:
34: public function testMethodFixtures()
35: {
36: return array(
37: array('get', 'get', true),
38: array('get', array('get', 'post'), true),
39: array('get', 'post', false),
40: array('get', 'GET', true),
41: array('get', array('GET', 'POST'), true),
42: array('get', 'POST', false),
43: );
44: }
45:
46: public function testScheme()
47: {
48: $httpRequest = $request = $request = Request::create('');
49: $httpsRequest = $request = $request = Request::create('', 'get', array(), array(), array(), array('HTTPS' => 'on'));
50:
51: $matcher = new RequestMatcher();
52: $matcher->matchScheme('https');
53: $this->assertFalse($matcher->matches($httpRequest));
54: $this->assertTrue($matcher->matches($httpsRequest));
55:
56: $matcher->matchScheme('http');
57: $this->assertFalse($matcher->matches($httpsRequest));
58: $this->assertTrue($matcher->matches($httpRequest));
59:
60: $matcher = new RequestMatcher();
61: $this->assertTrue($matcher->matches($httpsRequest));
62: $this->assertTrue($matcher->matches($httpRequest));
63: }
64:
65: 66: 67:
68: public function testHost($pattern, $isMatch)
69: {
70: $matcher = new RequestMatcher();
71: $request = Request::create('', 'get', array(), array(), array(), array('HTTP_HOST' => 'foo.example.com'));
72:
73: $matcher->matchHost($pattern);
74: $this->assertSame($isMatch, $matcher->matches($request));
75:
76: $matcher = new RequestMatcher(null, $pattern);
77: $this->assertSame($isMatch, $matcher->matches($request));
78: }
79:
80: public function testHostFixture()
81: {
82: return array(
83: array('.*\.example\.com', true),
84: array('\.example\.com$', true),
85: array('^.*\.example\.com$', true),
86: array('.*\.sensio\.com', false),
87: array('.*\.example\.COM', true),
88: array('\.example\.COM$', true),
89: array('^.*\.example\.COM$', true),
90: array('.*\.sensio\.COM', false),
91: );
92: }
93:
94: public function testPath()
95: {
96: $matcher = new RequestMatcher();
97:
98: $request = Request::create('/admin/foo');
99:
100: $matcher->matchPath('/admin/.*');
101: $this->assertTrue($matcher->matches($request));
102:
103: $matcher->matchPath('/admin');
104: $this->assertTrue($matcher->matches($request));
105:
106: $matcher->matchPath('^/admin/.*$');
107: $this->assertTrue($matcher->matches($request));
108:
109: $matcher->matchMethod('/blog/.*');
110: $this->assertFalse($matcher->matches($request));
111: }
112:
113: public function testPathWithLocaleIsNotSupported()
114: {
115: $matcher = new RequestMatcher();
116: $request = Request::create('/en/login');
117: $request->setLocale('en');
118:
119: $matcher->matchPath('^/{_locale}/login$');
120: $this->assertFalse($matcher->matches($request));
121: }
122:
123: public function testPathWithEncodedCharacters()
124: {
125: $matcher = new RequestMatcher();
126: $request = Request::create('/admin/fo%20o');
127: $matcher->matchPath('^/admin/fo o*$');
128: $this->assertTrue($matcher->matches($request));
129: }
130:
131: public function testAttributes()
132: {
133: $matcher = new RequestMatcher();
134:
135: $request = Request::create('/admin/foo');
136: $request->attributes->set('foo', 'foo_bar');
137:
138: $matcher->matchAttribute('foo', 'foo_.*');
139: $this->assertTrue($matcher->matches($request));
140:
141: $matcher->matchAttribute('foo', 'foo');
142: $this->assertTrue($matcher->matches($request));
143:
144: $matcher->matchAttribute('foo', '^foo_bar$');
145: $this->assertTrue($matcher->matches($request));
146:
147: $matcher->matchAttribute('foo', 'babar');
148: $this->assertFalse($matcher->matches($request));
149: }
150: }
151: