From: =?utf-8?q?David_Pr=C3=A9vot?= <david@tilapin.org>
Date: Wed, 25 Jul 2018 15:00:01 +0800
Subject: do not mock the session in token storage tests

Origin: backport, https://github.com/symfony/symfony/commit/919f93d91c73a8d95ccd991d66c9fc4ec5e7f5f2
---
 .../Tests/TokenStorage/SessionTokenStorageTest.php | 177 +++------------------
 1 file changed, 25 insertions(+), 152 deletions(-)

diff --git a/src/Symfony/Component/Security/Csrf/Tests/TokenStorage/SessionTokenStorageTest.php b/src/Symfony/Component/Security/Csrf/Tests/TokenStorage/SessionTokenStorageTest.php
index 0eac0a8..9ba1ab2 100644
--- a/src/Symfony/Component/Security/Csrf/Tests/TokenStorage/SessionTokenStorageTest.php
+++ b/src/Symfony/Component/Security/Csrf/Tests/TokenStorage/SessionTokenStorageTest.php
@@ -11,6 +11,8 @@
 
 namespace Symfony\Component\Security\Csrf\Tests\TokenStorage;
 
+use Symfony\Component\HttpFoundation\Session\Session;
+use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
 use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
 
 /**
@@ -21,7 +23,7 @@ class SessionTokenStorageTest extends \PHPUnit_Framework_TestCase
     const SESSION_NAMESPACE = 'foobar';
 
     /**
-     * @var \PHPUnit_Framework_MockObject_MockObject
+     * @var Session
      */
     private $session;
 
@@ -32,118 +34,53 @@ class SessionTokenStorageTest extends \PHPUnit_Framework_TestCase
 
     protected function setUp()
     {
-        $this->session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')
-            ->disableOriginalConstructor()
-            ->getMock();
+        $this->session = new Session(new MockArraySessionStorage());
         $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->assertTrue($this->session->isStarted());
     }
 
     public function testStoreTokenInActiveSession()
     {
-        $this->session->expects($this->any())
-            ->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->session->start();
         $this->storage->setToken('token_id', 'TOKEN');
+
+        $this->assertSame('TOKEN', $this->session->get(self::SESSION_NAMESPACE.'/token_id'));
     }
 
     public function testCheckTokenInClosedSession()
     {
-        $this->session->expects($this->any())
-            ->method('isStarted')
-            ->will($this->returnValue(false));
-
-        $this->session->expects($this->once())
-            ->method('start');
+        $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
 
-        $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'));
+        $this->assertTrue($this->storage->hasToken('token_id'));
+        $this->assertTrue($this->session->isStarted());
     }
 
     public function testCheckTokenInActiveSession()
     {
-        $this->session->expects($this->any())
-            ->method('isStarted')
-            ->will($this->returnValue(true));
-
-        $this->session->expects($this->never())
-            ->method('start');
+        $this->session->start();
+        $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
 
-        $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'));
+        $this->assertTrue($this->storage->hasToken('token_id'));
     }
 
     public function testGetExistingTokenFromClosedSession()
     {
-        $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(true));
-
-        $this->session->expects($this->once())
-            ->method('get')
-            ->with(self::SESSION_NAMESPACE.'/token_id')
-            ->will($this->returnValue('RESULT'));
+        $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
 
         $this->assertSame('RESULT', $this->storage->getToken('token_id'));
+        $this->assertTrue($this->session->isStarted());
     }
 
     public function testGetExistingTokenFromActiveSession()
     {
-        $this->session->expects($this->any())
-            ->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(true));
-
-        $this->session->expects($this->once())
-            ->method('get')
-            ->with(self::SESSION_NAMESPACE.'/token_id')
-            ->will($this->returnValue('RESULT'));
+        $this->session->start();
+        $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'RESULT');
 
         $this->assertSame('RESULT', $this->storage->getToken('token_id'));
     }
@@ -153,18 +90,6 @@ class SessionTokenStorageTest extends \PHPUnit_Framework_TestCase
      */
     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');
     }
 
@@ -173,85 +98,33 @@ class SessionTokenStorageTest extends \PHPUnit_Framework_TestCase
      */
     public function testGetNonExistingTokenFromActiveSession()
     {
-        $this->session->expects($this->any())
-            ->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->session->start();
         $this->storage->getToken('token_id');
     }
 
     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'));
     }
 
     public function testRemoveNonExistingTokenFromActiveSession()
     {
-        $this->session->expects($this->any())
-            ->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->session->start();
 
         $this->assertNull($this->storage->removeToken('token_id'));
     }
 
     public function testRemoveExistingTokenFromClosedSession()
     {
-        $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('TOKEN'));
+        $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'TOKEN');
 
         $this->assertSame('TOKEN', $this->storage->removeToken('token_id'));
     }
 
     public function testRemoveExistingTokenFromActiveSession()
     {
-        $this->session->expects($this->any())
-            ->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('TOKEN'));
+        $this->session->start();
+        $this->session->set(self::SESSION_NAMESPACE.'/token_id', 'TOKEN');
 
         $this->assertSame('TOKEN', $this->storage->removeToken('token_id'));
     }
