From 429cfca95cd49dc322c410dc608ad654c7184483 Mon Sep 17 00:00:00 2001 From: Vincent Cheung Date: Wed, 14 Oct 2020 15:55:32 -0700 Subject: [PATCH 1/2] Warn if opts are in params --- lib/ApiRequestor.php | 17 +++++++++++++++++ tests/Stripe/BaseStripeClientTest.php | 23 +++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lib/ApiRequestor.php b/lib/ApiRequestor.php index 4c6d4fa8e..d3d7e7af1 100644 --- a/lib/ApiRequestor.php +++ b/lib/ApiRequestor.php @@ -27,6 +27,8 @@ class ApiRequestor */ private static $requestTelemetry; + private static $OPTIONS_KEYS = ['api_key', 'idempotency_key', 'stripe_account', 'stripe_version', 'api_base']; + /** * ApiRequestor constructor. * @@ -350,6 +352,21 @@ private function _requestRaw($method, $url, $params, $headers) $clientUAInfo = $this->httpClient()->getUserAgentInfo(); } + if ($params && \is_array($params)) { + $optionKeysInParams = \array_filter( + static::$OPTIONS_KEYS, + function ($key) use ($params) { + return \array_key_exists($key, $params); + } + ); + if (\count($optionKeysInParams) > 0) { + $message = \sprintf('Options found in $params: %s. Options should ' + . 'be passed in their own array after $params. (HINT: pass an ' + . 'empty array to $params if you do not have any.)', \implode(', ', $optionKeysInParams)); + \trigger_error($message, \E_USER_WARNING); + } + } + $absUrl = $this->_apiBase . $url; $params = self::_encodeObjects($params); $defaultHeaders = $this->_defaultHeaders($myApiKey, $clientUAInfo); diff --git a/tests/Stripe/BaseStripeClientTest.php b/tests/Stripe/BaseStripeClientTest.php index 2dd6c45b1..ecaec3edb 100644 --- a/tests/Stripe/BaseStripeClientTest.php +++ b/tests/Stripe/BaseStripeClientTest.php @@ -178,4 +178,27 @@ public function testRequestCollectionThrowsForNonList() $client = new BaseStripeClient(['api_key' => 'sk_test_client', 'api_base' => MOCK_URL]); $client->requestCollection('get', '/v1/charges/ch_123', [], []); } + + public function testRequestWithOptsInParamsWarns() + { + $this->expectException(\PHPUnit_Framework_Error_Warning::class); + + $client = new BaseStripeClient([ + 'api_key' => 'sk_test_client', + 'stripe_account' => 'acct_123', + 'api_base' => MOCK_URL, + ]); + $charge = $client->request( + 'get', + '/v1/charges/ch_123', + [ + 'api_key' => 'sk_test_client', + 'stripe_account' => 'acct_123', + 'api_base' => MOCK_URL, + ], + ['stripe_account' => 'acct_456'] + ); + static::assertNotNull($charge); + static::assertSame('acct_456', $this->optsReflector->getValue($charge)->headers['Stripe-Account']); + } } From c9e927d3bc581712562d757d7f101c06b3d2aa62 Mon Sep 17 00:00:00 2001 From: Vincent Cheung Date: Wed, 14 Oct 2020 17:15:58 -0700 Subject: [PATCH 2/2] Assert on the warning message --- tests/Stripe/BaseStripeClientTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Stripe/BaseStripeClientTest.php b/tests/Stripe/BaseStripeClientTest.php index ecaec3edb..044abcc23 100644 --- a/tests/Stripe/BaseStripeClientTest.php +++ b/tests/Stripe/BaseStripeClientTest.php @@ -182,7 +182,8 @@ public function testRequestCollectionThrowsForNonList() public function testRequestWithOptsInParamsWarns() { $this->expectException(\PHPUnit_Framework_Error_Warning::class); - + $this->expectExceptionMessage('Options found in $params: api_key, stripe_account, api_base. Options should be ' + . 'passed in their own array after $params. (HINT: pass an empty array to $params if you do not have any.)'); $client = new BaseStripeClient([ 'api_key' => 'sk_test_client', 'stripe_account' => 'acct_123',