DocumentUploadTest.php 3.6 KB

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