do not mock the session in token storage tests

This commit is contained in:
Christian Flothmann 2018-05-18 20:00:42 +02:00
parent 05d69bb739
commit 919f93d91c

View File

@ -12,6 +12,8 @@
namespace Symfony\Component\Security\Csrf\Tests\TokenStorage; namespace Symfony\Component\Security\Csrf\Tests\TokenStorage;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage; use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
/** /**
@ -22,7 +24,7 @@ class SessionTokenStorageTest extends TestCase
const SESSION_NAMESPACE = 'foobar'; const SESSION_NAMESPACE = 'foobar';
/** /**
* @var \PHPUnit_Framework_MockObject_MockObject * @var Session
*/ */
private $session; private $session;
@ -33,118 +35,53 @@ class SessionTokenStorageTest extends TestCase
protected function setUp() protected function setUp()
{ {
$this->session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface') $this->session = new Session(new MockArraySessionStorage());
->disableOriginalConstructor()
->getMock();
$this->storage = new SessionTokenStorage($this->session, self::SESSION_NAMESPACE); $this->storage = new SessionTokenStorage($this->session, self::SESSION_NAMESPACE);
} }
public function testStoreTokenInClosedSession() public function testStoreTokenInNotStartedSessionStartsTheSession()
{ {
$this->session->expects($this->any())
->method('isStarted')
->will($this->returnValue(false));
$this->session->expects($this->once())
->method('start');
$this->session->expects($this->once())
->method('set')
->with(self::SESSION_NAMESPACE.'/token_id', 'TOKEN');
$this->storage->setToken('token_id', 'TOKEN'); $this->storage->setToken('token_id', 'TOKEN');
$this->assertTrue($this->session->isStarted());
} }
public function testStoreTokenInActiveSession() public function testStoreTokenInActiveSession()
{ {
$this->session->expects($this->any()) $this->session->start();
->method('isStarted')
->will($this->returnValue(true));
$this->session->expects($this->never())
->method('start');
$this->session->expects($this->once())
->method('set')
->with(self::SESSION_NAMESPACE.'/token_id', 'TOKEN');
$this->storage->setToken('token_id', 'TOKEN'); $this->storage->setToken('token_id', 'TOKEN');
$this->assertSame('TOKEN', $this->session->get(self::SESSION_NAMESPACE.'/token_id'));
} }
public function testCheckTokenInClosedSession() public function testCheckTokenInClosedSession()
{ {
$this->session->expects($this->any()) $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
->method('isStarted')
->will($this->returnValue(false));
$this->session->expects($this->once()) $this->assertTrue($this->storage->hasToken('token_id'));
->method('start'); $this->assertTrue($this->session->isStarted());
$this->session->expects($this->once())
->method('has')
->with(self::SESSION_NAMESPACE.'/token_id')
->will($this->returnValue('RESULT'));
$this->assertSame('RESULT', $this->storage->hasToken('token_id'));
} }
public function testCheckTokenInActiveSession() public function testCheckTokenInActiveSession()
{ {
$this->session->expects($this->any()) $this->session->start();
->method('isStarted') $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
->will($this->returnValue(true));
$this->session->expects($this->never()) $this->assertTrue($this->storage->hasToken('token_id'));
->method('start');
$this->session->expects($this->once())
->method('has')
->with(self::SESSION_NAMESPACE.'/token_id')
->will($this->returnValue('RESULT'));
$this->assertSame('RESULT', $this->storage->hasToken('token_id'));
} }
public function testGetExistingTokenFromClosedSession() public function testGetExistingTokenFromClosedSession()
{ {
$this->session->expects($this->any()) $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
->method('isStarted')
->will($this->returnValue(false));
$this->session->expects($this->once())
->method('start');
$this->session->expects($this->once())
->method('has')
->with(self::SESSION_NAMESPACE.'/token_id')
->will($this->returnValue(true));
$this->session->expects($this->once())
->method('get')
->with(self::SESSION_NAMESPACE.'/token_id')
->will($this->returnValue('RESULT'));
$this->assertSame('RESULT', $this->storage->getToken('token_id')); $this->assertSame('RESULT', $this->storage->getToken('token_id'));
$this->assertTrue($this->session->isStarted());
} }
public function testGetExistingTokenFromActiveSession() public function testGetExistingTokenFromActiveSession()
{ {
$this->session->expects($this->any()) $this->session->start();
->method('isStarted') $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
->will($this->returnValue(true));
$this->session->expects($this->never())
->method('start');
$this->session->expects($this->once())
->method('has')
->with(self::SESSION_NAMESPACE.'/token_id')
->will($this->returnValue(true));
$this->session->expects($this->once())
->method('get')
->with(self::SESSION_NAMESPACE.'/token_id')
->will($this->returnValue('RESULT'));
$this->assertSame('RESULT', $this->storage->getToken('token_id')); $this->assertSame('RESULT', $this->storage->getToken('token_id'));
} }
@ -154,18 +91,6 @@ class SessionTokenStorageTest extends TestCase
*/ */
public function testGetNonExistingTokenFromClosedSession() public function testGetNonExistingTokenFromClosedSession()
{ {
$this->session->expects($this->any())
->method('isStarted')
->will($this->returnValue(false));
$this->session->expects($this->once())
->method('start');
$this->session->expects($this->once())
->method('has')
->with(self::SESSION_NAMESPACE.'/token_id')
->will($this->returnValue(false));
$this->storage->getToken('token_id'); $this->storage->getToken('token_id');
} }
@ -174,85 +99,33 @@ class SessionTokenStorageTest extends TestCase
*/ */
public function testGetNonExistingTokenFromActiveSession() public function testGetNonExistingTokenFromActiveSession()
{ {
$this->session->expects($this->any()) $this->session->start();
->method('isStarted')
->will($this->returnValue(true));
$this->session->expects($this->never())
->method('start');
$this->session->expects($this->once())
->method('has')
->with(self::SESSION_NAMESPACE.'/token_id')
->will($this->returnValue(false));
$this->storage->getToken('token_id'); $this->storage->getToken('token_id');
} }
public function testRemoveNonExistingTokenFromClosedSession() public function testRemoveNonExistingTokenFromClosedSession()
{ {
$this->session->expects($this->any())
->method('isStarted')
->will($this->returnValue(false));
$this->session->expects($this->once())
->method('start');
$this->session->expects($this->once())
->method('remove')
->with(self::SESSION_NAMESPACE.'/token_id')
->will($this->returnValue(null));
$this->assertNull($this->storage->removeToken('token_id')); $this->assertNull($this->storage->removeToken('token_id'));
} }
public function testRemoveNonExistingTokenFromActiveSession() public function testRemoveNonExistingTokenFromActiveSession()
{ {
$this->session->expects($this->any()) $this->session->start();
->method('isStarted')
->will($this->returnValue(true));
$this->session->expects($this->never())
->method('start');
$this->session->expects($this->once())
->method('remove')
->with(self::SESSION_NAMESPACE.'/token_id')
->will($this->returnValue(null));
$this->assertNull($this->storage->removeToken('token_id')); $this->assertNull($this->storage->removeToken('token_id'));
} }
public function testRemoveExistingTokenFromClosedSession() public function testRemoveExistingTokenFromClosedSession()
{ {
$this->session->expects($this->any()) $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'TOKEN');
->method('isStarted')
->will($this->returnValue(false));
$this->session->expects($this->once())
->method('start');
$this->session->expects($this->once())
->method('remove')
->with(self::SESSION_NAMESPACE.'/token_id')
->will($this->returnValue('TOKEN'));
$this->assertSame('TOKEN', $this->storage->removeToken('token_id')); $this->assertSame('TOKEN', $this->storage->removeToken('token_id'));
} }
public function testRemoveExistingTokenFromActiveSession() public function testRemoveExistingTokenFromActiveSession()
{ {
$this->session->expects($this->any()) $this->session->start();
->method('isStarted') $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'TOKEN');
->will($this->returnValue(true));
$this->session->expects($this->never())
->method('start');
$this->session->expects($this->once())
->method('remove')
->with(self::SESSION_NAMESPACE.'/token_id')
->will($this->returnValue('TOKEN'));
$this->assertSame('TOKEN', $this->storage->removeToken('token_id')); $this->assertSame('TOKEN', $this->storage->removeToken('token_id'));
} }