Вход Регистрация
Файл: symfony-2.7/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterListenerTest.php
Строк: 246
<?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 SymfonyComponentHttpKernelTestsEventListener;

use 
SymfonyComponentHttpFoundationRequestStack;
use 
SymfonyComponentHttpFoundationRequest;
use 
SymfonyComponentHttpKernelEventListenerRouterListener;
use 
SymfonyComponentHttpKernelHttpKernelInterface;
use 
SymfonyComponentHttpKernelEventGetResponseEvent;
use 
SymfonyComponentRoutingRequestContext;

class 
RouterListenerTest extends PHPUnit_Framework_TestCase
{
    private 
$requestStack;

    public function 
setUp()
    {
        
$this->requestStack $this->getMock('SymfonyComponentHttpFoundationRequestStack', array(), array(), ''false);
    }

    
/**
     * @dataProvider getPortData
     */
    
public function testPort($defaultHttpPort$defaultHttpsPort$uri$expectedHttpPort$expectedHttpsPort)
    {
        
$urlMatcher $this->getMockBuilder('SymfonyComponentRoutingMatcherUrlMatcherInterface')
                             ->
disableOriginalConstructor()
                             ->
getMock();
        
$context = new RequestContext();
        
$context->setHttpPort($defaultHttpPort);
        
$context->setHttpsPort($defaultHttpsPort);
        
$urlMatcher->expects($this->any())
                     ->
method('getContext')
                     ->
will($this->returnValue($context));

        
$listener = new RouterListener($urlMatchernullnull$this->requestStack);
        
$event $this->createGetResponseEventForUri($uri);
        
$listener->onKernelRequest($event);

        
$this->assertEquals($expectedHttpPort$context->getHttpPort());
        
$this->assertEquals($expectedHttpsPort$context->getHttpsPort());
        
$this->assertEquals(=== strpos($uri'https') ? 'https' 'http'$context->getScheme());
    }

    public function 
getPortData()
    {
        return array(
            array(
80443'http://localhost/'80443),
            array(
80443'http://localhost:90/'90443),
            array(
80443'https://localhost/'80443),
            array(
80443'https://localhost:90/'8090),
        );
    }

    
/**
     * @param string $uri
     *
     * @return GetResponseEvent
     */
    
private function createGetResponseEventForUri($uri)
    {
        
$kernel $this->getMock('SymfonyComponentHttpKernelHttpKernelInterface');
        
$request Request::create($uri);
        
$request->attributes->set('_controller'null); // Prevents going in to routing process

        
return new GetResponseEvent($kernel$requestHttpKernelInterface::MASTER_REQUEST);
    }

    
/**
     * @expectedException InvalidArgumentException
     */
    
public function testInvalidMatcher()
    {
        new 
RouterListener(new stdClass(), nullnull$this->requestStack);
    }

    public function 
testRequestMatcher()
    {
        
$kernel $this->getMock('SymfonyComponentHttpKernelHttpKernelInterface');
        
$request Request::create('http://localhost/');
        
$event = new GetResponseEvent($kernel$requestHttpKernelInterface::MASTER_REQUEST);

        
$requestMatcher $this->getMock('SymfonyComponentRoutingMatcherRequestMatcherInterface');
        
$requestMatcher->expects($this->once())
                       ->
method('matchRequest')
                       ->
with($this->isInstanceOf('SymfonyComponentHttpFoundationRequest'))
                       ->
will($this->returnValue(array()));

        
$listener = new RouterListener($requestMatcher, new RequestContext(), null$this->requestStack);
        
$listener->onKernelRequest($event);
    }

    public function 
testSubRequestWithDifferentMethod()
    {
        
$kernel $this->getMock('SymfonyComponentHttpKernelHttpKernelInterface');
        
$request Request::create('http://localhost/''post');
        
$event = new GetResponseEvent($kernel$requestHttpKernelInterface::MASTER_REQUEST);

        
$requestMatcher $this->getMock('SymfonyComponentRoutingMatcherRequestMatcherInterface');
        
$requestMatcher->expects($this->any())
                       ->
method('matchRequest')
                       ->
with($this->isInstanceOf('SymfonyComponentHttpFoundationRequest'))
                       ->
will($this->returnValue(array()));

        
$context = new RequestContext();
        
$requestMatcher->expects($this->any())
                       ->
method('getContext')
                       ->
will($this->returnValue($context));

        
$listener = new RouterListener($requestMatcher, new RequestContext(), null$this->requestStack);
        
$listener->onKernelRequest($event);

        
// sub-request with another HTTP method
        
$kernel $this->getMock('SymfonyComponentHttpKernelHttpKernelInterface');
        
$request Request::create('http://localhost/''get');
        
$event = new GetResponseEvent($kernel$requestHttpKernelInterface::SUB_REQUEST);

        
$listener->onKernelRequest($event);

        
$this->assertEquals('GET'$context->getMethod());
    }
}
Онлайн: 0
Реклама