Skip to content

Commit 5c08e39

Browse files
ENGCOM-6446: [Customer] Cover CustomerData\Customer and CustomerData\JsLayoutDataProviderPool by Unit Test #25984
- Merge Pull Request #25984 from edenduong/magento2:2.4-testcover/cover_customer_customer_data - Merged commits: 1. c3a97c0
2 parents 1ad65a3 + c3a97c0 commit 5c08e39

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\Customer\Test\Unit\CustomerData;
10+
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
12+
use Magento\Customer\CustomerData\Customer as CustomerData;
13+
use Magento\Customer\Helper\Session\CurrentCustomer;
14+
use Magento\Customer\Helper\View;
15+
use Magento\Customer\Api\Data\CustomerInterface;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class CustomerTest extends TestCase
19+
{
20+
/**
21+
* @var ObjectManagerHelper
22+
*/
23+
private $objectManagerHelper;
24+
25+
/**
26+
* @var CustomerData
27+
*/
28+
private $customerData;
29+
30+
/**
31+
* @var CurrentCustomer
32+
*/
33+
private $currentCustomerMock;
34+
35+
/**
36+
* @var View
37+
*/
38+
private $customerViewHelperMock;
39+
40+
/**
41+
* Setup environment to test
42+
*/
43+
protected function setUp()
44+
{
45+
$this->currentCustomerMock = $this->createMock(CurrentCustomer::class);
46+
$this->customerViewHelperMock = $this->createMock(View::class);
47+
$this->objectManagerHelper = new ObjectManagerHelper($this);
48+
49+
$this->customerData = $this->objectManagerHelper->getObject(
50+
CustomerData::class,
51+
[
52+
'currentCustomer' => $this->currentCustomerMock,
53+
'customerViewHelper' => $this->customerViewHelperMock
54+
]
55+
);
56+
}
57+
58+
/**
59+
* Test getSectionData() without customer Id
60+
*/
61+
public function testGetSectionDataWithoutCustomerId()
62+
{
63+
$this->currentCustomerMock->expects($this->any())->method('getCustomerId')->willReturn(null);
64+
$this->assertEquals([], $this->customerData->getSectionData());
65+
}
66+
67+
/**
68+
* Test getSectionData() with customer
69+
*/
70+
public function testGetSectionDataWithCustomer()
71+
{
72+
$this->currentCustomerMock->expects($this->any())->method('getCustomerId')->willReturn(1);
73+
$customerMock = $this->createMock(CustomerInterface::class);
74+
$customerMock->expects($this->any())->method('getFirstname')->willReturn('John');
75+
$customerMock->expects($this->any())->method('getWebsiteId')->willReturn(1);
76+
$this->currentCustomerMock->expects($this->any())->method('getCustomer')->willReturn($customerMock);
77+
$this->customerViewHelperMock->expects($this->any())->method('getCustomerName')
78+
->with($customerMock)
79+
->willReturn('John Adam');
80+
81+
$this->assertEquals(
82+
[
83+
'fullname' => 'John Adam',
84+
'firstname' => 'John',
85+
'websiteId' => 1,
86+
],
87+
$this->customerData->getSectionData()
88+
);
89+
}
90+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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\Customer\Test\Unit\CustomerData;
10+
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
12+
use Magento\Tax\CustomerData\CheckoutTotalsJsLayoutDataProvider as CheckoutTotalsJs;
13+
use Magento\Customer\CustomerData\JsLayoutDataProviderPool;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class JsLayoutDataProviderPoolTest extends TestCase
17+
{
18+
/**
19+
* @var ObjectManagerHelper
20+
*/
21+
private $objectManagerHelper;
22+
23+
/**
24+
* @var CheckoutTotalsJs
25+
*/
26+
private $checkoutTotalsJsLayoutDataProviderMock;
27+
28+
/**
29+
* @var JsLayoutDataProviderPool
30+
*/
31+
private $jsLayoutDataProviderPool;
32+
33+
/**
34+
* Setup environment to test
35+
*/
36+
protected function setUp()
37+
{
38+
$this->checkoutTotalsJsLayoutDataProviderMock = $this->createMock(CheckoutTotalsJs::class);
39+
$this->objectManagerHelper = new ObjectManagerHelper($this);
40+
41+
$this->jsLayoutDataProviderPool = $this->objectManagerHelper->getObject(
42+
JsLayoutDataProviderPool::class,
43+
[
44+
'jsLayoutDataProviders' => [
45+
'checkout_totals' => $this->checkoutTotalsJsLayoutDataProviderMock
46+
]
47+
]
48+
);
49+
}
50+
51+
/**
52+
* Test getData() function
53+
*/
54+
public function testGetData()
55+
{
56+
$checkoutTotalsJsData = [
57+
'components' => [
58+
'minicart_content' => [
59+
'children' => [
60+
'subtotal.container' => [
61+
'children' => [
62+
'subtotal' => [
63+
'children' => [
64+
'subtotal.totals' => [
65+
'config' => [
66+
'display_cart_subtotal_incl_tax' => 1,
67+
'display_cart_subtotal_excl_tax' => 1
68+
]
69+
],
70+
],
71+
],
72+
],
73+
],
74+
],
75+
],
76+
]
77+
];
78+
$this->checkoutTotalsJsLayoutDataProviderMock->expects($this->any())
79+
->method('getData')
80+
->willReturn($checkoutTotalsJsData);
81+
82+
$this->assertEquals($checkoutTotalsJsData, $this->jsLayoutDataProviderPool->getData());
83+
}
84+
}

0 commit comments

Comments
 (0)