CreditCardTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Test\Unit;
  3. require_once dirname(__DIR__) . '/Setup.php';
  4. use DateTime;
  5. use Test\Setup;
  6. use Braintree;
  7. class CreditCardTest extends Setup
  8. {
  9. public function testGet_givesErrorIfInvalidProperty()
  10. {
  11. $this->setExpectedException('PHPUnit_Framework_Error', 'Undefined property on Braintree\CreditCard: foo');
  12. $cc = Braintree\CreditCard::factory([]);
  13. $cc->foo;
  14. }
  15. public function testCreate_throwsIfInvalidKey()
  16. {
  17. $this->setExpectedException('InvalidArgumentException', 'invalid keys: invalidKey');
  18. Braintree\CreditCard::create(['invalidKey' => 'foo']);
  19. }
  20. public function testIsDefault()
  21. {
  22. $creditCard = Braintree\CreditCard::factory(['default' => true]);
  23. $this->assertTrue($creditCard->isDefault());
  24. $creditCard = Braintree\CreditCard::factory(['default' => false]);
  25. $this->assertFalse($creditCard->isDefault());
  26. }
  27. public function testMaskedNumber()
  28. {
  29. $creditCard = Braintree\CreditCard::factory(['bin' => '123456', 'last4' => '7890']);
  30. $this->assertEquals('123456******7890', $creditCard->maskedNumber);
  31. }
  32. public function testCreateSignature()
  33. {
  34. $expected = [
  35. 'billingAddressId', 'cardholderName', 'cvv', 'number', 'deviceSessionId',
  36. 'expirationDate', 'expirationMonth', 'expirationYear', 'token', 'venmoSdkPaymentMethodCode',
  37. 'deviceData', 'fraudMerchantId', 'paymentMethodNonce',
  38. ['options' => ['makeDefault', 'verificationMerchantAccountId', 'verifyCard', 'verificationAmount', 'venmoSdkSession', 'failOnDuplicatePaymentMethod']],
  39. [
  40. 'billingAddress' => [
  41. 'firstName',
  42. 'lastName',
  43. 'company',
  44. 'countryCodeAlpha2',
  45. 'countryCodeAlpha3',
  46. 'countryCodeNumeric',
  47. 'countryName',
  48. 'extendedAddress',
  49. 'locality',
  50. 'region',
  51. 'postalCode',
  52. 'streetAddress',
  53. ],
  54. ],
  55. 'customerId'
  56. ];
  57. $this->assertEquals($expected, Braintree\CreditCardGateway::createSignature());
  58. }
  59. public function testUpdateSignature()
  60. {
  61. $expected = [
  62. 'billingAddressId', 'cardholderName', 'cvv', 'number', 'deviceSessionId',
  63. 'expirationDate', 'expirationMonth', 'expirationYear', 'token', 'venmoSdkPaymentMethodCode',
  64. 'deviceData', 'fraudMerchantId', 'paymentMethodNonce',
  65. [
  66. 'options' => [
  67. 'makeDefault',
  68. 'verificationMerchantAccountId',
  69. 'verifyCard',
  70. 'verificationAmount',
  71. 'venmoSdkSession',
  72. 'failOnDuplicatePaymentMethod',
  73. ]
  74. ],
  75. [
  76. 'billingAddress' => [
  77. 'firstName',
  78. 'lastName',
  79. 'company',
  80. 'countryCodeAlpha2',
  81. 'countryCodeAlpha3',
  82. 'countryCodeNumeric',
  83. 'countryName',
  84. 'extendedAddress',
  85. 'locality',
  86. 'region',
  87. 'postalCode',
  88. 'streetAddress',
  89. [
  90. 'options' => [
  91. 'updateExisting'
  92. ]
  93. ]
  94. ],
  95. ],
  96. ];
  97. $this->assertEquals($expected, Braintree\CreditCardGateway::updateSignature());
  98. }
  99. public function testErrorsOnFindWithBlankArgument()
  100. {
  101. $this->setExpectedException('InvalidArgumentException');
  102. Braintree\CreditCard::find('');
  103. }
  104. public function testErrorsOnFindWithWhitespaceArgument()
  105. {
  106. $this->setExpectedException('InvalidArgumentException');
  107. Braintree\CreditCard::find(' ');
  108. }
  109. public function testErrorsOnFindWithWhitespaceCharacterArgument()
  110. {
  111. $this->setExpectedException('InvalidArgumentException');
  112. Braintree\CreditCard::find('\t');
  113. }
  114. public function testVerificationIsLatestVerification()
  115. {
  116. $creditCard = Braintree\CreditCard::factory(
  117. [
  118. 'verifications' => [
  119. [
  120. 'id' => '123',
  121. 'createdAt' => DateTime::createFromFormat('Ymd', '20121212')
  122. ],
  123. [
  124. 'id' => '932',
  125. 'createdAt' => DateTime::createFromFormat('Ymd', '20121215')
  126. ],
  127. [
  128. 'id' => '456',
  129. 'createdAt' => DateTime::createFromFormat('Ymd', '20121213')
  130. ]
  131. ]
  132. ]
  133. );
  134. $this->assertEquals('932', $creditCard->verification->id);
  135. }
  136. }