Yaffs site version 1.1
[yaffs-website] / vendor / symfony / http-kernel / Tests / EventListener / RouterListenerTest.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\HttpKernel\Tests\EventListener;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpKernel\EventListener\RouterListener;
17 use Symfony\Component\HttpKernel\HttpKernelInterface;
18 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
19 use Symfony\Component\Routing\RequestContext;
20
21 class RouterListenerTest extends TestCase
22 {
23     private $requestStack;
24
25     protected function setUp()
26     {
27         $this->requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->disableOriginalConstructor()->getMock();
28     }
29
30     /**
31      * @dataProvider getPortData
32      */
33     public function testPort($defaultHttpPort, $defaultHttpsPort, $uri, $expectedHttpPort, $expectedHttpsPort)
34     {
35         $urlMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcherInterface')
36             ->disableOriginalConstructor()
37             ->getMock();
38         $context = new RequestContext();
39         $context->setHttpPort($defaultHttpPort);
40         $context->setHttpsPort($defaultHttpsPort);
41         $urlMatcher->expects($this->any())
42             ->method('getContext')
43             ->will($this->returnValue($context));
44
45         $listener = new RouterListener($urlMatcher, $this->requestStack);
46         $event = $this->createGetResponseEventForUri($uri);
47         $listener->onKernelRequest($event);
48
49         $this->assertEquals($expectedHttpPort, $context->getHttpPort());
50         $this->assertEquals($expectedHttpsPort, $context->getHttpsPort());
51         $this->assertEquals(0 === strpos($uri, 'https') ? 'https' : 'http', $context->getScheme());
52     }
53
54     public function getPortData()
55     {
56         return array(
57             array(80, 443, 'http://localhost/', 80, 443),
58             array(80, 443, 'http://localhost:90/', 90, 443),
59             array(80, 443, 'https://localhost/', 80, 443),
60             array(80, 443, 'https://localhost:90/', 80, 90),
61         );
62     }
63
64     /**
65      * @param string $uri
66      *
67      * @return GetResponseEvent
68      */
69     private function createGetResponseEventForUri($uri)
70     {
71         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
72         $request = Request::create($uri);
73         $request->attributes->set('_controller', null); // Prevents going in to routing process
74
75         return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
76     }
77
78     /**
79      * @expectedException \InvalidArgumentException
80      */
81     public function testInvalidMatcher()
82     {
83         new RouterListener(new \stdClass(), $this->requestStack);
84     }
85
86     public function testRequestMatcher()
87     {
88         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
89         $request = Request::create('http://localhost/');
90         $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
91
92         $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
93         $requestMatcher->expects($this->once())
94                        ->method('matchRequest')
95                        ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
96                        ->will($this->returnValue(array()));
97
98         $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext());
99         $listener->onKernelRequest($event);
100     }
101
102     public function testSubRequestWithDifferentMethod()
103     {
104         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
105         $request = Request::create('http://localhost/', 'post');
106         $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
107
108         $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
109         $requestMatcher->expects($this->any())
110                        ->method('matchRequest')
111                        ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
112                        ->will($this->returnValue(array()));
113
114         $context = new RequestContext();
115
116         $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext());
117         $listener->onKernelRequest($event);
118
119         // sub-request with another HTTP method
120         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
121         $request = Request::create('http://localhost/', 'get');
122         $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::SUB_REQUEST);
123
124         $listener->onKernelRequest($event);
125
126         $this->assertEquals('GET', $context->getMethod());
127     }
128
129     /**
130      * @dataProvider getLoggingParameterData
131      */
132     public function testLoggingParameter($parameter, $log)
133     {
134         $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
135         $requestMatcher->expects($this->once())
136             ->method('matchRequest')
137             ->will($this->returnValue($parameter));
138
139         $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
140         $logger->expects($this->once())
141             ->method('info')
142             ->with($this->equalTo($log));
143
144         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
145         $request = Request::create('http://localhost/');
146
147         $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext(), $logger);
148         $listener->onKernelRequest(new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST));
149     }
150
151     public function getLoggingParameterData()
152     {
153         return array(
154             array(array('_route' => 'foo'), 'Matched route "foo".'),
155             array(array(), 'Matched route "n/a".'),
156         );
157     }
158 }