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