Author: Fabien Potencier (fabien.potencier@symfony-project.com)
Inheritance: extends Symfony\Component\DependencyInjection\ContainerAware
コード例 #1
0
ファイル: RedirectControllerTest.php プロジェクト: hykz/Depot
 public function testFullURL()
 {
     $controller = new RedirectController();
     $returnResponse = $controller->urlRedirectAction('http://foo.bar/');
     $this->assertInstanceOf('\\Symfony\\Component\\HttpFoundation\\Response', $returnResponse);
     $this->assertEquals('http://foo.bar/', $returnResponse->headers->get('Location'));
     $this->assertEquals(302, $returnResponse->getStatusCode());
 }
コード例 #2
0
 public function testEmptyPath()
 {
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     $controller = new RedirectController();
     $controller->setContainer($container);
     $returnResponse = $controller->urlRedirectAction('');
     $this->assertInstanceOf('\\Symfony\\Component\\HttpFoundation\\Response', $returnResponse);
     $this->assertEquals(410, $returnResponse->getStatusCode());
 }
コード例 #3
0
 /**
  * Redirects to another route with the given name.
  *
  * The response status code is 302 if the permanent parameter is false (default),
  * and 301 if the redirection is permanent.
  *
  * In case the route name is empty, the status code will be 404 when permanent is false
  * and 410 otherwise.
  *
  * @param Request    $request          The request instance
  * @param string     $route            The route name to redirect to
  * @param bool       $permanent        Whether the redirection is permanent
  * @param bool|array $ignoreAttributes Whether to ignore attributes or an array of attributes to ignore
  * @param array|bool $ignoreQueryString
  *
  * @return RedirectResponse A Response instance
  *
  * @throws HttpException In case the route name is empty
  */
 public function redirectAction(Request $request, $route, $permanent = false, $ignoreAttributes = false, $ignoreQueryString = false)
 {
     if ($ignoreAttributes !== true) {
         $ignoreAttributes[] = 'ignoreQueryString';
     }
     /** @var RedirectResponse $response */
     $response = parent::redirectAction($request, $route, $permanent, $ignoreAttributes);
     // If the query string is irrelevant then don't manipulate
     if ($ignoreQueryString === true) {
         return $response;
     }
     $queryString = $request->getQueryString();
     if (is_array($ignoreQueryString)) {
         $pairs = explode('&', $queryString);
         foreach ($pairs as $index => $pair) {
             list($key) = explode('=', $pair);
             if (in_array($key, $ignoreQueryString)) {
                 unset($pairs[$index]);
             }
         }
         $queryString = implode('&', $pairs);
     }
     $targetUrl = $response->getTargetUrl();
     if ($queryString) {
         if (strpos($targetUrl, '?') === false) {
             $targetUrl .= '?' . $queryString;
         } else {
             $targetUrl .= '&' . $queryString;
         }
     }
     $response->setTargetUrl($targetUrl);
     return $response;
 }
コード例 #4
0
 /**
  * @dataProvider provider
  */
 public function testRoute($permanent, $expectedCode)
 {
     $request = new Request();
     $route = 'new-route';
     $url = '/redirect-url';
     $params = array('additional-parameter' => 'value');
     $request->attributes = new ParameterBag(array('route' => $route, '_route' => 'current-route', 'permanent' => $permanent) + $params);
     $router = $this->getMock('Symfony\\Component\\Routing\\RouterInterface');
     $router->expects($this->once())->method('generate')->with($this->equalTo($route), $this->equalTo($params))->will($this->returnValue($url));
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     $container->expects($this->at(0))->method('get')->with($this->equalTo('request'))->will($this->returnValue($request));
     $container->expects($this->at(1))->method('get')->with($this->equalTo('router'))->will($this->returnValue($router));
     $controller = new RedirectController();
     $controller->setContainer($container);
     $returnResponse = $controller->redirectAction($route, $permanent);
     $this->assertInstanceOf('\\Symfony\\Component\\HttpFoundation\\Response', $returnResponse);
     $this->assertTrue($returnResponse->isRedirect($url));
     $this->assertEquals($expectedCode, $returnResponse->getStatusCode());
 }
コード例 #5
0
 private function createRedirectController($httpPort = null, $httpsPort = null)
 {
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     if (null !== $httpPort) {
         $container->expects($this->once())->method('hasParameter')->with($this->equalTo('request_listener.http_port'))->will($this->returnValue(true));
         $container->expects($this->once())->method('getParameter')->with($this->equalTo('request_listener.http_port'))->will($this->returnValue($httpPort));
     }
     if (null !== $httpsPort) {
         $container->expects($this->once())->method('hasParameter')->with($this->equalTo('request_listener.https_port'))->will($this->returnValue(true));
         $container->expects($this->once())->method('getParameter')->with($this->equalTo('request_listener.https_port'))->will($this->returnValue($httpsPort));
     }
     $controller = new RedirectController();
     $controller->setContainer($container);
     return $controller;
 }