Skip to content

Commit

Permalink
Change StripeClient initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Mar 25, 2020
1 parent 66b0dcd commit 9f5a679
Show file tree
Hide file tree
Showing 59 changed files with 104 additions and 85 deletions.
56 changes: 38 additions & 18 deletions lib/BaseStripeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,42 @@ class BaseStripeClient implements StripeClientInterface
private $filesBase;

/**
* Initializes a new instance of the {@link StripeClient} class.
* Initializes a new instance of the {@link BaseStripeClient} class.
*
* @param null|string $apiKey the API key used by the client to make requests
* @param null|string $clientId the client ID used by the client in OAuth requests
* @param null|string $apiBase The base URL for Stripe's API. Defaults to {@link DEFAULT_API_BASE}.
* @param null|string $connectBase The base URL for Stripe's OAuth API. Defaults to {@link DEFAULT_CONNECT_BASE}.
* @param null|string $filesBase The base URL for Stripe's Files API. Defaults to {@link DEFAULT_FILES_BASE}.
* The constructor takes a single argument. The argument can be a string, in which case it
* should be the API key. It can also be an array with various configuration settings.
*
* Configuration settings include the following options:
*
* - api_key (string): the Stripe API key, to be used in regular API requests.
* - client_id (string): the Stripe client ID, to be used in OAuth requests.
* - api_base (string): the base URL for regular API requests. Defaults to
* {@link DEFAULT_API_BASE}. Changing this should rarely be necessary.
* - connect_base (string): the base URL for OAuth requests. Defaults to
* {@link DEFAULT_CONNECT_BASE}. Changing this should rarely be necessary.
* - files_base (string): the base URL for file creation requests. Defaults to
* {@link DEFAULT_FILES_BASE}. Changing this should rarely be necessary.
*
* @param array<string, mixed>|string $config the API key as a string, or an array containing
* the client configuration settings
*/
public function __construct(
$apiKey,
$clientId = null,
$apiBase = null,
$connectBase = null,
$filesBase = null
) {
public function __construct($config = [])
{
if (\is_string($config)) {
$config = ['api_key' => $config];
}

$defaults = [
'api_key' => null,
'client_id' => null,
'api_base' => self::DEFAULT_API_BASE,
'connect_base' => self::DEFAULT_CONNECT_BASE,
'files_base' => self::DEFAULT_FILES_BASE,
];
$config = \array_merge($defaults, $config);

$apiKey = $config['api_key'];

if (null !== $apiKey && ('' === $apiKey)) {
$msg = 'API key cannot be the empty string.';

Expand All @@ -57,11 +78,10 @@ public function __construct(
}

$this->apiKey = $apiKey;
$this->clientId = $clientId;

$this->apiBase = $apiBase ?: self::DEFAULT_API_BASE;
$this->connectBase = $connectBase ?: self::DEFAULT_CONNECT_BASE;
$this->filesBase = $filesBase ?: self::DEFAULT_FILES_BASE;
$this->clientId = $config['client_id'];
$this->apiBase = $config['api_base'];
$this->connectBase = $config['connect_base'];
$this->filesBase = $config['files_base'];
}

/**
Expand Down
15 changes: 7 additions & 8 deletions tests/Stripe/BaseStripeClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
*/
final class BaseStripeClientTest extends \PHPUnit\Framework\TestCase
{
public function testCtorDoesNotThrowIfApiKeyIsNull()
public function testCtorDoesNotThrowWhenNoParams()
{
$client = new BaseStripeClient(null);
$client = new BaseStripeClient();
static::assertNotNull($client);
static::assertNull($client->getApiKey());
}
Expand All @@ -33,7 +33,7 @@ public function testCtorThrowsIfApiKeyContainsWhitespace()

public function testRequestWithClientApiKey()
{
$client = new BaseStripeClient('sk_test_client', null, MOCK_URL);
$client = new BaseStripeClient(['api_key' => 'sk_test_client', 'api_base' => MOCK_URL]);
$charge = $client->request('get', '/v1/charges/ch_123', [], []);
static::assertNotNull($charge);
$optsReflector = new \ReflectionProperty(\Stripe\StripeObject::class, '_opts');
Expand All @@ -43,10 +43,9 @@ public function testRequestWithClientApiKey()

public function testRequestWithOptsApiKey()
{
$client = new BaseStripeClient(null, null, MOCK_URL);
$client = new BaseStripeClient(['api_base' => MOCK_URL]);
$charge = $client->request('get', '/v1/charges/ch_123', [], ['api_key' => 'sk_test_opts']);
static::assertNotNull($charge);
static::assertNotNull($charge);
$optsReflector = new \ReflectionProperty(\Stripe\StripeObject::class, '_opts');
$optsReflector->setAccessible(true);
static::assertSame('sk_test_opts', $optsReflector->getValue($charge)->apiKey);
Expand All @@ -57,7 +56,7 @@ public function testRequestThrowsIfNoApiKeyInClientAndOpts()
$this->expectException(\Stripe\Exception\AuthenticationException::class);
$this->expectExceptionMessage('No API key provided.');

$client = new BaseStripeClient(null, null, MOCK_URL);
$client = new BaseStripeClient(['api_base' => MOCK_URL]);
$charge = $client->request('get', '/v1/charges/ch_123', [], []);
static::assertNotNull($charge);
static::assertSame('ch_123', $charge->id);
Expand All @@ -68,7 +67,7 @@ public function testRequestThrowsIfOptsIsString()
$this->expectException(\Stripe\Exception\InvalidArgumentException::class);
$this->expectExceptionMessageRegExp('#Do not pass a string for request options.#');

$client = new BaseStripeClient(null, null, MOCK_URL);
$client = new BaseStripeClient(['api_base' => MOCK_URL]);
$charge = $client->request('get', '/v1/charges/ch_123', [], 'foo');
static::assertNotNull($charge);
static::assertSame('ch_123', $charge->id);
Expand All @@ -79,7 +78,7 @@ public function testRequestThrowsIfOptsIsArrayWithUnexpectedKeys()
$this->expectException(\Stripe\Exception\InvalidArgumentException::class);
$this->expectExceptionMessage('Got unexpected keys in options array: foo');

$client = new BaseStripeClient(null, null, MOCK_URL);
$client = new BaseStripeClient(['api_base' => MOCK_URL]);
$charge = $client->request('get', '/v1/charges/ch_123', [], ['foo' => 'bar']);
static::assertNotNull($charge);
static::assertSame('ch_123', $charge->id);
Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/AbstractServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class AbstractServiceTest extends \PHPUnit\Framework\TestCase
*/
public function setUpMockService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
// Testing with CouponService, because testing abstract classes is hard in PHP :/
$this->service = new \Stripe\Service\CouponService($this->client);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/AccountLinkServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class AccountLinkServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new AccountLinkService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/AccountServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class AccountServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new AccountService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/ApplePayDomainServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class ApplePayDomainServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new ApplePayDomainService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/ApplicationFeeServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class ApplicationFeeServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new ApplicationFeeService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/BalanceServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class BalanceServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new BalanceService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/BalanceTransactionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class BalanceTransactionServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new BalanceTransactionService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/ChargeServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class ChargeServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new ChargeService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/Checkout/SessionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class SessionServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new SessionService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/CoreServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class CoreServiceFactoryTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->serviceFactory = new CoreServiceFactory($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/CountrySpecServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class CountrySpecServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new CountrySpecService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/CouponServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class CouponServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new CouponService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/CreditNoteServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class CreditNoteServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new CreditNoteService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/CustomerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class CustomerServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new CustomerService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/DisputeServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class DisputeServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new DisputeService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/EphemeralKeyServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class EphemeralKeyServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new EphemeralKeyService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/EventServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class EventServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new EventService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/ExchangeRateServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class ExchangeRateServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new ExchangeRateService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/FileLinkServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class FileLinkServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new FileLinkService($this->client);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Stripe/Service/FileServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class FileServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new FileService($this->client);
}

Expand All @@ -40,7 +40,7 @@ public function testAll()

public function testCreateWithCURLFile()
{
$client = new \Stripe\StripeClient('sk_test_123', null, null, null, MOCK_URL);
$client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'files_base' => MOCK_URL]);
$service = new FileService($client);

$this->expectsRequest(
Expand All @@ -62,7 +62,7 @@ public function testCreateWithCURLFile()

public function testCreateWithFileHandle()
{
$client = new \Stripe\StripeClient('sk_test_123', null, null, null, MOCK_URL);
$client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'files_base' => MOCK_URL]);
$service = new FileService($client);

$this->expectsRequest(
Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/InvoiceItemServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class InvoiceItemServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new InvoiceItemService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/InvoiceServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class InvoiceServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new InvoiceService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/Issuing/AuthorizationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class AuthorizationServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new AuthorizationService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/Issuing/CardServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class CardServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new CardService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/Issuing/CardholderServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class CardholderServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new CardholderService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/Issuing/DisputeServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class DisputeServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new DisputeService($this->client);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Service/Issuing/TransactionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class TransactionServiceTest extends \PHPUnit\Framework\TestCase
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient('sk_test_123', null, MOCK_URL);
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL]);
$this->service = new TransactionService($this->client);
}

Expand Down
Loading

0 comments on commit 9f5a679

Please sign in to comment.