diff --git a/tests/Stripe/ApiRequestorTest.php b/tests/Stripe/ApiRequestorTest.php index 2de011888..07d001e99 100644 --- a/tests/Stripe/ApiRequestorTest.php +++ b/tests/Stripe/ApiRequestorTest.php @@ -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() @@ -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( @@ -144,6 +163,7 @@ public function testRaisesCardErrorOn402() 'code' => 'card_declined', 'decline_code' => 'generic_decline', 'charge' => 'ch_declined_charge', + 'param' => 'exp_month', ], ], 402 @@ -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)); } @@ -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( @@ -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']); + } } diff --git a/tests/Stripe/ApplicationFeeTest.php b/tests/Stripe/ApplicationFeeTest.php index d3f1de3d9..66e007ff3 100644 --- a/tests/Stripe/ApplicationFeeTest.php +++ b/tests/Stripe/ApplicationFeeTest.php @@ -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( diff --git a/tests/Stripe/Error/BaseTest.php b/tests/Stripe/Error/BaseTest.php new file mode 100644 index 000000000..4c2732ed1 --- /dev/null +++ b/tests/Stripe/Error/BaseTest.php @@ -0,0 +1,36 @@ +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); + } +} diff --git a/tests/Stripe/Error/SignatureVerificationTest.php b/tests/Stripe/Error/SignatureVerificationTest.php new file mode 100644 index 000000000..020a41f8f --- /dev/null +++ b/tests/Stripe/Error/SignatureVerificationTest.php @@ -0,0 +1,12 @@ +assertSame('sig_header', $e->getSigHeader()); + } +} diff --git a/tests/Stripe/OAuthTest.php b/tests/Stripe/OAuthTest.php index 52337ea49..b4e43a881 100644 --- a/tests/Stripe/OAuthTest.php +++ b/tests/Stripe/OAuthTest.php @@ -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([ @@ -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( @@ -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, diff --git a/tests/Stripe/SubscriptionTest.php b/tests/Stripe/SubscriptionTest.php index 9f29db21f..4a42e2182 100644 --- a/tests/Stripe/SubscriptionTest.php +++ b/tests/Stripe/SubscriptionTest.php @@ -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); } diff --git a/tests/TestCase.php b/tests/TestCase.php index bfa02b2d5..840c0ec58 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -16,6 +16,12 @@ class TestCase extends \PHPUnit_Framework_TestCase /** @var string original client ID */ protected $origClientId; + /** @var string original API version */ + protected $origApiVersion; + + /** @var string original account ID */ + protected $origAccountId; + /** @var object HTTP client mocker */ protected $clientMock; @@ -25,11 +31,15 @@ protected function setUp() $this->origApiBase = Stripe::$apiBase; $this->origApiKey = Stripe::getApiKey(); $this->origClientId = Stripe::getClientId(); + $this->origApiVersion = Stripe::getApiVersion(); + $this->origAccountId = Stripe::getAccountId(); // Set up host and credentials for stripe-mock Stripe::$apiBase = "http://localhost:" . MOCK_PORT; Stripe::setApiKey("sk_test_123"); Stripe::setClientId("ca_123"); + Stripe::setApiVersion(null); + Stripe::setAccountId(null); // Set up the HTTP client mocker $this->clientMock = $this->getMock('\Stripe\HttpClient\ClientInterface'); @@ -44,6 +54,8 @@ protected function tearDown() Stripe::$apiBase = $this->origApiBase; Stripe::setApiKey($this->origApiKey); Stripe::setClientId($this->origClientId); + Stripe::setApiVersion($this->origApiVersion); + Stripe::setAccountId($this->origAccountId); } /**