/
githubmirror
/
magento2
Обзор
Документация
Войти
/
githubmirror
/
magento2
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
2.4-develop
dev/tests/integration/testsuite/Magento/Framework/Session/SaveHandlerTest.php
131 строка
4 KB
Alexandra Zota
ACP2E-4019: CE copyright updates for 2.4.9-beta1
22 окт 2025, 10:44
22 окт 2025, 10:44
264bea2
Код
Авторство
О чём код?
<?php /** * Copyright 2016 Adobe * All Rights Reserved. */ namespace Magento\Framework\Session; use Magento\Framework\App\DeploymentConfig; use Magento\Framework\Exception\SessionException; use Magento\Framework\Phrase; use Magento\Framework\Session\Config\ConfigInterface; use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\ObjectManager; /** * Tests \Magento\Framework\Session\SaveHandler functionality. * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class SaveHandlerTest extends \PHPUnit\Framework\TestCase { /** * @var ObjectManager */ private $objectManager; /** * @var DeploymentConfig|\PHPUnit\Framework\MockObject\MockObject */ private $deploymentConfigMock; /** * @var SaveHandlerFactory|\PHPUnit\Framework\MockObject\MockObject */ private $saveHandlerFactoryMock; /** * @inheritdoc */ protected function setUp(): void { $this->objectManager = Bootstrap::getObjectManager(); $this->deploymentConfigMock = $this->createMock(DeploymentConfig::class); $this->objectManager->addSharedInstance($this->deploymentConfigMock, DeploymentConfig::class); $this->saveHandlerFactoryMock = $this->createMock(SaveHandlerFactory::class); $this->objectManager->addSharedInstance($this->saveHandlerFactoryMock, SaveHandlerFactory::class); } /** * @inheritdoc */ protected function tearDown(): void { $this->objectManager->removeSharedInstance(DeploymentConfig::class); $this->objectManager->removeSharedInstance(SaveHandlerFactory::class); } /** * @return void */ public function testRedisSaveHandler(): void { $this->deploymentConfigMock->method('get') ->willReturnMap( [ [Config::PARAM_SESSION_SAVE_METHOD, null, 'redis'], [Config::PARAM_SESSION_SAVE_PATH, null, 'explicit_save_path'], ] ); $redisHandlerMock = $this->getMockBuilder(SaveHandler\Redis::class) ->disableOriginalConstructor() ->getMock(); $redisHandlerMock->method('open') ->with('explicit_save_path', 'test_session_id') ->willReturn(true); $this->saveHandlerFactoryMock->expects($this->exactly(1)) ->method('create') ->with('redis') ->willReturn($redisHandlerMock); $sessionConfig = $this->objectManager->create(ConfigInterface::class); /** @var SaveHandler $saveHandler */ $saveHandler = $this->objectManager->create(SaveHandler::class, ['sessionConfig' => $sessionConfig]); $result = $saveHandler->open('explicit_save_path', 'test_session_id'); $this->assertTrue($result); } /** * @return void */ public function testRedisSaveHandlerFallbackToDefaultOnSessionException(): void { $this->deploymentConfigMock->method('get') ->willReturnMap( [ [Config::PARAM_SESSION_SAVE_METHOD, null, 'redis'], [Config::PARAM_SESSION_SAVE_PATH, null, 'explicit_save_path'], ] ); $redisHandlerMock = $this->getMockBuilder(SaveHandler\Redis::class) ->disableOriginalConstructor() ->getMock(); $redisHandlerMock->method('open') ->with('explicit_save_path', 'test_session_id') ->willThrowException(new SessionException(new Phrase('Session Exception'))); $defaultHandlerMock = $this->getMockBuilder(SaveHandler\Native::class) ->disableOriginalConstructor() ->getMock(); $defaultHandlerMock->expects($this->once())->method('open')->with('explicit_save_path', 'test_session_id'); $this->saveHandlerFactoryMock ->method('create') ->willReturnCallback(function ($arg) use ($redisHandlerMock, $defaultHandlerMock) { if ($arg == 'redis') { return $redisHandlerMock; } elseif($arg == SaveHandlerInterface::DEFAULT_HANDLER) { return $defaultHandlerMock; } }); $sessionConfig = $this->objectManager->create(ConfigInterface::class); /** @var SaveHandler $saveHandler */ $saveHandler = $this->objectManager->create(SaveHandler::class, ['sessionConfig' => $sessionConfig]); $saveHandler->open('explicit_save_path', 'test_session_id'); } }