AddressDataBuilderTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\AuthorizenetAcceptjs\Test\Unit\Gateway\Request;
  8. use Magento\AuthorizenetAcceptjs\Gateway\Request\AddressDataBuilder;
  9. use Magento\AuthorizenetAcceptjs\Gateway\SubjectReader;
  10. use Magento\Payment\Gateway\Data\AddressAdapterInterface;
  11. use Magento\Payment\Gateway\Data\OrderAdapterInterface;
  12. use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
  13. use Magento\Sales\Model\Order\Payment;
  14. use PHPUnit\Framework\MockObject\MockObject;
  15. use PHPUnit\Framework\TestCase;
  16. class AddressDataBuilderTest extends TestCase
  17. {
  18. /**
  19. * @var AddressDataBuilder
  20. */
  21. private $builder;
  22. /**
  23. * @var Payment|MockObject
  24. */
  25. private $paymentMock;
  26. /**
  27. * @var PaymentDataObjectInterface|MockObject
  28. */
  29. private $paymentDOMock;
  30. /**
  31. * @var OrderAdapterInterface|MockObject
  32. */
  33. private $orderMock;
  34. private $mockAddressData = [
  35. 'firstName' => [
  36. 'method' => 'getFirstname',
  37. 'sampleData' => 'John'
  38. ],
  39. 'lastName' => [
  40. 'method' => 'getLastname',
  41. 'sampleData' => 'Doe'
  42. ],
  43. 'company' => [
  44. 'method' => 'getCompany',
  45. 'sampleData' => 'Magento'
  46. ],
  47. 'address' => [
  48. 'method' => 'getStreetLine1',
  49. 'sampleData' => '11501 Domain Dr'
  50. ],
  51. 'city' => [
  52. 'method' => 'getCity',
  53. 'sampleData' => 'Austin'
  54. ],
  55. 'state' => [
  56. 'method' => 'getRegionCode',
  57. 'sampleData' => 'TX'
  58. ],
  59. 'zip' => [
  60. 'method' => 'getPostcode',
  61. 'sampleData' => '78758'
  62. ],
  63. 'country' => [
  64. 'method' => 'getCountryId',
  65. 'sampleData' => 'US'
  66. ],
  67. ];
  68. protected function setUp()
  69. {
  70. $this->paymentDOMock = $this->createMock(PaymentDataObjectInterface::class);
  71. $this->paymentMock = $this->createMock(Payment::class);
  72. $this->paymentDOMock->method('getPayment')
  73. ->willReturn($this->paymentMock);
  74. $this->orderMock = $this->createMock(OrderAdapterInterface::class);
  75. $this->paymentDOMock->method('getOrder')
  76. ->willReturn($this->orderMock);
  77. $this->builder = new AddressDataBuilder(new SubjectReader());
  78. }
  79. public function testBuildWithBothAddresses()
  80. {
  81. $billingAddress = $this->createAddressMock('billing');
  82. $shippingAddress = $this->createAddressMock('shipping');
  83. $this->orderMock->method('getBillingAddress')
  84. ->willReturn($billingAddress);
  85. $this->orderMock->method('getShippingAddress')
  86. ->willReturn($shippingAddress);
  87. $this->orderMock->method('getRemoteIp')
  88. ->willReturn('abc');
  89. $buildSubject = [
  90. 'payment' => $this->paymentDOMock
  91. ];
  92. $result = $this->builder->build($buildSubject);
  93. $this->validateAddressData($result['transactionRequest']['billTo'], 'billing');
  94. $this->validateAddressData($result['transactionRequest']['shipTo'], 'shipping');
  95. $this->assertEquals('abc', $result['transactionRequest']['customerIP']);
  96. }
  97. private function validateAddressData($responseData, $addressPrefix)
  98. {
  99. foreach ($this->mockAddressData as $fieldValue => $field) {
  100. $this->assertEquals($addressPrefix . $field['sampleData'], $responseData[$fieldValue]);
  101. }
  102. }
  103. private function createAddressMock($prefix)
  104. {
  105. $addressAdapterMock = $this->createMock(AddressAdapterInterface::class);
  106. foreach ($this->mockAddressData as $field) {
  107. $addressAdapterMock->method($field['method'])
  108. ->willReturn($prefix . $field['sampleData']);
  109. }
  110. return $addressAdapterMock;
  111. }
  112. }