/wp-content/plugins/woocommerce-gateway-paypal-powered-by-braintree/braintree_sdk/tests/unit/ConfigurationTest.php

https://gitlab.com/hunt9310/ras · PHP · 417 lines · 293 code · 64 blank · 60 comment · 0 complexity · 285c2b5523c5864499c7b86eeb27e435 MD5 · raw file

  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. ]);
  53. $this->assertEquals('sandbox', $config->getEnvironment());
  54. $this->assertEquals('sandbox_merchant_id', $config->getMerchantId());
  55. }
  56. public function testSetValidEnvironment()
  57. {
  58. Braintree\Configuration::environment('sandbox');
  59. $this->assertEquals('sandbox', Braintree\Configuration::environment());
  60. Braintree\Configuration::reset();
  61. }
  62. /**
  63. * @expectedException Braintree\Exception\Configuration
  64. * @expectedExceptionMessage "invalid" is not a valid environment.
  65. */
  66. public function testSetInvalidEnvironment()
  67. {
  68. Braintree\Configuration::environment('invalid');
  69. Braintree\Configuration::reset();
  70. }
  71. public function testMerchantPath()
  72. {
  73. $this->config->setMerchantId('abc123');
  74. $mp = $this->config->merchantPath();
  75. $this->assertEquals('/merchants/abc123', $mp);
  76. }
  77. public function testCaFile()
  78. {
  79. $this->config->setEnvironment('development');
  80. $this->setExpectedException('Braintree\Exception\SSLCaFileNotFound');
  81. $this->config->caFile('/does/not/exist/');
  82. }
  83. public function testSSLOn()
  84. {
  85. $this->config->setEnvironment('development');
  86. $on = $this->config->sslOn();
  87. $this->assertFalse($on);
  88. $this->config->setEnvironment('sandbox');
  89. $on = $this->config->sslOn();
  90. $this->assertTrue($on);
  91. $this->config->setEnvironment('production');
  92. $on = $this->config->sslOn();
  93. $this->assertTrue($on);
  94. }
  95. public function testPortNumber()
  96. {
  97. $this->config->setEnvironment('development');
  98. $pn = $this->config->portNumber();
  99. $this->assertEquals(getenv("GATEWAY_PORT") ? getenv("GATEWAY_PORT") : 3000, $pn);
  100. $this->config->setEnvironment('sandbox');
  101. $pn = $this->config->portNumber();
  102. $this->assertEquals(443, $pn);
  103. $this->config->setEnvironment('production');
  104. $pn = $this->config->portNumber();
  105. $this->assertEquals(443, $pn);
  106. }
  107. public function testProtocol()
  108. {
  109. $this->config->setEnvironment('development');
  110. $p = $this->config->protocol();
  111. $this->assertEquals('http', $p);
  112. $this->config->setEnvironment('sandbox');
  113. $p = $this->config->protocol();
  114. $this->assertEquals('https', $p);
  115. $this->config->setEnvironment('production');
  116. $p = $this->config->protocol();
  117. $this->assertEquals('https', $p);
  118. }
  119. public function testServerName()
  120. {
  121. $this->config->setEnvironment('development');
  122. $sn = $this->config->serverName();
  123. $this->assertEquals('localhost', $sn);
  124. $this->config->setEnvironment('sandbox');
  125. $sn = $this->config->serverName();
  126. $this->assertEquals('api.sandbox.braintreegateway.com', $sn);
  127. $this->config->setEnvironment('production');
  128. $sn = $this->config->serverName();
  129. $this->assertEquals('api.braintreegateway.com', $sn);
  130. }
  131. public function testAuthUrl()
  132. {
  133. $this->config->setEnvironment('development');
  134. $authUrl = $this->config->authUrl();
  135. $this->assertEquals('http://auth.venmo.dev:9292', $authUrl);
  136. $this->config->setEnvironment('qa');
  137. $authUrl = $this->config->authUrl();
  138. $this->assertEquals('https://auth.qa.venmo.com', $authUrl);
  139. $this->config->setEnvironment('sandbox');
  140. $authUrl = $this->config->authUrl();
  141. $this->assertEquals('https://auth.sandbox.venmo.com', $authUrl);
  142. $this->config->setEnvironment('production');
  143. $authUrl = $this->config->authUrl();
  144. $this->assertEquals('https://auth.venmo.com', $authUrl);
  145. }
  146. public function testBaseUrl()
  147. {
  148. $this->config->setEnvironment('development');
  149. $bu = $this->config->baseUrl();
  150. $this->assertEquals('http://localhost:' . $this->config->portNumber(), $bu);
  151. $this->config->setEnvironment('qa');
  152. $bu = $this->config->baseUrl();
  153. $this->assertEquals('https://gateway.qa.braintreepayments.com:443', $bu);
  154. $this->config->setEnvironment('sandbox');
  155. $bu = $this->config->baseUrl();
  156. $this->assertEquals('https://api.sandbox.braintreegateway.com:443', $bu);
  157. $this->config->setEnvironment('production');
  158. $bu = $this->config->baseUrl();
  159. $this->assertEquals('https://api.braintreegateway.com:443', $bu);
  160. }
  161. function testProxyHost()
  162. {
  163. $this->config->proxyHost('example.com');
  164. $this->assertEquals('example.com', $this->config->proxyHost());
  165. }
  166. function testProxyPort()
  167. {
  168. $this->config->proxyPort('1234');
  169. $this->assertEquals('1234', $this->config->proxyPort());
  170. }
  171. function testProxyType()
  172. {
  173. $this->config->proxyType('MY_PROXY');
  174. $this->assertEquals('MY_PROXY', $this->config->proxyType());
  175. }
  176. function testProxyIsConfigured()
  177. {
  178. $this->config->proxyHost('example.com');
  179. $this->config->proxyPort('1234');
  180. $this->assertTrue($this->config->isUsingProxy());
  181. }
  182. /**
  183. * @expectedException Braintree\Exception\Configuration
  184. * @expectedExceptionMessage environment needs to be set
  185. */
  186. public function testValidateEmptyEnvironment()
  187. {
  188. //Braintree\Configuration::environment('development');
  189. Braintree\Configuration::merchantId('integration_merchant_id');
  190. Braintree\Configuration::publicKey('integration_public_key');
  191. Braintree\Configuration::privateKey('integration_private_key');
  192. Braintree\Configuration::$global->assertHasAccessTokenOrKeys();
  193. }
  194. /**
  195. * @expectedException Braintree\Exception\Configuration
  196. * @expectedExceptionMessage merchantId needs to be set
  197. */
  198. public function testMerchantId()
  199. {
  200. Braintree\Configuration::environment('development');
  201. //Braintree\Configuration::merchantId('integration_merchant_id');
  202. Braintree\Configuration::publicKey('integration_public_key');
  203. Braintree\Configuration::privateKey('integration_private_key');
  204. Braintree\Configuration::$global->assertHasAccessTokenOrKeys();
  205. }
  206. /**
  207. * @expectedException Braintree\Exception\Configuration
  208. * @expectedExceptionMessage publicKey needs to be set
  209. */
  210. public function testPublicKey()
  211. {
  212. Braintree\Configuration::environment('development');
  213. Braintree\Configuration::merchantId('integration_merchant_id');
  214. //Braintree\Configuration::publicKey('integration_public_key');
  215. Braintree\Configuration::privateKey('integration_private_key');
  216. Braintree\Configuration::$global->assertHasAccessTokenOrKeys();
  217. }
  218. /**
  219. * @expectedException Braintree\Exception\Configuration
  220. * @expectedExceptionMessage privateKey needs to be set
  221. */
  222. public function testPrivateKey()
  223. {
  224. Braintree\Configuration::environment('development');
  225. Braintree\Configuration::merchantId('integration_merchant_id');
  226. Braintree\Configuration::publicKey('integration_public_key');
  227. //Braintree\Configuration::privateKey('integration_private_key');
  228. Braintree\Configuration::$global->assertHasAccessTokenOrKeys();
  229. }
  230. public function testValidWithOAuthClientCredentials()
  231. {
  232. $config = new Braintree\Configuration([
  233. 'clientId' => 'client_id$development$integration_client_id',
  234. 'clientSecret' => 'client_secret$development$integration_client_secret'
  235. ]);
  236. $config->assertHasClientCredentials();
  237. }
  238. /**
  239. * @expectedException Braintree\Exception\Configuration
  240. * @expectedExceptionMessage clientSecret needs to be passed
  241. */
  242. public function testInvalidWithOAuthClientCredentials()
  243. {
  244. $config = new Braintree\Configuration([
  245. 'clientId' => 'client_id$development$integration_client_id'
  246. ]);
  247. $config->assertHasClientCredentials();
  248. }
  249. public function testDetectEnvironmentFromClientId()
  250. {
  251. $config = new Braintree\Configuration([
  252. 'clientId' => 'client_id$development$integration_client_id',
  253. 'clientSecret' => 'client_secret$development$integration_client_secret'
  254. ]);
  255. $this->assertEquals('development', $config->getEnvironment());
  256. }
  257. /**
  258. * @expectedException Braintree\Exception\Configuration
  259. * @expectedExceptionMessage Mismatched credential environments: clientId environment is sandbox and clientSecret environment is development
  260. */
  261. public function testDetectEnvironmentFromClientIdFail()
  262. {
  263. $config = new Braintree\Configuration([
  264. 'clientId' => 'client_id$sandbox$integration_client_id',
  265. 'clientSecret' => 'client_secret$development$integration_client_secret'
  266. ]);
  267. }
  268. /**
  269. * @expectedException Braintree\Exception\Configuration
  270. * @expectedExceptionMessage Value passed for clientId is not a clientId
  271. */
  272. public function testClientIdTypeFail()
  273. {
  274. $config = new Braintree\Configuration([
  275. 'clientId' => 'client_secret$development$integration_client_id',
  276. 'clientSecret' => 'client_secret$development$integration_client_secret'
  277. ]);
  278. }
  279. public function testValidWithAccessToken()
  280. {
  281. $config = new Braintree\Configuration([
  282. 'accessToken' => 'access_token$development$integration_merchant_id$integration_access_token',
  283. ]);
  284. $config->assertHasAccessTokenOrKeys();
  285. }
  286. /**
  287. * @expectedException Braintree\Exception\Configuration
  288. * @expectedExceptionMessage Value passed for accessToken is not an accessToken
  289. */
  290. public function testInvalidAccessTokenType()
  291. {
  292. $config = new Braintree\Configuration([
  293. 'accessToken' => 'client_id$development$integration_merchant_id$integration_access_token',
  294. ]);
  295. }
  296. /**
  297. * @expectedException Braintree\Exception\Configuration
  298. * @expectedExceptionMessage Incorrect accessToken syntax. Expected: type$environment$merchant_id$token
  299. */
  300. public function testInvalidAccessTokenSyntax()
  301. {
  302. $config = new Braintree\Configuration([
  303. 'accessToken' => 'client_id$development$integration_client_id',
  304. ]);
  305. }
  306. /**
  307. * @expectedException Braintree\Exception\Configuration
  308. * @expectedExceptionMessage "invalid" is not a valid environment.
  309. */
  310. public function testInvalidAccessTokenEnvironment()
  311. {
  312. $config = new Braintree\Configuration([
  313. 'accessToken' => 'access_token$invalid$integration_merchant_id$integration_access_token',
  314. ]);
  315. }
  316. public function testValidWithOAuthClientCredentialsAndAccessToken()
  317. {
  318. $config = new Braintree\Configuration([
  319. 'clientId' => 'client_id$development$integration_client_id',
  320. 'clientSecret' => 'client_secret$development$integration_client_secret',
  321. 'accessToken' => 'access_token$development$integration_merchant_id$integration_access_token',
  322. ]);
  323. $config->assertHasClientCredentials();
  324. $config->assertHasAccessTokenOrKeys();
  325. }
  326. /**
  327. * @expectedException Braintree\Exception\Configuration
  328. * @expectedExceptionMessage Mismatched credential environments: clientId environment is development and accessToken environment is sandbox
  329. */
  330. public function testInvalidEnvironmentWithOAuthClientCredentialsAndAccessToken()
  331. {
  332. $config = new Braintree\Configuration([
  333. 'clientId' => 'client_id$development$integration_client_id',
  334. 'clientSecret' => 'client_secret$development$integration_client_secret',
  335. 'accessToken' => 'access_token$sandbox$integration_merchant_id$integration_access_token',
  336. ]);
  337. }
  338. /**
  339. * @expectedException Braintree\Exception\Configuration
  340. * @expectedExceptionMessage Cannot mix OAuth credentials (clientId, clientSecret, accessToken) with key credentials (publicKey, privateKey, environment, merchantId)
  341. */
  342. public function testCannotMixKeysWithOAuthCredentials()
  343. {
  344. $config = new Braintree\Configuration([
  345. 'clientId' => 'client_id$development$integration_client_id',
  346. 'clientSecret' => 'client_secret$development$integration_client_secret',
  347. 'environment' => 'development',
  348. 'merchantId' => 'integration_merchant_id',
  349. 'publicKey' => 'integration_public_key',
  350. 'privateKey' => 'integration_private_key'
  351. ]);
  352. }
  353. }