/tests/Request/Payments/AbstractPaymentResponseTest.php

https://github.com/yandex-money/yandex-checkout-sdk-php · PHP · 409 lines · 305 code · 28 blank · 76 comment · 33 complexity · a0ab6057c63b8cde14e8a3ee53cba0d8 MD5 · raw file

  1. <?php
  2. namespace Tests\YandexCheckout\Request\Payments;
  3. use PHPUnit\Framework\TestCase;
  4. use YandexCheckout\Helpers\Random;
  5. use YandexCheckout\Model\ConfirmationType;
  6. use YandexCheckout\Model\CurrencyCode;
  7. use YandexCheckout\Model\MonetaryAmount;
  8. use YandexCheckout\Model\PaymentMethodType;
  9. use YandexCheckout\Model\PaymentStatus;
  10. use YandexCheckout\Model\ReceiptRegistrationStatus;
  11. use YandexCheckout\Model\Transfer;
  12. use YandexCheckout\Model\TransferStatus;
  13. use YandexCheckout\Request\Payments\PaymentResponse;
  14. abstract class AbstractPaymentResponseTest extends TestCase
  15. {
  16. /**
  17. * @param $options
  18. * @return PaymentResponse
  19. */
  20. abstract protected function getTestInstance($options);
  21. /**
  22. * @dataProvider validDataProvider
  23. * @param array $options
  24. */
  25. public function testGetId($options)
  26. {
  27. $instance = $this->getTestInstance($options);
  28. self::assertEquals($options['id'], $instance->getId());
  29. }
  30. /**
  31. * @dataProvider validDataProvider
  32. * @param array $options
  33. */
  34. public function testGetStatus($options)
  35. {
  36. $instance = $this->getTestInstance($options);
  37. self::assertEquals($options['status'], $instance->getStatus());
  38. }
  39. /**
  40. * @dataProvider validDataProvider
  41. * @param array $options
  42. */
  43. public function testGetRecipient($options)
  44. {
  45. $instance = $this->getTestInstance($options);
  46. if (empty($options['recipient'])) {
  47. self::assertNull($instance->getRecipient());
  48. } else {
  49. if (!empty($options['recipient']['account_id'])) {
  50. self::assertEquals($options['recipient']['account_id'], $instance->getRecipient()->getAccountId());
  51. }
  52. if (!empty($options['recipient']['gateway_id'])) {
  53. self::assertEquals($options['recipient']['gateway_id'], $instance->getRecipient()->getGatewayId());
  54. }
  55. }
  56. }
  57. /**
  58. * @dataProvider validDataProvider
  59. * @param array $options
  60. */
  61. public function testGetAmount($options)
  62. {
  63. $instance = $this->getTestInstance($options);
  64. self::assertEquals(number_format($options['amount']['value'], 2, '.', ''), $instance->getAmount()->getValue());
  65. self::assertEquals($options['amount']['currency'], $instance->getAmount()->getCurrency());
  66. }
  67. /**
  68. * @dataProvider validDataProvider
  69. * @param array $options
  70. */
  71. public function testGetPaymentMethod($options)
  72. {
  73. $instance = $this->getTestInstance($options);
  74. if (empty($options['payment_method'])) {
  75. self::assertNull($instance->getPaymentMethod());
  76. } else {
  77. self::assertEquals($options['payment_method']['type'], $instance->getPaymentMethod()->getType());
  78. }
  79. }
  80. /**
  81. * @dataProvider validDataProvider
  82. * @param array $options
  83. */
  84. public function testGetCreatedAt($options)
  85. {
  86. $instance = $this->getTestInstance($options);
  87. if (empty($options['created_at'])) {
  88. self::assertNull($instance->getCreatedAt());
  89. } else {
  90. self::assertEquals($options['created_at'], $instance->getCreatedAt()->format(DATE_ATOM));
  91. }
  92. }
  93. /**
  94. * @dataProvider validDataProvider
  95. * @param array $options
  96. */
  97. public function testGetCapturedAt($options)
  98. {
  99. $instance = $this->getTestInstance($options);
  100. if (empty($options['captured_at'])) {
  101. self::assertNull($instance->getCapturedAt());
  102. } else {
  103. self::assertEquals($options['captured_at'], $instance->getCapturedAt()->format(DATE_ATOM));
  104. }
  105. }
  106. /**
  107. * @dataProvider validDataProvider
  108. * @param array $options
  109. */
  110. public function testGetConfirmation($options)
  111. {
  112. $instance = $this->getTestInstance($options);
  113. if (empty($options['confirmation'])) {
  114. self::assertNull($instance->getConfirmation());
  115. } else {
  116. self::assertEquals($options['confirmation']['type'], $instance->getConfirmation()->getType());
  117. }
  118. }
  119. /**
  120. * @dataProvider validDataProvider
  121. * @param array $options
  122. */
  123. public function testGetRefundedAmount($options)
  124. {
  125. $instance = $this->getTestInstance($options);
  126. if (empty($options['refunded_amount'])) {
  127. self::assertNull($instance->getRefundedAmount());
  128. } else {
  129. self::assertEquals(number_format($options['refunded_amount']['value'], 2, '.', ''), $instance->getRefundedAmount()->getValue());
  130. self::assertEquals((string)$options['refunded_amount']['currency'], $instance->getRefundedAmount()->getCurrency());
  131. }
  132. }
  133. /**
  134. * @dataProvider validDataProvider
  135. * @param array $options
  136. */
  137. public function testGetPaid($options)
  138. {
  139. $instance = $this->getTestInstance($options);
  140. if (empty($options['paid'])) {
  141. self::assertFalse($instance->getPaid());
  142. } else {
  143. self::assertEquals($options['paid'], $instance->getPaid());
  144. }
  145. }
  146. /**
  147. * @dataProvider validDataProvider
  148. * @param array $options
  149. */
  150. public function testGetRefundable($options)
  151. {
  152. $instance = $this->getTestInstance($options);
  153. if (empty($options['refundable'])) {
  154. self::assertFalse($instance->getRefundable());
  155. } else {
  156. self::assertEquals($options['refundable'], $instance->getRefundable());
  157. }
  158. }
  159. /**
  160. * @dataProvider validDataProvider
  161. * @param array $options
  162. */
  163. public function testGetTest($options)
  164. {
  165. $instance = $this->getTestInstance($options);
  166. if (empty($options['test'])) {
  167. self::assertNull($instance->getTest());
  168. } else {
  169. self::assertEquals($options['test'], $instance->getTest());
  170. }
  171. }
  172. /**
  173. * @dataProvider validDataProvider
  174. * @param array $options
  175. */
  176. public function testGetReceiptRegistration($options)
  177. {
  178. $instance = $this->getTestInstance($options);
  179. if (empty($options['receipt_registration'])) {
  180. self::assertNull($instance->getReceiptRegistration());
  181. } else {
  182. self::assertEquals($options['receipt_registration'], $instance->getReceiptRegistration());
  183. }
  184. }
  185. /**
  186. * @dataProvider validDataProvider
  187. * @param array $options
  188. */
  189. public function testGetMetadata($options)
  190. {
  191. $instance = $this->getTestInstance($options);
  192. if (empty($options['metadata'])) {
  193. self::assertNull($instance->getMetadata());
  194. } else {
  195. self::assertEquals($options['metadata'], $instance->getMetadata()->toArray());
  196. }
  197. }
  198. /**
  199. * @dataProvider validDataProvider
  200. * @param array $options
  201. */
  202. public function testGetTransfers($options)
  203. {
  204. $instance = $this->getTestInstance($options);
  205. if (empty($options['transfers'])) {
  206. self::assertEmpty($instance->getMetadata());
  207. } else {
  208. foreach ($instance->getTransfers() as $transfer) {
  209. self::assertInstanceOf('\YandexCheckout\Model\Transfer', $transfer);
  210. }
  211. }
  212. }
  213. public function validDataProvider()
  214. {
  215. $result = array();
  216. $statuses = PaymentStatus::getValidValues();
  217. $receiptRegistrations = ReceiptRegistrationStatus::getValidValues();
  218. $confirmations = array(
  219. array(
  220. 'type' => ConfirmationType::REDIRECT,
  221. 'confirmation_url' => Random::str(10),
  222. 'return_url' => Random::str(10),
  223. 'enforce' => false,
  224. ),
  225. array(
  226. 'type' => ConfirmationType::EXTERNAL,
  227. ),
  228. );
  229. for ($i = 0; $i < 10; $i++) {
  230. $payment = array(
  231. 'id' => Random::str(36),
  232. 'status' => Random::value($statuses),
  233. 'description' => Random::str(128),
  234. 'recipient' => array(
  235. 'account_id' => Random::str(1, 64, '0123456789'),
  236. 'gateway_id' => Random::str(1, 256),
  237. ),
  238. 'amount' => array(
  239. 'value' => Random::float(0.01, 1000000.0),
  240. 'currency' => Random::value(CurrencyCode::getEnabledValues()),
  241. ),
  242. 'payment_method' => array(
  243. 'type' => Random::value(PaymentMethodType::getEnabledValues()),
  244. ),
  245. 'created_at' => date(DATE_ATOM, Random::int(1, time())),
  246. 'captured_at' => date(DATE_ATOM, Random::int(1, time())),
  247. 'expires_at' => date(DATE_ATOM, Random::int(1, time())),
  248. 'confirmation' => Random::value($confirmations),
  249. 'refunded_amount' => array(
  250. 'value' => Random::float(0.01, 1000000.0),
  251. 'currency' => Random::value(CurrencyCode::getValidValues()),
  252. ),
  253. 'paid' => $i % 2 ? true : false,
  254. 'refundable' => $i % 2 ? true : false,
  255. 'test' => $i % 2 ? true : false,
  256. 'receipt_registration' => Random::value($receiptRegistrations),
  257. 'metadata' => array(
  258. 'value' => Random::float(0.01, 1000000.0),
  259. 'currency' => Random::str(1, 256),
  260. ),
  261. 'requestor' => array(
  262. 'type' => 'RequestorMarchant',
  263. 'account_id' => Random::int(100000, 999999),
  264. ),
  265. 'authorization_details' => array(
  266. 'rrn' => Random::str(10),
  267. 'auth_code' => Random::str(10),
  268. ),
  269. 'transfers' => array(
  270. new Transfer(array(
  271. 'account_id' => Random::str(36),
  272. 'amount' => new MonetaryAmount(Random::int(1, 1000), 'RUB'),
  273. 'platform_fee_amount' => new MonetaryAmount(Random::int(1, 1000), 'RUB'),
  274. 'status' => Random::value(TransferStatus::getValidValues()),
  275. )),
  276. )
  277. );
  278. $result[] = array($payment);
  279. }
  280. $trueFalse = Random::bool();
  281. $result[] = array(
  282. array(
  283. 'id' => Random::str(36),
  284. 'status' => Random::value($statuses),
  285. 'description' => Random::str(128),
  286. 'recipient' => array(
  287. 'account_id' => Random::str(1, 64, '0123456789'),
  288. 'gateway_id' => Random::str(1, 256),
  289. ),
  290. 'amount' => array(
  291. 'value' => Random::float(0.01, 1000000.0),
  292. 'currency' => Random::value(CurrencyCode::getValidValues()),
  293. ),
  294. 'payment_method' => array(
  295. 'type' => PaymentMethodType::WECHAT,
  296. ),
  297. 'created_at' => date(DATE_ATOM, Random::int(1, time())),
  298. 'captured_at' => date(DATE_ATOM, Random::int(1, time())),
  299. 'expires_at' => date(DATE_ATOM, Random::int(1, time())),
  300. 'requestor' => array(
  301. 'type' => 'RequestorThirdPartyService',
  302. 'client_id' => Random::int(100000, 999999),
  303. 'client_name' => Random::str(1, 50),
  304. ),
  305. 'confirmation' => array(
  306. 'type' => 'qr',
  307. 'confirmation_data' => 'weixin://wxpay/bizpayurl?pr=SqTE9cX'
  308. ),
  309. 'paid' => $trueFalse,
  310. 'refundable' => $trueFalse,
  311. 'test' => $trueFalse,
  312. 'metadata' => array(),
  313. 'transfers' => array(
  314. new Transfer(array(
  315. 'account_id' => Random::str(36),
  316. 'amount' => new MonetaryAmount(Random::int(1, 1000), 'RUB'),
  317. 'platform_fee_amount' => new MonetaryAmount(Random::int(1, 1000), 'RUB'),
  318. 'status' => Random::value(TransferStatus::getValidValues()),
  319. )),
  320. )
  321. )
  322. );
  323. return $result;
  324. }
  325. /**
  326. * @dataProvider validDataProvider
  327. * @param array $options
  328. */
  329. public function testGetCancellationDetails($options)
  330. {
  331. $instance = $this->getTestInstance($options);
  332. if (empty($options['cancellation_details'])) {
  333. self::assertNull($instance->getCancellationDetails());
  334. } else {
  335. self::assertEquals(
  336. $options['cancellation_details']['party'],
  337. $instance->getCancellationDetails()->getParty()
  338. );
  339. self::assertEquals(
  340. $options['cancellation_details']['reason'],
  341. $instance->getCancellationDetails()->getReason()
  342. );
  343. }
  344. }
  345. /**
  346. * @dataProvider validDataProvider
  347. * @param array $options
  348. */
  349. public function testGetAuthorizationDetails($options)
  350. {
  351. $instance = $this->getTestInstance($options);
  352. if (empty($options['authorization_details'])) {
  353. self::assertNull($instance->getAuthorizationDetails());
  354. } else {
  355. self::assertEquals(
  356. $options['authorization_details']['rrn'],
  357. $instance->getAuthorizationDetails()->getRrn()
  358. );
  359. self::assertEquals(
  360. $options['authorization_details']['auth_code'],
  361. $instance->getAuthorizationDetails()->getAuthCode()
  362. );
  363. }
  364. }
  365. /**
  366. * @dataProvider validDataProvider
  367. * @param $options
  368. */
  369. public function testGetRequestor($options)
  370. {
  371. $instance = $this->getTestInstance($options);
  372. if ($options['requestor']['type'] === 'RequestorThirdPartyService') {
  373. self::assertEquals($options['requestor']['type'], $instance->getRequestor()->getType());
  374. self::assertEquals($options['requestor']['client_id'], $instance->getRequestor()->getClientId());
  375. self::assertEquals($options['requestor']['client_name'], $instance->getRequestor()->getClientName());
  376. } else {
  377. self::assertEquals($options['requestor']['type'], $instance->getRequestor()->getType());
  378. self::assertEquals($options['requestor']['account_id'], $instance->getRequestor()->getAccountId());
  379. }
  380. }
  381. }