SubjectReaderTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Test\Unit\Gateway;
  7. use Braintree\Result\Successful;
  8. use Braintree\Transaction;
  9. use Magento\Braintree\Gateway\SubjectReader;
  10. /**
  11. * Class SubjectReaderTest
  12. */
  13. class SubjectReaderTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var SubjectReader
  17. */
  18. private $subjectReader;
  19. /**
  20. * @inheritdoc
  21. */
  22. protected function setUp()
  23. {
  24. $this->subjectReader = new SubjectReader();
  25. }
  26. /**
  27. * @covers \Magento\Braintree\Gateway\SubjectReader::readCustomerId
  28. * @expectedException \InvalidArgumentException
  29. * @expectedExceptionMessage The "customerId" field does not exists
  30. * @return void
  31. */
  32. public function testReadCustomerIdWithException(): void
  33. {
  34. $this->subjectReader->readCustomerId([]);
  35. }
  36. /**
  37. * @covers \Magento\Braintree\Gateway\SubjectReader::readCustomerId
  38. * @return void
  39. */
  40. public function testReadCustomerId(): void
  41. {
  42. $customerId = 1;
  43. $this->assertEquals($customerId, $this->subjectReader->readCustomerId(['customer_id' => $customerId]));
  44. }
  45. /**
  46. * @covers \Magento\Braintree\Gateway\SubjectReader::readPublicHash
  47. * @expectedException \InvalidArgumentException
  48. * @expectedExceptionMessage The "public_hash" field does not exists
  49. * @return void
  50. */
  51. public function testReadPublicHashWithException(): void
  52. {
  53. $this->subjectReader->readPublicHash([]);
  54. }
  55. /**
  56. * @covers \Magento\Braintree\Gateway\SubjectReader::readPublicHash
  57. * @return void
  58. */
  59. public function testReadPublicHash(): void
  60. {
  61. $hash = 'fj23djf2o1fd';
  62. $this->assertEquals($hash, $this->subjectReader->readPublicHash(['public_hash' => $hash]));
  63. }
  64. /**
  65. * @covers \Magento\Braintree\Gateway\SubjectReader::readPayPal
  66. * @expectedException \InvalidArgumentException
  67. * @expectedExceptionMessage Transaction has't paypal attribute
  68. * @return void
  69. */
  70. public function testReadPayPalWithException(): void
  71. {
  72. $transaction = Transaction::factory([
  73. 'id' => 'u38rf8kg6vn',
  74. ]);
  75. $this->subjectReader->readPayPal($transaction);
  76. }
  77. /**
  78. * @covers \Magento\Braintree\Gateway\SubjectReader::readPayPal
  79. * @return void
  80. */
  81. public function testReadPayPal(): void
  82. {
  83. $paypal = [
  84. 'paymentId' => '3ek7dk7fn0vi1',
  85. 'payerEmail' => 'payer@example.com',
  86. ];
  87. $transaction = Transaction::factory([
  88. 'id' => '4yr95vb',
  89. 'paypal' => $paypal,
  90. ]);
  91. $this->assertEquals($paypal, $this->subjectReader->readPayPal($transaction));
  92. }
  93. /**
  94. * Checks a case when subject reader retrieves successful Braintree transaction.
  95. *
  96. * @return void
  97. */
  98. public function testReadTransaction(): void
  99. {
  100. $transaction = Transaction::factory(['id' => 1]);
  101. $response = [
  102. 'object' => new Successful($transaction, 'transaction'),
  103. ];
  104. $actual = $this->subjectReader->readTransaction($response);
  105. $this->assertSame($transaction, $actual);
  106. }
  107. /**
  108. * Checks a case when subject reader retrieves invalid data instead transaction details.
  109. *
  110. * @param array $response
  111. * @param string $expectedMessage
  112. * @dataProvider invalidTransactionResponseDataProvider
  113. * @expectedException \InvalidArgumentException
  114. * @return void
  115. */
  116. public function testReadTransactionWithInvalidResponse(array $response, string $expectedMessage): void
  117. {
  118. $this->expectExceptionMessage($expectedMessage);
  119. $this->subjectReader->readTransaction($response);
  120. }
  121. /**
  122. * Gets list of variations with invalid subject data.
  123. *
  124. * @return array
  125. */
  126. public function invalidTransactionResponseDataProvider(): array
  127. {
  128. $transaction = new \stdClass();
  129. $response = new \stdClass();
  130. $response->transaction = $transaction;
  131. return [
  132. [
  133. 'response' => [
  134. 'object' => [],
  135. ],
  136. 'expectedMessage' => 'Response object does not exist.',
  137. ],
  138. [
  139. 'response' => [
  140. 'object' => new \stdClass(),
  141. ],
  142. 'expectedMessage' => 'The object is not a class \Braintree\Transaction.',
  143. ],
  144. [
  145. 'response' => [
  146. 'object' => $response,
  147. ],
  148. 'expectedMessage' => 'The object is not a class \Braintree\Transaction.',
  149. ],
  150. ];
  151. }
  152. }