Skip to content

Commit ad48079

Browse files
committed
[Persistent] Cover CustomerData by Unit Test
1 parent 1ad65a3 commit ad48079

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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\CustomerData;
10+
11+
use PHPUnit\Framework\TestCase;
12+
use Magento\Persistent\CustomerData\Persistent;
13+
use Magento\Customer\Api\CustomerRepositoryInterface;
14+
use Magento\Customer\Helper\View;
15+
use Magento\Persistent\Helper\Session;
16+
use Magento\Persistent\Model\Session as PersistentSession;
17+
use Magento\Customer\Api\Data\CustomerInterface;
18+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
19+
20+
class PersistentTest extends TestCase
21+
{
22+
/**
23+
* @var Persistent
24+
*/
25+
private $customerData;
26+
27+
/**
28+
* @var Session
29+
*/
30+
private $persistentSessionHelperMock;
31+
32+
/**
33+
* @var View
34+
*/
35+
private $customerViewHelperMock;
36+
37+
/**
38+
* @var CustomerRepositoryInterface
39+
*/
40+
private $customerRepositoryMock;
41+
42+
/**
43+
* Setup environment for test
44+
*/
45+
protected function setUp()
46+
{
47+
$this->persistentSessionHelperMock = $this->createMock(Session::class);
48+
$this->customerViewHelperMock = $this->createMock(View::class);
49+
$this->customerRepositoryMock = $this->createMock(CustomerRepositoryInterface::class);
50+
51+
$objectManager = new ObjectManagerHelper($this);
52+
53+
$this->customerData = $objectManager->getObject(
54+
Persistent::class,
55+
[
56+
'persistentSession' => $this->persistentSessionHelperMock,
57+
'customerViewHelper' => $this->customerViewHelperMock,
58+
'customerRepository' => $this->customerRepositoryMock
59+
]
60+
);
61+
}
62+
63+
/**
64+
* Test getSectionData() when disable persistent
65+
*/
66+
public function testGetSectionDataWhenDisablePersistent()
67+
{
68+
$this->persistentSessionHelperMock->method('isPersistent')->willReturn(false);
69+
70+
$this->assertEquals([], $this->customerData->getSectionData());
71+
}
72+
73+
/**
74+
* Test getSectionData() when customer doesn't login
75+
*/
76+
public function testGetSectionDataWithNotLogin()
77+
{
78+
$this->persistentSessionHelperMock->method('isPersistent')->willReturn(true);
79+
80+
$persistentSessionMock = $this->createPartialMock(PersistentSession::class, ['getCustomerId']);
81+
$persistentSessionMock->method('getCustomerId')->willReturn(null);
82+
$this->persistentSessionHelperMock->method('getSession')->willReturn($persistentSessionMock);
83+
84+
$this->assertEquals([], $this->customerData->getSectionData());
85+
}
86+
87+
/**
88+
* Test getSectionData() when customer login and enable persistent
89+
*/
90+
public function testGetSectionDataCustomerLoginAndEnablePersistent()
91+
{
92+
$this->persistentSessionHelperMock->method('isPersistent')->willReturn(true);
93+
94+
$persistentSessionMock = $this->createPartialMock(PersistentSession::class, ['getCustomerId']);
95+
$persistentSessionMock->method('getCustomerId')->willReturn(1);
96+
$this->persistentSessionHelperMock->method('getSession')->willReturn($persistentSessionMock);
97+
98+
$customerMock = $this->createMock(CustomerInterface::class);
99+
$this->customerRepositoryMock->method('getById')->with(1)->willReturn($customerMock);
100+
$this->customerViewHelperMock->method('getCustomerName')->with($customerMock)->willReturn('Adam John');
101+
102+
$this->assertEquals(
103+
[
104+
'fullname' => 'Adam John'
105+
],
106+
$this->customerData->getSectionData()
107+
);
108+
}
109+
}

0 commit comments

Comments
 (0)