InstantPurchaseTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\InstantPurchase\Model;
  7. use Magento\Customer\Api\CustomerRepositoryInterface;
  8. use Magento\Customer\Model\Address;
  9. use Magento\Customer\Model\Customer;
  10. use Magento\Framework\ObjectManagerInterface;
  11. use Magento\Quote\Api\Data\ShippingMethodInterface;
  12. use Magento\Store\Api\StoreRepositoryInterface;
  13. use Magento\Store\Model\Store;
  14. use Magento\TestFramework\Helper\Bootstrap;
  15. use Magento\Vault\Api\Data\PaymentTokenInterface;
  16. use PHPUnit\Framework\TestCase;
  17. /**
  18. * @magentoAppIsolation enabled
  19. */
  20. class InstantPurchaseTest extends TestCase
  21. {
  22. /**
  23. * @var ObjectManagerInterface
  24. */
  25. private $objectManager;
  26. public function setUp()
  27. {
  28. $this->objectManager = Bootstrap::getObjectManager();
  29. }
  30. /**
  31. * @magentoDataFixture Magento/Customer/_files/customer.php
  32. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  33. * @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
  34. */
  35. public function testAvailableWhenEverythingSetUp()
  36. {
  37. $option = $this->invokeTestInstantPurchaseOptionCalculation();
  38. $this->assertTrue($option->isAvailable());
  39. $this->assertInstanceOf(PaymentTokenInterface::class, $option->getPaymentToken());
  40. $this->assertInstanceOf(Address::class, $option->getShippingAddress());
  41. $this->assertInstanceOf(Address::class, $option->getBillingAddress());
  42. $this->assertInstanceOf(ShippingMethodInterface::class, $option->getShippingMethod());
  43. }
  44. /**
  45. * @magentoDataFixture Magento/Customer/_files/customer.php
  46. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  47. */
  48. public function testNotAvailableWithoutPaymentToken()
  49. {
  50. $option = $this->invokeTestInstantPurchaseOptionCalculation();
  51. $this->assertFalse($option->isAvailable());
  52. }
  53. /**
  54. * @magentoDataFixture Magento/Customer/_files/customer.php
  55. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  56. * @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
  57. * @magentoConfigFixture current_store payment/fake_vault/active 0
  58. */
  59. public function testNotAvailableWhenVaultNotActive()
  60. {
  61. $option = $this->invokeTestInstantPurchaseOptionCalculation();
  62. $this->assertFalse($option->isAvailable());
  63. }
  64. /**
  65. * @magentoDataFixture Magento/Customer/_files/customer.php
  66. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  67. * @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
  68. * @magentoConfigFixture current_store payment/fake/active 0
  69. */
  70. public function testNotAvailableWhenVaultProviderNotActive()
  71. {
  72. $option = $this->invokeTestInstantPurchaseOptionCalculation();
  73. $this->assertFalse($option->isAvailable());
  74. }
  75. /**
  76. * @magentoDataFixture Magento/Customer/_files/customer.php
  77. * @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
  78. */
  79. public function testNotAvailableWithoutAddresses()
  80. {
  81. $option = $this->invokeTestInstantPurchaseOptionCalculation();
  82. $this->assertFalse($option->isAvailable());
  83. }
  84. /**
  85. * @magentoDataFixture Magento/Customer/_files/customer.php
  86. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  87. * @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
  88. * @magentoConfigFixture current_store carriers/flatrate/active 0
  89. */
  90. public function testNotAvailableWhenShippingMethodsDisabled()
  91. {
  92. $option = $this->invokeTestInstantPurchaseOptionCalculation();
  93. $this->assertFalse($option->isAvailable());
  94. }
  95. /**
  96. * @magentoDataFixture Magento/Customer/_files/customer.php
  97. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  98. * @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
  99. * @magentoConfigFixture current_store sales/instant_purchase/active 0
  100. */
  101. public function testNotAvailableWhenDisabledInConfig()
  102. {
  103. $option = $this->invokeTestInstantPurchaseOptionCalculation();
  104. $this->assertFalse($option->isAvailable());
  105. }
  106. /**
  107. * @magentoDataFixture Magento/Customer/_files/customer.php
  108. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  109. * @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
  110. * @magentoConfigFixture current_store payment/fake_vault/instant_purchase/supported 0
  111. */
  112. public function testNotAvailableWhenSupportSwitchedOffForVault()
  113. {
  114. $option = $this->invokeTestInstantPurchaseOptionCalculation();
  115. $this->assertFalse($option->isAvailable());
  116. }
  117. /**
  118. * Run system under test
  119. *
  120. * @return InstantPurchaseOption
  121. */
  122. private function invokeTestInstantPurchaseOptionCalculation(): InstantPurchaseOption
  123. {
  124. /** @var InstantPurchaseInterface $instantPurchase */
  125. $instantPurchase = $this->objectManager->create(InstantPurchaseInterface::class);
  126. $store = $this->getFixtureStore();
  127. $customer = $this->getFixtureCustomer();
  128. $option = $instantPurchase->getOption($store, $customer);
  129. return $option;
  130. }
  131. /**
  132. * Returns Store created by fixture.
  133. *
  134. * @return Store
  135. */
  136. private function getFixtureStore(): Store
  137. {
  138. $repository = $this->objectManager->create(StoreRepositoryInterface::class);
  139. $store = $repository->get('default');
  140. return $store;
  141. }
  142. /**
  143. * Returns Customer created by fixture.
  144. *
  145. * @return Customer
  146. */
  147. private function getFixtureCustomer(): Customer
  148. {
  149. $repository = $this->objectManager->create(CustomerRepositoryInterface::class);
  150. $customerData = $repository->getById(1);
  151. $customer = $this->objectManager->create(Customer::class);
  152. $customer->updateData($customerData);
  153. return $customer;
  154. }
  155. }