Skip to content

Commit a434f2e

Browse files
ENGCOM-6248: [Instant Purchase] Unit test for CustomerData/InstantPurchase #25519
2 parents 51b3d5f + 9ac11a3 commit a434f2e

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\InstantPurchase\Test\Unit\CustomerData;
10+
11+
use Magento\InstantPurchase\CustomerData\InstantPurchase as CustomerData;
12+
use Magento\Customer\Model\Session;
13+
use Magento\InstantPurchase\Model\InstantPurchaseInterface as InstantPurchaseModel;
14+
use Magento\InstantPurchase\Model\Ui\CustomerAddressesFormatter;
15+
use Magento\InstantPurchase\Model\Ui\PaymentTokenFormatter;
16+
use Magento\InstantPurchase\Model\Ui\ShippingMethodFormatter;
17+
use Magento\Store\Model\StoreManagerInterface;
18+
use Magento\Store\Model\Store;
19+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
20+
use Magento\InstantPurchase\Model\InstantPurchaseOption;
21+
use Magento\Customer\Model\Customer;
22+
23+
/**
24+
* Test class for InstantPurchase Customer Data
25+
*
26+
* Class \Magento\InstantPurchase\Test\Unit\CustomerData\InstantPurchaseTest
27+
*/
28+
class InstantPurchaseTest extends \PHPUnit\Framework\TestCase
29+
{
30+
/**
31+
* @var objectManagerHelper
32+
*/
33+
private $objectManager;
34+
35+
/**
36+
* @var CustomerData | \PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $customerData;
39+
40+
/**
41+
* @var Session | \PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $customerSession;
44+
45+
/**
46+
* @var StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
private $storeManager;
49+
50+
/**
51+
* @var InstantPurchaseModel | \PHPUnit_Framework_MockObject_MockObject
52+
*/
53+
private $instantPurchase;
54+
55+
/**
56+
* @var PaymentTokenFormatter | \PHPUnit_Framework_MockObject_MockObject
57+
*/
58+
private $paymentTokenFormatter;
59+
60+
/**
61+
* @var CustomerAddressesFormatter | \PHPUnit_Framework_MockObject_MockObject
62+
*/
63+
private $customerAddressesFormatter;
64+
65+
/**
66+
* @var ShippingMethodFormatter | \PHPUnit_Framework_MockObject_MockObject
67+
*/
68+
private $shippingMethodFormatter;
69+
70+
/**
71+
* @var Store | \PHPUnit_Framework_MockObject_MockObject
72+
*/
73+
private $store;
74+
75+
/**
76+
* @var Customer | \PHPUnit_Framework_MockObject_MockObject
77+
*/
78+
private $customer;
79+
80+
/**
81+
* @var InstantPurchaseOption | \PHPUnit_Framework_MockObject_MockObject
82+
*/
83+
private $instantPurchaseOption;
84+
85+
/**
86+
* Setup environment for testing
87+
*/
88+
protected function setUp()
89+
{
90+
$this->customerSession = $this->createMock(Session::class);
91+
$this->storeManager = $this->createMock(StoreManagerInterface::class);
92+
$this->instantPurchase = $this->createMock(InstantPurchaseModel::class);
93+
$this->paymentTokenFormatter = $this->createMock(PaymentTokenFormatter::class);
94+
$this->customerAddressesFormatter = $this->createMock(CustomerAddressesFormatter::class);
95+
$this->shippingMethodFormatter = $this->createMock(ShippingMethodFormatter::class);
96+
$this->store = $this->createMock(Store::class);
97+
$this->customer = $this->createMock(Customer::class);
98+
$this->instantPurchaseOption = $this->createMock(InstantPurchaseOption::class);
99+
100+
$this->objectManager = new ObjectManagerHelper($this);
101+
$this->customerData = $this->objectManager->getObject(
102+
CustomerData::class,
103+
[
104+
'customerSession' => $this->customerSession,
105+
'storeManager' => $this->storeManager,
106+
'instantPurchase' => $this->instantPurchase,
107+
'paymentTokenFormatter' => $this->paymentTokenFormatter,
108+
'customerAddressesFormatter' => $this->customerAddressesFormatter,
109+
'shippingMethodFormatter' => $this->shippingMethodFormatter
110+
]
111+
);
112+
}
113+
114+
/**
115+
* Test getSectionData()
116+
*
117+
* @param $isLogin
118+
* @param $isAvailable
119+
* @param $expected
120+
* @dataProvider getSectionDataProvider
121+
*/
122+
public function testGetSectionData($isLogin, $isAvailable, $expected)
123+
{
124+
$this->customerSession->expects($this->any())->method('isLoggedIn')->willReturn($isLogin);
125+
126+
$this->storeManager->expects($this->any())->method('getStore')->willReturn($this->store);
127+
128+
$this->customerSession->expects($this->any())->method('getCustomer')
129+
->willReturn($this->customer);
130+
131+
$this->instantPurchase->expects($this->any())->method('getOption')
132+
->with($this->store, $this->customer)
133+
->willReturn($this->instantPurchaseOption);
134+
135+
$this->instantPurchaseOption->expects($this->any())->method('isAvailable')
136+
->willReturn($isAvailable);
137+
138+
$this->assertEquals($expected, $this->customerData->getSectionData());
139+
}
140+
141+
/**
142+
* Data Provider for test getSectionData()
143+
*
144+
* @return array
145+
*/
146+
public function getSectionDataProvider()
147+
{
148+
return [
149+
'No Login and available instant purchase' => [
150+
false,
151+
true,
152+
['available' => false]
153+
],
154+
155+
'Login and no available instant purchase option' => [
156+
true,
157+
false,
158+
['available' => false]
159+
],
160+
161+
'Login and available instant purchase option' => [
162+
true,
163+
true,
164+
[
165+
'available' => true,
166+
'paymentToken' => [
167+
'publicHash' => '',
168+
'summary' => ''
169+
],
170+
'shippingAddress' => [
171+
'id' => null,
172+
'summary' => ''
173+
],
174+
'billingAddress' => [
175+
'id' => null,
176+
'summary' => ''
177+
],
178+
'shippingMethod' => [
179+
'carrier' => null,
180+
'method' => null,
181+
'summary' => ''
182+
]
183+
]
184+
]
185+
];
186+
}
187+
}

0 commit comments

Comments
 (0)