ShippingAddressAssignmentTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Test\Unit\Model;
  7. class ShippingAddressAssignmentTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Quote\Model\ShippingAddressAssignment
  11. */
  12. private $model;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. private $shippingAssignmentProcessorMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $cartExtensionFactoryMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $quoteMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $addressMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $extensionAttributeMock;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $shippingAssignmentMock;
  37. public function setUp()
  38. {
  39. $this->cartExtensionFactoryMock = $this->createPartialMock(
  40. \Magento\Quote\Api\Data\CartExtensionFactory::class,
  41. ['create']
  42. );
  43. $this->shippingAssignmentProcessorMock = $this->createMock(
  44. \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor::class
  45. );
  46. $this->quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  47. $this->addressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  48. $this->extensionAttributeMock = $this->createPartialMock(
  49. \Magento\Quote\Api\Data\CartExtension::class,
  50. ['setShippingAssignments']
  51. );
  52. $this->shippingAssignmentMock = $this->createMock(\Magento\Quote\Api\Data\ShippingAssignmentInterface::class);
  53. //shipping assignment processing
  54. $this->quoteMock->expects($this->once())->method('getExtensionAttributes')->willReturn(null);
  55. $this->cartExtensionFactoryMock
  56. ->expects($this->once())
  57. ->method('create')
  58. ->willReturn($this->extensionAttributeMock);
  59. $this->shippingAssignmentProcessorMock
  60. ->expects($this->once())
  61. ->method('create')
  62. ->willReturn($this->shippingAssignmentMock);
  63. $this->extensionAttributeMock
  64. ->expects($this->once())
  65. ->method('setShippingAssignments')
  66. ->with([$this->shippingAssignmentMock])
  67. ->willReturnSelf();
  68. $this->quoteMock->expects($this->once())->method('setExtensionAttributes')->with($this->extensionAttributeMock);
  69. $this->model = new \Magento\Quote\Model\ShippingAddressAssignment(
  70. $this->cartExtensionFactoryMock,
  71. $this->shippingAssignmentProcessorMock
  72. );
  73. }
  74. public function testSetAddressUseForShippingTrue()
  75. {
  76. $addressId = 1;
  77. $addressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  78. $this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($addressMock);
  79. $addressMock->expects($this->once())->method('getId')->willReturn($addressId);
  80. $this->addressMock->expects($this->once())->method('setSameAsBilling')->with(1);
  81. $this->quoteMock->expects($this->once())->method('removeAddress')->with($addressId);
  82. $this->quoteMock->expects($this->once())->method('setShippingAddress')->with($this->addressMock);
  83. $this->model->setAddress($this->quoteMock, $this->addressMock, true);
  84. }
  85. public function testSetAddressUseForShippingFalse()
  86. {
  87. $addressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  88. $this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($addressMock);
  89. $addressMock->expects($this->once())->method('setSameAsBilling')->with(0)->willReturnSelf();
  90. $this->quoteMock->expects($this->once())->method('setShippingAddress')->with($addressMock);
  91. $this->model->setAddress($this->quoteMock, $this->addressMock, false);
  92. }
  93. }