From 568748db1b667338ed228401b05be02e756212d2 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Mon, 17 Sep 2018 17:27:52 +0200 Subject: [PATCH 1/3] Handle `file` objects like `file_upload` --- init.php | 2 +- lib/{FileUpload.php => File.php} | 15 ++++-- lib/Util/Util.php | 3 +- tests/Stripe/FileTest.php | 84 ++++++++++++++++++++++++++++++++ tests/Stripe/FileUploadTest.php | 16 ++++++ 5 files changed, 114 insertions(+), 6 deletions(-) rename lib/{FileUpload.php => File.php} (51%) create mode 100644 tests/Stripe/FileTest.php 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/FileUpload.php b/lib/File.php similarity index 51% rename from lib/FileUpload.php rename to lib/File.php index 6a012f6e0..21413cc12 100644 --- a/lib/FileUpload.php +++ b/lib/File.php @@ -3,7 +3,7 @@ namespace Stripe; /** - * Class FileUpload + * Class File * * @property string $id * @property string $object @@ -16,10 +16,14 @@ * * @package Stripe */ -class FileUpload extends ApiResource +class File extends ApiResource { - - const OBJECT_NAME = "file_upload"; + // This resource can have two different object names. In latter API + // versions, only `file` is used, but since stripe-php may be used with + // any API version, we need to support deserializing the older + // `file_upload` object into the same class. + const OBJECT_NAME = "file"; + const OBJECT_NAME_ALT = "file_upload"; use ApiOperations\All; use ApiOperations\Create; @@ -35,3 +39,6 @@ public static function classUrl() return '/v1/files'; } } + +// For backwards compatibility, the `File` class is aliased to `FileUpload`. +class_alias('Stripe\\File', 'Stripe\\FileUpload'); 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/FileTest.php b/tests/Stripe/FileTest.php new file mode 100644 index 000000000..b17936206 --- /dev/null +++ b/tests/Stripe/FileTest.php @@ -0,0 +1,84 @@ +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 testIsCreatableWithFileHandle() + { + $this->expectsRequest( + 'post', + '/v1/files', + null, + ['Content-Type: multipart/form-data'], + true + ); + $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 + ); + $curlFile = new \CurlFile(dirname(__FILE__) . '/../data/test.png'); + $resource = File::create([ + "purpose" => "dispute_evidence", + "file" => $curlFile, + ]); + $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/FileUploadTest.php b/tests/Stripe/FileUploadTest.php index a4f9a849e..889fa1048 100644 --- a/tests/Stripe/FileUploadTest.php +++ b/tests/Stripe/FileUploadTest.php @@ -65,4 +65,20 @@ public function testIsCreatableWithCurlFile() ]); $this->assertInstanceOf("Stripe\\FileUpload", $resource); } + + public function testDeserializesFromFile() + { + $obj = Util\Util::convertToStripeObject([ + 'object' => 'file', + ], null); + $this->assertInstanceOf("Stripe\\FileUpload", $obj); + } + + public function testDeserializesFromFileUpload() + { + $obj = Util\Util::convertToStripeObject([ + 'object' => 'file_upload', + ], null); + $this->assertInstanceOf("Stripe\\FileUpload", $obj); + } } From 71443145e0f9d066274aedc2c5add0f8d299d844 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Sat, 22 Sep 2018 17:21:44 +0200 Subject: [PATCH 2/3] Upgrade stripe-mock, and fix test --- .travis.yml | 2 +- tests/Stripe/Issuing/DisputeTest.php | 2 +- tests/bootstrap.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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/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/bootstrap.php b/tests/bootstrap.php index 0988db505..1bf4aa041 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,6 +1,6 @@ Date: Sat, 22 Sep 2018 22:07:57 +0200 Subject: [PATCH 3/3] Only use files.stripe.com for file creation requests --- lib/ApiOperations/Request.php | 3 +- lib/File.php | 22 ++++++-- lib/Stripe.php | 2 +- lib/Util/RequestOptions.php | 17 ++++-- tests/Stripe/FileCreationTest.php | 69 +++++++++++++++++++++++++ tests/Stripe/FileTest.php | 39 -------------- tests/Stripe/FileUploadCreationTest.php | 69 +++++++++++++++++++++++++ tests/Stripe/FileUploadTest.php | 39 -------------- tests/TestCase.php | 12 ++--- 9 files changed, 175 insertions(+), 97 deletions(-) create mode 100644 tests/Stripe/FileCreationTest.php create mode 100644 tests/Stripe/FileUploadCreationTest.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 index 21413cc12..dd1c55993 100644 --- a/lib/File.php +++ b/lib/File.php @@ -26,17 +26,29 @@ class File extends ApiResource const OBJECT_NAME_ALT = "file_upload"; use ApiOperations\All; - use ApiOperations\Create; + use ApiOperations\Create { + create as protected _create; + } use ApiOperations\Retrieve; - public static function baseUrl() + public static function classUrl() { - return Stripe::$apiUploadBase; + return '/v1/files'; } - public static function classUrl() + /** + * @param array|null $params + * @param array|string|null $options + * + * @return \Stripe\File The created resource. + */ + public static function create($params = null, $options = null) { - return '/v1/files'; + $opts = \Stripe\Util\RequestOptions::parse($options); + if (is_null($opts->apiBase)) { + $opts->apiBase = Stripe::$apiUploadBase; + } + return static::_create($params, $opts); } } diff --git a/lib/Stripe.php b/lib/Stripe.php index d861106af..2005143d6 100644 --- a/lib/Stripe.php +++ b/lib/Stripe.php @@ -22,7 +22,7 @@ class Stripe public static $connectBase = 'https://connect.stripe.com'; // @var string The base URL for the Stripe API uploads endpoint. - public static $apiUploadBase = 'https://uploads.stripe.com'; + public static $apiUploadBase = 'https://files.stripe.com'; // @var string|null The version of the Stripe API to use for requests. public static $apiVersion = null; diff --git a/lib/Util/RequestOptions.php b/lib/Util/RequestOptions.php index a31f4d322..495236224 100644 --- a/lib/Util/RequestOptions.php +++ b/lib/Util/RequestOptions.php @@ -16,11 +16,13 @@ class RequestOptions public $headers; public $apiKey; + public $apiBase; - public function __construct($key = null, $headers = []) + public function __construct($key = null, $headers = [], $base = null) { $this->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/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 index b17936206..5ea89184c 100644 --- a/tests/Stripe/FileTest.php +++ b/tests/Stripe/FileTest.php @@ -27,45 +27,6 @@ public function testIsRetrievable() $this->assertInstanceOf("Stripe\\File", $resource); } - public function testIsCreatableWithFileHandle() - { - $this->expectsRequest( - 'post', - '/v1/files', - null, - ['Content-Type: multipart/form-data'], - true - ); - $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 - ); - $curlFile = new \CurlFile(dirname(__FILE__) . '/../data/test.png'); - $resource = File::create([ - "purpose" => "dispute_evidence", - "file" => $curlFile, - ]); - $this->assertInstanceOf("Stripe\\File", $resource); - } - public function testDeserializesFromFile() { $obj = Util\Util::convertToStripeObject([ 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 889fa1048..4b23de67f 100644 --- a/tests/Stripe/FileUploadTest.php +++ b/tests/Stripe/FileUploadTest.php @@ -27,45 +27,6 @@ public function testIsRetrievable() $this->assertInstanceOf("Stripe\\FileUpload", $resource); } - public function testIsCreatableWithFileHandle() - { - $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); - } - - 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 - ); - $curlFile = new \CurlFile(dirname(__FILE__) . '/../data/test.png'); - $resource = FileUpload::create([ - "purpose" => "dispute_evidence", - "file" => $curlFile, - ]); - $this->assertInstanceOf("Stripe\\FileUpload", $resource); - } - public function testDeserializesFromFile() { $obj = Util\Util::convertToStripeObject([ 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();