GridTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /**
  9. * Integration tests for the \Magento\Customer\Block\Address\Grid class
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class GridTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\View\LayoutInterface
  16. */
  17. private $layout;
  18. /**
  19. * @var \Magento\Customer\Helper\Session\CurrentCustomer
  20. */
  21. protected $currentCustomer;
  22. protected function setUp()
  23. {
  24. /** @var \PHPUnit_Framework_MockObject_MockObject $blockMock */
  25. $blockMock = $this->getMockBuilder(
  26. \Magento\Framework\View\Element\BlockInterface::class
  27. )->disableOriginalConstructor()->setMethods(
  28. ['setTitle', 'toHtml']
  29. )->getMock();
  30. $blockMock->expects($this->any())->method('setTitle');
  31. $this->currentCustomer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  32. ->get(\Magento\Customer\Helper\Session\CurrentCustomer::class);
  33. $this->layout = Bootstrap::getObjectManager()->get(\Magento\Framework\View\LayoutInterface::class);
  34. $this->layout->setBlock('head', $blockMock);
  35. }
  36. protected function tearDown()
  37. {
  38. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  39. /** @var \Magento\Customer\Model\CustomerRegistry $customerRegistry */
  40. $customerRegistry = $objectManager->get(\Magento\Customer\Model\CustomerRegistry::class);
  41. // Cleanup customer from registry
  42. $customerRegistry->remove(1);
  43. }
  44. /**
  45. * @magentoDataFixture Magento/Customer/_files/customer.php
  46. * @magentoAppIsolation enabled
  47. */
  48. public function testGetAddressEditUrl()
  49. {
  50. $gridBlock = $this->createBlockForCustomer(1);
  51. $this->assertEquals(
  52. 'http://localhost/index.php/customer/address/edit/id/1/',
  53. $gridBlock->getAddressEditUrl(1)
  54. );
  55. }
  56. /**
  57. * @magentoDataFixture Magento/Customer/_files/customer.php
  58. * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
  59. * @magentoAppIsolation enabled
  60. */
  61. public function testGetAdditionalAddresses()
  62. {
  63. $gridBlock = $this->createBlockForCustomer(1);
  64. $this->assertNotNull($gridBlock->getAdditionalAddresses());
  65. $this->assertCount(1, $gridBlock->getAdditionalAddresses());
  66. $this->assertInstanceOf(
  67. \Magento\Customer\Api\Data\AddressInterface::class,
  68. $gridBlock->getAdditionalAddresses()[0]
  69. );
  70. $this->assertEquals(2, $gridBlock->getAdditionalAddresses()[0]->getId());
  71. }
  72. /**
  73. * @magentoDataFixture Magento/Customer/_files/customer_no_address.php
  74. * @dataProvider getAdditionalAddressesDataProvider
  75. * @magentoAppIsolation enabled
  76. */
  77. public function testGetAdditionalAddressesNegative($customerId, $expected)
  78. {
  79. $gridBlock = $this->createBlockForCustomer($customerId);
  80. $this->currentCustomer->setCustomerId($customerId);
  81. $this->assertEquals($expected, $gridBlock->getAdditionalAddresses());
  82. }
  83. public function getAdditionalAddressesDataProvider()
  84. {
  85. return ['5' => [5, []]];
  86. }
  87. /**
  88. * @magentoDataFixture Magento/Customer/_files/customer_no_address.php
  89. * @magentoAppIsolation enabled
  90. */
  91. public function testGetAddressHtmlWithoutAddress()
  92. {
  93. $gridBlock = $this->createBlockForCustomer(5);
  94. $this->assertEquals('', $gridBlock->getAddressHtml(null));
  95. }
  96. /**
  97. * @magentoDataFixture Magento/Customer/_files/customer.php
  98. * @magentoAppIsolation enabled
  99. */
  100. public function testGetCustomer()
  101. {
  102. $gridBlock = $this->createBlockForCustomer(1);
  103. /** @var CustomerRepositoryInterface $customerRepository */
  104. $customerRepository = Bootstrap::getObjectManager()->get(
  105. \Magento\Customer\Api\CustomerRepositoryInterface::class
  106. );
  107. $customer = $customerRepository->getById(1);
  108. $object = $gridBlock->getCustomer();
  109. $this->assertEquals($customer, $object);
  110. }
  111. /**
  112. * Create address book block for customer
  113. *
  114. * @param int $customerId
  115. * @return \Magento\Framework\View\Element\BlockInterface
  116. */
  117. private function createBlockForCustomer($customerId)
  118. {
  119. $this->currentCustomer->setCustomerId($customerId);
  120. return $this->layout->createBlock(
  121. \Magento\Customer\Block\Address\Grid::class,
  122. '',
  123. ['currentCustomer' => $this->currentCustomer]
  124. );
  125. }
  126. }