RelationTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\ResourceModel\Address;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. use Magento\Customer\Model\Address;
  9. /**
  10. * Class AddressTest
  11. */
  12. class RelationTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \Magento\Customer\Model\CustomerFactory | \PHPUnit_Framework_MockObject_MockObject */
  15. protected $customerFactoryMock;
  16. /** @var \Magento\Customer\Model\ResourceModel\Address\Relation */
  17. protected $relation;
  18. protected function setUp()
  19. {
  20. $this->customerFactoryMock = $this->createPartialMock(
  21. \Magento\Customer\Model\CustomerFactory::class,
  22. ['create']
  23. );
  24. $this->relation = (new ObjectManagerHelper($this))->getObject(
  25. \Magento\Customer\Model\ResourceModel\Address\Relation::class,
  26. [
  27. 'customerFactory' => $this->customerFactoryMock
  28. ]
  29. );
  30. }
  31. /**
  32. * @param $addressId
  33. * @param $isDefaultBilling
  34. * @param $isDefaultShipping
  35. * @dataProvider getRelationDataProvider
  36. */
  37. public function testProcessRelation($addressId, $isDefaultBilling, $isDefaultShipping)
  38. {
  39. $addressModel = $this->createPartialMock(Address::class, [
  40. '__wakeup',
  41. 'getId',
  42. 'getEntityTypeId',
  43. 'getIsDefaultBilling',
  44. 'getIsDefaultShipping',
  45. 'hasDataChanges',
  46. 'validateBeforeSave',
  47. 'beforeSave',
  48. 'afterSave',
  49. 'isSaveAllowed',
  50. 'getIsCustomerSaveTransaction'
  51. ]);
  52. $customerModel = $this->createPartialMock(
  53. \Magento\Customer\Model\Customer::class,
  54. [
  55. '__wakeup',
  56. 'setDefaultBilling',
  57. 'setDefaultShipping',
  58. 'save',
  59. 'load',
  60. 'getResource',
  61. 'getId',
  62. 'getDefaultShippingAddress',
  63. 'getDefaultBillingAddress'
  64. ]
  65. );
  66. $customerResource = $this->getMockForAbstractClass(
  67. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  68. [],
  69. '',
  70. false,
  71. false,
  72. true,
  73. ['getConnection', 'getTable']
  74. );
  75. $connectionMock = $this->getMockForAbstractClass(
  76. \Magento\Framework\DB\Adapter\AdapterInterface::class,
  77. [],
  78. '',
  79. false,
  80. false,
  81. true,
  82. ['update', 'quoteInto']
  83. );
  84. $customerModel->expects($this->any())->method('getResource')->willReturn($customerResource);
  85. $addressModel->expects($this->any())->method('getId')->willReturn($addressId);
  86. $addressModel->expects($this->any())->method('getIsDefaultShipping')->willReturn($isDefaultShipping);
  87. $addressModel->expects($this->any())->method('getIsDefaultBilling')->willReturn($isDefaultBilling);
  88. $addressModel->expects($this->any())->method('getIsCustomerSaveTransaction')->willReturn(false);
  89. $customerModel->expects($this->any())
  90. ->method('load')
  91. ->willReturnSelf();
  92. $this->customerFactoryMock->expects($this->any())
  93. ->method('create')
  94. ->willReturn($customerModel);
  95. if ($addressId && ($isDefaultBilling || $isDefaultShipping)) {
  96. $customerId = 1;
  97. $customerResource->expects($this->exactly(2))->method('getConnection')->willReturn($connectionMock);
  98. $customerModel->expects($this->any())->method('getId')->willReturn(1);
  99. $conditionSql = "entity_id = $customerId";
  100. $connectionMock->expects($this->once())->method('quoteInto')
  101. ->with('entity_id = ?', $customerId)
  102. ->willReturn($conditionSql);
  103. $customerResource->expects($this->once())->method('getTable')
  104. ->with('customer_entity')
  105. ->willReturn('customer_entity');
  106. $toUpdate = [];
  107. if ($isDefaultBilling) {
  108. $toUpdate['default_billing'] = $addressId;
  109. }
  110. if ($isDefaultShipping) {
  111. $toUpdate['default_shipping'] = $addressId;
  112. }
  113. $connectionMock->expects($this->once())->method('update')->with(
  114. 'customer_entity',
  115. $toUpdate,
  116. $conditionSql
  117. );
  118. }
  119. $result = $this->relation->processRelation($addressModel);
  120. $this->assertNull($result);
  121. }
  122. /**
  123. * Data provider for processRelation method
  124. *
  125. * @return array
  126. */
  127. public function getRelationDataProvider()
  128. {
  129. return [
  130. [null, true, true],
  131. [1, true, true],
  132. [1, true, false],
  133. [1, false, true],
  134. [1, false, false],
  135. ];
  136. }
  137. }