Skip to content

Commit

Permalink
Use union of literals for parameter throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
richardm-stripe committed Apr 27, 2023
1 parent ca6e69d commit 5512ba2
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/ApiOperations/NestedResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
trait NestedResource
{
/**
* @param string $method
* @param 'delete'|'get'|'post' $method
* @param string $url
* @param null|array $params
* @param null|array|string $options
Expand Down
8 changes: 4 additions & 4 deletions lib/ApiOperations/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected static function _validateParams($params = null)
}

/**
* @param string $method HTTP method ('get', 'post', etc.)
* @param 'delete'|'get'|'post' $method HTTP method ('get', 'post', etc.)
* @param string $url URL for the request
* @param array $params list of parameters for the request
* @param null|array|string $options
Expand All @@ -46,7 +46,7 @@ protected function _request($method, $url, $params = [], $options = null)
}

/**
* @param string $method HTTP method ('get', 'post', etc.)
* @param 'delete'|'get'|'post' $method HTTP method ('get', 'post', etc.)
* @param string $url URL for the request
* @param callable $readBodyChunk function that will receive chunks of data from a successful request body
* @param array $params list of parameters for the request
Expand All @@ -61,7 +61,7 @@ protected function _requestStream($method, $url, $readBodyChunk, $params = [], $
}

/**
* @param string $method HTTP method ('get', 'post', etc.)
* @param 'delete'|'get'|'post' $method HTTP method ('get', 'post', etc.)
* @param string $url URL for the request
* @param array $params list of parameters for the request
* @param null|array|string $options
Expand All @@ -82,7 +82,7 @@ protected static function _staticRequest($method, $url, $params, $options)
}

/**
* @param string $method HTTP method ('get', 'post', etc.)
* @param 'delete'|'get'|'post' $method HTTP method ('get', 'post', etc.)
* @param string $url URL for the request
* @param callable $readBodyChunk function that will receive chunks of data from a successful request body
* @param array $params list of parameters for the request
Expand Down
8 changes: 4 additions & 4 deletions lib/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private static function _encodeObjects($d)
}

/**
* @param string $method
* @param 'delete'|'get'|'post' $method
* @param string $url
* @param null|array $params
* @param null|array $headers
Expand All @@ -128,7 +128,7 @@ public function request($method, $url, $params = null, $headers = null)
}

/**
* @param string $method
* @param 'delete'|'get'|'post' $method
* @param string $url
* @param callable $readBodyChunkCallable
* @param null|array $params
Expand Down Expand Up @@ -432,7 +432,7 @@ function ($key) use ($params) {
}

/**
* @param string $method
* @param 'delete'|'get'|'post' $method
* @param string $url
* @param array $params
* @param array $headers
Expand Down Expand Up @@ -469,7 +469,7 @@ private function _requestRaw($method, $url, $params, $headers)
}

/**
* @param string $method
* @param 'delete'|'get'|'post' $method
* @param string $url
* @param array $params
* @param array $headers
Expand Down
8 changes: 4 additions & 4 deletions lib/BaseStripeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function getFilesBase()
/**
* Sends a request to Stripe's API.
*
* @param string $method the HTTP method
* @param 'delete'|'get'|'post' $method the HTTP method
* @param string $path the path of the request
* @param array $params the parameters of the request
* @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
Expand All @@ -143,7 +143,7 @@ public function request($method, $path, $params, $opts)
* Sends a request to Stripe's API, passing chunks of the streamed response
* into a user-provided $readBodyChunkCallable callback.
*
* @param string $method the HTTP method
* @param 'delete'|'get'|'post' $method the HTTP method
* @param string $path the path of the request
* @param callable $readBodyChunkCallable a function that will be called
* @param array $params the parameters of the request
Expand All @@ -161,7 +161,7 @@ public function requestStream($method, $path, $readBodyChunkCallable, $params, $
/**
* Sends a request to Stripe's API.
*
* @param string $method the HTTP method
* @param 'delete'|'get'|'post' $method the HTTP method
* @param string $path the path of the request
* @param array $params the parameters of the request
* @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
Expand All @@ -185,7 +185,7 @@ public function requestCollection($method, $path, $params, $opts)
/**
* Sends a request to Stripe's API.
*
* @param string $method the HTTP method
* @param 'delete'|'get'|'post' $method the HTTP method
* @param string $path the path of the request
* @param array $params the parameters of the request
* @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
Expand Down
2 changes: 1 addition & 1 deletion lib/HttpClient/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
interface ClientInterface
{
/**
* @param string $method The HTTP method being used
* @param 'delete'|'get'|'post' $method The HTTP method being used
* @param string $absUrl The URL being requested, including domain and protocol
* @param array $headers Headers to be used in the request (full strings, not KV pairs)
* @param array $params KV pairs for parameters. Can be nested for arrays and hashes
Expand Down
2 changes: 1 addition & 1 deletion lib/HttpClient/StreamingClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
interface StreamingClientInterface
{
/**
* @param string $method The HTTP method being used
* @param 'delete'|'get'|'post' $method The HTTP method being used
* @param string $absUrl The URL being requested, including domain and protocol
* @param array $headers Headers to be used in the request (full strings, not KV pairs)
* @param array $params KV pairs for parameters. Can be nested for arrays and hashes
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/OAuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class OAuthService extends \Stripe\Service\AbstractService
/**
* Sends a request to Stripe's Connect API.
*
* @param string $method the HTTP method
* @param 'delete'|'get'|'post' $method the HTTP method
* @param string $path the path of the request
* @param array $params the parameters of the request
* @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
Expand Down
2 changes: 1 addition & 1 deletion lib/StripeClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface StripeClientInterface extends BaseStripeClientInterface
/**
* Sends a request to Stripe's API.
*
* @param string $method the HTTP method
* @param 'delete'|'get'|'post' $method the HTTP method
* @param string $path the path of the request
* @param array $params the parameters of the request
* @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 1
level: 2

bootstrapFiles:
- tests/bootstrap.php
Expand Down
129 changes: 129 additions & 0 deletions tests/Stripe/BaseStripeClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,133 @@ public function testRequestWithOptsInParamsWarns()
static::assertNotNull($charge);
static::assertSame('acct_456', $this->optsReflector->getValue($charge)->headers['Stripe-Account']);
}

public function testJsonRawRequestGetWithURLParams()
{
$curlClientStub = $this->getMockBuilder(\Stripe\HttpClient\CurlClient::class)
->onlyMethods(['executeRequestWithRetries'])
->getMock()
;
$curlClientStub->method('executeRequestWithRetries')
->willReturn(['{}', 200, []])
;

$opts = null;
$curlClientStub->expects(static::once())
->method('executeRequestWithRetries')
->with(static::callback(function ($opts_) use (&$opts) {
$opts = $opts_;

return true;
}), MOCK_URL . '/v1/xyz?foo=bar')
;

ApiRequestor::setHttpClient($curlClientStub);
$client = new BaseStripeClient([
'api_key' => 'sk_test_client',
'stripe_account' => 'acct_123',
'api_base' => MOCK_URL,
]);
$client->rawRequest('get', '/v1/xyz?foo=bar', null, []);
static::assertArrayNotHasKey(\CURLOPT_POST, $opts);
static::assertArrayNotHasKey(\CURLOPT_POSTFIELDS, $opts);
$content_type = null;
foreach ($opts[\CURLOPT_HTTPHEADER] as $header) {
if (\str_starts_with($header, 'Content-Type:')) {
$content_type = $header;
}
}
// The library sends Content-Type even with no body, so assert this
// But it would be more correct to not send Content-Type
static::assertStringStartsWith('Content-Type: application/x-www-form-urlencoded', $content_type);
}

public function testJsonRawRequestPost()
{
$curlClientStub = $this->getMockBuilder(\Stripe\HttpClient\CurlClient::class)
->onlyMethods(['executeRequestWithRetries'])
->getMock()
;
$curlClientStub->method('executeRequestWithRetries')
->willReturn(['{"object": "xyz", "isPHPBestLanguage": true, "abc": {"object": "abc", "a": 2}}', 200, []])
;

$curlClientStub->expects(static::once())
->method('executeRequestWithRetries')
->with(static::callback(function ($opts) {
$this->assertSame(1, $opts[\CURLOPT_POST]);
$this->assertSame('{"foo":"bar","baz":{"qux":false}}', $opts[\CURLOPT_POSTFIELDS]);
$this->assertContains('Content-Type: application/json', $opts[\CURLOPT_HTTPHEADER]);

return true;
}), MOCK_URL . '/v1/xyz')
;

ApiRequestor::setHttpClient($curlClientStub);
$client = new BaseStripeClient([
'api_key' => 'sk_test_client',
'stripe_account' => 'acct_123',
'api_base' => MOCK_URL,
]);
$params = ['foo' => 'bar', 'baz' => ['qux' => false]];
$resp = $client->rawRequest('post', '/v1/xyz', $params, [
'encoding' => 'json',
]);

$decoded = \json_decode($resp->body, true);
$xyz = \Stripe\StripeObject::constructFrom($decoded);

static::assertSame('xyz', $xyz->object);
static::assertTrue($xyz->isPHPBestLanguage);
static::assertSame(2, $xyz->abc->a);
static::assertInstanceof(\Stripe\StripeObject::class, $xyz->abc);
}

public function testFormRawRequestPost()
{
$curlClientStub = $this->getMockBuilder(\Stripe\HttpClient\CurlClient::class)
->onlyMethods(['executeRequestWithRetries'])
->getMock()
;
$curlClientStub->method('executeRequestWithRetries')
->willReturn(['{}', 200, []])
;

$curlClientStub->expects(static::once())
->method('executeRequestWithRetries')
->with(static::callback(function ($opts) {
$this->assertSame(1, $opts[\CURLOPT_POST]);
$this->assertSame('foo=bar&baz[qux]=false', $opts[\CURLOPT_POSTFIELDS]);
$this->assertContains('Content-Type: application/x-www-form-urlencoded', $opts[\CURLOPT_HTTPHEADER]);

return true;
}), MOCK_URL . '/v1/xyz')
;

ApiRequestor::setHttpClient($curlClientStub);
$client = new BaseStripeClient([
'api_key' => 'sk_test_client',
'stripe_account' => 'acct_123',
'api_base' => MOCK_URL,
]);
$params = ['foo' => 'bar', 'baz' => ['qux' => false]];
$client->rawRequest('post', '/v1/xyz', $params, [
'encoding' => 'form',
]);
}

public function testJsonRawRequestGetWithNonNullParams()
{
$client = new BaseStripeClient([
'api_key' => 'sk_test_client',
'stripe_account' => 'acct_123',
'api_base' => MOCK_URL,
]);
$params = [];
$this->expectException(\Stripe\Exception\InvalidArgumentException::class);
$this->expectExceptionMessage('Error: rawRequest only supports $params on post requests. Please pass null and add your parameters to $path');
$client->rawRequest('get', '/v1/xyz', $params, [
'encoding' => 'json',
]);
}
}
10 changes: 5 additions & 5 deletions tests/TestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected function tearDownConfig()
* Sets up a request expectation with the provided parameters. The request
* will actually go through and be emitted.
*
* @param string $method HTTP method (e.g. 'post', 'get', etc.)
* @param 'delete'|'get'|'post' $method HTTP method (e.g. 'post', 'get', etc.)
* @param string $path relative path (e.g. '/v1/charges')
* @param null|array $params array of parameters. If null, parameters will
* not be checked.
Expand Down Expand Up @@ -106,7 +106,7 @@ function ($method, $absUrl, $headers, $params, $hasFile) {
* Sets up a request expectation with the provided parameters. The request
* will actually go through and be emitted.
*
* @param string $method HTTP method (e.g. 'post', 'get', etc.)
* @param 'delete'|'get'|'post' $method HTTP method (e.g. 'post', 'get', etc.)
* @param string $path relative path (e.g. '/v1/charges')
* @param null|array $params array of parameters. If null, parameters will
* not be checked.
Expand Down Expand Up @@ -142,7 +142,7 @@ function ($method, $absUrl, $readBodyChunkCallable, $headers, $params, $hasFile)
* will not actually be emitted, instead the provided response parameters
* will be returned.
*
* @param string $method HTTP method (e.g. 'post', 'get', etc.)
* @param 'delete'|'get'|'post' $method HTTP method (e.g. 'post', 'get', etc.)
* @param string $path relative path (e.g. '/v1/charges')
* @param null|array $params array of parameters. If null, parameters will
* not be checked.
Expand Down Expand Up @@ -175,7 +175,7 @@ protected function stubRequest(
* prepare the client mocker to expect an invocation of the `request` method
* with the provided arguments.
*
* @param string $method HTTP method (e.g. 'post', 'get', etc.)
* @param 'delete'|'get'|'post' $method HTTP method (e.g. 'post', 'get', etc.)
* @param string $path relative path (e.g. '/v1/charges')
* @param null|array $params array of parameters. If null, parameters will
* not be checked.
Expand Down Expand Up @@ -231,7 +231,7 @@ private function prepareRequestMock(
* prepare the client mocker to expect an invocation of the `request` method
* with the provided arguments.
*
* @param string $method HTTP method (e.g. 'post', 'get', etc.)
* @param 'delete'|'get'|'post' $method HTTP method (e.g. 'post', 'get', etc.)
* @param string $path relative path (e.g. '/v1/charges')
* @param null|array $params array of parameters. If null, parameters will
* not be checked.
Expand Down

0 comments on commit 5512ba2

Please sign in to comment.