DocumentUploadTest.php
4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace Test\Integration;
require_once dirname(__DIR__) . '/Setup.php';
use Test\Setup;
use Braintree;
class DocumentUploadTest extends Setup
{
private $gateway;
private $pngFile;
public function __construct() {
$this->gateway = new Braintree\Gateway([
'environment' => 'development',
'merchantId' => 'integration_merchant_id',
'publicKey' => 'integration_public_key',
'privateKey' => 'integration_private_key'
]);
$this->pngFile = fopen(dirname(__DIR__) . '/fixtures/bt_logo.png', 'rb');
}
public function testCreate_whenValid_returnsSuccessfulResult()
{
$result = Braintree\DocumentUpload::create([
"kind" => Braintree\DocumentUpload::EVIDENCE_DOCUMENT,
"file" => $this->pngFile
]);
$this->assertTrue($result->success);
}
public function testCreate_withUnsupportedFileType_returnsError()
{
$gifFile = fopen(dirname(__DIR__) . '/fixtures/gif_extension_bt_logo.gif', 'rb');
$result = Braintree\DocumentUpload::create([
"kind" => Braintree\DocumentUpload::EVIDENCE_DOCUMENT,
"file" => $gifFile
]);
$error = $result->errors->forKey('documentUpload')->errors[0];
$this->assertEquals(Braintree\Error\Codes::DOCUMENT_UPLOAD_FILE_TYPE_IS_INVALID, $error->code);
}
public function testCreate_withMalformedFile_returnsError()
{
$badPdfFile = fopen(dirname(__DIR__) . '/fixtures/malformed_pdf.pdf', 'rb');
$result = Braintree\DocumentUpload::create([
"kind" => Braintree\DocumentUpload::EVIDENCE_DOCUMENT,
"file" => $badPdfFile
]);
$error = $result->errors->forKey('documentUpload')->errors[0];
$this->assertEquals(Braintree\Error\Codes::DOCUMENT_UPLOAD_FILE_IS_MALFORMED_OR_ENCRYPTED, $error->code);
}
public function testCreate_withInvalidKind_returnsError()
{
$result = Braintree\DocumentUpload::create([
"kind" => "invalid_kind",
"file" => $this->pngFile
]);
$error = $result->errors->forKey('documentUpload')->errors[0];
$this->assertEquals(Braintree\Error\Codes::DOCUMENT_UPLOAD_KIND_IS_INVALID, $error->code);
}
public function testCreate_whenFileIsOver4Mb_returnsError()
{
$bigFile = fopen(dirname(__DIR__) . '/fixtures/large_file.png', 'w+');
foreach(range(0, 1048577) as $i) {
fwrite($bigFile, 'aaaa');
}
fclose($bigFile);
$bigFile = fopen(dirname(__DIR__) . '/fixtures/large_file.png', 'rb');
$result = Braintree\DocumentUpload::create([
"kind" => Braintree\DocumentUpload::EVIDENCE_DOCUMENT,
"file" => $bigFile
]);
$error = $result->errors->forKey('documentUpload')->errors[0];
$this->assertEquals(Braintree\Error\Codes::DOCUMENT_UPLOAD_FILE_IS_TOO_LARGE, $error->code);
}
public function testCreate_whenPDFFileIsOver50Pages_returnsError()
{
$tooLongFile = fopen(dirname(__DIR__) . '/fixtures/too_long.pdf', 'rb');
$result = Braintree\DocumentUpload::create([
"kind" => Braintree\DocumentUpload::EVIDENCE_DOCUMENT,
"file" => $tooLongFile
]);
$error = $result->errors->forKey('documentUpload')->errors[0];
$this->assertEquals(Braintree\Error\Codes::DOCUMENT_UPLOAD_FILE_IS_TOO_LONG, $error->code);
}
public function testCreate_whenInvalidSignature_throwsInvalidArgumentException()
{
$this->setExpectedException('InvalidArgumentException', 'invalid keys: bad_key');
Braintree\DocumentUpload::create([
"kind" => Braintree\DocumentUpload::EVIDENCE_DOCUMENT,
"bad_key" => "value"
]);
}
public function test_create_whenFileIsInvalid_throwsError()
{
$this->setExpectedException('InvalidArgumentException', 'file must be a stream resource');
$result = Braintree\DocumentUpload::create([
"kind" => Braintree\DocumentUpload::EVIDENCE_DOCUMENT,
"file" => "not-a-file"
]);
}
}