DeleteRelationTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\Model\AbstractModel;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. /**
  10. * Class AddressTest
  11. */
  12. class DeleteRelationTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \Magento\Customer\Model\ResourceModel\Address\DeleteRelation */
  15. protected $relation;
  16. protected function setUp()
  17. {
  18. $this->customerFactoryMock = $this->createPartialMock(
  19. \Magento\Customer\Model\CustomerFactory::class,
  20. ['create']
  21. );
  22. $this->relation = (new ObjectManagerHelper($this))->getObject(
  23. \Magento\Customer\Model\ResourceModel\Address\DeleteRelation::class
  24. );
  25. }
  26. /**
  27. * @param $addressId
  28. * @param $isDefaultBilling
  29. * @param $isDefaultShipping
  30. * @dataProvider getRelationDataProvider
  31. */
  32. public function testDeleteRelation($addressId, $isDefaultBilling, $isDefaultShipping)
  33. {
  34. /** @var AbstractModel | \PHPUnit_Framework_MockObject_MockObject $addressModel */
  35. $addressModel = $this->getMockBuilder(\Magento\Framework\Model\AbstractModel::class)
  36. ->disableOriginalConstructor()
  37. ->setMethods(['getIsCustomerSaveTransaction', 'getId', 'getResource'])
  38. ->getMock();
  39. /** @var \Magento\Customer\Model\Customer | \PHPUnit_Framework_MockObject_MockObject $customerModel */
  40. $customerModel = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
  41. ->disableOriginalConstructor()
  42. ->setMethods(['getDefaultBilling', 'getDefaultShipping', 'getId'])
  43. ->getMock();
  44. $addressResource = $this->getMockForAbstractClass(
  45. \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
  46. [],
  47. '',
  48. false,
  49. false,
  50. true,
  51. ['getConnection', 'getTable']
  52. );
  53. $connectionMock = $this->getMockForAbstractClass(
  54. \Magento\Framework\DB\Adapter\AdapterInterface::class,
  55. [],
  56. '',
  57. false,
  58. false,
  59. true,
  60. ['update', 'quoteInto']
  61. );
  62. $addressModel->expects($this->any())->method('getResource')->willReturn($addressResource);
  63. $addressModel->expects($this->any())->method('getId')->willReturn($addressId);
  64. $addressModel->expects($this->any())->method('getIsCustomerSaveTransaction')->willReturn(false);
  65. $customerModel->expects($this->any())->method("getDefaultBilling")->willReturn($isDefaultBilling);
  66. $customerModel->expects($this->any())->method("getDefaultShipping")->willReturn($isDefaultShipping);
  67. if ($addressId && ($isDefaultBilling || $isDefaultShipping)) {
  68. $customerId = 1;
  69. $addressResource->expects($this->exactly(2))->method('getConnection')->willReturn($connectionMock);
  70. $customerModel->expects($this->any())->method('getId')->willReturn(1);
  71. $conditionSql = "entity_id = $customerId";
  72. $connectionMock->expects($this->once())->method('quoteInto')
  73. ->with('entity_id = ?', $customerId)
  74. ->willReturn($conditionSql);
  75. $addressResource->expects($this->once())->method('getTable')
  76. ->with('customer_entity')
  77. ->willReturn('customer_entity');
  78. $toUpdate = [];
  79. if ($isDefaultBilling) {
  80. $toUpdate['default_billing'] = null;
  81. }
  82. if ($isDefaultShipping) {
  83. $toUpdate['default_shipping'] = null;
  84. }
  85. $connectionMock->expects($this->once())->method('update')->with(
  86. 'customer_entity',
  87. $toUpdate,
  88. $conditionSql
  89. );
  90. }
  91. $result = $this->relation->deleteRelation($addressModel, $customerModel);
  92. $this->assertNull($result);
  93. }
  94. /**
  95. * Data provider for processRelation method
  96. *
  97. * @return array
  98. */
  99. public function getRelationDataProvider()
  100. {
  101. return [
  102. [null, true, true],
  103. [1, true, true],
  104. [1, true, false],
  105. [1, false, true],
  106. [1, false, false],
  107. ];
  108. }
  109. }