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