SessionAuthenticationStrategyTest.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Http\Tests\Session;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy;
  13. class SessionAuthenticationStrategyTest extends TestCase
  14. {
  15. public function testSessionIsNotChanged()
  16. {
  17. $request = $this->getRequest();
  18. $request->expects($this->never())->method('getSession');
  19. $strategy = new SessionAuthenticationStrategy(SessionAuthenticationStrategy::NONE);
  20. $strategy->onAuthentication($request, $this->getToken());
  21. }
  22. /**
  23. * @expectedException \RuntimeException
  24. * @expectedExceptionMessage Invalid session authentication strategy "foo"
  25. */
  26. public function testUnsupportedStrategy()
  27. {
  28. $request = $this->getRequest();
  29. $request->expects($this->never())->method('getSession');
  30. $strategy = new SessionAuthenticationStrategy('foo');
  31. $strategy->onAuthentication($request, $this->getToken());
  32. }
  33. public function testSessionIsMigrated()
  34. {
  35. if (\PHP_VERSION_ID >= 50400 && \PHP_VERSION_ID < 50411) {
  36. $this->markTestSkipped('We cannot destroy the old session on PHP 5.4.0 - 5.4.10.');
  37. }
  38. $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock();
  39. $session->expects($this->once())->method('migrate')->with($this->equalTo(true));
  40. $strategy = new SessionAuthenticationStrategy(SessionAuthenticationStrategy::MIGRATE);
  41. $strategy->onAuthentication($this->getRequest($session), $this->getToken());
  42. }
  43. public function testSessionIsMigratedWithPhp54Workaround()
  44. {
  45. if (\PHP_VERSION_ID < 50400 || \PHP_VERSION_ID >= 50411) {
  46. $this->markTestSkipped('This PHP version is not affected.');
  47. }
  48. $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock();
  49. $session->expects($this->once())->method('migrate')->with($this->equalTo(false));
  50. $strategy = new SessionAuthenticationStrategy(SessionAuthenticationStrategy::MIGRATE);
  51. $strategy->onAuthentication($this->getRequest($session), $this->getToken());
  52. }
  53. public function testSessionIsInvalidated()
  54. {
  55. $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock();
  56. $session->expects($this->once())->method('invalidate');
  57. $strategy = new SessionAuthenticationStrategy(SessionAuthenticationStrategy::INVALIDATE);
  58. $strategy->onAuthentication($this->getRequest($session), $this->getToken());
  59. }
  60. private function getRequest($session = null)
  61. {
  62. $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
  63. if (null !== $session) {
  64. $request->expects($this->any())->method('getSession')->will($this->returnValue($session));
  65. }
  66. return $request;
  67. }
  68. private function getToken()
  69. {
  70. return $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  71. }
  72. }