Skip to content

Commit 035bfaf

Browse files
committed
[Persistent] Cover Session Helper by Unit Test
1 parent 65763df commit 035bfaf

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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() when the session has id and enable persistent
95+
*/
96+
public function testIsPersistentWhenSessionId()
97+
{
98+
$this->session->expects($this->any())->method('getId')
99+
->willReturn(1);
100+
$this->helper->expects($this->any())->method('getSession')
101+
->willReturn($this->session);
102+
$this->dataHelper->expects($this->any())->method('isEnabled')
103+
->willReturn(true);
104+
105+
$this->assertEquals(true, $this->helper->isPersistent());
106+
}
107+
108+
/***
109+
* Test isPersistent() when the no session id and enable persistent
110+
*/
111+
public function testIsPersistentWhenNoSessionId()
112+
{
113+
$this->session->expects($this->any())->method('getId')
114+
->willReturn(null);
115+
$this->helper->expects($this->any())->method('getSession')
116+
->willReturn($this->session);
117+
$this->dataHelper->expects($this->any())->method('isEnabled')
118+
->willReturn(true);
119+
120+
$this->assertEquals(false, $this->helper->isPersistent());
121+
}
122+
123+
/**
124+
* Test isRememberMeChecked() when enable all config
125+
*/
126+
public function testIsRememberMeCheckedWhenEnabledAll()
127+
{
128+
$testCase = [
129+
'dataset' => [
130+
'enabled' => true,
131+
'remember_me_enabled' => true,
132+
'remember_me_checked_default' => true
133+
],
134+
'expected' => true
135+
];
136+
$this->executeTestIsRememberMeChecked($testCase);
137+
}
138+
139+
/**
140+
* Test isRememberMeChecked() when config persistent is disabled
141+
*/
142+
public function testIsRememberMeCheckedWhenAtLeastOnceDisabled()
143+
{
144+
$testCase = [
145+
'dataset' => [
146+
'enabled' => false,
147+
'remember_me_enabled' => true,
148+
'remember_me_checked_default' => true
149+
],
150+
'expected' => false
151+
];
152+
$this->executeTestIsRememberMeChecked($testCase);
153+
}
154+
155+
/**
156+
* Test isRememberMeChecked() when setRememberMeChecked(false)
157+
*/
158+
public function testIsRememberMeCheckedWhenSetValue()
159+
{
160+
$testCase = [
161+
'dataset' => [
162+
'enabled' => true,
163+
'remember_me_enabled' => true,
164+
'remember_me_checked_default' => true
165+
],
166+
'expected' => false
167+
];
168+
$this->helper->setRememberMeChecked(false);
169+
$this->executeTestIsRememberMeChecked($testCase);
170+
}
171+
172+
/**
173+
* Execute test isRememberMeChecked() function
174+
*
175+
* @param array $testCase
176+
*/
177+
public function executeTestIsRememberMeChecked($testCase)
178+
{
179+
$this->dataHelper->expects($this->any())->method('isEnabled')
180+
->willReturn($testCase['dataset']['enabled']);
181+
$this->dataHelper->expects($this->any())->method('isRememberMeEnabled')
182+
->willReturn($testCase['dataset']['remember_me_enabled']);
183+
$this->dataHelper->expects($this->any())->method('isRememberMeCheckedDefault')
184+
->willReturn($testCase['dataset']['remember_me_checked_default']);
185+
$this->assertEquals($testCase['expected'], $this->helper->isRememberMeChecked());
186+
}
187+
}

0 commit comments

Comments
 (0)