UtilTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace Test\Unit;
  3. require_once dirname(__DIR__) . '/Setup.php';
  4. use stdClass;
  5. use DateTime;
  6. use Test\Setup;
  7. use Braintree;
  8. class UtilTest extends Setup
  9. {
  10. /**
  11. * @expectedException Braintree\Exception\Authentication
  12. */
  13. public function testThrow401Exception()
  14. {
  15. Braintree\Util::throwStatusCodeException(401);
  16. }
  17. /**
  18. * @expectedException Braintree\Exception\Authorization
  19. */
  20. public function testThrow403Exception()
  21. {
  22. Braintree\Util::throwStatusCodeException(403);
  23. }
  24. /**
  25. * @expectedException Braintree\Exception\NotFound
  26. */
  27. public function testThrow404Exception()
  28. {
  29. Braintree\Util::throwStatusCodeException(404);
  30. }
  31. /**
  32. * @expectedException Braintree\Exception\UpgradeRequired
  33. */
  34. public function testThrow426Exception()
  35. {
  36. Braintree\Util::throwStatusCodeException(426);
  37. }
  38. /**
  39. * @expectedException Braintree\Exception\TooManyRequests
  40. */
  41. public function testThrow429Exception()
  42. {
  43. Braintree\Util::throwStatusCodeException(429);
  44. }
  45. /**
  46. * @expectedException Braintree\Exception\ServerError
  47. */
  48. public function testThrow500Exception()
  49. {
  50. Braintree\Util::throwStatusCodeException(500);
  51. }
  52. /**
  53. * @expectedException Braintree\Exception\DownForMaintenance
  54. */
  55. public function testThrow503Exception()
  56. {
  57. Braintree\Util::throwStatusCodeException(503);
  58. }
  59. /**
  60. * @expectedException Braintree\Exception\Unexpected
  61. */
  62. public function testThrowUnknownException()
  63. {
  64. Braintree\Util::throwStatusCodeException(999);
  65. }
  66. public function testExtractAttributeAsArrayReturnsEmptyArray()
  67. {
  68. $attributes = [];
  69. $this->assertEquals([], Braintree\Util::extractAttributeAsArray($attributes, "foo"));
  70. }
  71. public function testExtractAttributeAsArrayReturnsSingleElementArray()
  72. {
  73. $attributes = ['verification' => 'val1'];
  74. $this->assertEquals(['val1'], Braintree\Util::extractAttributeAsArray($attributes, "verification"));
  75. }
  76. public function testExtractAttributeAsArrayReturnsArrayOfObjects()
  77. {
  78. $attributes = ['verification' => [['status' => 'val1']]];
  79. $expected = new Braintree\CreditCardVerification(['status' => 'val1']);
  80. $this->assertEquals([$expected], Braintree\Util::extractAttributeAsArray($attributes, "verification"));
  81. }
  82. public function testDelimeterToUnderscore()
  83. {
  84. $this->assertEquals("a_b_c", Braintree\Util::delimiterToUnderscore("a-b-c"));
  85. }
  86. public function testCleanClassName()
  87. {
  88. $cn = Braintree\Util::cleanClassName('Braintree\Transaction');
  89. $this->assertEquals('transaction', $cn);
  90. }
  91. public function testBuildClassName()
  92. {
  93. $cn = Braintree\Util::buildClassName('creditCard');
  94. $this->assertEquals('Braintree\CreditCard', $cn);
  95. }
  96. public function testimplodeAssociativeArray()
  97. {
  98. $array = [
  99. 'test1' => 'val1',
  100. 'test2' => 'val2',
  101. 'test3' => new DateTime('2015-05-15 17:21:00'),
  102. ];
  103. $string = Braintree\Util::implodeAssociativeArray($array);
  104. $this->assertEquals('test1=val1, test2=val2, test3=Fri, 15 May 2015 17:21:00 +0000', $string);
  105. }
  106. public function testVerifyKeys_withThreeLevels()
  107. {
  108. $signature = [
  109. 'firstName',
  110. ['creditCard' => ['number', ['billingAddress' => ['streetAddress']]]]
  111. ];
  112. $data = [
  113. 'firstName' => 'Dan',
  114. 'creditCard' => [
  115. 'number' => '5100',
  116. 'billingAddress' => [
  117. 'streetAddress' => '1 E Main St'
  118. ]
  119. ]
  120. ];
  121. Braintree\Util::verifyKeys($signature, $data);
  122. }
  123. public function testVerifyKeys_withArrayOfArrays()
  124. {
  125. $signature = [
  126. ['addOns' => [['update' => ['amount', 'existingId']]]]
  127. ];
  128. $goodData = [
  129. 'addOns' => [
  130. 'update' => [
  131. [
  132. 'amount' => '50.00',
  133. 'existingId' => 'increase_10',
  134. ],
  135. [
  136. 'amount' => '60.00',
  137. 'existingId' => 'increase_20',
  138. ]
  139. ]
  140. ]
  141. ];
  142. Braintree\Util::verifyKeys($signature, $goodData);
  143. $badData = [
  144. 'addOns' => [
  145. 'update' => [
  146. [
  147. 'invalid' => '50.00',
  148. ]
  149. ]
  150. ]
  151. ];
  152. $this->setExpectedException('InvalidArgumentException');
  153. Braintree\Util::verifyKeys($signature, $badData);
  154. }
  155. public function testVerifyKeys_arrayAsValue()
  156. {
  157. $signature = ['key'];
  158. $data = ['key' => ['value']];
  159. $this->setExpectedException('InvalidArgumentException');
  160. Braintree\Util::verifyKeys($signature, $data);
  161. }
  162. public function testVerifyKeys()
  163. {
  164. $signature = [
  165. 'amount', 'customerId', 'orderId', 'channel', 'paymentMethodToken', 'type',
  166. ['creditCard' =>
  167. ['token', 'cvv', 'expirationDate', 'number'],
  168. ],
  169. ['customer' =>
  170. [
  171. 'id', 'company', 'email', 'fax', 'firstName',
  172. 'lastName', 'phone', 'website'],
  173. ],
  174. ['billing' =>
  175. [
  176. 'firstName', 'lastName', 'company', 'countryName',
  177. 'extendedAddress', 'locality', 'postalCode', 'region',
  178. 'streetAddress'],
  179. ],
  180. ['shipping' =>
  181. [
  182. 'firstName', 'lastName', 'company', 'countryName',
  183. 'extendedAddress', 'locality', 'postalCode', 'region',
  184. 'streetAddress'],
  185. ],
  186. ['options' =>
  187. [
  188. 'storeInVault', 'submitForSettlement',
  189. 'addBillingAddressToPaymentMethod'],
  190. ],
  191. ['customFields' => ['_anyKey_']
  192. ],
  193. ];
  194. // test valid
  195. $userKeys = [
  196. 'amount' => '100.00',
  197. 'customFields' => ['HEY' => 'HO',
  198. 'WAY' => 'NO'],
  199. 'creditCard' => [
  200. 'number' => '5105105105105100',
  201. 'expirationDate' => '05/12',
  202. ],
  203. ];
  204. $n = Braintree\Util::verifyKeys($signature, $userKeys);
  205. $this->assertNull($n);
  206. $userKeys = [
  207. 'amount' => '100.00',
  208. 'customFields' => ['HEY' => 'HO',
  209. 'WAY' => 'NO'],
  210. 'bogus' => 'FAKE',
  211. 'totallyFake' => 'boom',
  212. 'creditCard' => [
  213. 'number' => '5105105105105100',
  214. 'expirationDate' => '05/12',
  215. ],
  216. ];
  217. // test invalid
  218. $this->setExpectedException('InvalidArgumentException');
  219. Braintree\Util::verifyKeys($signature, $userKeys);
  220. }
  221. /**
  222. * @expectedException Braintree\Exception\ValidationsFailed
  223. */
  224. public function testReturnException()
  225. {
  226. $this->success = false;
  227. Braintree\Util::returnObjectOrThrowException('Braintree\Transaction', $this);
  228. }
  229. public function testReturnObject()
  230. {
  231. $this->success = true;
  232. $this->transaction = new stdClass();
  233. $t = Braintree\Util::returnObjectOrThrowException('Braintree\Transaction', $this);
  234. $this->assertInternalType('object', $t);
  235. }
  236. }