1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Block\Account\Dashboard;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
class AddressTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Customer\Block\Account\Dashboard\Address
*/
protected $_block;
/** @var \Magento\Customer\Model\Session */
protected $_customerSession;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $objectManager;
protected function setUp()
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->_customerSession = $this->objectManager->get(\Magento\Customer\Model\Session::class);
$this->_block = $this->objectManager->get(\Magento\Framework\View\LayoutInterface::class)
->createBlock(
\Magento\Customer\Block\Account\Dashboard\Address::class,
'',
['customerSession' => $this->_customerSession]
);
$this->objectManager->get(\Magento\Framework\App\ViewInterface::class)->setIsLayoutLoaded(true);
}
protected function tearDown()
{
$this->_customerSession->unsCustomerId();
/** @var \Magento\Customer\Model\CustomerRegistry $customerRegistry */
$customerRegistry = $this->objectManager->get(\Magento\Customer\Model\CustomerRegistry::class);
//Cleanup customer from registry
$customerRegistry->remove(1);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
*/
public function testGetCustomer()
{
$objectManager = Bootstrap::getObjectManager();
$layout = $objectManager->get(\Magento\Framework\View\LayoutInterface::class);
$layout->setIsCacheable(false);
/** @var CustomerRepositoryInterface $customerRepository */
$customerRepository = $objectManager
->get(\Magento\Customer\Api\CustomerRepositoryInterface::class);
$customer = $customerRepository->getById(1);
$this->_customerSession->setCustomerId(1);
$object = $this->_block->getCustomer();
$this->assertEquals($customer, $object);
$layout->setIsCacheable(true);
}
public function testGetCustomerMissingCustomer()
{
$moduleManager = $this->objectManager->get(\Magento\Framework\Module\Manager::class);
if ($moduleManager->isEnabled('Magento_PageCache')) {
$customerDataFactory = $this->objectManager->create(
\Magento\Customer\Api\Data\CustomerInterfaceFactory::class
);
$customerData = $customerDataFactory->create()->setGroupId($this->_customerSession->getCustomerGroupId());
$this->assertEquals($customerData, $this->_block->getCustomer());
} else {
$this->assertNull($this->_block->getCustomer());
}
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
* @magentoDataFixture Magento/Customer/_files/customer_no_address.php
* @dataProvider getPrimaryShippingAddressHtmlDataProvider
*/
public function testGetPrimaryShippingAddressHtml($customerId, $expected)
{
// todo: this test is sensitive to caching impact
if (!empty($customerId)) {
$this->_customerSession->setCustomerId($customerId);
}
$html = $this->_block->getPrimaryShippingAddressHtml();
$this->assertEquals($expected, $html);
}
public function getPrimaryShippingAddressHtmlDataProvider()
{
$expected = "John Smith<br />\nCompanyName<br />\nGreen str, 67<br />\n\n\n\nCityM, Alabama, 75477<br />"
. "\nUnited States<br />\nT: <a href=\"tel:3468676\">3468676</a>\n\n";
return [
'0' => [0, 'You have not set a default shipping address.'],
'1' => [1, $expected],
'5' => [5, 'You have not set a default shipping address.']
];
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
* @magentoDataFixture Magento/Customer/_files/customer_no_address.php
* @dataProvider getPrimaryBillingAddressHtmlDataProvider
*/
public function testGetPrimaryBillingAddressHtml($customerId, $expected)
{
if (!empty($customerId)) {
$this->_customerSession->setCustomerId($customerId);
}
$html = $this->_block->getPrimaryBillingAddressHtml();
$this->assertEquals($expected, $html);
}
public function getPrimaryBillingAddressHtmlDataProvider()
{
$expected = "John Smith<br />\nCompanyName<br />\nGreen str, 67<br />\n\n\n\nCityM, Alabama, 75477<br />"
. "\nUnited States<br />\nT: <a href=\"tel:3468676\">3468676</a>\n\n";
return [
'0' => [0, 'You have not set a default billing address.'],
'1' => [1, $expected],
'5' => [5, 'You have not set a default billing address.'],
];
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
* @magentoDataFixture Magento/Customer/_files/customer_no_address.php
* @dataProvider getPrimaryAddressEditUrlDataProvider
*/
public function testGetPrimaryShippingAddressEditUrl($customerId, $expected)
{
if (!empty($customerId)) {
$this->_customerSession->setCustomerId($customerId);
}
$url = $this->_block->getPrimaryShippingAddressEditUrl();
$this->assertEquals($expected, $url);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
* @magentoDataFixture Magento/Customer/_files/customer_no_address.php
* @dataProvider getPrimaryAddressEditUrlDataProvider
*/
public function testGetPrimaryBillingAddressEditUrl($customerId, $expected)
{
if (!empty($customerId)) {
$this->_customerSession->setCustomerId($customerId);
}
$url = $this->_block->getPrimaryBillingAddressEditUrl();
$this->assertEquals($expected, $url);
}
public function getPrimaryAddressEditUrlDataProvider()
{
return [
'1' => [1, 'http://localhost/index.php/customer/address/edit/id/1/'],
];
}
}