BookTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Address;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. class BookTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Customer\Block\Address\Book
  12. */
  13. protected $_block;
  14. /**
  15. * @var \Magento\Customer\Helper\Session\CurrentCustomer
  16. */
  17. protected $currentCustomer;
  18. protected function setUp()
  19. {
  20. /** @var \PHPUnit_Framework_MockObject_MockObject $blockMock */
  21. $blockMock = $this->getMockBuilder(
  22. \Magento\Framework\View\Element\BlockInterface::class
  23. )->disableOriginalConstructor()->setMethods(
  24. ['setTitle', 'toHtml']
  25. )->getMock();
  26. $blockMock->expects($this->any())->method('setTitle');
  27. $this->currentCustomer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  28. ->get(\Magento\Customer\Helper\Session\CurrentCustomer::class);
  29. /** @var \Magento\Framework\View\LayoutInterface $layout */
  30. $layout = Bootstrap::getObjectManager()->get(\Magento\Framework\View\LayoutInterface::class);
  31. $layout->setBlock('head', $blockMock);
  32. $this->_block = $layout
  33. ->createBlock(
  34. \Magento\Customer\Block\Address\Book::class,
  35. '',
  36. ['currentCustomer' => $this->currentCustomer]
  37. );
  38. }
  39. protected function tearDown()
  40. {
  41. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  42. /** @var \Magento\Customer\Model\CustomerRegistry $customerRegistry */
  43. $customerRegistry = $objectManager->get(\Magento\Customer\Model\CustomerRegistry::class);
  44. // Cleanup customer from registry
  45. $customerRegistry->remove(1);
  46. }
  47. public function testGetAddressEditUrl()
  48. {
  49. $this->assertEquals(
  50. 'http://localhost/index.php/customer/address/edit/id/1/',
  51. $this->_block->getAddressEditUrl(1)
  52. );
  53. }
  54. /**
  55. * @magentoDataFixture Magento/Customer/_files/customer.php
  56. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  57. * @magentoDataFixture Magento/Customer/_files/customer_no_address.php
  58. * @dataProvider hasPrimaryAddressDataProvider
  59. * @magentoAppIsolation enabled
  60. */
  61. public function testHasPrimaryAddress($customerId, $expected)
  62. {
  63. if (!empty($customerId)) {
  64. $this->currentCustomer->setCustomerId($customerId);
  65. }
  66. $this->assertEquals($expected, $this->_block->hasPrimaryAddress());
  67. }
  68. public function hasPrimaryAddressDataProvider()
  69. {
  70. return ['0' => [0, false], '1' => [1, true], '5' => [5, false]];
  71. }
  72. /**
  73. * @magentoDataFixture Magento/Customer/_files/customer.php
  74. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  75. * @magentoAppIsolation enabled
  76. */
  77. public function testGetAdditionalAddresses()
  78. {
  79. $this->currentCustomer->setCustomerId(1);
  80. $this->assertNotNull($this->_block->getAdditionalAddresses());
  81. $this->assertCount(1, $this->_block->getAdditionalAddresses());
  82. $this->assertInstanceOf(
  83. \Magento\Customer\Api\Data\AddressInterface::class,
  84. $this->_block->getAdditionalAddresses()[0]
  85. );
  86. $this->assertEquals(2, $this->_block->getAdditionalAddresses()[0]->getId());
  87. }
  88. /**
  89. * @magentoDataFixture Magento/Customer/_files/customer_no_address.php
  90. * @dataProvider getAdditionalAddressesDataProvider
  91. * @magentoAppIsolation enabled
  92. */
  93. public function testGetAdditionalAddressesNegative($customerId, $expected)
  94. {
  95. if (!empty($customerId)) {
  96. $this->currentCustomer->setCustomerId($customerId);
  97. }
  98. $this->assertEquals($expected, $this->_block->getAdditionalAddresses());
  99. }
  100. public function getAdditionalAddressesDataProvider()
  101. {
  102. return ['0' => [0, false], '5' => [5, false]];
  103. }
  104. /**
  105. * @magentoDataFixture Magento/Customer/_files/customer.php
  106. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  107. * @magentoAppIsolation enabled
  108. */
  109. public function testGetAddressHtml()
  110. {
  111. $expected = "John Smith<br />\nCompanyName<br />\nGreen str, 67<br />\n\n\n\nCityM, Alabama, 75477<br />" .
  112. "\nUnited States<br />\nT: <a href=\"tel:3468676\">3468676</a>\n\n";
  113. $address = Bootstrap::getObjectManager()->get(
  114. \Magento\Customer\Api\AddressRepositoryInterface::class
  115. )->getById(1);
  116. $html = $this->_block->getAddressHtml($address);
  117. $this->assertEquals($expected, $html);
  118. }
  119. public function testGetAddressHtmlWithoutAddress()
  120. {
  121. $this->assertEquals('', $this->_block->getAddressHtml(null));
  122. }
  123. /**
  124. * @magentoDataFixture Magento/Customer/_files/customer.php
  125. * @magentoAppIsolation enabled
  126. */
  127. public function testGetCustomer()
  128. {
  129. /** @var CustomerRepositoryInterface $customerRepository */
  130. $customerRepository = Bootstrap::getObjectManager()->get(
  131. \Magento\Customer\Api\CustomerRepositoryInterface::class
  132. );
  133. $customer = $customerRepository->getById(1);
  134. $this->currentCustomer->setCustomerId(1);
  135. $object = $this->_block->getCustomer();
  136. $this->assertEquals($customer, $object);
  137. }
  138. public function testGetCustomerMissingCustomer()
  139. {
  140. $this->assertNull($this->_block->getCustomer());
  141. }
  142. /**
  143. * @magentoDataFixture Magento/Customer/_files/customer.php
  144. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  145. * @magentoDataFixture Magento/Customer/_files/customer_no_address.php
  146. * @dataProvider getDefaultBillingDataProvider
  147. * @magentoAppIsolation enabled
  148. */
  149. public function testGetDefaultBilling($customerId, $expected)
  150. {
  151. $this->currentCustomer->setCustomerId($customerId);
  152. $this->assertEquals($expected, $this->_block->getDefaultBilling());
  153. }
  154. public function getDefaultBillingDataProvider()
  155. {
  156. return ['0' => [0, null], '1' => [1, 1], '5' => [5, null]];
  157. }
  158. /**
  159. * @magentoDataFixture Magento/Customer/_files/customer.php
  160. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  161. * @magentoDataFixture Magento/Customer/_files/customer_no_address.php
  162. * @dataProvider getDefaultShippingDataProvider
  163. * @magentoAppIsolation enabled
  164. */
  165. public function testGetDefaultShipping($customerId, $expected)
  166. {
  167. if (!empty($customerId)) {
  168. $this->currentCustomer->setCustomerId($customerId);
  169. }
  170. $this->assertEquals($expected, $this->_block->getDefaultShipping());
  171. }
  172. public function getDefaultShippingDataProvider()
  173. {
  174. return ['0' => [0, null], '1' => [1, 1], '5' => [5, null]];
  175. }
  176. /**
  177. * @magentoDataFixture Magento/Customer/_files/customer.php
  178. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  179. */
  180. public function testGetAddressById()
  181. {
  182. $this->assertInstanceOf(\Magento\Customer\Api\Data\AddressInterface::class, $this->_block->getAddressById(1));
  183. $this->assertNull($this->_block->getAddressById(5));
  184. }
  185. }