PaymentTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Test\Unit\Model\Quote;
  7. use Magento\Framework\DataObject;
  8. use Magento\Framework\Event\ManagerInterface;
  9. use Magento\Payment\Model\Checks\Composite;
  10. use Magento\Payment\Model\Checks\SpecificationFactory;
  11. use Magento\Payment\Model\MethodInterface;
  12. use Magento\Quote\Api\Data\PaymentInterface;
  13. use Magento\Quote\Model\Quote;
  14. use \Magento\Quote\Model\Quote\Payment;
  15. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  16. class PaymentTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var Payment
  20. */
  21. private $model;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject|SpecificationFactory
  24. */
  25. private $specificationFactory;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject|ManagerInterface
  28. */
  29. private $eventManager;
  30. /**
  31. * @var \Magento\Framework\Serialize\JsonValidator|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $jsonValidatorMock;
  34. protected function setUp()
  35. {
  36. $objectManager = new ObjectManager($this);
  37. $this->specificationFactory = $this->getMockBuilder(
  38. SpecificationFactory::class
  39. )->disableOriginalConstructor()
  40. ->getMock();
  41. $this->eventManager = $this->createMock(ManagerInterface::class);
  42. $serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
  43. ->setMethods(['unserialize'])
  44. ->disableOriginalConstructor()
  45. ->getMockForAbstractClass();
  46. $serializer->expects($this->any())
  47. ->method('unserialize')
  48. ->willReturnCallback(
  49. function ($value) {
  50. return json_decode($value, true);
  51. }
  52. );
  53. $this->jsonValidatorMock = $this->getMockBuilder(\Magento\Framework\Serialize\JsonValidator::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->model = $objectManager->getObject(
  57. Payment::class,
  58. [
  59. 'methodSpecificationFactory' => $this->specificationFactory,
  60. 'eventDispatcher' => $this->eventManager,
  61. 'serializer' => $serializer,
  62. 'jsonValidator' => $this->jsonValidatorMock
  63. ]
  64. );
  65. }
  66. /**
  67. * @param int|string|null $databaseValue
  68. * @param int|string|null $expectedValue
  69. * @dataProvider yearValueDataProvider
  70. */
  71. public function testGetCcExpYearReturnsValidValue($databaseValue, $expectedValue)
  72. {
  73. $this->model->setData('cc_exp_year', $databaseValue);
  74. static::assertEquals($expectedValue, $this->model->getCcExpYear());
  75. }
  76. /**
  77. * @return array
  78. */
  79. public function yearValueDataProvider()
  80. {
  81. return [
  82. [null, null],
  83. [0, null],
  84. ['0', 0],
  85. [1939, 1939],
  86. ];
  87. }
  88. /**
  89. * @param array $data
  90. * @param array $convertedData
  91. * @param array $dataToAssign
  92. * @param array $checks
  93. * @dataProvider importDataPositiveCheckDataProvider
  94. */
  95. public function testImportDataPositiveCheck(
  96. array $data,
  97. array $convertedData,
  98. array $dataToAssign,
  99. array $checks
  100. ) {
  101. $quoteId = 1;
  102. $storeId = 1;
  103. $paymentMethod = $this->createMock(MethodInterface::class);
  104. $quote = $this->getMockBuilder(Quote::class)
  105. ->disableOriginalConstructor()
  106. ->getMock();
  107. $methodSpecification = $this->getMockBuilder(Composite::class)
  108. ->disableOriginalConstructor()
  109. ->getMock();
  110. $quote->expects(static::once())
  111. ->method('getId')
  112. ->willReturn($quoteId);
  113. $this->model->setQuote($quote);
  114. $this->model->setMethodInstance($paymentMethod);
  115. $this->eventManager->expects(static::once())
  116. ->method('dispatch')
  117. ->with(
  118. 'sales_quote_payment_import_data_before',
  119. [
  120. 'payment' => $this->model,
  121. 'input' => new DataObject($convertedData)
  122. ]
  123. );
  124. $quote->expects(static::once())
  125. ->method('getStoreId')
  126. ->willReturn($storeId);
  127. $quote->expects(static::once())
  128. ->method('collectTotals');
  129. $this->specificationFactory->expects(static::once())
  130. ->method('create')
  131. ->with($checks)
  132. ->willReturn($methodSpecification);
  133. $paymentMethod->expects(static::once())
  134. ->method('isAvailable')
  135. ->with($quote)
  136. ->willReturn(true);
  137. $methodSpecification->expects(static::once())
  138. ->method('isApplicable')
  139. ->with($paymentMethod, $quote)
  140. ->willReturn(true);
  141. $paymentMethod->expects(static::once())
  142. ->method('assignData')
  143. ->with(new DataObject($dataToAssign));
  144. $paymentMethod->expects(static::once())
  145. ->method('validate');
  146. $this->model->importData($data);
  147. }
  148. /**
  149. * @param mixed $expected
  150. * @param mixed $additionalData
  151. * @param int $isValidCalledNum
  152. * @param int|null $isValid
  153. * @dataProvider getAdditionalDataDataProvider
  154. */
  155. public function testGetAdditionalData($expected, $additionalData, $isValidCalledNum, $isValid = null)
  156. {
  157. $this->jsonValidatorMock->expects($this->exactly($isValidCalledNum))
  158. ->method('isValid')
  159. ->with($additionalData)
  160. ->willReturn($isValid);
  161. $this->model->setData(Payment::KEY_ADDITIONAL_DATA, $additionalData);
  162. $this->assertSame($expected, $this->model->getAdditionalData());
  163. }
  164. /**
  165. * @return array
  166. */
  167. public function getAdditionalDataDataProvider()
  168. {
  169. return [
  170. [
  171. ['field1' => 'value1', 'field2' => 'value2'],
  172. ['field1' => 'value1', 'field2' => 'value2'],
  173. 0
  174. ],
  175. [
  176. ['field1' => 'value1', 'field2' => 'value2'],
  177. '{"field1":"value1","field2":"value2"}',
  178. 1,
  179. true
  180. ],
  181. [
  182. null,
  183. '{"field1":field2":"value2"}',
  184. 1,
  185. false
  186. ],
  187. [
  188. null,
  189. 123,
  190. 0
  191. ],
  192. ];
  193. }
  194. /**
  195. * @return array
  196. */
  197. public function importDataPositiveCheckDataProvider()
  198. {
  199. return [
  200. [
  201. [
  202. PaymentInterface::KEY_METHOD => 'payment_method_code',
  203. 'cc_number' => '1111',
  204. 'cc_type' => 'VI',
  205. 'cc_owner' => 'John Doe'
  206. ],
  207. [
  208. PaymentInterface::KEY_METHOD => 'payment_method_code',
  209. PaymentInterface::KEY_PO_NUMBER => null,
  210. PaymentInterface::KEY_ADDITIONAL_DATA => [
  211. 'cc_number' => '1111',
  212. 'cc_type' => 'VI',
  213. 'cc_owner' => 'John Doe'
  214. ],
  215. 'checks' => []
  216. ],
  217. [
  218. PaymentInterface::KEY_METHOD => 'payment_method_code',
  219. PaymentInterface::KEY_PO_NUMBER => null,
  220. PaymentInterface::KEY_ADDITIONAL_DATA => [
  221. 'cc_number' => '1111',
  222. 'cc_type' => 'VI',
  223. 'cc_owner' => 'John Doe'
  224. ],
  225. 'checks' => []
  226. ],
  227. []
  228. ],
  229. [
  230. [
  231. PaymentInterface::KEY_METHOD => 'payment_method_code',
  232. 'cc_number' => '1111',
  233. 'cc_type' => 'VI',
  234. 'cc_owner' => 'John Doe',
  235. 'checks' => ['check_code1', 'check_code2']
  236. ],
  237. [
  238. PaymentInterface::KEY_METHOD => 'payment_method_code',
  239. PaymentInterface::KEY_PO_NUMBER => null,
  240. PaymentInterface::KEY_ADDITIONAL_DATA => [
  241. 'cc_number' => '1111',
  242. 'cc_type' => 'VI',
  243. 'cc_owner' => 'John Doe'
  244. ],
  245. 'checks' => ['check_code1', 'check_code2']
  246. ],
  247. [
  248. PaymentInterface::KEY_METHOD => 'payment_method_code',
  249. PaymentInterface::KEY_PO_NUMBER => null,
  250. PaymentInterface::KEY_ADDITIONAL_DATA => [
  251. 'cc_number' => '1111',
  252. 'cc_type' => 'VI',
  253. 'cc_owner' => 'John Doe'
  254. ],
  255. 'checks' => ['check_code1', 'check_code2']
  256. ],
  257. ['check_code1', 'check_code2']
  258. ]
  259. ];
  260. }
  261. }