BillingTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Block\Express\Review;
  7. use Magento\Customer\Model\Context;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. /**
  10. * Class BillingTest
  11. */
  12. class BillingTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \Magento\Paypal\Block\Express\Review\Billing */
  15. protected $_block;
  16. /** @var \Magento\Customer\Api\AddressRepositoryInterface */
  17. protected $_addressRepository;
  18. /** @var \Magento\Quote\Model\Quote\AddressFactory */
  19. protected $_quoteAddressFactory;
  20. /** @var \Magento\Customer\Api\CustomerRepositoryInterface */
  21. protected $_customerRepository;
  22. const FIXTURE_CUSTOMER_ID = 1;
  23. const FIXTURE_ADDRESS_ID = 1;
  24. const SAMPLE_FIRST_NAME = 'UpdatedFirstName';
  25. const SAMPLE_LAST_NAME = 'UpdatedLastName';
  26. protected function setUp()
  27. {
  28. parent::setUp();
  29. $objectManager = Bootstrap::getObjectManager();
  30. $this->_customerRepository = $objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  31. $customer = $this->_customerRepository->getById(self::FIXTURE_CUSTOMER_ID);
  32. $customerSession = $objectManager->get(\Magento\Customer\Model\Session::class);
  33. $customerSession->setCustomerData($customer);
  34. $this->_addressRepository = $objectManager->get(\Magento\Customer\Api\AddressRepositoryInterface::class);
  35. //fetch sample address
  36. $address = $this->_addressRepository->getById(self::FIXTURE_ADDRESS_ID);
  37. /** @var \Magento\Quote\Model\ResourceModel\Quote\Collection $quoteCollection */
  38. $quoteCollection = $objectManager->get(\Magento\Quote\Model\ResourceModel\Quote\Collection::class);
  39. /** @var $quote \Magento\Quote\Model\Quote */
  40. $quote = $quoteCollection->getLastItem();
  41. $quote->setCustomer($customer);
  42. /** @var $quoteAddressFactory \Magento\Quote\Model\Quote\AddressFactory */
  43. $this->_quoteAddressFactory = $objectManager->get(\Magento\Quote\Model\Quote\AddressFactory::class);
  44. $billingAddress = $this->_quoteAddressFactory->create()->importCustomerAddressData($address);
  45. $quote->setBillingAddress($billingAddress);
  46. $quote->save();
  47. /** @var \Magento\Checkout\Model\Session $checkoutSession */
  48. $checkoutSession = $objectManager->get(\Magento\Checkout\Model\Session::class);
  49. $checkoutSession->setQuoteId($quote->getId());
  50. $checkoutSession->setLoadInactive(true);
  51. $objectManager->get(\Magento\Framework\App\Http\Context::class)
  52. ->setValue(Context::CONTEXT_AUTH, true, false);
  53. $this->_block = $objectManager->get(\Magento\Framework\View\LayoutInterface::class)
  54. ->createBlock(
  55. \Magento\Paypal\Block\Express\Review\Billing::class,
  56. '',
  57. ['customerSession' => $customerSession, 'resourceSession' => $checkoutSession]
  58. );
  59. }
  60. /**
  61. * @magentoDataFixture Magento/Customer/_files/customer.php
  62. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  63. * @magentoDataFixture Magento/Checkout/_files/quote_with_product_and_payment.php
  64. */
  65. public function testGetAddress()
  66. {
  67. $addressFromFixture = $this->_addressRepository->getById(self::FIXTURE_ADDRESS_ID);
  68. $address = $this->_block->getAddress();
  69. $this->assertEquals($addressFromFixture->getFirstname(), $address->getFirstname());
  70. $this->assertEquals($addressFromFixture->getLastname(), $address->getLastname());
  71. $this->assertEquals($addressFromFixture->getCustomerId(), $address->getCustomerId());
  72. }
  73. /**
  74. * @magentoDataFixture Magento/Customer/_files/customer.php
  75. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  76. * @magentoDataFixture Magento/Checkout/_files/quote_with_product_and_payment.php
  77. */
  78. public function testGetAddressNotSetInQuote()
  79. {
  80. $this->_updateQuoteCustomerName();
  81. $address = $this->_block->getAddress();
  82. //Make sure the data from sample address was set correctly to the block from customer
  83. $this->assertEquals(self::SAMPLE_FIRST_NAME, $address->getFirstname());
  84. $this->assertEquals(self::SAMPLE_LAST_NAME, $address->getLastname());
  85. }
  86. /**
  87. * @magentoDataFixture Magento/Customer/_files/customer.php
  88. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  89. * @magentoDataFixture Magento/Checkout/_files/quote_with_product_and_payment.php
  90. */
  91. public function testGetFirstNameAndLastName()
  92. {
  93. $this->_updateQuoteCustomerName();
  94. //Make sure the data from sample address was set correctly to the block from customer
  95. $this->assertEquals(self::SAMPLE_FIRST_NAME, $this->_block->getFirstname());
  96. $this->assertEquals(self::SAMPLE_LAST_NAME, $this->_block->getLastname());
  97. }
  98. /**
  99. * Update Customer name in Quote
  100. */
  101. protected function _updateQuoteCustomerName()
  102. {
  103. /** @var $emptyAddress \Magento\Quote\Model\Quote\Address */
  104. $emptyAddress = $this->_quoteAddressFactory->create();
  105. $emptyAddress->setFirstname(null);
  106. $emptyAddress->setLastname(null);
  107. $this->_block->getQuote()->setBillingAddress($emptyAddress);
  108. $customer = $this->_customerRepository->getById(self::FIXTURE_CUSTOMER_ID);
  109. $customer->setFirstname(
  110. self::SAMPLE_FIRST_NAME
  111. )->setLastname(
  112. self::SAMPLE_LAST_NAME
  113. );
  114. $this->_block->getQuote()->setCustomer($customer);
  115. $this->_block->getQuote()->save();
  116. $this->assertEquals(self::SAMPLE_FIRST_NAME, $this->_block->getFirstname());
  117. $this->assertEquals(self::SAMPLE_LAST_NAME, $this->_block->getLastname());
  118. }
  119. }