Skip to content

Commit

Permalink
Handle file objects like file_upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Sep 17, 2018
1 parent b078870 commit 79b6a25
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 6 deletions.
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
15 changes: 11 additions & 4 deletions lib/FileUpload.php → lib/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Stripe;

/**
* Class FileUpload
* Class File
*
* @property string $id
* @property string $object
Expand All @@ -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;
Expand All @@ -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');
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
84 changes: 84 additions & 0 deletions tests/Stripe/FileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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 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);
}
}
16 changes: 16 additions & 0 deletions tests/Stripe/FileUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 79b6a25

Please sign in to comment.