Skip to content

Commit 86619b5

Browse files
ENGCOM-6225: [Persistent] Cover Session Helper by Unit Test #25422
- Merge Pull Request #25422 from edenduong/magento2:2.3-testcover/test_persistent_session_helper - Merged commits: 1. 035bfaf 2. 0afddc2
2 parents 8dfe126 + 0afddc2 commit 86619b5

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Persistent\Test\Unit\Helper;
10+
11+
use Magento\Persistent\Helper\Session as SessionHelper;
12+
use Magento\Framework\App\Helper\Context;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Checkout\Model\Session as CheckoutSession;
15+
use Magento\Persistent\Helper\Data as DataHelper;
16+
use Magento\Persistent\Model\SessionFactory;
17+
use Magento\Persistent\Model\Session;
18+
19+
/**
20+
* Class \Magento\Persistent\Test\Unit\Helper\SessionTest
21+
*/
22+
class SessionTest extends \PHPUnit\Framework\TestCase
23+
{
24+
/**
25+
* @var \PHPUnit_Framework_MockObject_MockObject|Context
26+
*/
27+
private $context;
28+
29+
/**
30+
* @var \PHPUnit_Framework_MockObject_MockObject|SessionHelper
31+
*/
32+
private $helper;
33+
34+
/**
35+
* @var \PHPUnit_Framework_MockObject_MockObject|DataHelper
36+
*/
37+
private $dataHelper;
38+
39+
/**
40+
* @var \PHPUnit_Framework_MockObject_MockObject|CheckoutSession
41+
*/
42+
private $checkoutSession;
43+
44+
/**
45+
* @var \PHPUnit_Framework_MockObject_MockObject|SessionFactory
46+
*/
47+
private $sessionFactory;
48+
49+
/**
50+
* @var \PHPUnit_Framework_MockObject_MockObject|Session
51+
*/
52+
private $session;
53+
54+
/**
55+
* Setup environment
56+
*/
57+
protected function setUp()
58+
{
59+
$this->context = $this->getMockBuilder(Context::class)
60+
->disableOriginalConstructor()
61+
->getMock();
62+
$this->dataHelper = $this->getMockBuilder(DataHelper::class)
63+
->disableOriginalConstructor()
64+
->getMock();
65+
$this->checkoutSession = $this->getMockBuilder(CheckoutSession::class)
66+
->disableOriginalConstructor()
67+
->getMock();
68+
$this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
69+
->disableOriginalConstructor()
70+
->getMock();
71+
$this->sessionFactory = $this->getMockBuilder(SessionFactory::class)
72+
->disableOriginalConstructor()
73+
->setMethods(['create'])
74+
->getMock();
75+
$this->session = $this->getMockBuilder(Session::class)
76+
->disableOriginalConstructor()
77+
->getMock();
78+
$this->sessionFactory->expects($this->any())->method('create')->willReturn($this->session);
79+
80+
$this->helper = $this->getMockBuilder(SessionHelper::class)
81+
->setMethods(['getSession'])
82+
->setConstructorArgs(
83+
[
84+
'context' => $this->context,
85+
'persistentData' => $this->dataHelper,
86+
'checkoutSession' => $this->checkoutSession,
87+
'sessionFactory' => $this->sessionFactory
88+
]
89+
)
90+
->getMock();
91+
}
92+
93+
/**
94+
* Test isPersistent() function
95+
*
96+
* @param int|null $id
97+
* @param boolean $isEnabled
98+
* @param boolean $expected
99+
* @dataProvider isPersistentDataProvider
100+
*/
101+
public function testIsPersistent($id, $isEnabled, $expected)
102+
{
103+
$this->session->expects($this->any())->method('getId')
104+
->willReturn($id);
105+
$this->helper->expects($this->any())->method('getSession')
106+
->willReturn($this->session);
107+
$this->dataHelper->expects($this->any())->method('isEnabled')
108+
->willReturn($isEnabled);
109+
110+
$this->assertEquals($expected, $this->helper->isPersistent());
111+
}
112+
113+
/**
114+
* Data Provider for test isPersistent()
115+
*
116+
* @return array
117+
*/
118+
public function isPersistentDataProvider()
119+
{
120+
return [
121+
'session_id_and_enable_persistent' => [
122+
1,
123+
true,
124+
true
125+
],
126+
'no_session_id_and_enable_persistent' => [
127+
null,
128+
true,
129+
false
130+
]
131+
];
132+
}
133+
134+
/**
135+
* Test isRememberMeChecked() function
136+
*
137+
* @param boolean|null $checked
138+
* @param boolean $isEnabled
139+
* @param boolean $isRememberMeEnabled
140+
* @param boolean $isRememberMeCheckedDefault
141+
* @param boolean $expected
142+
* @dataProvider isRememberMeCheckedProvider
143+
*/
144+
public function testIsRememberMeChecked(
145+
$checked,
146+
$isEnabled,
147+
$isRememberMeEnabled,
148+
$isRememberMeCheckedDefault,
149+
$expected
150+
) {
151+
$this->helper->setRememberMeChecked($checked);
152+
$this->dataHelper->expects($this->any())->method('isEnabled')
153+
->willReturn($isEnabled);
154+
$this->dataHelper->expects($this->any())->method('isRememberMeEnabled')
155+
->willReturn($isRememberMeEnabled);
156+
$this->dataHelper->expects($this->any())->method('isRememberMeCheckedDefault')
157+
->willReturn($isRememberMeCheckedDefault);
158+
159+
$this->assertEquals($expected, $this->helper->isRememberMeChecked());
160+
}
161+
162+
/**
163+
* Data Provider for test isRememberMeChecked()
164+
*
165+
* @return array
166+
*/
167+
public function isRememberMeCheckedProvider()
168+
{
169+
return [
170+
'enable_all_config' => [
171+
null,
172+
true,
173+
true,
174+
true,
175+
true
176+
],
177+
'at_least_once_disabled' => [
178+
null,
179+
false,
180+
true,
181+
true,
182+
false
183+
],
184+
'set_remember_me_checked_false' => [
185+
false,
186+
true,
187+
true,
188+
true,
189+
false
190+
],
191+
'set_remember_me_checked_true' => [
192+
true,
193+
false,
194+
true,
195+
true,
196+
true
197+
]
198+
];
199+
}
200+
}

0 commit comments

Comments
 (0)