forked from symfony/symfony
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
75 additions
and
75 deletions.
There are no files selected for viewing
150 changes: 75 additions & 75 deletions
150
src/Symfony/Bundle/FrameworkBundle/Tests/RequestListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\Routing\RequestContext; | ||
use Symfony\Bundle\FrameworkBundle\RequestListener; | ||
|
||
class RequestListenerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $container; | ||
private $router; | ||
protected function setUp() | ||
{ | ||
$this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); | ||
// TODO: Change to Symfony\Component\Routing\RouterInterface once has setContext method | ||
$this->router = $this->getMockBuilder('Symfony\Component\Routing\Router') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
} | ||
|
||
public function testConstructPortGetsPassedInRouterSetContext() | ||
{ | ||
$listener = new RequestListener($this->container, $this->router, 99); | ||
$expectedContext = new RequestContext(); | ||
$expectedContext->setHttpPort(99); | ||
$this->router->expects($this->once()) | ||
->method('setContext') | ||
->with($expectedContext); | ||
|
||
$event = $this->createGetResponseEventForUri('http://localhost:99/'); | ||
$listener->onCoreRequest($event); | ||
} | ||
public function testRequestPortGetsPassedInRouterSetContextIfNoConstructorPort() | ||
{ | ||
$listener = new RequestListener($this->container, $this->router); | ||
$expectedContext = new RequestContext(); | ||
$expectedContext->setHttpPort(99); | ||
$this->router->expects($this->once()) | ||
->method('setContext') | ||
->with($expectedContext); | ||
|
||
$event = $this->createGetResponseEventForUri('http://localhost:99/'); | ||
$listener->onCoreRequest($event); | ||
} | ||
/** | ||
* @param string $uri | ||
* @return GetResponseEvent | ||
*/ | ||
private function createGetResponseEventForUri($uri) | ||
{ | ||
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); | ||
$request = Request::create($uri); | ||
$request->attributes->set('_controller', null); // Prevents going in to routing process | ||
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); | ||
return $event; | ||
} | ||
} | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\Routing\RequestContext; | ||
use Symfony\Bundle\FrameworkBundle\RequestListener; | ||
|
||
class RequestListenerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $container; | ||
private $router; | ||
|
||
protected function setUp() | ||
{ | ||
$this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); | ||
$this->router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
} | ||
|
||
/** | ||
* @dataProvider getPortData | ||
*/ | ||
public function testPort($defaultHttpPort, $defaultHttpsPort, $uri, $expectedHttpPort, $expectedHttpsPort) | ||
{ | ||
$listener = new RequestListener($this->container, $this->router, $defaultHttpPort, $defaultHttpsPort); | ||
|
||
$expectedContext = new RequestContext(); | ||
$expectedContext->setHttpPort($expectedHttpPort); | ||
$expectedContext->setHttpsPort($expectedHttpsPort); | ||
$expectedContext->setScheme(0 === strpos($uri, 'https') ? 'https' : 'http'); | ||
$this->router->expects($this->once()) | ||
->method('setContext') | ||
->with($expectedContext); | ||
|
||
$event = $this->createGetResponseEventForUri($uri); | ||
$listener->onCoreRequest($event); | ||
} | ||
|
||
public function getPortData() | ||
{ | ||
return array( | ||
array(80, 443, 'http://localhost/', 80, 443), | ||
array(80, 443, 'http://localhost:90/', 90, 443), | ||
array(80, 443, 'https://localhost/', 80, 443), | ||
array(80, 443, 'https://localhost:90/', 80, 90), | ||
); | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* | ||
* @return GetResponseEvent | ||
*/ | ||
private function createGetResponseEventForUri($uri) | ||
{ | ||
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); | ||
$request = Request::create($uri); | ||
$request->attributes->set('_controller', null); // Prevents going in to routing process | ||
|
||
return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); | ||
} | ||
} |