SaveHandlerTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Session;
  7. use Magento\Framework\App\DeploymentConfig;
  8. use Magento\Framework\Exception\SessionException;
  9. use Magento\Framework\Phrase;
  10. use Magento\Framework\Session\Config\ConfigInterface;
  11. use Magento\Framework\App\ObjectManager;
  12. class SaveHandlerTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\TestFramework\ObjectManager
  16. */
  17. private $objectManager;
  18. /**
  19. * @var DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $deploymentConfigMock;
  22. /**
  23. * @var SaveHandlerFactory|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $saveHandlerFactoryMock;
  26. protected function setUp()
  27. {
  28. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  29. $this->deploymentConfigMock = $this->createMock(DeploymentConfig::class);
  30. $this->objectManager->addSharedInstance($this->deploymentConfigMock, DeploymentConfig::class);
  31. $this->saveHandlerFactoryMock = $this->createMock(SaveHandlerFactory::class);
  32. $this->objectManager->addSharedInstance($this->saveHandlerFactoryMock, SaveHandlerFactory::class);
  33. }
  34. protected function tearDown()
  35. {
  36. $this->objectManager->removeSharedInstance(DeploymentConfig::class);
  37. $this->objectManager->removeSharedInstance(SaveHandlerFactory::class);
  38. }
  39. /**
  40. * Tests that the session handler is correctly set when object is created.
  41. *
  42. * @dataProvider saveHandlerProvider
  43. * @param string $deploymentConfigHandler
  44. */
  45. public function testConstructor($deploymentConfigHandler)
  46. {
  47. $expected = $this->getExpectedSaveHandler($deploymentConfigHandler, ini_get('session.save_handler'));
  48. $this->deploymentConfigMock->method('get')
  49. ->willReturnCallback(function ($configPath) use ($deploymentConfigHandler) {
  50. switch ($configPath) {
  51. case Config::PARAM_SESSION_SAVE_METHOD:
  52. return $deploymentConfigHandler;
  53. case Config::PARAM_SESSION_CACHE_LIMITER:
  54. return 'private_no_expire';
  55. case Config::PARAM_SESSION_SAVE_PATH:
  56. return 'explicit_save_path';
  57. default:
  58. return null;
  59. }
  60. });
  61. $this->saveHandlerFactoryMock->expects($this->once())
  62. ->method('create')
  63. ->with($expected);
  64. $sessionConfig = $this->objectManager->create(ConfigInterface::class);
  65. $this->objectManager->create(SaveHandler::class, ['sessionConfig' => $sessionConfig]);
  66. // Test expectation
  67. $this->assertEquals(
  68. $expected,
  69. $sessionConfig->getOption('session.save_handler')
  70. );
  71. }
  72. public function saveHandlerProvider()
  73. {
  74. return [
  75. ['db'],
  76. ['redis'],
  77. ['files'],
  78. [false],
  79. ];
  80. }
  81. /**
  82. * Retrieve expected session.save_handler
  83. *
  84. * @param string $deploymentConfigHandler
  85. * @param string $iniHandler
  86. * @return string
  87. */
  88. private function getExpectedSaveHandler($deploymentConfigHandler, $iniHandler)
  89. {
  90. if ($deploymentConfigHandler) {
  91. return $deploymentConfigHandler;
  92. } elseif ($iniHandler) {
  93. return $iniHandler;
  94. } else {
  95. return SaveHandlerInterface::DEFAULT_HANDLER;
  96. }
  97. }
  98. public function testConstructorWithException()
  99. {
  100. $this->deploymentConfigMock->method('get')
  101. ->willReturnCallback(function ($configPath) {
  102. switch ($configPath) {
  103. case Config::PARAM_SESSION_SAVE_METHOD:
  104. return 'db';
  105. case Config::PARAM_SESSION_CACHE_LIMITER:
  106. return 'private_no_expire';
  107. case Config::PARAM_SESSION_SAVE_PATH:
  108. return 'explicit_save_path';
  109. default:
  110. return null;
  111. }
  112. });
  113. $this->saveHandlerFactoryMock->expects($this->at(0))
  114. ->method('create')
  115. ->willThrowException(new SessionException(new Phrase('Session Exception')));
  116. $this->saveHandlerFactoryMock->expects($this->at(1))
  117. ->method('create')
  118. ->with(SaveHandlerInterface::DEFAULT_HANDLER);
  119. $sessionConfig = $this->objectManager->create(ConfigInterface::class);
  120. $this->objectManager->create(SaveHandler::class, ['sessionConfig' => $sessionConfig]);
  121. // Test expectation
  122. $this->assertEquals(
  123. 'db',
  124. $sessionConfig->getOption('session.save_handler')
  125. );
  126. }
  127. }