Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
DeleteRelationTest.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class DeleteRelationTest extends \PHPUnit\Framework\TestCase
15 {
17  protected $relation;
18 
19  protected function setUp()
20  {
21  $this->customerFactoryMock = $this->createPartialMock(
22  \Magento\Customer\Model\CustomerFactory::class,
23  ['create']
24  );
25  $this->relation = (new ObjectManagerHelper($this))->getObject(
26  \Magento\Customer\Model\ResourceModel\Address\DeleteRelation::class
27  );
28  }
29 
36  public function testDeleteRelation($addressId, $isDefaultBilling, $isDefaultShipping)
37  {
39  $addressModel = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
40  ->disableOriginalConstructor()
41  ->setMethods(['getIsCustomerSaveTransaction', 'getId', 'getResource'])
42  ->getMock();
44  $customerModel = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
45  ->disableOriginalConstructor()
46  ->setMethods(['getDefaultBilling', 'getDefaultShipping', 'getId'])
47  ->getMock();
48 
49  $addressResource = $this->getMockForAbstractClass(
50  \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
51  [],
52  '',
53  false,
54  false,
55  true,
56  ['getConnection', 'getTable']
57  );
58  $connectionMock = $this->getMockForAbstractClass(
59  \Magento\Framework\DB\Adapter\AdapterInterface::class,
60  [],
61  '',
62  false,
63  false,
64  true,
65  ['update', 'quoteInto']
66  );
67  $addressModel->expects($this->any())->method('getResource')->willReturn($addressResource);
68  $addressModel->expects($this->any())->method('getId')->willReturn($addressId);
69  $addressModel->expects($this->any())->method('getIsCustomerSaveTransaction')->willReturn(false);
70 
71  $customerModel->expects($this->any())->method("getDefaultBilling")->willReturn($isDefaultBilling);
72  $customerModel->expects($this->any())->method("getDefaultShipping")->willReturn($isDefaultShipping);
73 
74  if ($addressId && ($isDefaultBilling || $isDefaultShipping)) {
75  $customerId = 1;
76  $addressResource->expects($this->exactly(2))->method('getConnection')->willReturn($connectionMock);
77  $customerModel->expects($this->any())->method('getId')->willReturn(1);
78  $conditionSql = "entity_id = $customerId";
79  $connectionMock->expects($this->once())->method('quoteInto')
80  ->with('entity_id = ?', $customerId)
81  ->willReturn($conditionSql);
82  $addressResource->expects($this->once())->method('getTable')
83  ->with('customer_entity')
84  ->willReturn('customer_entity');
85  $toUpdate = [];
86  if ($isDefaultBilling) {
87  $toUpdate['default_billing'] = null;
88  }
89  if ($isDefaultShipping) {
90  $toUpdate['default_shipping'] = null;
91  }
92  $connectionMock->expects($this->once())->method('update')->with(
93  'customer_entity',
94  $toUpdate,
95  $conditionSql
96  );
97  }
98  $result = $this->relation->deleteRelation($addressModel, $customerModel);
99  $this->assertNull($result);
100  }
101 
107  public function getRelationDataProvider()
108  {
109  return [
110  [null, true, true],
111  [1, true, true],
112  [1, true, false],
113  [1, false, true],
114  [1, false, false],
115  ];
116  }
117 }