| 1 |
<?php |
| 2 |
|
| 3 |
namespace Stripe; |
| 4 |
|
| 5 |
/** |
| 6 |
* Base class for Stripe test cases, provides some utility methods for creating |
| 7 |
* objects. |
| 8 |
*/ |
| 9 |
class TestCase extends \PHPUnit_Framework_TestCase |
| 10 |
{ |
| 11 |
const API_KEY = 'tGN0bIwXnHdwOa85VABjPdSn8nWY7G7I'; |
| 12 |
|
| 13 |
private $mock; |
| 14 |
|
| 15 |
protected static function authorizeFromEnv() |
| 16 |
{ |
| 17 |
$apiKey = getenv('STRIPE_API_KEY'); |
| 18 |
if (!$apiKey) { |
| 19 |
$apiKey = self::API_KEY; |
| 20 |
} |
| 21 |
|
| 22 |
Stripe::setApiKey($apiKey); |
| 23 |
} |
| 24 |
|
| 25 |
protected function setUp() |
| 26 |
{ |
| 27 |
ApiRequestor::setHttpClient(HttpClient\CurlClient::instance()); |
| 28 |
|
| 29 |
// Peg the API version so that it can be varied independently of the |
| 30 |
// one set on the test account. |
| 31 |
Stripe::setApiVersion('2017-04-06'); |
| 32 |
|
| 33 |
$this->mock = null; |
| 34 |
$this->call = 0; |
| 35 |
} |
| 36 |
|
| 37 |
protected function mockRequest($method, $path, $params = array(), $return = array('id' => 'myId'), $rcode = 200) |
| 38 |
{ |
| 39 |
$mock = $this->setUpMockRequest(); |
| 40 |
$mock->expects($this->at($this->call++)) |
| 41 |
->method('request') |
| 42 |
->with(strtolower($method), 'https://api.stripe.com' . $path, $this->anything(), $params, false) |
| 43 |
->willReturn(array(json_encode($return), $rcode, array())); |
| 44 |
} |
| 45 |
|
| 46 |
private function setUpMockRequest() |
| 47 |
{ |
| 48 |
if (!$this->mock) { |
| 49 |
self::authorizeFromEnv(); |
| 50 |
$this->mock = $this->getMock('\Stripe\HttpClient\ClientInterface'); |
| 51 |
ApiRequestor::setHttpClient($this->mock); |
| 52 |
} |
| 53 |
return $this->mock; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Create a valid test charge. |
| 58 |
*/ |
| 59 |
protected static function createTestCharge(array $attributes = array()) |
| 60 |
{ |
| 61 |
self::authorizeFromEnv(); |
| 62 |
|
| 63 |
return Charge::create( |
| 64 |
$attributes + array( |
| 65 |
'amount' => 2000, |
| 66 |
'currency' => 'usd', |
| 67 |
'description' => 'Charge for [email protected]', |
| 68 |
'card' => 'tok_visa', |
| 69 |
) |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Create a valid test transfer. |
| 75 |
*/ |
| 76 |
protected static function createTestTransfer(array $attributes = array(), $opts = null) |
| 77 |
{ |
| 78 |
self::authorizeFromEnv(); |
| 79 |
|
| 80 |
$recipient = self::createTestRecipient(); |
| 81 |
|
| 82 |
return Transfer::create( |
| 83 |
$attributes + array( |
| 84 |
'amount' => 2000, |
| 85 |
'currency' => 'usd', |
| 86 |
'description' => 'Transfer to [email protected]', |
| 87 |
'recipient' => $recipient->id |
| 88 |
), |
| 89 |
$opts |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Create a valid test customer. |
| 95 |
*/ |
| 96 |
protected static function createTestCustomer(array $attributes = array()) |
| 97 |
{ |
| 98 |
self::authorizeFromEnv(); |
| 99 |
|
| 100 |
return Customer::create( |
| 101 |
$attributes + array( |
| 102 |
'card' => 'tok_visa', |
| 103 |
) |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Create a valid test recipient |
| 109 |
*/ |
| 110 |
protected static function createTestRecipient(array $attributes = array()) |
| 111 |
{ |
| 112 |
self::authorizeFromEnv(); |
| 113 |
|
| 114 |
return Recipient::create( |
| 115 |
$attributes + array( |
| 116 |
'name' => 'PHP Test', |
| 117 |
'type' => 'individual', |
| 118 |
'tax_id' => '000000000', |
| 119 |
'bank_account' => array( |
| 120 |
'country' => 'US', |
| 121 |
'routing_number' => '110000000', |
| 122 |
'account_number' => '000123456789' |
| 123 |
), |
| 124 |
) |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Create a test account |
| 130 |
*/ |
| 131 |
protected static function createTestAccount(array $attributes = array()) |
| 132 |
{ |
| 133 |
self::authorizeFromEnv(); |
| 134 |
|
| 135 |
return Account::create( |
| 136 |
$attributes + array( |
| 137 |
'managed' => false, |
| 138 |
'country' => 'US', |
| 139 |
'email' => self::generateRandomEmail(), |
| 140 |
) |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Create a test account |
| 146 |
*/ |
| 147 |
protected static function createTestManagedAccount(array $attributes = array()) |
| 148 |
{ |
| 149 |
self::authorizeFromEnv(); |
| 150 |
|
| 151 |
return Account::create( |
| 152 |
$attributes + array( |
| 153 |
'managed' => true, |
| 154 |
'country' => 'US', |
| 155 |
'external_account' => array( |
| 156 |
'object' => 'bank_account', |
| 157 |
'country' => 'US', |
| 158 |
'currency' => 'usd', |
| 159 |
'routing_number' => '110000000', |
| 160 |
'account_number' => '000123456789' |
| 161 |
), |
| 162 |
'legal_entity' => array( |
| 163 |
'type' => 'individual', |
| 164 |
'personal_id_number' => '000000000', |
| 165 |
'type' => 'individual', |
| 166 |
'dob' => array('year' => '1980', 'month' => '01', 'day' => '01'), |
| 167 |
'first_name' => 'John', |
| 168 |
'last_name' => 'Doe', |
| 169 |
'address' => array( |
| 170 |
'line1' => '1234 Main Street', |
| 171 |
'postal_code' => '94110', |
| 172 |
'city' => 'San Francisco' |
| 173 |
), |
| 174 |
'personal_address' => array( |
| 175 |
'line1' => '1234 Main Street', |
| 176 |
'postal_code' => '94110', |
| 177 |
'city' => 'San Francisco' |
| 178 |
) |
| 179 |
), |
| 180 |
'tos_acceptance' => array('date' => time(), 'ip' => '127.0.0.1') |
| 181 |
) |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Verify that a plan with a given ID exists, or create a new one if it does |
| 187 |
* not. |
| 188 |
*/ |
| 189 |
protected static function retrieveOrCreatePlan($id) |
| 190 |
{ |
| 191 |
self::authorizeFromEnv(); |
| 192 |
|
| 193 |
try { |
| 194 |
$plan = Plan::retrieve($id); |
| 195 |
} catch (Error\InvalidRequest $exception) { |
| 196 |
$plan = Plan::create( |
| 197 |
array( |
| 198 |
'id' => $id, |
| 199 |
'amount' => 0, |
| 200 |
'currency' => 'usd', |
| 201 |
'interval' => 'month', |
| 202 |
'name' => 'Gold Test Plan', |
| 203 |
) |
| 204 |
); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Verify that a coupon with a given ID exists, or create a new one if it |
| 210 |
* does not. |
| 211 |
*/ |
| 212 |
protected static function retrieveOrCreateCoupon($id) |
| 213 |
{ |
| 214 |
self::authorizeFromEnv(); |
| 215 |
|
| 216 |
try { |
| 217 |
$coupon = Coupon::retrieve($id); |
| 218 |
} catch (Error\InvalidRequest $exception) { |
| 219 |
$coupon = Coupon::create( |
| 220 |
array( |
| 221 |
'id' => $id, |
| 222 |
'duration' => 'forever', |
| 223 |
'percent_off' => 25, |
| 224 |
) |
| 225 |
); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Generate a semi-random string |
| 231 |
*/ |
| 232 |
protected static function generateRandomString($length = 24) |
| 233 |
{ |
| 234 |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU'; |
| 235 |
$charactersLength = strlen($characters); |
| 236 |
$randomString = ''; |
| 237 |
for ($i = 0; $i < $length; $i++) { |
| 238 |
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
| 239 |
} |
| 240 |
return $randomString; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Generate a semi-random email. |
| 245 |
*/ |
| 246 |
protected static function generateRandomEmail() |
| 247 |
{ |
| 248 |
return 'dev-platform-bots+php-'.self::generateRandomString(12).'@stripe.com'; |
| 249 |
} |
| 250 |
|
| 251 |
protected static function createTestBitcoinReceiver($email) |
| 252 |
{ |
| 253 |
$receiver = BitcoinReceiver::create( |
| 254 |
array( |
| 255 |
'amount' => 100, |
| 256 |
'currency' => 'usd', |
| 257 |
'description' => 'some details', |
| 258 |
'email' => $email |
| 259 |
) |
| 260 |
); |
| 261 |
return $receiver; |
| 262 |
} |
| 263 |
} |
| 264 |
|