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