SelectTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Multishipping\Test\Unit\Block\Checkout\Address;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class SelectTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Multishipping\Block\Checkout\Address\Select
  16. */
  17. protected $block;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $addressMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $multishippingMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $customerMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $filterBuilderMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $searchCriteriaBuilderMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $addressRepositoryMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $filterMock;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $searchCriteriaMock;
  50. /**
  51. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  52. */
  53. protected $objectManager;
  54. protected function setUp()
  55. {
  56. $this->objectManager = new ObjectManager($this);
  57. $this->multishippingMock =
  58. $this->createMock(\Magento\Multishipping\Model\Checkout\Type\Multishipping::class);
  59. $this->addressMock = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
  60. $this->customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  61. $this->filterBuilderMock = $this->createMock(\Magento\Framework\Api\FilterBuilder::class);
  62. $this->searchCriteriaBuilderMock =
  63. $this->createMock(\Magento\Framework\Api\SearchCriteriaBuilder::class);
  64. $this->addressRepositoryMock = $this->createMock(\Magento\Customer\Api\AddressRepositoryInterface::class);
  65. $this->filterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
  66. $this->searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  67. $this->block = $this->objectManager->getObject(
  68. \Magento\Multishipping\Block\Checkout\Address\Select::class,
  69. [
  70. 'multishipping' => $this->multishippingMock,
  71. 'addressRepository' => $this->addressRepositoryMock,
  72. 'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
  73. 'filterBuilder' => $this->filterBuilderMock
  74. ]
  75. );
  76. }
  77. /**
  78. * @param string $id
  79. * @param bool $expectedValue
  80. * @dataProvider isDefaultAddressDataProvider
  81. */
  82. public function testIsAddressDefaultBilling($id, $expectedValue)
  83. {
  84. $this->addressMock->expects($this->once())->method('getId')->willReturn(1);
  85. $this->multishippingMock->expects($this->once())->method('getCustomer')->willReturn($this->customerMock);
  86. $this->customerMock->expects($this->once())->method('getDefaultBilling')->willReturn($id);
  87. $this->assertEquals($expectedValue, $this->block->isAddressDefaultBilling($this->addressMock));
  88. }
  89. /**
  90. * @param string $id
  91. * @param bool $expectedValue
  92. * @dataProvider isDefaultAddressDataProvider
  93. */
  94. public function testIsAddressDefaultShipping($id, $expectedValue)
  95. {
  96. $this->addressMock->expects($this->once())->method('getId')->willReturn(1);
  97. $this->multishippingMock->expects($this->once())->method('getCustomer')->willReturn($this->customerMock);
  98. $this->customerMock->expects($this->once())->method('getDefaultShipping')->willReturn($id);
  99. $this->assertEquals($expectedValue, $this->block->isAddressDefaultShipping($this->addressMock));
  100. }
  101. /**
  102. * @return array
  103. */
  104. public function isDefaultAddressDataProvider()
  105. {
  106. return [
  107. 'yes' => [1, true],
  108. 'no' => [2, false],
  109. ];
  110. }
  111. public function testGetAddress()
  112. {
  113. $searchResultMock = $this->createMock(\Magento\Customer\Api\Data\AddressSearchResultsInterface::class);
  114. $this->multishippingMock->expects($this->once())->method('getCustomer')->willReturn($this->customerMock);
  115. $this->customerMock->expects($this->once())->method('getId')->willReturn(1);
  116. $this->filterBuilderMock->expects($this->once())->method('setField')->with('parent_id')->willReturnSelf();
  117. $this->filterBuilderMock->expects($this->once())->method('setValue')->with(1)->willReturnSelf();
  118. $this->filterBuilderMock->expects($this->once())->method('setConditionType')->with('eq')->willReturnSelf();
  119. $this->filterBuilderMock->expects($this->once())->method('create')->willReturn($this->filterMock);
  120. $this->searchCriteriaBuilderMock
  121. ->expects($this->once())
  122. ->method('addFilters')
  123. ->with([$this->filterMock])
  124. ->willReturnSelf();
  125. $this->searchCriteriaBuilderMock
  126. ->expects($this->once())
  127. ->method('create')
  128. ->willReturn($this->searchCriteriaMock);
  129. $this->addressRepositoryMock
  130. ->expects($this->once())
  131. ->method('getList')
  132. ->with($this->searchCriteriaMock)
  133. ->willReturn($searchResultMock);
  134. $searchResultMock->expects($this->once())->method('getItems')->willReturn([$this->addressMock]);
  135. $this->assertEquals([$this->addressMock], $this->block->getAddress());
  136. $this->assertEquals([$this->addressMock], $this->block->getData('address_collection'));
  137. }
  138. public function testGetAlreadyExistingAddress()
  139. {
  140. $this->block = $this->objectManager->getObject(
  141. \Magento\Multishipping\Block\Checkout\Address\Select::class,
  142. [
  143. 'addressRepository' => $this->addressRepositoryMock,
  144. 'filterBuilder' => $this->filterBuilderMock,
  145. 'data' => [
  146. 'address_collection' => [$this->addressMock
  147. ]
  148. ]
  149. ]
  150. );
  151. $this->filterBuilderMock->expects($this->never())->method('setField');
  152. $this->addressRepositoryMock
  153. ->expects($this->never())
  154. ->method('getList');
  155. $this->assertEquals([$this->addressMock], $this->block->getAddress());
  156. }
  157. public function testGetAddressWhenItNotExistInCustomer()
  158. {
  159. $searchResultMock = $this->createMock(\Magento\Customer\Api\Data\AddressSearchResultsInterface::class);
  160. $this->multishippingMock->expects($this->once())->method('getCustomer')->willReturn($this->customerMock);
  161. $this->customerMock->expects($this->once())->method('getId')->willReturn(1);
  162. $this->filterBuilderMock->expects($this->once())->method('setField')->with('parent_id')->willReturnSelf();
  163. $this->filterBuilderMock->expects($this->once())->method('setValue')->with(1)->willReturnSelf();
  164. $this->filterBuilderMock->expects($this->once())->method('setConditionType')->with('eq')->willReturnSelf();
  165. $this->filterBuilderMock->expects($this->once())->method('create')->willReturn($this->filterMock);
  166. $this->searchCriteriaBuilderMock
  167. ->expects($this->once())
  168. ->method('addFilters')
  169. ->with([$this->filterMock])
  170. ->willReturnSelf();
  171. $this->searchCriteriaBuilderMock
  172. ->expects($this->once())
  173. ->method('create')
  174. ->willReturn($this->searchCriteriaMock);
  175. $this->addressRepositoryMock
  176. ->expects($this->once())
  177. ->method('getList')
  178. ->with($this->searchCriteriaMock)
  179. ->willReturn($searchResultMock);
  180. $searchResultMock->expects($this->once())->method('getItems')->willThrowException(new NoSuchEntityException());
  181. $this->assertEquals([], $this->block->getAddress());
  182. }
  183. }