TransactionMapTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Test\Unit\Model\Report;
  7. use Braintree\Transaction;
  8. use Braintree\Transaction\PayPalDetails;
  9. use DateTime;
  10. use Magento\Braintree\Model\Report\Row\TransactionMap;
  11. use Magento\Framework\Api\AttributeValue;
  12. use Magento\Framework\Api\AttributeValueFactory;
  13. use Magento\Framework\Phrase;
  14. use Magento\Framework\Phrase\RendererInterface;
  15. use Magento\Store\Model\StoreManagerInterface;
  16. /**
  17. * Class TransactionMapTest
  18. *
  19. * Test for class \Magento\Braintree\Model\Report\\Row\TransactionMap
  20. */
  21. class TransactionMapTest extends \PHPUnit\Framework\TestCase
  22. {
  23. /**
  24. * @var Transaction|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $transactionStub;
  27. /**
  28. * @var AttributeValueFactory|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $attributeValueFactoryMock;
  31. /**
  32. * @var RendererInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $defaultRenderer;
  35. /**
  36. * @var RendererInterface|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $rendererMock;
  39. /**
  40. * Setup
  41. */
  42. protected function setUp()
  43. {
  44. $this->attributeValueFactoryMock = $this->getMockBuilder(AttributeValueFactory::class)
  45. ->setMethods(['create'])
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->defaultRenderer = Phrase::getRenderer();
  49. $this->rendererMock = $this->getMockBuilder(RendererInterface::class)
  50. ->getMock();
  51. }
  52. /**
  53. * Get items
  54. *
  55. * @param array $transaction
  56. * @dataProvider getConfigDataProvider
  57. */
  58. public function testGetCustomAttributes($transaction)
  59. {
  60. $this->transactionStub = Transaction::factory($transaction);
  61. $fields = TransactionMap::$simpleFieldsMap;
  62. $fieldsQty = count($fields);
  63. $this->attributeValueFactoryMock->expects($this->exactly($fieldsQty))
  64. ->method('create')
  65. ->willReturnCallback(function () {
  66. return new AttributeValue();
  67. });
  68. $map = new TransactionMap(
  69. $this->attributeValueFactoryMock,
  70. $this->transactionStub
  71. );
  72. Phrase::setRenderer($this->rendererMock);
  73. /** @var AttributeValue[] $result */
  74. $result = $map->getCustomAttributes();
  75. $this->assertEquals($fieldsQty, count($result));
  76. $this->assertInstanceOf(AttributeValue::class, $result[1]);
  77. $this->assertEquals($transaction['id'], $result[0]->getValue());
  78. $this->assertEquals($transaction['paypalDetails']->paymentId, $result[4]->getValue());
  79. $this->assertEquals(
  80. $transaction['createdAt']->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
  81. $result[6]->getValue()
  82. );
  83. $this->assertEquals(implode(', ', $transaction['refundIds']), $result[11]->getValue());
  84. $this->assertEquals($transaction['merchantAccountId'], $result[1]->getValue());
  85. $this->assertEquals($transaction['orderId'], $result[2]->getValue());
  86. $this->assertEquals($transaction['amount'], $result[7]->getValue());
  87. $this->assertEquals($transaction['processorSettlementResponseCode'], $result[8]->getValue());
  88. $this->assertEquals($transaction['processorSettlementResponseText'], $result[10]->getValue());
  89. $this->assertEquals($transaction['settlementBatchId'], $result[12]->getValue());
  90. $this->assertEquals($transaction['currencyIsoCode'], $result[13]->getValue());
  91. $this->rendererMock->expects($this->at(0))
  92. ->method('render')
  93. ->with([$transaction['paymentInstrumentType']])
  94. ->willReturn('Credit card');
  95. $this->assertEquals('Credit card', $result[3]->getValue()->render());
  96. $this->rendererMock->expects($this->at(0))
  97. ->method('render')
  98. ->with([$transaction['type']])
  99. ->willReturn('Sale');
  100. $this->assertEquals('Sale', $result[5]->getValue()->render());
  101. $this->rendererMock->expects($this->at(0))
  102. ->method('render')
  103. ->with([$transaction['status']])
  104. ->willReturn('Pending for settlement');
  105. $this->assertEquals('Pending for settlement', $result[9]->getValue()->render());
  106. }
  107. /**
  108. * @return array
  109. */
  110. public function getConfigDataProvider()
  111. {
  112. return [
  113. [
  114. 'transaction' => [
  115. 'id' => 1,
  116. 'createdAt' => new \DateTime(),
  117. 'paypalDetails' => new PayPalDetails(['paymentId' => 10]),
  118. 'refundIds' => [1, 2, 3, 4, 5],
  119. 'merchantAccountId' => 'MerchantId',
  120. 'orderId' => 1,
  121. 'paymentInstrumentType' => 'credit_card',
  122. 'type' => 'sale',
  123. 'amount' => '$19.99',
  124. 'processorSettlementResponseCode' => 1,
  125. 'status' => 'pending_for_settlement',
  126. 'processorSettlementResponseText' => 'sample text',
  127. 'settlementBatchId' => 2,
  128. 'currencyIsoCode' => 'USD'
  129. ]
  130. ]
  131. ];
  132. }
  133. /**
  134. * @return void
  135. */
  136. protected function tearDown()
  137. {
  138. Phrase::setRenderer($this->defaultRenderer);
  139. }
  140. }