DashboardTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Account;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. class DashboardTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var Dashboard */
  11. private $block;
  12. /** @var \Magento\Customer\Model\Session */
  13. private $customerSession;
  14. /** @var \Magento\Customer\Api\CustomerRepositoryInterface */
  15. private $customerRepository;
  16. /**
  17. * Execute per test initialization.
  18. */
  19. public function setUp()
  20. {
  21. $this->customerSession = Bootstrap::getObjectManager()->get(\Magento\Customer\Model\Session::class);
  22. $this->customerRepository = Bootstrap::getObjectManager()->get(
  23. \Magento\Customer\Api\CustomerRepositoryInterface::class
  24. );
  25. $this->block = Bootstrap::getObjectManager()->get(
  26. \Magento\Framework\View\LayoutInterface::class
  27. )->createBlock(
  28. \Magento\Customer\Block\Account\Dashboard::class,
  29. '',
  30. [
  31. 'customerSession' => $this->customerSession,
  32. 'customerRepository' => $this->customerRepository
  33. ]
  34. );
  35. }
  36. /**
  37. * Execute per test cleanup.
  38. */
  39. public function tearDown()
  40. {
  41. $this->customerSession->setCustomerId(null);
  42. /** @var \Magento\Customer\Model\CustomerRegistry $customerRegistry */
  43. $customerRegistry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  44. ->get(\Magento\Customer\Model\CustomerRegistry::class);
  45. //Cleanup customer from registry
  46. $customerRegistry->remove(1);
  47. }
  48. /**
  49. * Verify that the Dashboard::getCustomer() method returns a valid Customer Data.
  50. *
  51. * @magentoDataFixture Magento/Customer/_files/customer.php
  52. */
  53. public function testGetCustomer()
  54. {
  55. $customer = $this->customerRepository->getById(1);
  56. $this->customerSession->setCustomerId(1);
  57. $object = $this->block->getCustomer();
  58. $this->assertEquals($customer, $object);
  59. $this->assertInstanceOf(\Magento\Customer\Api\Data\CustomerInterface::class, $object);
  60. }
  61. /**
  62. * Verify that the specified customer has neither a default billing no shipping address.
  63. *
  64. * @magentoDataFixture Magento/Customer/_files/customer_no_address.php
  65. */
  66. public function testGetPrimaryAddressesNoAddresses()
  67. {
  68. $this->customerSession->setCustomerId(5);
  69. $this->assertFalse($this->block->getPrimaryAddresses());
  70. }
  71. /**
  72. * Verify that the specified customer has the same default billing and shipping address.
  73. *
  74. * @magentoDataFixture Magento/Customer/_files/customer.php
  75. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  76. */
  77. public function testGetPrimaryAddressesBillingShippingSame()
  78. {
  79. $customer = $this->customerRepository->getById(1);
  80. $this->customerSession->setCustomerId(1);
  81. $addresses = $this->block->getPrimaryAddresses();
  82. $this->assertCount(1, $addresses);
  83. $address = $addresses[0];
  84. $this->assertInstanceOf(\Magento\Customer\Api\Data\AddressInterface::class, $address);
  85. $this->assertEquals((int)$customer->getDefaultBilling(), $address->getId());
  86. $this->assertEquals((int)$customer->getDefaultShipping(), $address->getId());
  87. }
  88. /**
  89. * Verify that the specified customer has different default billing and shipping addresses.
  90. *
  91. * @magentoDataFixture Magento/Customer/_files/customer.php
  92. * @magentoDataFixture Magento/Customer/_files/customer_primary_addresses.php
  93. */
  94. public function testGetPrimaryAddressesBillingShippingDifferent()
  95. {
  96. $this->customerSession->setCustomerId(1);
  97. $addresses = $this->block->getPrimaryAddresses();
  98. $this->assertCount(2, $addresses);
  99. $this->assertNotEquals($addresses[0], $addresses[1]);
  100. $this->assertTrue($addresses[0]->isDefaultBilling());
  101. $this->assertTrue($addresses[1]->isDefaultShipping());
  102. }
  103. }