Skip to content

Commit

Permalink
Merge pull request #520 from stripe/ob-file-resource
Browse files Browse the repository at this point in the history
Handle `file` objects like `file_upload`
  • Loading branch information
ob-stripe authored Sep 24, 2018
2 parents 1957f7c + e32b1b7 commit 000c46a
Show file tree
Hide file tree
Showing 15 changed files with 275 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ php:

env:
global:
- STRIPE_MOCK_VERSION=0.30.0
- STRIPE_MOCK_VERSION=0.32.0
matrix:
- AUTOLOAD=1
- AUTOLOAD=0
Expand Down
2 changes: 1 addition & 1 deletion init.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion lib/ApiOperations/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
56 changes: 56 additions & 0 deletions lib/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Stripe;

/**
* Class File
*
* @property string $id
* @property string $object
* @property int $created
* @property string $filename
* @property string $purpose
* @property int $size
* @property string $type
* @property string $url
*
* @package Stripe
*/
class File extends ApiResource
{
// 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 {
create as protected _create;
}
use ApiOperations\Retrieve;

public static function classUrl()
{
return '/v1/files';
}

/**
* @param array|null $params
* @param array|string|null $options
*
* @return \Stripe\File The created resource.
*/
public static function create($params = null, $options = null)
{
$opts = \Stripe\Util\RequestOptions::parse($options);
if (is_null($opts->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');
37 changes: 0 additions & 37 deletions lib/FileUpload.php

This file was deleted.

2 changes: 1 addition & 1 deletion lib/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 13 additions & 4 deletions lib/Util/RequestOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}
Expand Down Expand Up @@ -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'];
}
Expand All @@ -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 '
Expand Down
3 changes: 2 additions & 1 deletion lib/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
69 changes: 69 additions & 0 deletions tests/Stripe/FileCreationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Stripe;

/*
* These tests should really be part of `FileTest`, but because the file creation requests use a
* different host, the tests for these methods need their own setup and teardown methods.
*/
class FileCreationTest extends TestCase
{
/**
* @before
*/
public function setUpUploadBase()
{
Stripe::$apiUploadBase = Stripe::$apiBase;
Stripe::$apiBase = null;
}

/**
* @after
*/
public function tearDownUploadBase()
{
Stripe::$apiBase = Stripe::$apiUploadBase;
Stripe::$apiUploadBase = 'https://files.stripe.com';
}

public function testIsCreatableWithFileHandle()
{
$this->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);
}
}
45 changes: 45 additions & 0 deletions tests/Stripe/FileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Stripe;

class FileTest extends TestCase
{
const TEST_RESOURCE_ID = 'file_123';

public function testIsListable()
{
$this->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);
}
}
69 changes: 69 additions & 0 deletions tests/Stripe/FileUploadCreationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Stripe;

/*
* These tests should really be part of `FileUploadTest`, but because the file creation requests
* use a different host, the tests for these methods need their own setup and teardown methods.
*/
class FileUploadCreationTest extends TestCase
{
/**
* @before
*/
public function setUpUploadBase()
{
Stripe::$apiUploadBase = Stripe::$apiBase;
Stripe::$apiBase = null;
}

/**
* @after
*/
public function tearDownUploadBase()
{
Stripe::$apiBase = Stripe::$apiUploadBase;
Stripe::$apiUploadBase = 'https://files.stripe.com';
}

public function testIsCreatableWithFileHandle()
{
$this->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);
}
}
Loading

0 comments on commit 000c46a

Please sign in to comment.