Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests #418

Merged
merged 1 commit into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions tests/Stripe/ApiRequestorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public function testEncodeObjects()
$v = ['customer' => "\xe9"];
$enc = $method->invoke(null, $v);
$this->assertSame($enc, ['customer' => "\xc3\xa9"]);

// Encodes booleans
$v = true;
$enc = $method->invoke(null, $v);
$this->assertSame('true', $enc);

$v = false;
$enc = $method->invoke(null, $v);
$this->assertSame('false', $enc);
}

public function testHttpClientInjection()
Expand Down Expand Up @@ -69,6 +78,16 @@ public function testDefaultHeaders()
$this->assertSame($headers['Authorization'], 'Bearer ' . $apiKey);
}

/**
* @expectedException \Stripe\Error\Authentication
* @expectedExceptionMessageRegExp #No API key provided#
*/
public function testRaisesAuthenticationErrorWhenNoApiKey()
{
Stripe::setApiKey(null);
Charge::create();
}

public function testRaisesInvalidRequestErrorOn400()
{
$this->stubRequest(
Expand Down Expand Up @@ -144,6 +163,7 @@ public function testRaisesCardErrorOn402()
'code' => 'card_declined',
'decline_code' => 'generic_decline',
'charge' => 'ch_declined_charge',
'param' => 'exp_month',
],
],
402
Expand All @@ -158,6 +178,7 @@ public function testRaisesCardErrorOn402()
$this->assertSame('Your card was declined.', $e->getMessage());
$this->assertSame('card_declined', $e->getStripeCode());
$this->assertSame('generic_decline', $e->getDeclineCode());
$this->assertSame('exp_month', $e->getStripeParam());
} catch (\Exception $e) {
$this->fail("Unexpected exception: " . get_class($e));
}
Expand Down Expand Up @@ -251,6 +272,35 @@ public function testRaisesRateLimitErrorOn429()
}
}

public function testRaisesRateLimitErrorOn400AndCodeRateLimit()
{
$this->stubRequest(
'POST',
'/v1/charges',
[],
null,
false,
[
'error' => [
'code' => 'rate_limit',
'message' => 'Too many requests',
],
],
400
);

try {
Charge::create();
$this->fail("Did not raise error");
} catch (Error\RateLimit $e) {
$this->assertSame(400, $e->getHttpStatus());
$this->assertTrue(is_array($e->getJsonBody()));
$this->assertSame('Too many requests', $e->getMessage());
} catch (\Exception $e) {
$this->fail("Unexpected exception: " . get_class($e));
}
}

public function testRaisesOAuthInvalidRequestError()
{
$this->stubRequest(
Expand Down Expand Up @@ -334,4 +384,160 @@ public function testRaisesOAuthInvalidGrantError()
$this->fail("Unexpected exception: " . get_class($e));
}
}

public function testRaisesOAuthInvalidScopeError()
{
$this->stubRequest(
'POST',
'/oauth/token',
[],
null,
false,
[
'error' => 'invalid_scope',
'error_description' => 'Invalid scope provided: invalid_scope.',
],
400,
Stripe::$connectBase
);

try {
OAuth::token();
$this->fail("Did not raise error");
} catch (Error\OAuth\InvalidScope $e) {
$this->assertSame(400, $e->getHttpStatus());
$this->assertSame('invalid_scope', $e->getErrorCode());
$this->assertSame('Invalid scope provided: invalid_scope.', $e->getMessage());
} catch (\Exception $e) {
$this->fail("Unexpected exception: " . get_class($e));
}
}

public function testRaisesOAuthUnsupportedGrantTypeError()
{
$this->stubRequest(
'POST',
'/oauth/token',
[],
null,
false,
[
'error' => 'unsupported_grant_type',
],
400,
Stripe::$connectBase
);

try {
OAuth::token();
$this->fail("Did not raise error");
} catch (Error\OAuth\UnsupportedGrantType $e) {
$this->assertSame(400, $e->getHttpStatus());
$this->assertSame('unsupported_grant_type', $e->getErrorCode());
} catch (\Exception $e) {
$this->fail("Unexpected exception: " . get_class($e));
}
}

public function testRaisesOAuthUnsupportedResponseTypeError()
{
$this->stubRequest(
'POST',
'/oauth/token',
[],
null,
false,
[
'error' => 'unsupported_response_type',
'error_description' => "Only 'code' response_type is supported, but 'unsupported_response_type' was provided",
],
400,
Stripe::$connectBase
);

try {
OAuth::token();
$this->fail("Did not raise error");
} catch (Error\OAuth\UnsupportedResponseType $e) {
$this->assertSame(400, $e->getHttpStatus());
$this->assertSame('unsupported_response_type', $e->getErrorCode());
$this->assertSame("Only 'code' response_type is supported, but 'unsupported_response_type' was provided", $e->getMessage());
} catch (\Exception $e) {
$this->fail("Unexpected exception: " . get_class($e));
}
}

public function testHeaderStripeVersionGlobal()
{
Stripe::setApiVersion('2222-22-22');
$this->stubRequest(
'POST',
'/v1/charges',
[],
[
'Stripe-Version: 2222-22-22',
],
false,
[
'id' => 'ch_123',
'object' => 'charge',
]
);
Charge::create();
}

public function testHeaderStripeVersionRequestOptions()
{
$this->stubRequest(
'POST',
'/v1/charges',
[],
[
'Stripe-Version: 2222-22-22',
],
false,
[
'id' => 'ch_123',
'object' => 'charge',
]
);
Charge::create([], ['stripe_version' => '2222-22-22']);
}

public function testHeaderStripeAccountGlobal()
{
Stripe::setAccountId('acct_123');
$this->stubRequest(
'POST',
'/v1/charges',
[],
[
'Stripe-Account: acct_123',
],
false,
[
'id' => 'ch_123',
'object' => 'charge',
]
);
Charge::create();
}

public function testHeaderStripeAccountRequestOptions()
{
$this->stubRequest(
'POST',
'/v1/charges',
[],
[
'Stripe-Account: acct_123',
],
false,
[
'id' => 'ch_123',
'object' => 'charge',
]
);
Charge::create([], ['stripe_account' => 'acct_123']);
}
}
12 changes: 12 additions & 0 deletions tests/Stripe/ApplicationFeeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ public function testIsRetrievable()
$this->assertInstanceOf("Stripe\\ApplicationFee", $resource);
}

public function testIsRefundable()
{
$fee = ApplicationFee::retrieve(self::TEST_RESOURCE_ID);
$this->expectsRequest(
'post',
'/v1/application_fees/' . $fee->id . '/refunds'
);
$resource = $fee->refund();
$this->assertInstanceOf("Stripe\\ApplicationFee", $resource);
$this->assertSame($resource, $fee);
}

public function testCanCreateRefund()
{
$this->expectsRequest(
Expand Down
36 changes: 36 additions & 0 deletions tests/Stripe/Error/BaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Stripe;

class BaseTest extends TestCase
{
public function createFixture($params = [])
{
return $this->getMockForAbstractClass('Stripe\\Error\\Base', [
'message',
200,
'{"key": "value"}',
['key' => 'value'],
[
'Some-Header' => 'Some Value',
'Request-Id' => 'req_test',
],
]);
}

public function testGetters()
{
$e = $this->createFixture();
$this->assertSame(200, $e->getHttpStatus());
$this->assertSame('{"key": "value"}', $e->getHttpBody());
$this->assertSame(['key' => 'value'], $e->getJsonBody());
$this->assertSame('Some Value', $e->getHttpHeaders()['Some-Header']);
$this->assertSame('req_test', $e->getRequestId());
}

public function testToString()
{
$e = $this->createFixture();
$this->assertContains("from API request 'req_test'", (string)$e);
}
}
12 changes: 12 additions & 0 deletions tests/Stripe/Error/SignatureVerificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Stripe;

class SignatureVerificationTest extends TestCase
{
public function testGetters()
{
$e = new Error\SignatureVerification('message', 'sig_header');
$this->assertSame('sig_header', $e->getSigHeader());
}
}
30 changes: 12 additions & 18 deletions tests/Stripe/OAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@

class OAuthTest extends TestCase
{
/**
* @before
*/
public function setUpClientId()
{
Stripe::setClientId('ca_test');
}

/**
* @after
*/
public function tearDownClientId()
{
Stripe::setClientId(null);
}

public function testAuthorizeUrl()
{
$uriStr = OAuth::authorizeUrl([
Expand All @@ -39,13 +23,23 @@ public function testAuthorizeUrl()
$this->assertSame('connect.stripe.com', $uri['host']);
$this->assertSame('/oauth/authorize', $uri['path']);

$this->assertSame('ca_test', $params['client_id']);
$this->assertSame('ca_123', $params['client_id']);
$this->assertSame('read_write', $params['scope']);
$this->assertSame('test@example.com', $params['stripe_user']['email']);
$this->assertSame('https://example.com/profile/test', $params['stripe_user']['url']);
$this->assertSame('US', $params['stripe_user']['country']);
}

/**
* @expectedException \Stripe\Error\Authentication
* @expectedExceptionMessageRegExp #No client_id provided#
*/
public function testRaisesAuthenticationErrorWhenNoClientId()
{
Stripe::setClientId(null);
OAuth::authorizeUrl();
}

public function testToken()
{
$this->stubRequest(
Expand Down Expand Up @@ -84,7 +78,7 @@ public function testDeauthorize()
'/oauth/deauthorize',
[
'stripe_user_id' => 'acct_test_deauth',
'client_id' => 'ca_test',
'client_id' => 'ca_123',
],
null,
false,
Expand Down
9 changes: 7 additions & 2 deletions tests/Stripe/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ public function testIsCancelable()
$resource = Subscription::retrieve(self::TEST_RESOURCE_ID);
$this->expectsRequest(
'delete',
'/v1/subscriptions/' . $resource->id
'/v1/subscriptions/' . $resource->id,
[
'at_period_end' => 'true',
]
);
$resource->cancel();
$resource->cancel([
'at_period_end' => true,
]);
$this->assertInstanceOf("Stripe\\Subscription", $resource);
}

Expand Down
Loading