PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Stripe/Util/RequestOptionsTest.php

http://github.com/stripe/stripe-php
PHP | 172 lines | 147 code | 21 blank | 4 comment | 0 complexity | 40b293e6fac659b65eca245af5d3858e MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. namespace Stripe\Util;
  3. /**
  4. * @internal
  5. * @covers \Stripe\Util\RequestOptions
  6. */
  7. final class RequestOptionsTest extends \PHPUnit\Framework\TestCase
  8. {
  9. use \Stripe\TestHelper;
  10. public function testParseString()
  11. {
  12. $opts = RequestOptions::parse('foo');
  13. static::assertSame('foo', $opts->apiKey);
  14. static::assertSame([], $opts->headers);
  15. static::assertNull($opts->apiBase);
  16. }
  17. public function testParseStringStrict()
  18. {
  19. $this->expectException(\Stripe\Exception\InvalidArgumentException::class);
  20. $this->expectExceptionMessageRegExp('#Do not pass a string for request options.#');
  21. $opts = RequestOptions::parse('foo', true);
  22. }
  23. public function testParseNull()
  24. {
  25. $opts = RequestOptions::parse(null);
  26. static::assertNull($opts->apiKey);
  27. static::assertSame([], $opts->headers);
  28. static::assertNull($opts->apiBase);
  29. }
  30. public function testParseArrayEmpty()
  31. {
  32. $opts = RequestOptions::parse([]);
  33. static::assertNull($opts->apiKey);
  34. static::assertSame([], $opts->headers);
  35. static::assertNull($opts->apiBase);
  36. }
  37. public function testParseArrayWithAPIKey()
  38. {
  39. $opts = RequestOptions::parse(
  40. [
  41. 'api_key' => 'foo',
  42. ]
  43. );
  44. static::assertSame('foo', $opts->apiKey);
  45. static::assertSame([], $opts->headers);
  46. static::assertNull($opts->apiBase);
  47. }
  48. public function testParseArrayWithIdempotencyKey()
  49. {
  50. $opts = RequestOptions::parse(
  51. [
  52. 'idempotency_key' => 'foo',
  53. ]
  54. );
  55. static::assertNull($opts->apiKey);
  56. static::assertSame(['Idempotency-Key' => 'foo'], $opts->headers);
  57. static::assertNull($opts->apiBase);
  58. }
  59. public function testParseArrayWithAPIKeyAndIdempotencyKey()
  60. {
  61. $opts = RequestOptions::parse(
  62. [
  63. 'api_key' => 'foo',
  64. 'idempotency_key' => 'foo',
  65. ]
  66. );
  67. static::assertSame('foo', $opts->apiKey);
  68. static::assertSame(['Idempotency-Key' => 'foo'], $opts->headers);
  69. static::assertNull($opts->apiBase);
  70. }
  71. public function testParseArrayWithAPIKeyAndUnexpectedKeys()
  72. {
  73. $opts = RequestOptions::parse(
  74. [
  75. 'api_key' => 'foo',
  76. 'foo' => 'bar',
  77. ]
  78. );
  79. static::assertSame('foo', $opts->apiKey);
  80. static::assertSame([], $opts->headers);
  81. static::assertNull($opts->apiBase);
  82. }
  83. public function testParseArrayWithAPIKeyAndUnexpectedKeysStrict()
  84. {
  85. $this->expectException(\Stripe\Exception\InvalidArgumentException::class);
  86. $this->expectExceptionMessage('Got unexpected keys in options array: foo');
  87. $opts = RequestOptions::parse(
  88. [
  89. 'api_key' => 'foo',
  90. 'foo' => 'bar',
  91. ],
  92. true
  93. );
  94. }
  95. public function testParseArrayWithAPIBase()
  96. {
  97. $opts = RequestOptions::parse(
  98. [
  99. 'api_base' => 'https://example.com',
  100. ]
  101. );
  102. static::assertNull($opts->apiKey);
  103. static::assertSame([], $opts->headers);
  104. static::assertSame('https://example.com', $opts->apiBase);
  105. }
  106. public function testParseWrongType()
  107. {
  108. $this->expectException(\Stripe\Exception\InvalidArgumentException::class);
  109. $opts = RequestOptions::parse(5);
  110. }
  111. public function testMerge()
  112. {
  113. $baseOpts = RequestOptions::parse(
  114. [
  115. 'api_key' => 'foo',
  116. 'idempotency_key' => 'foo',
  117. ]
  118. );
  119. $opts = $baseOpts->merge(
  120. [
  121. 'idempotency_key' => 'bar',
  122. ]
  123. );
  124. static::assertSame('foo', $opts->apiKey);
  125. static::assertSame(['Idempotency-Key' => 'bar'], $opts->headers);
  126. static::assertNull($opts->apiBase);
  127. }
  128. public function testDiscardNonPersistentHeaders()
  129. {
  130. $opts = RequestOptions::parse(
  131. [
  132. 'stripe_account' => 'foo',
  133. 'idempotency_key' => 'foo',
  134. ]
  135. );
  136. $opts->discardNonPersistentHeaders();
  137. static::assertSame(['Stripe-Account' => 'foo'], $opts->headers);
  138. }
  139. public function testDebugInfo()
  140. {
  141. $opts = RequestOptions::parse(['api_key' => 'sk_test_1234567890abcdefghijklmn']);
  142. $debugInfo = \print_r($opts, true);
  143. static::assertContains('[apiKey] => sk_test_********************klmn', $debugInfo);
  144. $opts = RequestOptions::parse(['api_key' => 'sk_1234567890abcdefghijklmn']);
  145. $debugInfo = \print_r($opts, true);
  146. static::assertContains('[apiKey] => sk_********************klmn', $debugInfo);
  147. $opts = RequestOptions::parse(['api_key' => '1234567890abcdefghijklmn']);
  148. $debugInfo = \print_r($opts, true);
  149. static::assertContains('[apiKey] => ********************klmn', $debugInfo);
  150. }
  151. }