From d1d2eddf64d2cd8934cfb96a61cf4c2ec672a29c Mon Sep 17 00:00:00 2001 From: jlinn Date: Sun, 31 May 2015 13:39:39 -0700 Subject: [PATCH] Fix tests. Update CustomerResponse to use new 'sources' field. --- src/Api/Accounts.php | 19 ++++ src/Request/Accounts/BankAccountRequest.php | 104 ++++++++++++++++++ src/Request/Accounts/CreateAccountRequest.php | 104 ++++++++++++++++++ .../Transfers/CreateTransferRequest.php | 31 +++++- src/Response/Customers/CustomerResponse.php | 24 ++-- tests/Api/AccountsTest.php | 9 ++ tests/Api/BalanceTest.php | 21 ++++ tests/Api/TransfersTest.php | 33 +++++- tests/StripeTestCase.php | 16 +++ 9 files changed, 343 insertions(+), 18 deletions(-) create mode 100644 src/Request/Accounts/BankAccountRequest.php create mode 100644 src/Request/Accounts/CreateAccountRequest.php diff --git a/src/Api/Accounts.php b/src/Api/Accounts.php index 1719ec6..5c33c57 100644 --- a/src/Api/Accounts.php +++ b/src/Api/Accounts.php @@ -7,6 +7,7 @@ namespace Stripe\Api; +use Stripe\Request\Accounts\CreateAccountRequest; use Stripe\Response\Accounts\AccountResponse; class Accounts extends AbstractApi @@ -21,4 +22,22 @@ public function getAccount() { return $this->client->get('account', self::ACCOUNT_RESPONSE_CLASS); } + + /** + * @param CreateAccountRequest $request + * @return AccountResponse + * @link https://stripe.com/docs/api#create_account + */ + public function createAccount(CreateAccountRequest $request) + { + return $this->client->post('accounts', self::ACCOUNT_RESPONSE_CLASS, $request); + } + + /** + * @return CreateAccountRequest + */ + public function createAccountRequest() + { + return new CreateAccountRequest(); + } } diff --git a/src/Request/Accounts/BankAccountRequest.php b/src/Request/Accounts/BankAccountRequest.php new file mode 100644 index 0000000..92ed55c --- /dev/null +++ b/src/Request/Accounts/BankAccountRequest.php @@ -0,0 +1,104 @@ +country; + } + + /** + * @param string $country + * @return $this + */ + public function setCountry($country) + { + $this->country = $country; + return $this; + } + + /** + * @return string + */ + public function getCurrency() + { + return $this->currency; + } + + /** + * @param string $currency + * @return $this + */ + public function setCurrency($currency) + { + $this->currency = $currency; + return $this; + } + + /** + * @return string + */ + public function getRoutingNumber() + { + return $this->routingNumber; + } + + /** + * @param string $routingNumber + * @return $this + */ + public function setRoutingNumber($routingNumber) + { + $this->routingNumber = $routingNumber; + return $this; + } + + /** + * @return string + */ + public function getAccountNumber() + { + return $this->accountNumber; + } + + /** + * @param string $accountNumber + * @return $this + */ + public function setAccountNumber($accountNumber) + { + $this->accountNumber = $accountNumber; + return $this; + } +} \ No newline at end of file diff --git a/src/Request/Accounts/CreateAccountRequest.php b/src/Request/Accounts/CreateAccountRequest.php new file mode 100644 index 0000000..540fa06 --- /dev/null +++ b/src/Request/Accounts/CreateAccountRequest.php @@ -0,0 +1,104 @@ +managed; + } + + /** + * @param boolean $managed + * @return $this + */ + public function setManaged($managed) + { + $this->managed = $managed; + return $this; + } + + /** + * @return string + */ + public function getCountry() + { + return $this->country; + } + + /** + * @param string $country + * @return $this + */ + public function setCountry($country) + { + $this->country = $country; + return $this; + } + + /** + * @return string + */ + public function getEmail() + { + return $this->email; + } + + /** + * @param string $email + * @return $this + */ + public function setEmail($email) + { + $this->email = $email; + return $this; + } + + /** + * @return BankAccountRequest + */ + public function getBankAccount() + { + return $this->bankAccount; + } + + /** + * @param BankAccountRequest $bankAccount + * @return $this + */ + public function setBankAccount($bankAccount) + { + $this->bankAccount = $bankAccount; + return $this; + } +} \ No newline at end of file diff --git a/src/Request/Transfers/CreateTransferRequest.php b/src/Request/Transfers/CreateTransferRequest.php index caea5ba..d5e3a33 100644 --- a/src/Request/Transfers/CreateTransferRequest.php +++ b/src/Request/Transfers/CreateTransferRequest.php @@ -25,6 +25,11 @@ class CreateTransferRequest */ protected $recipient; + /** + * @var string + */ + protected $destination; + /** * @var string */ @@ -43,12 +48,12 @@ class CreateTransferRequest /** * @param int $amount * @param string $currency - * @param string $recipient + * @param string $destination */ - public function __construct($amount, $currency, $recipient) + public function __construct($amount, $currency, $destination) { $this->setAmount($amount)->setCurrency($currency); - $this->setRecipient($recipient); + $this->setDestination($destination); } /** @@ -124,6 +129,7 @@ public function setMetadata($metadata) } /** + * @deprecated use destination * @return string */ public function getRecipient() @@ -132,6 +138,7 @@ public function getRecipient() } /** + * @deprecated use destination * @param string $recipient * @return $this */ @@ -158,4 +165,22 @@ public function setStatementDescription($statementDescription) $this->statementDescription = $statementDescription; return $this; } + + /** + * @return string + */ + public function getDestination() + { + return $this->destination; + } + + /** + * @param string $destination + * @return $this + */ + public function setDestination($destination) + { + $this->destination = $destination; + return $this; + } } \ No newline at end of file diff --git a/src/Response/Customers/CustomerResponse.php b/src/Response/Customers/CustomerResponse.php index 99e4d3d..cb898af 100644 --- a/src/Response/Customers/CustomerResponse.php +++ b/src/Response/Customers/CustomerResponse.php @@ -37,7 +37,7 @@ class CustomerResponse * @Type("Stripe\Response\Cards\ListCardsResponse") * @var ListCardsResponse */ - protected $cards; + protected $sources; /** * @Type("integer") @@ -61,7 +61,7 @@ class CustomerResponse * @Type("string") * @var string */ - protected $defaultCard; + protected $defaultSource; /** * @Type("boolean") @@ -120,18 +120,18 @@ public function setAccountBalance($accountBalance) /** * @return \Stripe\Response\Cards\ListCardsResponse */ - public function getCards() + public function getSources() { - return $this->cards; + return $this->sources; } /** - * @param \Stripe\Response\Cards\ListCardsResponse $cards + * @param \Stripe\Response\Cards\ListCardsResponse $sources * @return $this */ - public function setCards($cards) + public function setSources($sources) { - $this->cards = $cards; + $this->sources = $sources; return $this; } @@ -174,18 +174,18 @@ public function setCurrency($currency) /** * @return string */ - public function getDefaultCard() + public function getDefaultSource() { - return $this->defaultCard; + return $this->defaultSource; } /** - * @param string $defaultCard + * @param string $defaultSource * @return $this */ - public function setDefaultCard($defaultCard) + public function setDefaultSource($defaultSource) { - $this->defaultCard = $defaultCard; + $this->defaultSource = $defaultSource; return $this; } diff --git a/tests/Api/AccountsTest.php b/tests/Api/AccountsTest.php index f26ebef..e5dea72 100644 --- a/tests/Api/AccountsTest.php +++ b/tests/Api/AccountsTest.php @@ -34,4 +34,13 @@ public function testGetAccount() $account = $this->accounts->getAccount(); $this->assertInstanceOf(Accounts::ACCOUNT_RESPONSE_CLASS, $account); } + + public function testCreateAccount() + { + $request = $this->accounts->createAccountRequest(); + $request->setEmail("bob".$this->randomString()."@loblaw.com"); + $account = $this->accounts->createAccount($request); + + $this->assertInstanceOf(Accounts::ACCOUNT_RESPONSE_CLASS, $account); + } } diff --git a/tests/Api/BalanceTest.php b/tests/Api/BalanceTest.php index a505c8f..9ce067b 100644 --- a/tests/Api/BalanceTest.php +++ b/tests/Api/BalanceTest.php @@ -8,8 +8,10 @@ namespace Stripe\Tests\Api; +use Stripe\Api\Accounts; use Stripe\Api\Balance; use Stripe\Api\Transfers; +use Stripe\Request\Accounts\BankAccountRequest; use Stripe\Request\Balance\ListBalanceHistoryRequest; use Stripe\Tests\StripeTestCase; @@ -25,11 +27,17 @@ class BalanceTest extends StripeTestCase */ protected $transfers; + /** + * @var Accounts + */ + private $accounts; + protected function setUp() { parent::setUp(); $this->transfers = new Transfers($this->client); $this->balance = new Balance($this->client); + $this->accounts = new Accounts($this->client); } public function testGetBalance() @@ -41,6 +49,19 @@ public function testGetBalance() public function testGetBalanceTransaction() { + $this->markTestSkipped("Unable to set up external accounts for testing."); + + $createAccountRequest = $this->accounts->createAccountRequest(); + $createAccountRequest->setEmail("foo".$this->randomString()."@bar.com"); + $createAccountRequest->setCountry("US"); + $bankAccountRequest = new BankAccountRequest(); + $bankAccountRequest->setCountry("US"); + $bankAccountRequest->setCurrency("USD"); + $bankAccountRequest->setAccountNumber($this::ACCOUNT_NUMBER); + $bankAccountRequest->setRoutingNumber($this::ROUTING_NUMBER); + $createAccountRequest->setBankAccount($bankAccountRequest); + $this->accounts->createAccount($createAccountRequest); + $transfer = $this->transfers->createTransfer($this->transfers->createTransferRequest(350, "usd", "self")); $this->assertInstanceOf(Transfers::TRANSFER_RESPONSE_CLASS, $transfer); diff --git a/tests/Api/TransfersTest.php b/tests/Api/TransfersTest.php index 7963b8c..1437a5f 100644 --- a/tests/Api/TransfersTest.php +++ b/tests/Api/TransfersTest.php @@ -8,8 +8,11 @@ namespace Stripe\Tests\Api; +use Stripe\Api\Accounts; use Stripe\Api\Transfers; +use Stripe\Request\Accounts\BankAccountRequest; use Stripe\Request\Transfers\ListTransfersRequest; +use Stripe\Response\Accounts\AccountResponse; use Stripe\Response\Transfers\TransferResponse; use Stripe\Tests\StripeTestCase; @@ -20,16 +23,21 @@ class TransfersTest extends StripeTestCase */ protected $transfers; + /** + * @var Accounts + */ + protected $accounts; + protected function setUp() { parent::setUp(); $this->transfers = new Transfers($this->client); + $this->accounts = new Accounts($this->client); } public function testCreateTransfer() { - $request = $this->transfers->createTransferRequest(350, "usd", "self"); - $createResponse = $this->transfers->createTransfer($request); + $createResponse = $this->createTransfer(); $this->assertInstanceOf(Transfers::TRANSFER_RESPONSE_CLASS, $createResponse); $this->assertEquals(350, $createResponse->getAmount()); @@ -66,7 +74,8 @@ public function testCancelTransfer() $this->assertInstanceOf(Transfers::TRANSFER_RESPONSE_CLASS, $createResponse); - $this->setExpectedException('Stripe\StripeException', "Transfer cannot be canceled, because it has already been submitted. You can currently only cancel pending transfers."); + // we cannot cancel this transfer because it has already been accepted + $this->setExpectedException('Stripe\StripeException'); $this->transfers->cancelTransfer($createResponse->getId()); } @@ -86,7 +95,25 @@ public function testListTransfers() protected function createTransfer() { $request = $this->transfers->createTransferRequest(350, "usd", "self"); + $request->setDestination($this->createAccount()->getId()); + $request->setRecipient(null); return $this->transfers->createTransfer($request); } + + /** + * @return AccountResponse + */ + protected function createAccount() + { + $request = $this->accounts->createAccountRequest(); + $request->setEmail("foo" . $this->randomString() . "@bar.com"); + $account = new BankAccountRequest(); + $account->setCountry("us"); + $account->setCurrency("usd"); + $account->setRoutingNumber(self::ROUTING_NUMBER); + $account->setAccountNumber(self::ACCOUNT_NUMBER); + $request->setBankAccount($account); + return $this->accounts->createAccount($request); + } } \ No newline at end of file diff --git a/tests/StripeTestCase.php b/tests/StripeTestCase.php index 7632d1b..103a732 100644 --- a/tests/StripeTestCase.php +++ b/tests/StripeTestCase.php @@ -45,4 +45,20 @@ protected function setUp() // the API_KEY value should be set in phpunit.xml $this->client = new Client($_SERVER['API_KEY']); } + + /** + * Generate and return a random string + * @param int $length length of the random string + * @return string a random string + */ + protected function randomString($length = 10) + { + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $charactersLength = strlen($characters); + $randomString = ''; + for ($i = 0; $i < $length; $i++) { + $randomString .= $characters[rand(0, $charactersLength - 1)]; + } + return $randomString; + } }