ShippingSavedTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Multishipping\Test\Unit\Controller\Checkout\Address;
  7. use Magento\Multishipping\Controller\Checkout\Address\ShippingSaved;
  8. /**
  9. * @SuppressWarnings(PHPMD.LongVariable)
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class ShippingSavedTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var ShippingSaved
  16. */
  17. private $controller;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $contextMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $addressRepositoryMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $filterBuilderMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $criteriaBuilderMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $objectManagerMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $checkoutMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $redirectMock;
  46. protected function setUp()
  47. {
  48. $this->checkoutMock = $this->createMock(\Magento\Multishipping\Model\Checkout\Type\Multishipping::class);
  49. $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  50. $this->objectManagerMock->expects($this->any())
  51. ->method('get')
  52. ->with(\Magento\Multishipping\Model\Checkout\Type\Multishipping::class)
  53. ->willReturn($this->checkoutMock);
  54. $this->contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
  55. $requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  56. $responseMock = $this->createMock(\Magento\Framework\App\ResponseInterface::class);
  57. $this->redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
  58. $this->contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock);
  59. $this->contextMock->expects($this->any())->method('getRequest')->willReturn($requestMock);
  60. $this->contextMock->expects($this->any())->method('getResponse')->willReturn($responseMock);
  61. $this->contextMock->expects($this->any())->method('getRedirect')->willReturn($this->redirectMock);
  62. $this->addressRepositoryMock = $this->createMock(\Magento\Customer\Api\AddressRepositoryInterface::class);
  63. $this->filterBuilderMock = $this->createMock(\Magento\Framework\Api\FilterBuilder::class);
  64. $this->criteriaBuilderMock = $this->createMock(\Magento\Framework\Api\SearchCriteriaBuilder::class);
  65. $this->controller = new \Magento\Multishipping\Controller\Checkout\Address\ShippingSaved(
  66. $this->contextMock,
  67. $this->addressRepositoryMock,
  68. $this->filterBuilderMock,
  69. $this->criteriaBuilderMock
  70. );
  71. }
  72. public function testExecuteResetsCheckoutIfCustomerHasAddedNewShippingAddressAndItIsTheOnlyAddressHeHas()
  73. {
  74. $customerId = 1;
  75. $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  76. $customerMock->expects($this->any())->method('getId')->willReturn($customerId);
  77. $this->checkoutMock->expects($this->any())->method('getCustomer')->willReturn($customerMock);
  78. $this->mockCustomerAddressRepository(
  79. $customerId,
  80. [$this->createMock(\Magento\Customer\Api\Data\AddressInterface::class)]
  81. );
  82. // check that checkout is reset
  83. $this->checkoutMock->expects($this->once())->method('reset');
  84. $this->controller->execute();
  85. }
  86. public function testExecuteDoesNotResetCheckoutIfCustomerHasMoreThanOneAddress()
  87. {
  88. $customerId = 1;
  89. $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  90. $customerMock->expects($this->any())->method('getId')->willReturn($customerId);
  91. $this->checkoutMock->expects($this->any())->method('getCustomer')->willReturn($customerMock);
  92. $this->mockCustomerAddressRepository(
  93. $customerId,
  94. [
  95. $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class),
  96. $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class),
  97. ]
  98. );
  99. // check that checkout is not reset
  100. $this->checkoutMock->expects($this->never())->method('reset');
  101. $this->controller->execute();
  102. }
  103. /**
  104. * Mock customer address repository
  105. *
  106. * @param int $customerId
  107. * @param array $addresses list of customer addresses
  108. */
  109. private function mockCustomerAddressRepository($customerId, array $addresses)
  110. {
  111. $filterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
  112. $this->filterBuilderMock->expects($this->once())->method('setField')->with('parent_id')->willReturnSelf();
  113. $this->filterBuilderMock->expects($this->once())->method('setValue')->with($customerId)->willReturnSelf();
  114. $this->filterBuilderMock->expects($this->once())->method('setConditionType')->with('eq')->willReturnSelf();
  115. $this->filterBuilderMock->expects($this->once())->method('create')->willReturn($filterMock);
  116. $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  117. $this->criteriaBuilderMock->expects($this->once())->method('addFilters')->with([$filterMock])->willReturnSelf();
  118. $this->criteriaBuilderMock->expects($this->once())->method('create')->willReturn($searchCriteriaMock);
  119. $searchResultMock = $this->createMock(\Magento\Customer\Api\Data\AddressSearchResultsInterface::class);
  120. $this->addressRepositoryMock->expects($this->once())
  121. ->method('getList')
  122. ->with($searchCriteriaMock)
  123. ->willReturn($searchResultMock);
  124. $searchResultMock->expects($this->once())->method('getItems')->willReturn($addresses);
  125. }
  126. }