0f3cdeabac39feb054a60bc04f5bb95f1cac561c
[yaffs-website] / vendor / symfony / routing / Tests / Matcher / RedirectableUrlMatcherTest.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\Routing\Tests\Matcher;
13
14 use Symfony\Component\Routing\RequestContext;
15 use Symfony\Component\Routing\Route;
16 use Symfony\Component\Routing\RouteCollection;
17
18 class RedirectableUrlMatcherTest extends UrlMatcherTest
19 {
20     public function testMissingTrailingSlash()
21     {
22         $coll = new RouteCollection();
23         $coll->add('foo', new Route('/foo/'));
24
25         $matcher = $this->getUrlMatcher($coll);
26         $matcher->expects($this->once())->method('redirect')->will($this->returnValue(array()));
27         $matcher->match('/foo');
28     }
29
30     /**
31      * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
32      */
33     public function testRedirectWhenNoSlashForNonSafeMethod()
34     {
35         $coll = new RouteCollection();
36         $coll->add('foo', new Route('/foo/'));
37
38         $context = new RequestContext();
39         $context->setMethod('POST');
40         $matcher = $this->getUrlMatcher($coll, $context);
41         $matcher->match('/foo');
42     }
43
44     public function testSchemeRedirectRedirectsToFirstScheme()
45     {
46         $coll = new RouteCollection();
47         $coll->add('foo', new Route('/foo', array(), array(), array(), '', array('FTP', 'HTTPS')));
48
49         $matcher = $this->getUrlMatcher($coll);
50         $matcher
51             ->expects($this->once())
52             ->method('redirect')
53             ->with('/foo', 'foo', 'ftp')
54             ->will($this->returnValue(array('_route' => 'foo')))
55         ;
56         $matcher->match('/foo');
57     }
58
59     public function testNoSchemaRedirectIfOneOfMultipleSchemesMatches()
60     {
61         $coll = new RouteCollection();
62         $coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https', 'http')));
63
64         $matcher = $this->getUrlMatcher($coll);
65         $matcher
66             ->expects($this->never())
67             ->method('redirect');
68         $matcher->match('/foo');
69     }
70
71     public function testSchemeRedirectWithParams()
72     {
73         $coll = new RouteCollection();
74         $coll->add('foo', new Route('/foo/{bar}', array(), array(), array(), '', array('https')));
75
76         $matcher = $this->getUrlMatcher($coll);
77         $matcher
78             ->expects($this->once())
79             ->method('redirect')
80             ->with('/foo/baz', 'foo', 'https')
81             ->will($this->returnValue(array('redirect' => 'value')))
82         ;
83         $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'redirect' => 'value'), $matcher->match('/foo/baz'));
84     }
85
86     public function testSlashRedirectWithParams()
87     {
88         $coll = new RouteCollection();
89         $coll->add('foo', new Route('/foo/{bar}/'));
90
91         $matcher = $this->getUrlMatcher($coll);
92         $matcher
93             ->expects($this->once())
94             ->method('redirect')
95             ->with('/foo/baz/', 'foo', null)
96             ->will($this->returnValue(array('redirect' => 'value')))
97         ;
98         $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'redirect' => 'value'), $matcher->match('/foo/baz'));
99     }
100
101     public function testRedirectPreservesUrlEncoding()
102     {
103         $coll = new RouteCollection();
104         $coll->add('foo', new Route('/foo:bar/'));
105
106         $matcher = $this->getUrlMatcher($coll);
107         $matcher->expects($this->once())->method('redirect')->with('/foo%3Abar/')->willReturn(array());
108         $matcher->match('/foo%3Abar');
109     }
110
111     public function testSchemeRequirement()
112     {
113         $coll = new RouteCollection();
114         $coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https')));
115         $matcher = $this->getUrlMatcher($coll, new RequestContext());
116         $matcher->expects($this->once())->method('redirect')->with('/foo', 'foo', 'https')->willReturn(array());
117         $this->assertSame(array('_route' => 'foo'), $matcher->match('/foo'));
118     }
119
120     protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
121     {
122         return $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($routes, $context ?: new RequestContext()));
123     }
124 }