PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/tests/api-functional/testsuite/Magento/Quote/Api/PaymentMethodManagementTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 332 lines | 233 code | 45 blank | 54 comment | 0 complexity | 9fb40c2bd4159c39d4a8900aa232968d MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Api;
  7. class PaymentMethodManagementTest extends \Magento\TestFramework\TestCase\WebapiAbstract
  8. {
  9. const SERVICE_VERSION = 'V1';
  10. const SERVICE_NAME = 'quotePaymentMethodManagementV1';
  11. const RESOURCE_PATH = '/V1/carts/';
  12. /**
  13. * @var \Magento\TestFramework\ObjectManager
  14. */
  15. protected $objectManager;
  16. protected function setUp()
  17. {
  18. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  19. }
  20. /**
  21. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_payment_saved.php
  22. */
  23. public function testReSetPayment()
  24. {
  25. /** @var \Magento\Quote\Model\Quote $quote */
  26. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  27. $quote->load('test_order_1_with_payment', 'reserved_order_id');
  28. $cartId = $quote->getId();
  29. $serviceInfo = [
  30. 'rest' => [
  31. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method',
  32. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  33. ],
  34. 'soap' => [
  35. 'service' => self::SERVICE_NAME,
  36. 'serviceVersion' => self::SERVICE_VERSION,
  37. 'operation' => self::SERVICE_NAME . 'set',
  38. ],
  39. ];
  40. $requestData = [
  41. "cartId" => $cartId,
  42. "method" => [
  43. 'method' => 'checkmo',
  44. 'po_number' => null
  45. ],
  46. ];
  47. $this->assertNotNull($this->_webApiCall($serviceInfo, $requestData));
  48. }
  49. /**
  50. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php
  51. */
  52. public function testSetPaymentWithVirtualProduct()
  53. {
  54. /** @var \Magento\Quote\Model\Quote $quote */
  55. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  56. $quote->load('test_order_with_virtual_product', 'reserved_order_id');
  57. $cartId = $quote->getId();
  58. $serviceInfo = [
  59. 'rest' => [
  60. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method',
  61. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  62. ],
  63. 'soap' => [
  64. 'service' => self::SERVICE_NAME,
  65. 'serviceVersion' => self::SERVICE_VERSION,
  66. 'operation' => self::SERVICE_NAME . 'set',
  67. ],
  68. ];
  69. $requestData = [
  70. "cartId" => $cartId,
  71. "method" => [
  72. 'method' => 'checkmo',
  73. 'po_number' => '200'
  74. ],
  75. ];
  76. $this->assertNotNull($this->_webApiCall($serviceInfo, $requestData));
  77. }
  78. /**
  79. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  80. */
  81. public function testSetPaymentWithSimpleProduct()
  82. {
  83. /** @var \Magento\Quote\Model\Quote $quote */
  84. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  85. $quote->load('test_order_1', 'reserved_order_id');
  86. $cartId = $quote->getId();
  87. $serviceInfo = [
  88. 'rest' => [
  89. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method',
  90. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  91. ],
  92. 'soap' => [
  93. 'service' => self::SERVICE_NAME,
  94. 'serviceVersion' => self::SERVICE_VERSION,
  95. 'operation' => self::SERVICE_NAME . 'set',
  96. ],
  97. ];
  98. $requestData = [
  99. "cartId" => $cartId,
  100. "method" => [
  101. 'method' => 'checkmo',
  102. 'po_number' => '200'
  103. ],
  104. ];
  105. $this->assertNotNull($this->_webApiCall($serviceInfo, $requestData));
  106. }
  107. /**
  108. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
  109. * @expectedException \Exception
  110. * @expectedExceptionMessage Shipping address is not set
  111. */
  112. public function testSetPaymentWithSimpleProductWithoutAddress()
  113. {
  114. /** @var \Magento\Quote\Model\Quote $quote */
  115. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  116. $quote->load('test_order_with_simple_product_without_address', 'reserved_order_id');
  117. $cartId = $quote->getId();
  118. $serviceInfo = [
  119. 'rest' => [
  120. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method',
  121. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  122. ],
  123. 'soap' => [
  124. 'service' => self::SERVICE_NAME,
  125. 'serviceVersion' => self::SERVICE_VERSION,
  126. 'operation' => self::SERVICE_NAME . 'set',
  127. ],
  128. ];
  129. $requestData = [
  130. "cartId" => $cartId,
  131. "method" => [
  132. 'method' => 'checkmo',
  133. 'po_number' => '200'
  134. ],
  135. ];
  136. $this->assertNotNull($this->_webApiCall($serviceInfo, $requestData));
  137. }
  138. /**
  139. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  140. */
  141. public function testGetList()
  142. {
  143. /** @var \Magento\Quote\Model\Quote $quote */
  144. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  145. $quote->load('test_order_1', 'reserved_order_id');
  146. $cartId = $quote->getId();
  147. $serviceInfo = [
  148. 'rest' => [
  149. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/payment-methods',
  150. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  151. ],
  152. 'soap' => [
  153. 'service' => self::SERVICE_NAME,
  154. 'serviceVersion' => self::SERVICE_VERSION,
  155. 'operation' => self::SERVICE_NAME . 'getList',
  156. ],
  157. ];
  158. $requestData = ["cartId" => $cartId];
  159. $requestResponse = $this->_webApiCall($serviceInfo, $requestData);
  160. $expectedResponse = [
  161. 'code' => 'checkmo',
  162. 'title' => 'Check / Money order',
  163. ];
  164. $this->assertGreaterThan(0, count($requestResponse));
  165. $this->assertContains($expectedResponse, $requestResponse);
  166. }
  167. /**
  168. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_payment_saved.php
  169. */
  170. public function testGet()
  171. {
  172. /** @var \Magento\Quote\Model\Quote $quote */
  173. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  174. $quote->load('test_order_1_with_payment', 'reserved_order_id');
  175. $cartId = $quote->getId();
  176. $serviceInfo = [
  177. 'rest' => [
  178. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/selected-payment-method',
  179. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  180. ],
  181. 'soap' => [
  182. 'service' => self::SERVICE_NAME,
  183. 'serviceVersion' => self::SERVICE_VERSION,
  184. 'operation' => self::SERVICE_NAME . 'get',
  185. ],
  186. ];
  187. $requestData = ["cartId" => $cartId];
  188. $requestResponse = $this->_webApiCall($serviceInfo, $requestData);
  189. foreach ($this->getPaymentMethodFieldsForAssert() as $field) {
  190. $this->assertArrayHasKey($field, $requestResponse);
  191. $this->assertNotNull($requestResponse[$field]);
  192. }
  193. $this->assertEquals('checkmo', $requestResponse['method']);
  194. }
  195. /**
  196. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  197. */
  198. public function testGetListMine()
  199. {
  200. $this->_markTestAsRestOnly();
  201. /** @var \Magento\Quote\Model\Quote $quote */
  202. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  203. $quote->load('test_order_1', 'reserved_order_id');
  204. $serviceInfo = [
  205. 'rest' => [
  206. 'resourcePath' => self::RESOURCE_PATH . 'mine/payment-methods',
  207. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  208. 'token' => $this->getCustomerToken()
  209. ]
  210. ];
  211. $requestResponse = $this->_webApiCall($serviceInfo);
  212. $expectedResponse = [
  213. 'code' => 'checkmo',
  214. 'title' => 'Check / Money order',
  215. ];
  216. $this->assertGreaterThan(0, count($requestResponse));
  217. $this->assertContains($expectedResponse, $requestResponse);
  218. }
  219. /**
  220. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_payment_saved.php
  221. */
  222. public function testGetMine()
  223. {
  224. $this->_markTestAsRestOnly();
  225. /** @var \Magento\Quote\Model\Quote $quote */
  226. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  227. $quote->load('test_order_1_with_payment', 'reserved_order_id');
  228. $serviceInfo = [
  229. 'rest' => [
  230. 'resourcePath' => self::RESOURCE_PATH . 'mine/selected-payment-method',
  231. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  232. 'token' => $this->getCustomerToken()
  233. ]
  234. ];
  235. $requestResponse = $this->_webApiCall($serviceInfo);
  236. foreach ($this->getPaymentMethodFieldsForAssert() as $field) {
  237. $this->assertArrayHasKey($field, $requestResponse);
  238. $this->assertNotNull($requestResponse[$field]);
  239. }
  240. $this->assertEquals('checkmo', $requestResponse['method']);
  241. }
  242. /**
  243. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  244. */
  245. public function testSetPaymentWithSimpleProductMine()
  246. {
  247. $this->_markTestAsRestOnly();
  248. /** @var \Magento\Quote\Model\Quote $quote */
  249. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  250. $quote->load('test_order_1', 'reserved_order_id');
  251. $serviceInfo = [
  252. 'rest' => [
  253. 'resourcePath' => self::RESOURCE_PATH . 'mine/selected-payment-method',
  254. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  255. 'token' => $this->getCustomerToken()
  256. ]
  257. ];
  258. $requestData = [
  259. "method" => [
  260. 'method' => 'checkmo',
  261. 'po_number' => '200'
  262. ],
  263. ];
  264. $this->assertNotNull($this->_webApiCall($serviceInfo, $requestData));
  265. }
  266. /**
  267. * @return array
  268. */
  269. protected function getPaymentMethodFieldsForAssert()
  270. {
  271. return ['method', 'po_number', 'additional_data'];
  272. }
  273. /**
  274. * Get customer ID token
  275. *
  276. * @return string
  277. */
  278. protected function getCustomerToken()
  279. {
  280. /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
  281. $customerTokenService = $this->objectManager->create(
  282. \Magento\Integration\Api\CustomerTokenServiceInterface::class
  283. );
  284. $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
  285. return $token;
  286. }
  287. }