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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Test\Unit\Model\ResourceModel\Address;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
/**
* Class AddressTest
*/
class DeleteRelationTest extends \PHPUnit\Framework\TestCase
{
/** @var \Magento\Customer\Model\ResourceModel\Address\DeleteRelation */
protected $relation;
protected function setUp()
{
$this->customerFactoryMock = $this->createPartialMock(
\Magento\Customer\Model\CustomerFactory::class,
['create']
);
$this->relation = (new ObjectManagerHelper($this))->getObject(
\Magento\Customer\Model\ResourceModel\Address\DeleteRelation::class
);
}
/**
* @param $addressId
* @param $isDefaultBilling
* @param $isDefaultShipping
* @dataProvider getRelationDataProvider
*/
public function testDeleteRelation($addressId, $isDefaultBilling, $isDefaultShipping)
{
/** @var AbstractModel | \PHPUnit_Framework_MockObject_MockObject $addressModel */
$addressModel = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
->disableOriginalConstructor()
->setMethods(['getIsCustomerSaveTransaction', 'getId', 'getResource'])
->getMock();
/** @var \Magento\Customer\Model\Customer | \PHPUnit_Framework_MockObject_MockObject $customerModel */
$customerModel = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
->disableOriginalConstructor()
->setMethods(['getDefaultBilling', 'getDefaultShipping', 'getId'])
->getMock();
$addressResource = $this->getMockForAbstractClass(
\Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
[],
'',
false,
false,
true,
['getConnection', 'getTable']
);
$connectionMock = $this->getMockForAbstractClass(
\Magento\Framework\DB\Adapter\AdapterInterface::class,
[],
'',
false,
false,
true,
['update', 'quoteInto']
);
$addressModel->expects($this->any())->method('getResource')->willReturn($addressResource);
$addressModel->expects($this->any())->method('getId')->willReturn($addressId);
$addressModel->expects($this->any())->method('getIsCustomerSaveTransaction')->willReturn(false);
$customerModel->expects($this->any())->method("getDefaultBilling")->willReturn($isDefaultBilling);
$customerModel->expects($this->any())->method("getDefaultShipping")->willReturn($isDefaultShipping);
if ($addressId && ($isDefaultBilling || $isDefaultShipping)) {
$customerId = 1;
$addressResource->expects($this->exactly(2))->method('getConnection')->willReturn($connectionMock);
$customerModel->expects($this->any())->method('getId')->willReturn(1);
$conditionSql = "entity_id = $customerId";
$connectionMock->expects($this->once())->method('quoteInto')
->with('entity_id = ?', $customerId)
->willReturn($conditionSql);
$addressResource->expects($this->once())->method('getTable')
->with('customer_entity')
->willReturn('customer_entity');
$toUpdate = [];
if ($isDefaultBilling) {
$toUpdate['default_billing'] = null;
}
if ($isDefaultShipping) {
$toUpdate['default_shipping'] = null;
}
$connectionMock->expects($this->once())->method('update')->with(
'customer_entity',
$toUpdate,
$conditionSql
);
}
$result = $this->relation->deleteRelation($addressModel, $customerModel);
$this->assertNull($result);
}
/**
* Data provider for processRelation method
*
* @return array
*/
public function getRelationDataProvider()
{
return [
[null, true, true],
[1, true, true],
[1, true, false],
[1, false, true],
[1, false, false],
];
}
}