9
9
use Magento \Framework \Exception \SessionException ;
10
10
use Magento \Framework \Phrase ;
11
11
use Magento \Framework \Session \Config \ConfigInterface ;
12
- use Magento \Framework \App \ObjectManager ;
12
+ use Magento \TestFramework \Helper \Bootstrap ;
13
+ use Magento \TestFramework \ObjectManager ;
13
14
15
+ /**
16
+ * Tests \Magento\Framework\Session\SaveHandler functionality.
17
+ *
18
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19
+ */
14
20
class SaveHandlerTest extends \PHPUnit \Framework \TestCase
15
21
{
16
22
/**
17
- * @var \Magento\TestFramework\ ObjectManager
23
+ * @var ObjectManager
18
24
*/
19
25
private $ objectManager ;
20
26
@@ -28,115 +34,64 @@ class SaveHandlerTest extends \PHPUnit\Framework\TestCase
28
34
*/
29
35
private $ saveHandlerFactoryMock ;
30
36
37
+ /**
38
+ * @inheritdoc
39
+ */
31
40
protected function setUp ()
32
41
{
33
- $ this ->objectManager = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ();
42
+ $ this ->objectManager = Bootstrap::getObjectManager ();
34
43
$ this ->deploymentConfigMock = $ this ->createMock (DeploymentConfig::class);
35
44
$ this ->objectManager ->addSharedInstance ($ this ->deploymentConfigMock , DeploymentConfig::class);
36
45
$ this ->saveHandlerFactoryMock = $ this ->createMock (SaveHandlerFactory::class);
37
46
$ this ->objectManager ->addSharedInstance ($ this ->saveHandlerFactoryMock , SaveHandlerFactory::class);
38
47
}
39
48
49
+ /**
50
+ * @inheritdoc
51
+ */
40
52
protected function tearDown ()
41
53
{
42
54
$ this ->objectManager ->removeSharedInstance (DeploymentConfig::class);
43
55
$ this ->objectManager ->removeSharedInstance (SaveHandlerFactory::class);
44
56
}
45
57
46
58
/**
47
- * Tests that the session handler is correctly set when object is created.
48
- *
49
- * @dataProvider saveHandlerProvider
50
- * @param string $deploymentConfigHandler
59
+ * @return void
51
60
*/
52
- public function testConstructor ( $ deploymentConfigHandler )
61
+ public function testRedisSaveHandlerFallbackToDefaultOnSessionException (): void
53
62
{
54
- $ expected = $ this ->getExpectedSaveHandler ($ deploymentConfigHandler , ini_get ('session.save_handler ' ));
55
-
56
63
$ this ->deploymentConfigMock ->method ('get ' )
57
- ->willReturnCallback (function ($ configPath ) use ($ deploymentConfigHandler ) {
58
- switch ($ configPath ) {
59
- case Config::PARAM_SESSION_SAVE_METHOD :
60
- return $ deploymentConfigHandler ;
61
- case Config::PARAM_SESSION_CACHE_LIMITER :
62
- return 'private_no_expire ' ;
63
- case Config::PARAM_SESSION_SAVE_PATH :
64
- return 'explicit_save_path ' ;
65
- default :
66
- return null ;
67
- }
68
- });
64
+ ->willReturnMap (
65
+ [
66
+ [Config::PARAM_SESSION_SAVE_METHOD , null , 'redis ' ],
67
+ [Config::PARAM_SESSION_SAVE_PATH , null , 'explicit_save_path ' ],
68
+ ]
69
+ );
69
70
70
- $ this ->saveHandlerFactoryMock ->expects ($ this ->once ())
71
- ->method ('create ' )
72
- ->with ($ expected );
73
- $ sessionConfig = $ this ->objectManager ->create (ConfigInterface::class);
74
- $ this ->objectManager ->create (SaveHandler::class, ['sessionConfig ' => $ sessionConfig ]);
75
-
76
- // Test expectation
77
- $ this ->assertEquals (
78
- $ expected ,
79
- $ sessionConfig ->getOption ('session.save_handler ' )
80
- );
81
- }
82
-
83
- public function saveHandlerProvider ()
84
- {
85
- return [
86
- ['db ' ],
87
- ['redis ' ],
88
- ['files ' ],
89
- [false ],
90
- ];
91
- }
92
-
93
- /**
94
- * Retrieve expected session.save_handler
95
- *
96
- * @param string $deploymentConfigHandler
97
- * @param string $iniHandler
98
- * @return string
99
- */
100
- private function getExpectedSaveHandler ($ deploymentConfigHandler , $ iniHandler )
101
- {
102
- if ($ deploymentConfigHandler ) {
103
- return $ deploymentConfigHandler ;
104
- } elseif ($ iniHandler ) {
105
- return $ iniHandler ;
106
- } else {
107
- return SaveHandlerInterface::DEFAULT_HANDLER ;
108
- }
109
- }
71
+ $ redisHandlerMock = $ this ->getMockBuilder (SaveHandler \Redis::class)
72
+ ->disableOriginalConstructor ()
73
+ ->getMock ();
74
+ $ redisHandlerMock ->method ('open ' )
75
+ ->with ('explicit_save_path ' , 'test_session_id ' )
76
+ ->willThrowException (new SessionException (new Phrase ('Session Exception ' )));
110
77
111
- public function testConstructorWithException ()
112
- {
113
- $ this ->deploymentConfigMock ->method ('get ' )
114
- ->willReturnCallback (function ($ configPath ) {
115
- switch ($ configPath ) {
116
- case Config::PARAM_SESSION_SAVE_METHOD :
117
- return 'db ' ;
118
- case Config::PARAM_SESSION_CACHE_LIMITER :
119
- return 'private_no_expire ' ;
120
- case Config::PARAM_SESSION_SAVE_PATH :
121
- return 'explicit_save_path ' ;
122
- default :
123
- return null ;
124
- }
125
- });
78
+ $ defaultHandlerMock = $ this ->getMockBuilder (SaveHandler \Native::class)
79
+ ->disableOriginalConstructor ()
80
+ ->getMock ();
81
+ $ defaultHandlerMock ->expects ($ this ->once ())->method ('open ' )->with ('explicit_save_path ' , 'test_session_id ' );
126
82
127
83
$ this ->saveHandlerFactoryMock ->expects ($ this ->at (0 ))
128
84
->method ('create ' )
129
- ->willThrowException (new SessionException (new Phrase ('Session Exception ' )));
85
+ ->with ('redis ' )
86
+ ->willReturn ($ redisHandlerMock );
130
87
$ this ->saveHandlerFactoryMock ->expects ($ this ->at (1 ))
131
88
->method ('create ' )
132
- ->with (SaveHandlerInterface::DEFAULT_HANDLER );
133
- $ sessionConfig = $ this ->objectManager ->create (ConfigInterface::class);
134
- $ this ->objectManager ->create (SaveHandler::class, ['sessionConfig ' => $ sessionConfig ]);
89
+ ->with (SaveHandlerInterface::DEFAULT_HANDLER )
90
+ ->willReturn ($ defaultHandlerMock );
135
91
136
- // Test expectation
137
- $ this ->assertEquals (
138
- 'db ' ,
139
- $ sessionConfig ->getOption ('session.save_handler ' )
140
- );
92
+ $ sessionConfig = $ this ->objectManager ->create (ConfigInterface::class);
93
+ /** @var SaveHandler $saveHandler */
94
+ $ saveHandler = $ this ->objectManager ->create (SaveHandler::class, ['sessionConfig ' => $ sessionConfig ]);
95
+ $ saveHandler ->open ('explicit_save_path ' , 'test_session_id ' );
141
96
}
142
97
}
0 commit comments