Файл: gapps/vendor/symfony/http-kernel/Tests/EventListener/TestSessionListenerTest.php
Строк: 191
<?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 SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpKernelHttpKernelInterface;
use SymfonyComponentHttpKernelEventFilterResponseEvent;
use SymfonyComponentHttpFoundationSessionSessionInterface;
/**
 * SessionListenerTest.
 *
 * Tests SessionListener.
 *
 * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
 */
class TestSessionListenerTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var TestSessionListener
     */
    private $listener;
    /**
     * @var SessionInterface
     */
    private $session;
    protected function setUp()
    {
        $this->listener = $this->getMockForAbstractClass('SymfonyComponentHttpKernelEventListenerTestSessionListener');
        $this->session = $this->getSession();
    }
    public function testShouldSaveMasterRequestSession()
    {
        $this->sessionHasBeenStarted();
        $this->sessionMustBeSaved();
        $this->filterResponse(new Request());
    }
    public function testShouldNotSaveSubRequestSession()
    {
        $this->sessionMustNotBeSaved();
        $this->filterResponse(new Request(), HttpKernelInterface::SUB_REQUEST);
    }
    public function testDoesNotDeleteCookieIfUsingSessionLifetime()
    {
        $this->sessionHasBeenStarted();
        $params = session_get_cookie_params();
        session_set_cookie_params(0, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
        $response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST);
        $cookies = $response->headers->getCookies();
        $this->assertEquals(0, reset($cookies)->getExpiresTime());
    }
    public function testUnstartedSessionIsNotSave()
    {
        $this->sessionHasNotBeenStarted();
        $this->sessionMustNotBeSaved();
        $this->filterResponse(new Request());
    }
    private function filterResponse(Request $request, $type = HttpKernelInterface::MASTER_REQUEST)
    {
        $request->setSession($this->session);
        $response = new Response();
        $kernel = $this->getMock('SymfonyComponentHttpKernelHttpKernelInterface');
        $event = new FilterResponseEvent($kernel, $request, $type, $response);
        $this->listener->onKernelResponse($event);
        $this->assertSame($response, $event->getResponse());
        return $response;
    }
    private function sessionMustNotBeSaved()
    {
        $this->session->expects($this->never())
            ->method('save');
    }
    private function sessionMustBeSaved()
    {
        $this->session->expects($this->once())
            ->method('save');
    }
    private function sessionHasBeenStarted()
    {
        $this->session->expects($this->once())
            ->method('isStarted')
            ->will($this->returnValue(true));
    }
    private function sessionHasNotBeenStarted()
    {
        $this->session->expects($this->once())
            ->method('isStarted')
            ->will($this->returnValue(false));
    }
    private function getSession()
    {
        $mock = $this->getMockBuilder('SymfonyComponentHttpFoundationSessionSession')
            ->disableOriginalConstructor()
            ->getMock();
        // set return value for getName()
        $mock->expects($this->any())->method('getName')->will($this->returnValue('MOCKSESSID'));
        return $mock;
    }
}