ConfigurationTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. namespace Test\Unit;
  3. require_once dirname(__DIR__) . '/Setup.php';
  4. use Test\Setup;
  5. use Braintree;
  6. class ConfigurationTest extends Setup
  7. {
  8. public function setup()
  9. {
  10. Braintree\Configuration::reset();
  11. $this->config = new Braintree\Configuration();
  12. }
  13. public function teardown()
  14. {
  15. Braintree\Configuration::environment('development');
  16. Braintree\Configuration::merchantId('integration_merchant_id');
  17. Braintree\Configuration::publicKey('integration_public_key');
  18. Braintree\Configuration::privateKey('integration_private_key');
  19. }
  20. public function testAssertGlobalHasAccessTokenOrKeys()
  21. {
  22. Braintree\Configuration::environment('development');
  23. Braintree\Configuration::merchantId('integration_merchant_id');
  24. Braintree\Configuration::publicKey('integration_public_key');
  25. Braintree\Configuration::privateKey('integration_private_key');
  26. try {
  27. Braintree\Configuration::assertGlobalHasAccessTokenOrKeys();
  28. } catch (Exception $notExpected) {
  29. $this->fail();
  30. }
  31. $this->assertTrue(TRUE);
  32. }
  33. /**
  34. * @expectedException Braintree\Exception\Configuration
  35. * @expectedExceptionMessage Configuration::publicKey needs to be set
  36. */
  37. public function testAssertGlobalHasAccessTokenOrKeysWithoutPublicKey()
  38. {
  39. Braintree\Configuration::environment('development');
  40. Braintree\Configuration::merchantId('integration_merchant_id');
  41. Braintree\Configuration::publicKey('');
  42. Braintree\Configuration::privateKey('integration_private_key');
  43. Braintree\Configuration::assertGlobalHasAccessTokenOrKeys();
  44. }
  45. public function testConstructWithArrayOfCredentials()
  46. {
  47. $config = new Braintree\Configuration([
  48. 'environment' => 'sandbox',
  49. 'merchantId' => 'sandbox_merchant_id',
  50. 'publicKey' => 'sandbox_public_key',
  51. 'privateKey' => 'sandbox_private_key',
  52. 'timeout' => 120,
  53. 'acceptGzipEncoding' => false,
  54. ]);
  55. $this->assertEquals('sandbox', $config->getEnvironment());
  56. $this->assertEquals('sandbox_merchant_id', $config->getMerchantId());
  57. $this->assertEquals(120, $config->getTimeout());
  58. $this->assertFalse($config->getAcceptGzipEncoding());
  59. }
  60. public function testSetValidEnvironment()
  61. {
  62. Braintree\Configuration::environment('sandbox');
  63. $this->assertEquals('sandbox', Braintree\Configuration::environment());
  64. Braintree\Configuration::reset();
  65. }
  66. /**
  67. * @expectedException Braintree\Exception\Configuration
  68. * @expectedExceptionMessage "invalid" is not a valid environment.
  69. */
  70. public function testSetInvalidEnvironment()
  71. {
  72. Braintree\Configuration::environment('invalid');
  73. Braintree\Configuration::reset();
  74. }
  75. public function testMerchantPath()
  76. {
  77. $this->config->setMerchantId('abc123');
  78. $mp = $this->config->merchantPath();
  79. $this->assertEquals('/merchants/abc123', $mp);
  80. }
  81. public function testCaFile()
  82. {
  83. $this->config->setEnvironment('development');
  84. $this->setExpectedException('Braintree\Exception\SSLCaFileNotFound');
  85. $this->config->caFile('/does/not/exist/');
  86. }
  87. public function testSSLOn()
  88. {
  89. $this->config->setEnvironment('development');
  90. $on = $this->config->sslOn();
  91. $this->assertFalse($on);
  92. $this->config->setEnvironment('sandbox');
  93. $on = $this->config->sslOn();
  94. $this->assertTrue($on);
  95. $this->config->setEnvironment('production');
  96. $on = $this->config->sslOn();
  97. $this->assertTrue($on);
  98. }
  99. public function testPortNumber()
  100. {
  101. $this->config->setEnvironment('development');
  102. $pn = $this->config->portNumber();
  103. $this->assertEquals(getenv("GATEWAY_PORT") ? getenv("GATEWAY_PORT") : 3000, $pn);
  104. $this->config->setEnvironment('sandbox');
  105. $pn = $this->config->portNumber();
  106. $this->assertEquals(443, $pn);
  107. $this->config->setEnvironment('production');
  108. $pn = $this->config->portNumber();
  109. $this->assertEquals(443, $pn);
  110. }
  111. public function testProtocol()
  112. {
  113. $this->config->setEnvironment('development');
  114. $p = $this->config->protocol();
  115. $this->assertEquals('http', $p);
  116. $this->config->setEnvironment('sandbox');
  117. $p = $this->config->protocol();
  118. $this->assertEquals('https', $p);
  119. $this->config->setEnvironment('production');
  120. $p = $this->config->protocol();
  121. $this->assertEquals('https', $p);
  122. }
  123. public function testServerName()
  124. {
  125. $this->config->setEnvironment('development');
  126. $sn = $this->config->serverName();
  127. $this->assertEquals('localhost', $sn);
  128. $this->config->setEnvironment('sandbox');
  129. $sn = $this->config->serverName();
  130. $this->assertEquals('api.sandbox.braintreegateway.com', $sn);
  131. $this->config->setEnvironment('production');
  132. $sn = $this->config->serverName();
  133. $this->assertEquals('api.braintreegateway.com', $sn);
  134. }
  135. public function testAuthUrl()
  136. {
  137. $this->config->setEnvironment('development');
  138. $authUrl = $this->config->authUrl();
  139. $this->assertEquals('http://auth.venmo.dev:9292', $authUrl);
  140. $this->config->setEnvironment('qa');
  141. $authUrl = $this->config->authUrl();
  142. $this->assertEquals('https://auth.qa.venmo.com', $authUrl);
  143. $this->config->setEnvironment('sandbox');
  144. $authUrl = $this->config->authUrl();
  145. $this->assertEquals('https://auth.sandbox.venmo.com', $authUrl);
  146. $this->config->setEnvironment('production');
  147. $authUrl = $this->config->authUrl();
  148. $this->assertEquals('https://auth.venmo.com', $authUrl);
  149. }
  150. public function testBaseUrl()
  151. {
  152. $this->config->setEnvironment('development');
  153. $bu = $this->config->baseUrl();
  154. $this->assertEquals('http://localhost:' . $this->config->portNumber(), $bu);
  155. $this->config->setEnvironment('qa');
  156. $bu = $this->config->baseUrl();
  157. $this->assertEquals('https://gateway.qa.braintreepayments.com:443', $bu);
  158. $this->config->setEnvironment('sandbox');
  159. $bu = $this->config->baseUrl();
  160. $this->assertEquals('https://api.sandbox.braintreegateway.com:443', $bu);
  161. $this->config->setEnvironment('production');
  162. $bu = $this->config->baseUrl();
  163. $this->assertEquals('https://api.braintreegateway.com:443', $bu);
  164. }
  165. function testProxyHost()
  166. {
  167. $this->config->proxyHost('example.com');
  168. $this->assertEquals('example.com', $this->config->proxyHost());
  169. }
  170. function testProxyPort()
  171. {
  172. $this->config->proxyPort('1234');
  173. $this->assertEquals('1234', $this->config->proxyPort());
  174. }
  175. function testProxyType()
  176. {
  177. $this->config->proxyType('MY_PROXY');
  178. $this->assertEquals('MY_PROXY', $this->config->proxyType());
  179. }
  180. function testProxyIsConfigured()
  181. {
  182. $this->config->proxyHost('example.com');
  183. $this->config->proxyPort('1234');
  184. $this->assertTrue($this->config->isUsingProxy());
  185. }
  186. function testProxyUser()
  187. {
  188. $this->config->proxyUser('user');
  189. $this->assertEquals('user', $this->config->proxyUser());
  190. }
  191. function testProxyPassword()
  192. {
  193. $this->config->proxyPassword('password');
  194. $this->assertEquals('password', $this->config->proxyPassword());
  195. }
  196. function testIsAuthenticatedProxy()
  197. {
  198. $this->config->proxyUser('username');
  199. $this->config->proxyPassword('password');
  200. $this->assertTrue($this->config->isAuthenticatedProxy());
  201. }
  202. function testTimeout()
  203. {
  204. $this->config->timeout(30);
  205. $this->assertEquals(30, $this->config->timeout());
  206. }
  207. function testTimeoutDefaultsToSixty()
  208. {
  209. $this->assertEquals(60, $this->config->timeout());
  210. }
  211. function testSslVersion()
  212. {
  213. $this->config->sslVersion(6);
  214. $this->assertEquals(6, $this->config->sslVersion());
  215. }
  216. function testSslVersionDefaultsToNull()
  217. {
  218. $this->assertEquals(null, $this->config->sslVersion());
  219. }
  220. public function testAcceptEncodingDefaultsTrue()
  221. {
  222. $this->assertTrue($this->config->acceptGzipEncoding());
  223. }
  224. public function testAcceptGzipEncoding()
  225. {
  226. $this->assertTrue($this->config->acceptGzipEncoding());
  227. $this->config->acceptGzipEncoding(false);
  228. $this->assertFalse($this->config->acceptGzipEncoding());
  229. }
  230. /**
  231. * @expectedException Braintree\Exception\Configuration
  232. * @expectedExceptionMessage environment needs to be set
  233. */
  234. public function testValidateAbsentEnvironment()
  235. {
  236. //Braintree\Configuration::environment('development');
  237. Braintree\Configuration::merchantId('integration_merchant_id');
  238. Braintree\Configuration::publicKey('integration_public_key');
  239. Braintree\Configuration::privateKey('integration_private_key');
  240. Braintree\Configuration::$global->assertHasAccessTokenOrKeys();
  241. }
  242. /**
  243. * @expectedException Braintree\Exception\Configuration
  244. * @expectedExceptionMessage environment needs to be set
  245. */
  246. public function testValidateEmptyStringEnvironment()
  247. {
  248. Braintree\Configuration::environment('');
  249. Braintree\Configuration::merchantId('integration_merchant_id');
  250. Braintree\Configuration::publicKey('integration_public_key');
  251. Braintree\Configuration::privateKey('integration_private_key');
  252. Braintree\Configuration::$global->assertHasAccessTokenOrKeys();
  253. }
  254. /**
  255. * @expectedException Braintree\Exception\Configuration
  256. * @expectedExceptionMessage merchantId needs to be set
  257. */
  258. public function testAbsentMerchantId()
  259. {
  260. Braintree\Configuration::environment('development');
  261. //Braintree\Configuration::merchantId('integration_merchant_id');
  262. Braintree\Configuration::publicKey('integration_public_key');
  263. Braintree\Configuration::privateKey('integration_private_key');
  264. Braintree\Configuration::$global->assertHasAccessTokenOrKeys();
  265. }
  266. /**
  267. * @expectedException Braintree\Exception\Configuration
  268. * @expectedExceptionMessage merchantId needs to be set
  269. */
  270. public function testEmptyStringMerchantId()
  271. {
  272. Braintree\Configuration::environment('development');
  273. Braintree\Configuration::merchantId('');
  274. Braintree\Configuration::publicKey('integration_public_key');
  275. Braintree\Configuration::privateKey('integration_private_key');
  276. Braintree\Configuration::$global->assertHasAccessTokenOrKeys();
  277. }
  278. /**
  279. * @expectedException Braintree\Exception\Configuration
  280. * @expectedExceptionMessage publicKey needs to be set
  281. */
  282. public function testAbsentPublicKey()
  283. {
  284. Braintree\Configuration::environment('development');
  285. Braintree\Configuration::merchantId('integration_merchant_id');
  286. //Braintree\Configuration::publicKey('integration_public_key');
  287. Braintree\Configuration::privateKey('integration_private_key');
  288. Braintree\Configuration::$global->assertHasAccessTokenOrKeys();
  289. }
  290. /**
  291. * @expectedException Braintree\Exception\Configuration
  292. * @expectedExceptionMessage publicKey needs to be set
  293. */
  294. public function testEmptyStringPublicKey()
  295. {
  296. Braintree\Configuration::environment('development');
  297. Braintree\Configuration::merchantId('integration_merchant_id');
  298. Braintree\Configuration::publicKey('');
  299. Braintree\Configuration::privateKey('integration_private_key');
  300. Braintree\Configuration::$global->assertHasAccessTokenOrKeys();
  301. }
  302. /**
  303. * @expectedException Braintree\Exception\Configuration
  304. * @expectedExceptionMessage privateKey needs to be set
  305. */
  306. public function testAbsentPrivateKey()
  307. {
  308. Braintree\Configuration::environment('development');
  309. Braintree\Configuration::merchantId('integration_merchant_id');
  310. Braintree\Configuration::publicKey('integration_public_key');
  311. //Braintree\Configuration::privateKey('integration_private_key');
  312. Braintree\Configuration::$global->assertHasAccessTokenOrKeys();
  313. }
  314. /**
  315. * @expectedException Braintree\Exception\Configuration
  316. * @expectedExceptionMessage privateKey needs to be set
  317. */
  318. public function testEmptyStringPrivateKey()
  319. {
  320. Braintree\Configuration::environment('development');
  321. Braintree\Configuration::merchantId('integration_merchant_id');
  322. Braintree\Configuration::publicKey('integration_public_key');
  323. Braintree\Configuration::privateKey('');
  324. Braintree\Configuration::$global->assertHasAccessTokenOrKeys();
  325. }
  326. public function testValidWithOAuthClientCredentials()
  327. {
  328. $config = new Braintree\Configuration([
  329. 'clientId' => 'client_id$development$integration_client_id',
  330. 'clientSecret' => 'client_secret$development$integration_client_secret'
  331. ]);
  332. $config->assertHasClientCredentials();
  333. }
  334. /**
  335. * @expectedException Braintree\Exception\Configuration
  336. * @expectedExceptionMessage clientSecret needs to be passed
  337. */
  338. public function testInvalidWithOAuthClientCredentials()
  339. {
  340. $config = new Braintree\Configuration([
  341. 'clientId' => 'client_id$development$integration_client_id'
  342. ]);
  343. $config->assertHasClientCredentials();
  344. }
  345. public function testDetectEnvironmentFromClientId()
  346. {
  347. $config = new Braintree\Configuration([
  348. 'clientId' => 'client_id$development$integration_client_id',
  349. 'clientSecret' => 'client_secret$development$integration_client_secret'
  350. ]);
  351. $this->assertEquals('development', $config->getEnvironment());
  352. }
  353. /**
  354. * @expectedException Braintree\Exception\Configuration
  355. * @expectedExceptionMessage Mismatched credential environments: clientId environment is sandbox and clientSecret environment is development
  356. */
  357. public function testDetectEnvironmentFromClientIdFail()
  358. {
  359. $config = new Braintree\Configuration([
  360. 'clientId' => 'client_id$sandbox$integration_client_id',
  361. 'clientSecret' => 'client_secret$development$integration_client_secret'
  362. ]);
  363. }
  364. /**
  365. * @expectedException Braintree\Exception\Configuration
  366. * @expectedExceptionMessage Value passed for clientId is not a clientId
  367. */
  368. public function testClientIdTypeFail()
  369. {
  370. $config = new Braintree\Configuration([
  371. 'clientId' => 'client_secret$development$integration_client_id',
  372. 'clientSecret' => 'client_secret$development$integration_client_secret'
  373. ]);
  374. }
  375. public function testValidWithAccessToken()
  376. {
  377. $config = new Braintree\Configuration([
  378. 'accessToken' => 'access_token$development$integration_merchant_id$integration_access_token',
  379. ]);
  380. $config->assertHasAccessTokenOrKeys();
  381. }
  382. /**
  383. * @expectedException Braintree\Exception\Configuration
  384. * @expectedExceptionMessage Value passed for accessToken is not an accessToken
  385. */
  386. public function testInvalidAccessTokenType()
  387. {
  388. $config = new Braintree\Configuration([
  389. 'accessToken' => 'client_id$development$integration_merchant_id$integration_access_token',
  390. ]);
  391. }
  392. /**
  393. * @expectedException Braintree\Exception\Configuration
  394. * @expectedExceptionMessage Incorrect accessToken syntax. Expected: type$environment$merchant_id$token
  395. */
  396. public function testInvalidAccessTokenSyntax()
  397. {
  398. $config = new Braintree\Configuration([
  399. 'accessToken' => 'client_id$development$integration_client_id',
  400. ]);
  401. }
  402. /**
  403. * @expectedException Braintree\Exception\Configuration
  404. * @expectedExceptionMessage "invalid" is not a valid environment.
  405. */
  406. public function testInvalidAccessTokenEnvironment()
  407. {
  408. $config = new Braintree\Configuration([
  409. 'accessToken' => 'access_token$invalid$integration_merchant_id$integration_access_token',
  410. ]);
  411. }
  412. public function testValidWithOAuthClientCredentialsAndAccessToken()
  413. {
  414. $config = new Braintree\Configuration([
  415. 'clientId' => 'client_id$development$integration_client_id',
  416. 'clientSecret' => 'client_secret$development$integration_client_secret',
  417. 'accessToken' => 'access_token$development$integration_merchant_id$integration_access_token',
  418. ]);
  419. $config->assertHasClientCredentials();
  420. $config->assertHasAccessTokenOrKeys();
  421. }
  422. /**
  423. * @expectedException Braintree\Exception\Configuration
  424. * @expectedExceptionMessage Mismatched credential environments: clientId environment is development and accessToken environment is sandbox
  425. */
  426. public function testInvalidEnvironmentWithOAuthClientCredentialsAndAccessToken()
  427. {
  428. $config = new Braintree\Configuration([
  429. 'clientId' => 'client_id$development$integration_client_id',
  430. 'clientSecret' => 'client_secret$development$integration_client_secret',
  431. 'accessToken' => 'access_token$sandbox$integration_merchant_id$integration_access_token',
  432. ]);
  433. }
  434. /**
  435. * @expectedException Braintree\Exception\Configuration
  436. * @expectedExceptionMessage Cannot mix OAuth credentials (clientId, clientSecret, accessToken) with key credentials (publicKey, privateKey, environment, merchantId)
  437. */
  438. public function testCannotMixKeysWithOAuthCredentials()
  439. {
  440. $config = new Braintree\Configuration([
  441. 'clientId' => 'client_id$development$integration_client_id',
  442. 'clientSecret' => 'client_secret$development$integration_client_secret',
  443. 'environment' => 'development',
  444. 'merchantId' => 'integration_merchant_id',
  445. 'publicKey' => 'integration_public_key',
  446. 'privateKey' => 'integration_private_key'
  447. ]);
  448. }
  449. }