PaymentMethodTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Test\Unit;
  3. require_once dirname(__DIR__) . '/Setup.php';
  4. use Test\Setup;
  5. use Braintree;
  6. class PaymentMethodTest extends Setup
  7. {
  8. public function testCreate_throwsIfInvalidKey()
  9. {
  10. $this->setExpectedException('InvalidArgumentException', 'invalid keys: invalidKey');
  11. Braintree\PaymentMethod::create(['invalidKey' => 'foo']);
  12. }
  13. public function testCreateSignature()
  14. {
  15. $expected = [
  16. 'billingAddressId',
  17. 'cardholderName',
  18. 'cvv',
  19. 'deviceData',
  20. 'expirationDate',
  21. 'expirationMonth',
  22. 'expirationYear',
  23. 'number',
  24. 'paymentMethodNonce',
  25. 'token',
  26. ['options' => [
  27. 'failOnDuplicatePaymentMethod',
  28. 'makeDefault',
  29. 'verificationMerchantAccountId',
  30. 'verifyCard',
  31. 'verificationAmount',
  32. 'usBankAccountVerificationMethod',
  33. ['paypal' => [
  34. 'payee_email',
  35. 'payeeEmail',
  36. 'order_id',
  37. 'orderId',
  38. 'custom_field',
  39. 'customField',
  40. 'description',
  41. 'amount',
  42. ['shipping' =>
  43. [
  44. 'firstName', 'lastName', 'company', 'countryName',
  45. 'countryCodeAlpha2', 'countryCodeAlpha3', 'countryCodeNumeric',
  46. 'extendedAddress', 'locality', 'postalCode', 'region',
  47. 'streetAddress'],
  48. ],
  49. ]],
  50. ]],
  51. ['billingAddress' => Braintree\AddressGateway::createSignature()],
  52. 'customerId',
  53. 'paypalRefreshToken',
  54. 'paypalVaultWithoutUpgrade'
  55. ];
  56. $this->assertEquals($expected, Braintree\PaymentMethodGateway::createSignature());
  57. }
  58. public function testErrorsOnFindWithBlankArgument()
  59. {
  60. $this->setExpectedException('InvalidArgumentException');
  61. Braintree\PaymentMethod::find('');
  62. }
  63. public function testErrorsOnFindWithWhitespaceArgument()
  64. {
  65. $this->setExpectedException('InvalidArgumentException');
  66. Braintree\PaymentMethod::find(' ');
  67. }
  68. public function testErrorsOnFindWithWhitespaceCharacterArgument()
  69. {
  70. $this->setExpectedException('InvalidArgumentException');
  71. Braintree\PaymentMethod::find('\t');
  72. }
  73. public function testDeleteWithRevokeAllGrantsAsTrue()
  74. {
  75. $paymentMethodGateway = $this->mockPaymentMethodGatewayDoDelete();
  76. $expectedURL = "/payment_methods/any/some_token?revoke_all_grants=1";
  77. $paymentMethodGateway->expects($this->once())->method('_doDelete')->with($this->equalTo($expectedURL));
  78. $paymentMethodGateway->delete("some_token", ['revokeAllGrants' => true]);
  79. }
  80. public function testDeleteWithRevokeAllGrantsAsFalse()
  81. {
  82. $paymentMethodGateway = $this->mockPaymentMethodGatewayDoDelete();
  83. $expectedURL = "/payment_methods/any/some_token?revoke_all_grants=0";
  84. $paymentMethodGateway->expects($this->once())->method('_doDelete')->with($this->equalTo($expectedURL));
  85. $paymentMethodGateway->delete("some_token", ['revokeAllGrants' => false]);
  86. }
  87. public function testDeleteWithoutRevokeAllGrantsOption()
  88. {
  89. $paymentMethodGateway = $this->mockPaymentMethodGatewayDoDelete();
  90. $expectedURL = "/payment_methods/any/some_token";
  91. $paymentMethodGateway->expects($this->once())->method('_doDelete')->with($this->equalTo($expectedURL));
  92. $paymentMethodGateway->delete("some_token");
  93. }
  94. public function testDeleteWithInvalidOption()
  95. {
  96. $paymentMethodGateway = $this->mockPaymentMethodGatewayDoDelete();
  97. $this->setExpectedException('InvalidArgumentException');
  98. $paymentMethodGateway->expects($this->never())->method('_doDelete');
  99. $paymentMethodGateway->delete("some_token", ['invalidKey' => false]);
  100. }
  101. private function mockPaymentMethodGatewayDoDelete()
  102. {
  103. return $this->getMockBuilder('Braintree\PaymentMethodGateway')
  104. ->setConstructorArgs(array(Braintree\Configuration::gateway()))
  105. ->setMethods(array('_doDelete'))
  106. ->getMock();
  107. }
  108. }