Backup of db before drupal security update
[yaffs-website] / vendor / symfony-cmf / routing / Tests / Routing / ProviderBasedGeneratorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony CMF package.
5  *
6  * (c) 2011-2015 Symfony CMF
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\Cmf\Component\Routing\Tests\Routing;
13
14 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
15 use Symfony\Component\Routing\RequestContext;
16 use Symfony\Component\Routing\Route;
17 use Symfony\Cmf\Component\Routing\ProviderBasedGenerator;
18 use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
19
20 class ProviderBasedGeneratorTest extends CmfUnitTestCase
21 {
22     protected $routeDocument;
23     protected $routeCompiled;
24     protected $provider;
25
26     /** @var ProviderBasedGenerator */
27     protected $generator;
28     protected $context;
29
30     public function setUp()
31     {
32         $this->routeDocument = $this->buildMock('Symfony\Component\Routing\Route', array('getDefaults', 'compile'));
33         $this->routeCompiled = $this->buildMock('Symfony\Component\Routing\CompiledRoute');
34         $this->provider = $this->buildMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
35         $this->context = $this->buildMock('Symfony\Component\Routing\RequestContext');
36
37         $this->generator = new TestableProviderBasedGenerator($this->provider);
38     }
39
40     public function testGenerateFromName()
41     {
42         $name = 'foo/bar';
43
44         $this->provider->expects($this->once())
45             ->method('getRouteByName')
46             ->with($name)
47             ->will($this->returnValue($this->routeDocument))
48         ;
49         $this->routeDocument->expects($this->once())
50             ->method('compile')
51             ->will($this->returnValue($this->routeCompiled))
52         ;
53
54         $this->assertEquals('result_url', $this->generator->generate($name));
55     }
56
57     /**
58      * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
59      */
60     public function testGenerateNotFound()
61     {
62         $name = 'foo/bar';
63
64         $this->provider->expects($this->once())
65             ->method('getRouteByName')
66             ->with($name)
67             ->will($this->returnValue(null))
68         ;
69
70         $this->generator->generate($name);
71     }
72
73     public function testGenerateFromRoute()
74     {
75         $this->provider->expects($this->never())
76             ->method('getRouteByName')
77         ;
78         $this->routeDocument->expects($this->once())
79             ->method('compile')
80             ->will($this->returnValue($this->routeCompiled))
81         ;
82
83         $this->assertEquals('result_url', $this->generator->generate($this->routeDocument));
84     }
85
86     public function testSupports()
87     {
88         $this->assertTrue($this->generator->supports('foo/bar'));
89         $this->assertTrue($this->generator->supports($this->routeDocument));
90         $this->assertFalse($this->generator->supports($this));
91     }
92
93     public function testGetRouteDebugMessage()
94     {
95         $this->assertContains('/some/key', $this->generator->getRouteDebugMessage(new RouteObject()));
96         $this->assertContains('/de/test', $this->generator->getRouteDebugMessage(new Route('/de/test')));
97         $this->assertContains('/some/route', $this->generator->getRouteDebugMessage('/some/route'));
98         $this->assertContains('a:1:{s:10:"route_name";s:7:"example";}', $this->generator->getRouteDebugMessage(array('route_name' => 'example')));
99     }
100
101     /**
102      * Tests the generate method with passing in a route object into generate().
103      *
104      * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
105      */
106     public function testGenerateByRoute()
107     {
108         $this->generator = new ProviderBasedGenerator($this->provider);
109
110         // Setup a route with a numeric parameter, but pass in a string, so it
111         // fails and getRouteDebugMessage should be triggered.
112         $route = new Route('/test');
113         $route->setPath('/test/{number}');
114         $route->setRequirement('number', '\+d');
115
116         $this->generator->setStrictRequirements(true);
117
118         $context = new RequestContext();
119         $this->generator->setContext($context);
120
121         $this->assertSame(null, $this->generator->generate($route, array('number' => 'string')));
122     }
123 }
124
125 /**
126  * Overwrite doGenerate to reduce amount of mocking needed.
127  */
128 class TestableProviderBasedGenerator extends ProviderBasedGenerator
129 {
130     protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array())
131     {
132         return 'result_url';
133     }
134 }
135
136 class RouteObject implements RouteObjectInterface
137 {
138     public function getRouteKey()
139     {
140         return '/some/key';
141     }
142
143     public function getContent()
144     {
145         return;
146     }
147 }