Skip to content

Commit

Permalink
[FrameworkBundle] refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Apr 30, 2011
1 parent 2adc56d commit bb7b7e8
Showing 1 changed file with 75 additions and 75 deletions.
150 changes: 75 additions & 75 deletions src/Symfony/Bundle/FrameworkBundle/Tests/RequestListenerTest.php
@@ -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);
}
}

0 comments on commit bb7b7e8

Please sign in to comment.