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,96 @@ 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 testRedisSaveHandler (): 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 ]);
71
+ $ redisHandlerMock = $ this ->getMockBuilder (SaveHandler \Redis::class)
72
+ ->disableOriginalConstructor ()
73
+ ->getMock ();
74
+ $ redisHandlerMock ->method ('open ' )
75
+ ->with ('explicit_save_path ' , 'test_session_id ' )
76
+ ->willReturn (true );
75
77
76
- // Test expectation
77
- $ this ->assertEquals (
78
- $ expected ,
79
- $ sessionConfig ->getOption ('session.save_handler ' )
80
- );
81
- }
78
+ $ this ->saveHandlerFactoryMock ->expects ($ this ->exactly (1 ))
79
+ ->method ('create ' )
80
+ ->with ('redis ' )
81
+ ->willReturn ($ redisHandlerMock );
82
82
83
- public function saveHandlerProvider ()
84
- {
85
- return [
86
- ['db ' ],
87
- ['redis ' ],
88
- ['files ' ],
89
- [false ],
90
- ];
83
+ $ sessionConfig = $ this ->objectManager ->create (ConfigInterface::class);
84
+ /** @var SaveHandler $saveHandler */
85
+ $ saveHandler = $ this ->objectManager ->create (SaveHandler::class, ['sessionConfig ' => $ sessionConfig ]);
86
+ $ result = $ saveHandler ->open ('explicit_save_path ' , 'test_session_id ' );
87
+ $ this ->assertTrue ($ result );
91
88
}
92
89
93
90
/**
94
- * Retrieve expected session.save_handler
95
- *
96
- * @param string $deploymentConfigHandler
97
- * @param string $iniHandler
98
- * @return string
91
+ * @return void
99
92
*/
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 ()
93
+ public function testRedisSaveHandlerFallbackToDefaultOnSessionException (): void
112
94
{
113
95
$ 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
- });
96
+ ->willReturnMap (
97
+ [
98
+ [Config::PARAM_SESSION_SAVE_METHOD , null , 'redis ' ],
99
+ [Config::PARAM_SESSION_SAVE_PATH , null , 'explicit_save_path ' ],
100
+ ]
101
+ );
102
+
103
+ $ redisHandlerMock = $ this ->getMockBuilder (SaveHandler \Redis::class)
104
+ ->disableOriginalConstructor ()
105
+ ->getMock ();
106
+ $ redisHandlerMock ->method ('open ' )
107
+ ->with ('explicit_save_path ' , 'test_session_id ' )
108
+ ->willThrowException (new SessionException (new Phrase ('Session Exception ' )));
109
+
110
+ $ defaultHandlerMock = $ this ->getMockBuilder (SaveHandler \Native::class)
111
+ ->disableOriginalConstructor ()
112
+ ->getMock ();
113
+ $ defaultHandlerMock ->expects ($ this ->once ())->method ('open ' )->with ('explicit_save_path ' , 'test_session_id ' );
126
114
127
115
$ this ->saveHandlerFactoryMock ->expects ($ this ->at (0 ))
128
116
->method ('create ' )
129
- ->willThrowException (new SessionException (new Phrase ('Session Exception ' )));
117
+ ->with ('redis ' )
118
+ ->willReturn ($ redisHandlerMock );
130
119
$ this ->saveHandlerFactoryMock ->expects ($ this ->at (1 ))
131
120
->method ('create ' )
132
- ->with (SaveHandlerInterface::DEFAULT_HANDLER );
133
- $ sessionConfig = $ this ->objectManager ->create (ConfigInterface::class);
134
- $ this ->objectManager ->create (SaveHandler::class, ['sessionConfig ' => $ sessionConfig ]);
121
+ ->with (SaveHandlerInterface::DEFAULT_HANDLER )
122
+ ->willReturn ($ defaultHandlerMock );
135
123
136
- // Test expectation
137
- $ this ->assertEquals (
138
- 'db ' ,
139
- $ sessionConfig ->getOption ('session.save_handler ' )
140
- );
124
+ $ sessionConfig = $ this ->objectManager ->create (ConfigInterface::class);
125
+ /** @var SaveHandler $saveHandler */
126
+ $ saveHandler = $ this ->objectManager ->create (SaveHandler::class, ['sessionConfig ' => $ sessionConfig ]);
127
+ $ saveHandler ->open ('explicit_save_path ' , 'test_session_id ' );
141
128
}
142
129
}
0 commit comments