DisputeTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. namespace Test\Unit;
  3. require_once dirname(__DIR__) . '/Setup.php';
  4. use DateTime;
  5. use Test\Setup;
  6. use Braintree;
  7. class DisputeTest extends Setup
  8. {
  9. private $attributes;
  10. public function __construct() {
  11. $this->attributes = [
  12. 'amount' => '100.00',
  13. 'amountDisputed' => '100.00',
  14. 'amountWon' => '0.00',
  15. 'caseNumber' => 'CB123456',
  16. 'createdAt' => DateTime::createFromFormat('Ymd-His', '20130410-105039'),
  17. 'currencyIsoCode' => 'USD',
  18. 'dateOpened' => DateTime::createFromFormat('Ymd-His', '20130401-000000'),
  19. 'dateWon' => DateTime::createFromFormat('Ymd-His', '20130402-000000'),
  20. 'forwardedComments' => 'Forwarded comments',
  21. 'id' => '123456',
  22. 'kind' => 'chargeback',
  23. 'merchantAccountId' => 'abc123',
  24. 'originalDisputeId' => 'original_dispute_id',
  25. 'reason' => 'fraud',
  26. 'reasonCode' => '83',
  27. 'reasonDescription' => 'Reason code 83 description',
  28. 'receivedDate' => DateTime::createFromFormat('Ymd-His', '20130410-000410'),
  29. 'referenceNumber' => '123456',
  30. 'replyByDate' => DateTime::createFromFormat('Ymd-His', '20130417-0000417'),
  31. 'status' => 'open',
  32. 'updatedAt' => DateTime::createFromFormat('Ymd-His', '20130410-105039'),
  33. 'evidence' => [[
  34. 'category' => NULL,
  35. 'comment' => NULL,
  36. 'createdAt' => DateTime::createFromFormat('Ymd-His', '20130411-105039'),
  37. 'id' => 'evidence1',
  38. 'sentToProcessorAt' => NULL,
  39. 'sequenceNumber' => NULL,
  40. 'url' => 'url_of_file_evidence',
  41. ],[
  42. 'comment' => 'text evidence',
  43. 'createdAt' => DateTime::createFromFormat('Ymd-His', '20130411-105039'),
  44. 'id' => 'evidence2',
  45. 'sentToProcessorAt' => '2009-04-11',
  46. 'url' => NULL,
  47. ]],
  48. 'statusHistory' => [[
  49. 'effectiveDate' => '2013-04-10',
  50. 'status' => 'open',
  51. 'timestamp' => DateTime::createFromFormat('Ymd-His', '20130410-105039'),
  52. ]],
  53. 'transaction' => [
  54. 'id' => 'transaction_id',
  55. 'amount' => '100.00',
  56. 'createdAt' => DateTime::createFromFormat('Ymd-His', '20130319-105039'),
  57. 'orderId' => NULL,
  58. 'purchaseOrderNumber' => 'po',
  59. 'paymentInstrumentSubtype' => 'Visa',
  60. ]
  61. ];
  62. }
  63. public function testLegacyConstructor()
  64. {
  65. $legacyParams = [
  66. 'transaction' => [
  67. 'id' => 'transaction_id',
  68. 'amount' => '100.00',
  69. ],
  70. 'id' => '123456',
  71. 'currencyIsoCode' => 'USD',
  72. 'status' => 'open',
  73. 'amount' => '100.00',
  74. 'receivedDate' => DateTime::createFromFormat('Ymd-His', '20130410-000410'),
  75. 'replyByDate' => DateTime::createFromFormat('Ymd-His', '20130421-000421'),
  76. 'reason' => 'fraud',
  77. 'transactionIds' => [
  78. 'asdf', 'qwer'
  79. ],
  80. 'dateOpened' => DateTime::createFromFormat('Ymd-His', '20130410-000410'),
  81. 'dateWon' =>DateTime::createFromFormat('Ymd-His', '20130422-000422'),
  82. 'kind' => 'chargeback'
  83. ];
  84. $dispute = Braintree\Dispute::factory($legacyParams);
  85. $this->assertEquals('123456', $dispute->id);
  86. $this->assertEquals('100.00', $dispute->amount);
  87. $this->assertEquals('USD', $dispute->currencyIsoCode);
  88. $this->assertEquals(Braintree\Dispute::FRAUD, $dispute->reason);
  89. $this->assertEquals(Braintree\Dispute::OPEN, $dispute->status);
  90. $this->assertEquals(Braintree\Dispute::Open, $dispute->status);
  91. $this->assertEquals('transaction_id', $dispute->transactionDetails->id);
  92. $this->assertEquals('100.00', $dispute->transactionDetails->amount);
  93. $this->assertEquals(DateTime::createFromFormat('Ymd-His', '20130410-000410'), $dispute->dateOpened);
  94. $this->assertEquals(DateTime::createFromFormat('Ymd-His', '20130422-000422'), $dispute->dateWon);
  95. $this->assertEquals(Braintree\Dispute::CHARGEBACK, $dispute->kind);
  96. }
  97. public function testLegacyParamsWithNewAttributes()
  98. {
  99. $dispute = Braintree\Dispute::factory($this->attributes);
  100. $this->assertEquals('123456', $dispute->id);
  101. $this->assertEquals('100.00', $dispute->amount);
  102. $this->assertEquals('USD', $dispute->currencyIsoCode);
  103. $this->assertEquals(Braintree\Dispute::FRAUD, $dispute->reason);
  104. $this->assertEquals(Braintree\Dispute::Open, $dispute->status);
  105. $this->assertEquals(Braintree\Dispute::OPEN, $dispute->status);
  106. $this->assertEquals('transaction_id', $dispute->transactionDetails->id);
  107. $this->assertEquals('100.00', $dispute->transactionDetails->amount);
  108. $this->assertEquals(DateTime::createFromFormat('Ymd-His', '20130401-000000'), $dispute->dateOpened);
  109. $this->assertEquals(DateTime::createFromFormat('Ymd-His', '20130402-000000'), $dispute->dateWon);
  110. $this->assertEquals(Braintree\Dispute::CHARGEBACK, $dispute->kind);
  111. }
  112. public function testConstructorPopulatesNewFields()
  113. {
  114. $dispute = Braintree\Dispute::factory($this->attributes);
  115. $this->assertEquals("100.00", $dispute->amountDisputed);
  116. $this->assertEquals("0.00", $dispute->amountWon);
  117. $this->assertEquals("CB123456", $dispute->caseNumber);
  118. $this->assertEquals(DateTime::createFromFormat('Ymd-His', '20130410-105039'), $dispute->createdAt);
  119. $this->assertEquals("Forwarded comments", $dispute->forwardedComments);
  120. $this->assertEquals("abc123", $dispute->merchantAccountId);
  121. $this->assertEquals("original_dispute_id", $dispute->originalDisputeId);
  122. $this->assertEquals("83", $dispute->reasonCode);
  123. $this->assertEquals("Reason code 83 description", $dispute->reasonDescription);
  124. $this->assertEquals("123456", $dispute->referenceNumber);
  125. $this->assertEquals(DateTime::createFromFormat('Ymd-His', '20130410-105039'), $dispute->updatedAt);
  126. $this->assertNull($dispute->evidence[0]->comment);
  127. $this->assertEquals(DateTime::createFromFormat('Ymd-His', '20130411-105039'), $dispute->evidence[0]->createdAt);
  128. $this->assertNull($dispute->evidence[0]->category);
  129. $this->assertEquals('evidence1', $dispute->evidence[0]->id);
  130. $this->assertNull($dispute->evidence[0]->sentToProcessorAt);
  131. $this->assertNull($dispute->evidence[0]->sequenceNumber);
  132. $this->assertEquals('url_of_file_evidence', $dispute->evidence[0]->url);
  133. $this->assertEquals('text evidence', $dispute->evidence[1]->comment);
  134. $this->assertEquals(DateTime::createFromFormat('Ymd-His', '20130411-105039'), $dispute->evidence[1]->createdAt);
  135. $this->assertEquals('evidence2', $dispute->evidence[1]->id);
  136. $this->assertEquals('2009-04-11', $dispute->evidence[1]->sentToProcessorAt);
  137. $this->assertNull($dispute->evidence[1]->url);
  138. $this->assertEquals('2013-04-10', $dispute->statusHistory[0]->effectiveDate);
  139. $this->assertEquals('open', $dispute->statusHistory[0]->status);
  140. $this->assertEquals(DateTime::createFromFormat('Ymd-His', '20130410-105039'), $dispute->statusHistory[0]->timestamp);
  141. }
  142. public function testConstructorHandlesNullFields()
  143. {
  144. $emptyAttributes = [
  145. 'amount' => NULL,
  146. 'dateOpened' => NULL,
  147. 'dateWon' => NULL,
  148. 'evidence' => NULL,
  149. 'replyByDate' => NULL,
  150. 'statusHistory' => NULL
  151. ];
  152. $attrs = array_merge([], $this->attributes, $emptyAttributes);
  153. $dispute = Braintree\Dispute::factory($attrs);
  154. $this->assertNull($dispute->amount);
  155. $this->assertNull($dispute->dateOpened);
  156. $this->assertNull($dispute->dateWon);
  157. $this->assertNull($dispute->evidence);
  158. $this->assertNull($dispute->replyByDate);
  159. $this->assertNull($dispute->statusHistory);
  160. }
  161. public function testConstructorPopulatesTransaction()
  162. {
  163. $dispute = Braintree\Dispute::factory($this->attributes);
  164. $this->assertEquals('transaction_id', $dispute->transaction->id);
  165. $this->assertEquals('100.00', $dispute->transaction->amount);
  166. $this->assertEquals(DateTime::createFromFormat('Ymd-His', '20130319-105039'), $dispute->transaction->createdAt);
  167. $this->assertNull($dispute->transaction->orderId);
  168. $this->assertEquals('po', $dispute->transaction->purchaseOrderNumber);
  169. $this->assertEquals('Visa', $dispute->transaction->paymentInstrumentSubtype);
  170. }
  171. public function testAcceptNullRaisesNotFoundException()
  172. {
  173. $this->setExpectedException('Braintree\Exception\NotFound', 'dispute with id "" not found');
  174. Braintree\Dispute::accept(null);
  175. }
  176. public function testAcceptEmptyIdRaisesNotFoundException()
  177. {
  178. $this->setExpectedException('Braintree\Exception\NotFound', 'dispute with id " " not found');
  179. Braintree\Dispute::accept(" ");
  180. }
  181. public function testAddTextEvidenceEmptyIdRaisesNotFoundException()
  182. {
  183. $this->setExpectedException('Braintree\Exception\NotFound', 'dispute with id " " not found');
  184. Braintree\Dispute::addTextEvidence(" ", "evidence");
  185. }
  186. public function testAddTextEvidenceNullIdRaisesNotFoundException()
  187. {
  188. $this->setExpectedException('Braintree\Exception\NotFound', 'dispute with id "" not found');
  189. Braintree\Dispute::addTextEvidence(null, "evidence");
  190. }
  191. public function testAddTextEvidenceEmptyEvidenceRaisesValueException()
  192. {
  193. $this->setExpectedException('InvalidArgumentException', 'content cannot be blank');
  194. Braintree\Dispute::addTextEvidence("disputeId", " ");
  195. }
  196. public function testAddTextEvidenceNullEvidenceRaisesValueException()
  197. {
  198. $this->setExpectedException('InvalidArgumentException', 'content cannot be blank');
  199. Braintree\Dispute::addTextEvidence("disputeId", null);
  200. }
  201. public function testAddTextEvidenceBlankRequestContentRaisesValueException()
  202. {
  203. $this->setExpectedException('InvalidArgumentException', 'content cannot be blank');
  204. Braintree\Dispute::addTextEvidence("disputeId",
  205. [
  206. 'content' => ' ',
  207. 'category' => 'CARRIER_NAME',
  208. 'sequenceNumber' => '0',
  209. ]
  210. );
  211. }
  212. public function testAddTextEvidenceNullRequestContentRaisesValueException()
  213. {
  214. $this->setExpectedException('InvalidArgumentException', 'content cannot be blank');
  215. Braintree\Dispute::addTextEvidence("disputeId",
  216. [
  217. 'content' => null,
  218. 'category' => 'CARRIER_NAME',
  219. 'sequenceNumber' => '0',
  220. ]
  221. );
  222. }
  223. public function testAddTextEvidenceBlankRequestCategoryRaisesValueException()
  224. {
  225. $this->setExpectedException('InvalidArgumentException', 'category cannot be blank');
  226. Braintree\Dispute::addTextEvidence("disputeId",
  227. [
  228. 'content' => 'UPS',
  229. 'category' => '',
  230. 'sequenceNumber' => '0',
  231. ]
  232. );
  233. }
  234. public function testAddTextEvidenceBlankRequestSequenceNumberRaisesValueException()
  235. {
  236. $this->setExpectedException('InvalidArgumentException', 'sequenceNumber cannot be blank');
  237. Braintree\Dispute::addTextEvidence("disputeId",
  238. [
  239. 'content' => 'UPS',
  240. 'category' => 'CARRIER_NAME',
  241. 'sequenceNumber' => '',
  242. ]
  243. );
  244. }
  245. public function testAddTextEvidenceNonIntegerNumberRequestSequenceNumberRaisesValueException()
  246. {
  247. $this->setExpectedException('InvalidArgumentException', 'sequenceNumber must be an int');
  248. Braintree\Dispute::addTextEvidence("disputeId",
  249. [
  250. 'content' => 'UPS',
  251. 'category' => 'CARRIER_NAME',
  252. 'sequenceNumber' => '4.5',
  253. ]
  254. );
  255. }
  256. public function testAddTextEvidenceNonIntegerStringRequestSequenceNumberRaisesValueException()
  257. {
  258. $this->setExpectedException('InvalidArgumentException', 'sequenceNumber must be an int');
  259. Braintree\Dispute::addTextEvidence("disputeId",
  260. [
  261. 'content' => 'UPS',
  262. 'category' => 'CARRIER_NAME',
  263. 'sequenceNumber' => 'Blah',
  264. ]
  265. );
  266. }
  267. public function testAddFileEvidenceEmptyIdRaisesNotFoundException()
  268. {
  269. $this->setExpectedException('Braintree\Exception\NotFound', 'dispute with id " " not found');
  270. Braintree\Dispute::addFileEvidence(" ", 1);
  271. }
  272. public function testAddFileEvidenceNullIdRaisesNotFoundException()
  273. {
  274. $this->setExpectedException('Braintree\Exception\NotFound', 'dispute with id "" not found');
  275. Braintree\Dispute::addFileEvidence(null, 1);
  276. }
  277. public function testAddFileEvidenceEmptyEvidenceRaisesValueException()
  278. {
  279. $this->setExpectedException('Braintree\Exception\NotFound', 'document with id " " not found');
  280. Braintree\Dispute::addFileEvidence("disputeId", " ");
  281. }
  282. public function testAddFileEvidenceNullEvidenceRaisesValueException()
  283. {
  284. $this->setExpectedException('Braintree\Exception\NotFound', 'document with id "" not found');
  285. Braintree\Dispute::addFileEvidence("disputeId", null);
  286. }
  287. public function testAddFileEvidenceBlankRequestContentRaisesValueException()
  288. {
  289. $this->setExpectedException('Braintree\Exception\NotFound', 'document with id " " not found');
  290. Braintree\Dispute::addFileEvidence("disputeId",
  291. [
  292. 'documentId' => ' ',
  293. 'category' => 'GENERAL',
  294. ]
  295. );
  296. }
  297. public function testAddFileEvidenceNullRequestContentRaisesValueException()
  298. {
  299. $this->setExpectedException('Braintree\Exception\NotFound', 'document with id "" not found');
  300. Braintree\Dispute::addFileEvidence("disputeId",
  301. [
  302. 'documentId' => null,
  303. 'category' => 'GENERAL',
  304. ]
  305. );
  306. }
  307. public function testAddFileEvidenceBlankRequestCategoryRaisesValueException()
  308. {
  309. $this->setExpectedException('InvalidArgumentException', 'category cannot be blank');
  310. Braintree\Dispute::addFileEvidence("disputeId",
  311. [
  312. 'documentId' => '123',
  313. 'category' => '',
  314. ]
  315. );
  316. }
  317. public function testFinalizeNullRaisesNotFoundException()
  318. {
  319. $this->setExpectedException('Braintree\Exception\NotFound', 'dispute with id "" not found');
  320. Braintree\Dispute::finalize(null);
  321. }
  322. public function testFinalizeEmptyIdRaisesNotFoundException()
  323. {
  324. $this->setExpectedException('Braintree\Exception\NotFound', 'dispute with id " " not found');
  325. Braintree\Dispute::finalize(" ");
  326. }
  327. public function testFindingNullRaisesNotFoundException()
  328. {
  329. $this->setExpectedException('Braintree\Exception\NotFound', 'dispute with id "" not found');
  330. Braintree\Dispute::find(null);
  331. }
  332. public function testFindingEmptyIdRaisesNotFoundException()
  333. {
  334. $this->setExpectedException('Braintree\Exception\NotFound', 'dispute with id " " not found');
  335. Braintree\Dispute::find(" ");
  336. }
  337. public function testRemoveEvidenceEmptyDisputeIdRaisesNotFoundException()
  338. {
  339. $this->setExpectedException('Braintree\Exception\NotFound', 'evidence with id "evidence" for dispute with id " " not found');
  340. Braintree\Dispute::removeEvidence(" ", "evidence");
  341. }
  342. public function testRemoveEvidenceNullDisputeIdRaisesNotFoundException()
  343. {
  344. $this->setExpectedException('Braintree\Exception\NotFound', 'evidence with id "evidence" for dispute with id "" not found');
  345. Braintree\Dispute::removeEvidence(null, "evidence");
  346. }
  347. public function testRemoveEvidenceEvidenceNullIdRaisesNotFoundException()
  348. {
  349. $this->setExpectedException('Braintree\Exception\NotFound', 'evidence with id "" for dispute with id "dispute_id" not found');
  350. Braintree\Dispute::removeEvidence("dispute_id", null);
  351. }
  352. public function testRemoveEvidenceEmptyEvidenceIdRaisesValueException()
  353. {
  354. $this->setExpectedException('Braintree\Exception\NotFound', 'evidence with id " " for dispute with id "dispute_id" not found');
  355. Braintree\Dispute::removeEvidence("dispute_id", " ");
  356. }
  357. }