diff --git a/.travis.yml b/.travis.yml index 4a3c854dc..7cd031e41 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ php: env: global: - - STRIPE_MOCK_VERSION=0.30.0 + - STRIPE_MOCK_VERSION=0.32.0 matrix: - AUTOLOAD=1 - AUTOLOAD=0 diff --git a/init.php b/init.php index 377c187ba..3f0e5f985 100644 --- a/init.php +++ b/init.php @@ -75,8 +75,8 @@ require(dirname(__FILE__) . '/lib/EphemeralKey.php'); require(dirname(__FILE__) . '/lib/Event.php'); require(dirname(__FILE__) . '/lib/ExchangeRate.php'); +require(dirname(__FILE__) . '/lib/File.php'); require(dirname(__FILE__) . '/lib/FileLink.php'); -require(dirname(__FILE__) . '/lib/FileUpload.php'); require(dirname(__FILE__) . '/lib/Invoice.php'); require(dirname(__FILE__) . '/lib/InvoiceItem.php'); require(dirname(__FILE__) . '/lib/InvoiceLineItem.php'); diff --git a/lib/ApiOperations/Request.php b/lib/ApiOperations/Request.php index c6b06585a..dd048dc5f 100644 --- a/lib/ApiOperations/Request.php +++ b/lib/ApiOperations/Request.php @@ -52,7 +52,8 @@ protected function _request($method, $url, $params = [], $options = null) protected static function _staticRequest($method, $url, $params, $options) { $opts = \Stripe\Util\RequestOptions::parse($options); - $requestor = new \Stripe\ApiRequestor($opts->apiKey, static::baseUrl()); + $baseUrl = isset($opts->apiBase) ? $opts->apiBase : static::baseUrl(); + $requestor = new \Stripe\ApiRequestor($opts->apiKey, $baseUrl); list($response, $opts->apiKey) = $requestor->request($method, $url, $params, $opts->headers); $opts->discardNonPersistentHeaders(); return [$response, $opts]; diff --git a/lib/File.php b/lib/File.php new file mode 100644 index 000000000..dd1c55993 --- /dev/null +++ b/lib/File.php @@ -0,0 +1,56 @@ +apiBase)) { + $opts->apiBase = Stripe::$apiUploadBase; + } + return static::_create($params, $opts); + } +} + +// For backwards compatibility, the `File` class is aliased to `FileUpload`. +class_alias('Stripe\\File', 'Stripe\\FileUpload'); diff --git a/lib/FileUpload.php b/lib/FileUpload.php deleted file mode 100644 index 6a012f6e0..000000000 --- a/lib/FileUpload.php +++ /dev/null @@ -1,37 +0,0 @@ -apiKey = $key; $this->headers = $headers; + $this->apiBase = $base; } /** @@ -36,6 +38,9 @@ public function merge($options) if ($other_options->apiKey === null) { $other_options->apiKey = $this->apiKey; } + if ($other_options->apiBase === null) { + $other_options->apiBase = $this->apiBase; + } $other_options->headers = array_merge($this->headers, $other_options->headers); return $other_options; } @@ -65,16 +70,17 @@ public static function parse($options) } if (is_null($options)) { - return new RequestOptions(null, []); + return new RequestOptions(null, [], null); } if (is_string($options)) { - return new RequestOptions($options, []); + return new RequestOptions($options, [], null); } if (is_array($options)) { $headers = []; $key = null; + $base = null; if (array_key_exists('api_key', $options)) { $key = $options['api_key']; } @@ -87,7 +93,10 @@ public static function parse($options) if (array_key_exists('stripe_version', $options)) { $headers['Stripe-Version'] = $options['stripe_version']; } - return new RequestOptions($key, $headers); + if (array_key_exists('api_base', $options)) { + $base = $options['api_base']; + } + return new RequestOptions($key, $headers, $base); } $message = 'The second argument to Stripe API method calls is an ' diff --git a/lib/Util/Util.php b/lib/Util/Util.php index 49275d370..e11efbebe 100644 --- a/lib/Util/Util.php +++ b/lib/Util/Util.php @@ -90,8 +90,9 @@ public static function convertToStripeObject($resp, $opts) \Stripe\Event::OBJECT_NAME => 'Stripe\\Event', \Stripe\ExchangeRate::OBJECT_NAME => 'Stripe\\ExchangeRate', \Stripe\ApplicationFeeRefund::OBJECT_NAME => 'Stripe\\ApplicationFeeRefund', + \Stripe\File::OBJECT_NAME => 'Stripe\\File', + \Stripe\File::OBJECT_NAME_ALT => 'Stripe\\File', \Stripe\FileLink::OBJECT_NAME => 'Stripe\\FileLink', - \Stripe\FileUpload::OBJECT_NAME => 'Stripe\\FileUpload', \Stripe\Invoice::OBJECT_NAME => 'Stripe\\Invoice', \Stripe\InvoiceItem::OBJECT_NAME => 'Stripe\\InvoiceItem', \Stripe\InvoiceLineItem::OBJECT_NAME => 'Stripe\\InvoiceLineItem', diff --git a/tests/Stripe/FileCreationTest.php b/tests/Stripe/FileCreationTest.php new file mode 100644 index 000000000..9a65452c6 --- /dev/null +++ b/tests/Stripe/FileCreationTest.php @@ -0,0 +1,69 @@ +expectsRequest( + 'post', + '/v1/files', + null, + ['Content-Type: multipart/form-data'], + true, + Stripe::$apiUploadBase + ); + $fp = fopen(dirname(__FILE__) . '/../data/test.png', 'r'); + $resource = File::create([ + "purpose" => "dispute_evidence", + "file" => $fp, + ]); + $this->assertInstanceOf("Stripe\\File", $resource); + } + + public function testIsCreatableWithCurlFile() + { + if (!class_exists('\CurlFile', false)) { + // Older PHP versions don't support this + return; + } + + $this->expectsRequest( + 'post', + '/v1/files', + null, + ['Content-Type: multipart/form-data'], + true, + Stripe::$apiUploadBase + ); + $curlFile = new \CurlFile(dirname(__FILE__) . '/../data/test.png'); + $resource = File::create([ + "purpose" => "dispute_evidence", + "file" => $curlFile, + ]); + $this->assertInstanceOf("Stripe\\File", $resource); + } +} diff --git a/tests/Stripe/FileTest.php b/tests/Stripe/FileTest.php new file mode 100644 index 000000000..5ea89184c --- /dev/null +++ b/tests/Stripe/FileTest.php @@ -0,0 +1,45 @@ +expectsRequest( + 'get', + '/v1/files' + ); + $resources = File::all(); + $this->assertTrue(is_array($resources->data)); + $this->assertInstanceOf("Stripe\\File", $resources->data[0]); + } + + public function testIsRetrievable() + { + $this->expectsRequest( + 'get', + '/v1/files/' . self::TEST_RESOURCE_ID + ); + $resource = File::retrieve(self::TEST_RESOURCE_ID); + $this->assertInstanceOf("Stripe\\File", $resource); + } + + public function testDeserializesFromFile() + { + $obj = Util\Util::convertToStripeObject([ + 'object' => 'file', + ], null); + $this->assertInstanceOf("Stripe\\File", $obj); + } + + public function testDeserializesFromFileUpload() + { + $obj = Util\Util::convertToStripeObject([ + 'object' => 'file_upload', + ], null); + $this->assertInstanceOf("Stripe\\File", $obj); + } +} diff --git a/tests/Stripe/FileUploadCreationTest.php b/tests/Stripe/FileUploadCreationTest.php new file mode 100644 index 000000000..8d5bd870e --- /dev/null +++ b/tests/Stripe/FileUploadCreationTest.php @@ -0,0 +1,69 @@ +expectsRequest( + 'post', + '/v1/files', + null, + ['Content-Type: multipart/form-data'], + true, + Stripe::$apiUploadBase + ); + $fp = fopen(dirname(__FILE__) . '/../data/test.png', 'r'); + $resource = FileUpload::create([ + "purpose" => "dispute_evidence", + "file" => $fp, + ]); + $this->assertInstanceOf("Stripe\\FileUpload", $resource); + } + + public function testIsCreatableWithCurlFile() + { + if (!class_exists('\CurlFile', false)) { + // Older PHP versions don't support this + return; + } + + $this->expectsRequest( + 'post', + '/v1/files', + null, + ['Content-Type: multipart/form-data'], + true, + Stripe::$apiUploadBase + ); + $curlFile = new \CurlFile(dirname(__FILE__) . '/../data/test.png'); + $resource = FileUpload::create([ + "purpose" => "dispute_evidence", + "file" => $curlFile, + ]); + $this->assertInstanceOf("Stripe\\FileUpload", $resource); + } +} diff --git a/tests/Stripe/FileUploadTest.php b/tests/Stripe/FileUploadTest.php index a4f9a849e..4b23de67f 100644 --- a/tests/Stripe/FileUploadTest.php +++ b/tests/Stripe/FileUploadTest.php @@ -27,42 +27,19 @@ public function testIsRetrievable() $this->assertInstanceOf("Stripe\\FileUpload", $resource); } - public function testIsCreatableWithFileHandle() + public function testDeserializesFromFile() { - $this->expectsRequest( - 'post', - '/v1/files', - null, - ['Content-Type: multipart/form-data'], - true - ); - $fp = fopen(dirname(__FILE__) . '/../data/test.png', 'r'); - $resource = FileUpload::create([ - "purpose" => "dispute_evidence", - "file" => $fp, - ]); - $this->assertInstanceOf("Stripe\\FileUpload", $resource); + $obj = Util\Util::convertToStripeObject([ + 'object' => 'file', + ], null); + $this->assertInstanceOf("Stripe\\FileUpload", $obj); } - public function testIsCreatableWithCurlFile() + public function testDeserializesFromFileUpload() { - if (!class_exists('\CurlFile', false)) { - // Older PHP versions don't support this - return; - } - - $this->expectsRequest( - 'post', - '/v1/files', - null, - ['Content-Type: multipart/form-data'], - true - ); - $curlFile = new \CurlFile(dirname(__FILE__) . '/../data/test.png'); - $resource = FileUpload::create([ - "purpose" => "dispute_evidence", - "file" => $curlFile, - ]); - $this->assertInstanceOf("Stripe\\FileUpload", $resource); + $obj = Util\Util::convertToStripeObject([ + 'object' => 'file_upload', + ], null); + $this->assertInstanceOf("Stripe\\FileUpload", $obj); } } diff --git a/tests/Stripe/Issuing/DisputeTest.php b/tests/Stripe/Issuing/DisputeTest.php index 7cbc5ebad..068868d30 100644 --- a/tests/Stripe/Issuing/DisputeTest.php +++ b/tests/Stripe/Issuing/DisputeTest.php @@ -10,7 +10,7 @@ public function testIsCreatable() { $params = [ "reason" => "fraudulent", - "transaction" => "ipi_123", + "disputed_transaction" => "ipi_123", ]; $this->expectsRequest( diff --git a/tests/TestCase.php b/tests/TestCase.php index b013003f0..93be3d128 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -13,9 +13,6 @@ class TestCase extends \PHPUnit_Framework_TestCase /** @var string original API key */ protected $origApiKey; - /** @var string original upload base URL */ - protected $origApiUploadBase; - /** @var string original client ID */ protected $origClientId; @@ -33,7 +30,6 @@ protected function setUp() // Save original values so that we can restore them after running tests $this->origApiBase = Stripe::$apiBase; $this->origApiKey = Stripe::getApiKey(); - $this->origApiUploadBase = Stripe::$apiUploadBase; $this->origClientId = Stripe::getClientId(); $this->origApiVersion = Stripe::getApiVersion(); $this->origAccountId = Stripe::getAccountId(); @@ -41,7 +37,6 @@ protected function setUp() // Set up host and credentials for stripe-mock Stripe::$apiBase = "http://localhost:" . MOCK_PORT; Stripe::setApiKey("sk_test_123"); - Stripe::$apiUploadBase = "http://localhost:" . MOCK_PORT; Stripe::setClientId("ca_123"); Stripe::setApiVersion(null); Stripe::setAccountId(null); @@ -58,7 +53,6 @@ protected function tearDown() // Restore original values Stripe::$apiBase = $this->origApiBase; Stripe::setApiKey($this->origApiKey); - Stripe::$apiUploadBase = $this->origApiUploadBase; Stripe::setClientId($this->origClientId); Stripe::setApiVersion($this->origApiVersion); Stripe::setAccountId($this->origAccountId); @@ -76,15 +70,17 @@ protected function tearDown() * exhaustive. If null, headers are not checked. * @param bool $hasFile Whether the request parameters contains a file. * Defaults to false. + * @param string|null $base base URL (e.g. 'https://api.stripe.com') */ protected function expectsRequest( $method, $path, $params = null, $headers = null, - $hasFile = false + $hasFile = false, + $base = null ) { - $this->prepareRequestMock($method, $path, $params, $headers, $hasFile) + $this->prepareRequestMock($method, $path, $params, $headers, $hasFile, $base) ->will($this->returnCallback( function ($method, $absUrl, $headers, $params, $hasFile) { $curlClient = HttpClient\CurlClient::instance(); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 0988db505..1bf4aa041 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,6 +1,6 @@