GridTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Block\Address;
  7. use Magento\Customer\Model\ResourceModel\Address\CollectionFactory;
  8. /**
  9. * Unit tests for \Magento\Customer\Block\Address\Grid class
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class GridTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  16. */
  17. private $objectManager;
  18. /**
  19. * @var \Magento\Customer\Helper\Session\CurrentCustomer|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $addressCollectionFactory;
  22. /**
  23. * @var \Magento\Customer\Model\ResourceModel\Address\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $currentCustomer;
  26. /**
  27. * @var \Magento\Directory\Model\CountryFactory|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $countryFactory;
  30. /**
  31. * @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $urlBuilder;
  34. /**
  35. * @var \Magento\Customer\Block\Address\Grid
  36. */
  37. private $gridBlock;
  38. protected function setUp()
  39. {
  40. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  41. $this->currentCustomer = $this->getMockBuilder(\Magento\Customer\Helper\Session\CurrentCustomer::class)
  42. ->disableOriginalConstructor()
  43. ->setMethods(['getCustomer'])
  44. ->getMock();
  45. $this->addressCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
  46. ->disableOriginalConstructor()
  47. ->setMethods(['create'])
  48. ->getMock();
  49. $this->countryFactory = $this->getMockBuilder(\Magento\Directory\Model\CountryFactory::class)
  50. ->disableOriginalConstructor()
  51. ->setMethods(['create'])
  52. ->getMock();
  53. $this->urlBuilder = $this->getMockForAbstractClass(\Magento\Framework\UrlInterface::class);
  54. $this->gridBlock = $this->objectManager->getObject(
  55. \Magento\Customer\Block\Address\Grid::class,
  56. [
  57. 'addressCollectionFactory' => $this->addressCollectionFactory,
  58. 'currentCustomer' => $this->currentCustomer,
  59. 'countryFactory' => $this->countryFactory,
  60. '_urlBuilder' => $this->urlBuilder
  61. ]
  62. );
  63. }
  64. /**
  65. * Test for \Magento\Customer\Block\Address\Book::getChildHtml method with 'pager' argument
  66. */
  67. public function testGetChildHtml()
  68. {
  69. $customerId = 1;
  70. /** @var \Magento\Framework\View\Element\BlockInterface|\PHPUnit_Framework_MockObject_MockObject $block */
  71. $block = $this->getMockBuilder(\Magento\Framework\View\Element\BlockInterface::class)
  72. ->setMethods(['setCollection'])
  73. ->getMockForAbstractClass();
  74. /** @var $layout \Magento\Framework\View\LayoutInterface|\PHPUnit_Framework_MockObject_MockObject */
  75. $layout = $this->getMockForAbstractClass(\Magento\Framework\View\LayoutInterface::class);
  76. /** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customer */
  77. $customer = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\CustomerInterface::class);
  78. /** @var \PHPUnit_Framework_MockObject_MockObject */
  79. $addressCollection = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Address\Collection::class)
  80. ->disableOriginalConstructor()
  81. ->setMethods(['setOrder', 'setCustomerFilter', 'load'])
  82. ->getMock();
  83. $layout->expects($this->atLeastOnce())->method('getChildName')->with('NameInLayout', 'pager')
  84. ->willReturn('ChildName');
  85. $layout->expects($this->atLeastOnce())->method('renderElement')->with('ChildName', true)
  86. ->willReturn('OutputString');
  87. $layout->expects($this->atLeastOnce())->method('createBlock')
  88. ->with(\Magento\Theme\Block\Html\Pager::class, 'customer.addresses.pager')->willReturn($block);
  89. $customer->expects($this->atLeastOnce())->method('getId')->willReturn($customerId);
  90. $this->currentCustomer->expects($this->atLeastOnce())->method('getCustomer')->willReturn($customer);
  91. $addressCollection->expects($this->atLeastOnce())->method('setOrder')->with('entity_id', 'desc')
  92. ->willReturnSelf();
  93. $addressCollection->expects($this->atLeastOnce())->method('setCustomerFilter')->with([$customerId])
  94. ->willReturnSelf();
  95. $this->addressCollectionFactory->expects($this->atLeastOnce())->method('create')
  96. ->willReturn($addressCollection);
  97. $block->expects($this->atLeastOnce())->method('setCollection')->with($addressCollection)->willReturnSelf();
  98. $this->gridBlock->setNameInLayout('NameInLayout');
  99. $this->gridBlock->setLayout($layout);
  100. $this->assertEquals('OutputString', $this->gridBlock->getChildHtml('pager'));
  101. }
  102. /**
  103. * Test for \Magento\Customer\Block\Address\Grid::getAddressEditUrl method
  104. */
  105. public function testGetAddAddressUrl()
  106. {
  107. $addressId = 1;
  108. $expectedUrl = 'expected_url';
  109. $this->urlBuilder->expects($this->atLeastOnce())->method('getUrl')
  110. ->with('customer/address/edit', ['_secure' => true, 'id' => $addressId])
  111. ->willReturn($expectedUrl);
  112. $this->assertEquals($expectedUrl, $this->gridBlock->getAddressEditUrl($addressId));
  113. }
  114. public function testGetAdditionalAddresses()
  115. {
  116. $customerId = 1;
  117. /** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customer */
  118. $customer = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\CustomerInterface::class);
  119. /** @var \PHPUnit_Framework_MockObject_MockObject */
  120. $addressCollection = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Address\Collection::class)
  121. ->disableOriginalConstructor()
  122. ->setMethods(['setOrder', 'setCustomerFilter', 'load', 'getIterator'])
  123. ->getMock();
  124. $addressDataModel = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\AddressInterface::class);
  125. $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
  126. ->disableOriginalConstructor()
  127. ->setMethods(['getId', 'getDataModel'])
  128. ->getMock();
  129. $collection = [$address, $address, $address];
  130. $address->expects($this->exactly(3))->method('getId')
  131. ->willReturnOnConsecutiveCalls(1, 2, 3);
  132. $address->expects($this->atLeastOnce())->method('getDataModel')->willReturn($addressDataModel);
  133. $customer->expects($this->atLeastOnce())->method('getId')->willReturn($customerId);
  134. $customer->expects($this->atLeastOnce())->method('getDefaultBilling')->willReturn('1');
  135. $customer->expects($this->atLeastOnce())->method('getDefaultShipping')->willReturn('2');
  136. $this->currentCustomer->expects($this->atLeastOnce())->method('getCustomer')->willReturn($customer);
  137. $addressCollection->expects($this->atLeastOnce())->method('setOrder')->with('entity_id', 'desc')
  138. ->willReturnSelf();
  139. $addressCollection->expects($this->atLeastOnce())->method('setCustomerFilter')->with([$customerId])
  140. ->willReturnSelf();
  141. $addressCollection->expects($this->atLeastOnce())->method('getIterator')
  142. ->willReturn(new \ArrayIterator($collection));
  143. $this->addressCollectionFactory->expects($this->atLeastOnce())->method('create')
  144. ->willReturn($addressCollection);
  145. $this->assertEquals($addressDataModel, $this->gridBlock->getAdditionalAddresses()[0]);
  146. }
  147. /**
  148. * Test for \Magento\Customer\ViewModel\CustomerAddress::getStreetAddress method
  149. */
  150. public function testGetStreetAddress()
  151. {
  152. $street = ['Line 1', 'Line 2'];
  153. $expectedAddress = 'Line 1, Line 2';
  154. $address = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\AddressInterface::class);
  155. $address->expects($this->atLeastOnce())->method('getStreet')->willReturn($street);
  156. $this->assertEquals($expectedAddress, $this->gridBlock->getStreetAddress($address));
  157. }
  158. /**
  159. * Test for \Magento\Customer\ViewModel\CustomerAddress::getCountryByCode method
  160. */
  161. public function testGetCountryByCode()
  162. {
  163. $countryId = 'US';
  164. $countryName = 'United States';
  165. $country = $this->getMockBuilder(\Magento\Directory\Model\Country::class)
  166. ->disableOriginalConstructor()
  167. ->setMethods(['loadByCode', 'getName'])
  168. ->getMock();
  169. $this->countryFactory->expects($this->atLeastOnce())->method('create')->willReturn($country);
  170. $country->expects($this->atLeastOnce())->method('loadByCode')->with($countryId)->willReturnSelf();
  171. $country->expects($this->atLeastOnce())->method('getName')->willReturn($countryName);
  172. $this->assertEquals($countryName, $this->gridBlock->getCountryByCode($countryId));
  173. }
  174. }