MigratingSessionHandlerTest

class MigratingSessionHandlerTest extends TestCase

Methods

protected setUp() No description
public testInstanceOf() No description
public testClose() No description
public testDestroy() No description
public testGc() No description
public testOpen() No description
public testRead() No description
public testWrite() No description
public testValidateId() No description
public testUpdateTimestamp() No description

Details

at line 23

setUp()

protected setUp()
at line 31

testInstanceOf()

public testInstanceOf()
at line 37

testClose()

public testClose()
at line 52

testDestroy()

public testDestroy()
at line 71

testGc()

public testGc()
at line 89

testOpen()

public testOpen()
at line 109

testRead()

public testRead()
at line 128

testWrite()

public testWrite()
at line 148

testValidateId()

public testValidateId()
at line 167

testUpdateTimestamp()

public testUpdateTimestamp()

Source code

<?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\Component\HttpFoundation\Tests\Session\Storage\Handler;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MigratingSessionHandler;

class MigratingSessionHandlerTest extends TestCase
{
    private $dualHandler;
    private $currentHandler;
    private $writeOnlyHandler;

    protected function setUp()
    {
        $this->currentHandler = $this->createMock(\SessionHandlerInterface::class);
        $this->writeOnlyHandler = $this->createMock(\SessionHandlerInterface::class);

        $this->dualHandler = new MigratingSessionHandler($this->currentHandler, $this->writeOnlyHandler);
    }

    public function testInstanceOf()
    {
        $this->assertInstanceOf(\SessionHandlerInterface::class, $this->dualHandler);
        $this->assertInstanceOf(\SessionUpdateTimestampHandlerInterface::class, $this->dualHandler);
    }

    public function testClose()
    {
        $this->currentHandler->expects($this->once())
            ->method('close')
            ->will($this->returnValue(true));

        $this->writeOnlyHandler->expects($this->once())
            ->method('close')
            ->will($this->returnValue(false));

        $result = $this->dualHandler->close();

        $this->assertTrue($result);
    }

    public function testDestroy()
    {
        $sessionId = 'xyz';

        $this->currentHandler->expects($this->once())
            ->method('destroy')
            ->with($sessionId)
            ->will($this->returnValue(true));

        $this->writeOnlyHandler->expects($this->once())
            ->method('destroy')
            ->with($sessionId)
            ->will($this->returnValue(false));

        $result = $this->dualHandler->destroy($sessionId);

        $this->assertTrue($result);
    }

    public function testGc()
    {
        $maxlifetime = 357;

        $this->currentHandler->expects($this->once())
            ->method('gc')
            ->with($maxlifetime)
            ->will($this->returnValue(true));

        $this->writeOnlyHandler->expects($this->once())
            ->method('gc')
            ->with($maxlifetime)
            ->will($this->returnValue(false));

        $result = $this->dualHandler->gc($maxlifetime);
        $this->assertTrue($result);
    }

    public function testOpen()
    {
        $savePath = '/path/to/save/location';
        $sessionName = 'xyz';

        $this->currentHandler->expects($this->once())
            ->method('open')
            ->with($savePath, $sessionName)
            ->will($this->returnValue(true));

        $this->writeOnlyHandler->expects($this->once())
            ->method('open')
            ->with($savePath, $sessionName)
            ->will($this->returnValue(false));

        $result = $this->dualHandler->open($savePath, $sessionName);

        $this->assertTrue($result);
    }

    public function testRead()
    {
        $sessionId = 'xyz';
        $readValue = 'something';

        $this->currentHandler->expects($this->once())
            ->method('read')
            ->with($sessionId)
            ->will($this->returnValue($readValue));

        $this->writeOnlyHandler->expects($this->never())
            ->method('read')
            ->with($this->any());

        $result = $this->dualHandler->read($sessionId);

        $this->assertSame($readValue, $result);
    }

    public function testWrite()
    {
        $sessionId = 'xyz';
        $data = 'my-serialized-data';

        $this->currentHandler->expects($this->once())
            ->method('write')
            ->with($sessionId, $data)
            ->will($this->returnValue(true));

        $this->writeOnlyHandler->expects($this->once())
            ->method('write')
            ->with($sessionId, $data)
            ->will($this->returnValue(false));

        $result = $this->dualHandler->write($sessionId, $data);

        $this->assertTrue($result);
    }

    public function testValidateId()
    {
        $sessionId = 'xyz';
        $readValue = 'something';

        $this->currentHandler->expects($this->once())
            ->method('read')
            ->with($sessionId)
            ->will($this->returnValue($readValue));

        $this->writeOnlyHandler->expects($this->never())
            ->method('read')
            ->with($this->any());

        $result = $this->dualHandler->validateId($sessionId);

        $this->assertTrue($result);
    }

    public function testUpdateTimestamp()
    {
        $sessionId = 'xyz';
        $data = 'my-serialized-data';

        $this->currentHandler->expects($this->once())
            ->method('write')
            ->with($sessionId, $data)
            ->will($this->returnValue(true));

        $this->writeOnlyHandler->expects($this->once())
            ->method('write')
            ->with($sessionId, $data)
            ->will($this->returnValue(false));

        $result = $this->dualHandler->updateTimestamp($sessionId, $data);

        $this->assertTrue($result);
    }
}