PlaceOrderTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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\Catalog\Api\Data\ProductCustomOptionInterface;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\Catalog\Model\Product;
  10. use Magento\Customer\Api\CustomerRepositoryInterface;
  11. use Magento\Customer\Model\Customer;
  12. use Magento\Framework\ObjectManagerInterface;
  13. use Magento\Store\Api\StoreRepositoryInterface;
  14. use Magento\Store\Model\Store;
  15. use PHPUnit\Framework\TestCase;
  16. use Magento\TestFramework\Helper\Bootstrap;
  17. /**
  18. * @magentoAppIsolation enabled
  19. * @magentoDbIsolation enabled
  20. */
  21. class PlaceOrderTest extends TestCase
  22. {
  23. /**
  24. * @var ObjectManagerInterface
  25. */
  26. private $objectManager;
  27. public function setUp()
  28. {
  29. $this->objectManager = Bootstrap::getObjectManager();
  30. }
  31. /**
  32. * @magentoDataFixture Magento/Customer/_files/customer.php
  33. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  34. * @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
  35. * @magentoDataFixture Magento/Catalog/_files/product_simple.php
  36. */
  37. public function testPlaceOrderForSimpleProduct()
  38. {
  39. $this->invokeTestProductPlacement(
  40. 'simple',
  41. [],
  42. 'Simple Product Ordered.'
  43. );
  44. }
  45. /**
  46. * @magentoDataFixture Magento/Customer/_files/customer.php
  47. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  48. * @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
  49. * @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
  50. */
  51. public function testPlaceOrderForConfigurableProduct()
  52. {
  53. $configurable = $this->getFixtureProduct('configurable');
  54. $simple = $this->getFixtureProduct('simple_10');
  55. $configurableAttributes = $configurable->getTypeInstance()->getConfigurableAttributes($configurable);
  56. $selectedOptions = [];
  57. foreach ($configurableAttributes as $configurableAttribute) {
  58. $configurableAttributeId = $configurableAttribute->getData('attribute_id');
  59. $configurableAttributeCode = $configurableAttribute->getProductAttribute()->getData('attribute_code');
  60. $configurableAttributeValueInSimple = $simple->getCustomAttribute($configurableAttributeCode)->getValue();
  61. $selectedOptions[$configurableAttributeId] = $configurableAttributeValueInSimple;
  62. }
  63. $this->invokeTestProductPlacement(
  64. $configurable->getSku(),
  65. [
  66. 'super_attribute' => $selectedOptions,
  67. ],
  68. 'Configurable Product Ordered.'
  69. );
  70. }
  71. /**
  72. * @magentoDataFixture Magento/Customer/_files/customer.php
  73. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  74. * @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
  75. * @magentoDataFixture Magento/GroupedProduct/_files/product_grouped.php
  76. */
  77. public function testPlaceOrderForGroupedProduct()
  78. {
  79. $grouped = $this->getFixtureProduct('grouped-product');
  80. $selectedQuantities = [];
  81. foreach ($grouped->getTypeInstance()->getAssociatedProductIds($grouped) as $associatedProductId) {
  82. $selectedQuantities[$associatedProductId] = rand(1, 3);
  83. }
  84. $this->invokeTestProductPlacement(
  85. $grouped->getSku(),
  86. [
  87. 'super_group' => $selectedQuantities,
  88. ],
  89. 'Grouped Product Ordered.'
  90. );
  91. }
  92. /**
  93. * @magentoDataFixture Magento/Customer/_files/customer.php
  94. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  95. * @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
  96. * @magentoDataFixture Magento/Downloadable/_files/product_downloadable.php
  97. */
  98. public function testPlaceOrderForDownloadableProduct()
  99. {
  100. $downloadable = $this->getFixtureProduct('downloadable-product');
  101. $selectedLinks = [];
  102. foreach ($downloadable->getTypeInstance()->getLinks($downloadable) as $link) {
  103. $selectedLinks[] = $link->getId();
  104. }
  105. $this->invokeTestProductPlacement(
  106. $downloadable->getSku(),
  107. [
  108. 'links' => $selectedLinks
  109. ],
  110. 'Downloadable Product Ordered.'
  111. );
  112. }
  113. /**
  114. * @magentoDataFixture Magento/Customer/_files/customer.php
  115. * @magentoDataFixture Magento/Customer/_files/customer_address.php
  116. * @magentoDataFixture Magento/InstantPurchase/_files/fake_payment_token.php
  117. * @magentoDataFixture Magento/Catalog/_files/product_virtual.php
  118. */
  119. public function testPlaceOrderForVirtualProduct()
  120. {
  121. $this->invokeTestProductPlacement(
  122. 'virtual-product',
  123. [],
  124. 'Virtual Product Ordered.'
  125. );
  126. }
  127. /**
  128. * Run system under test.
  129. *
  130. * @param $productSku
  131. * @param array $productRequest
  132. * @param string $expectedResult
  133. * @return int order identifier
  134. */
  135. private function invokeTestProductPlacement($productSku, array $productRequest, string $expectedResult): int
  136. {
  137. /** @var PlaceOrder $model */
  138. $model = $this->objectManager->create(PlaceOrder::class);
  139. $store = $this->getFixtureStore();
  140. $customer = $this->getFixtureCustomer();
  141. $instantPurchaseOption = $this->createInstantPurchaseOptionFromFixture();
  142. $product = $this->getFixtureProduct($productSku);
  143. $orderId = $model->placeOrder(
  144. $store,
  145. $customer,
  146. $instantPurchaseOption,
  147. $product,
  148. array_merge(
  149. [
  150. 'qty' => '1',
  151. 'options' => $this->createProductOptionsRequest($product)
  152. ],
  153. $productRequest
  154. )
  155. );
  156. $this->assertNotEmpty($orderId, $expectedResult);
  157. return $orderId;
  158. }
  159. /**
  160. * Returns Store created by fixture.
  161. *
  162. * @return Store
  163. */
  164. private function getFixtureStore(): Store
  165. {
  166. $repository = $this->objectManager->create(StoreRepositoryInterface::class);
  167. $store = $repository->get('default');
  168. return $store;
  169. }
  170. /**
  171. * Returns Customer created by fixture.
  172. *
  173. * @return Customer
  174. */
  175. private function getFixtureCustomer(): Customer
  176. {
  177. $repository = $this->objectManager->create(CustomerRepositoryInterface::class);
  178. $customerData = $repository->getById(1);
  179. $customer = $this->objectManager->create(Customer::class);
  180. $customer->updateData($customerData);
  181. return $customer;
  182. }
  183. /**
  184. * Returns Product created by fixture.
  185. *
  186. * @param string $sku
  187. * @return Product
  188. */
  189. private function getFixtureProduct(string $sku): Product
  190. {
  191. $repository = $this->objectManager->create(ProductRepositoryInterface::class);
  192. $product = $repository->get($sku, false, $this->getFixtureStore()->getId());
  193. $product->setData('salable', true);
  194. return $product;
  195. }
  196. /**
  197. * Creates instant purchase option based on data from fixture.
  198. *
  199. * @return InstantPurchaseOption
  200. */
  201. private function createInstantPurchaseOptionFromFixture(): InstantPurchaseOption
  202. {
  203. $factory = $this->objectManager->get(InstantPurchaseOptionLoadingFactory::class);
  204. $fixtureCustomer = $this->getFixtureCustomer();
  205. $option = $factory->create(
  206. $fixtureCustomer->getId(),
  207. 'fakePublicHash', // @see Magento/InstantPurchase/_files/fake_payment_token.php
  208. $fixtureCustomer->getDefaultShippingAddress()->getId(),
  209. $fixtureCustomer->getDefaultBillingAddress()->getId(),
  210. 'instant-purchase',
  211. 'cheapest'
  212. );
  213. return $option;
  214. }
  215. /**
  216. * Creates custom options selection product request data.
  217. *
  218. * @param Product $product
  219. * @return array
  220. */
  221. private function createProductOptionsRequest(Product $product): array
  222. {
  223. $options = [];
  224. /** @var Product\Option $option */
  225. foreach ($product->getOptions() as $option) {
  226. switch ($option->getGroupByType()) {
  227. case ProductCustomOptionInterface::OPTION_GROUP_DATE:
  228. $value = [
  229. 'year' => date('Y'),
  230. 'month' => date('n'),
  231. 'day' => date('j'),
  232. 'hour' => date('G'),
  233. 'minute' => date('i')
  234. ];
  235. break;
  236. case ProductCustomOptionInterface::OPTION_GROUP_SELECT:
  237. $value = key($option->getValues());
  238. break;
  239. default:
  240. $value = 'test';
  241. break;
  242. }
  243. $options[$option->getId()] = $value;
  244. }
  245. return $options;
  246. }
  247. }