|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainAltapayBundle\Tests\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use Loevgaard\AltaPay\Client; |
|
6
|
|
|
use Loevgaard\AltaPay\Entity\Transaction; |
|
7
|
|
|
use Loevgaard\AltaPay\Payload\OrderLine; |
|
8
|
|
|
use Loevgaard\AltaPay\Payload\RefundCapturedReservation as RefundCapturedReservationPayload; |
|
9
|
|
|
use Loevgaard\AltaPay\Response\CaptureReservation as CaptureReservationResponse; |
|
10
|
|
|
use Loevgaard\AltaPay\Response\RefundCapturedReservation as RefundCapturedReservationResponse; |
|
11
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\Payment; |
|
12
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\PaymentLine; |
|
13
|
|
|
use Loevgaard\DandomainAltapayBundle\Handler\PaymentHandler; |
|
14
|
|
|
use Loevgaard\DandomainAltapayBundle\Manager\PaymentManager; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
|
|
17
|
|
|
final class PaymentHandlerTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public function testCapture() |
|
20
|
|
|
{ |
|
21
|
|
|
$payment = $this->getPayment(); |
|
22
|
|
|
$payment->setAltapayId('altapayid'); |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
$altapayClient = $this->getAltapayClient(); |
|
25
|
|
|
$altapayClient |
|
26
|
|
|
->method('captureReservation') |
|
27
|
|
|
->willReturnCallback(function () { |
|
28
|
|
|
$response = $this->getMockBuilder(CaptureReservationResponse::class) |
|
29
|
|
|
->disableOriginalConstructor() |
|
30
|
|
|
->getMock() |
|
31
|
|
|
; |
|
32
|
|
|
$response->method('isSuccessful')->willReturn(true); |
|
33
|
|
|
|
|
34
|
|
|
return $response; |
|
35
|
|
|
}) |
|
36
|
|
|
; |
|
37
|
|
|
|
|
38
|
|
|
$paymentHandler = $this->getPaymentHandler($altapayClient); |
|
39
|
|
|
$res = $paymentHandler->capture($payment, 99.95); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
$this->assertInstanceOf(CaptureReservationResponse::class, $res); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testRefund1() |
|
45
|
|
|
{ |
|
46
|
|
|
$amount = null; |
|
47
|
|
|
$orderLines = []; |
|
48
|
|
|
|
|
49
|
|
|
$altapayClient = $this->getAltapayClient(); |
|
50
|
|
|
$altapayClient |
|
|
|
|
|
|
51
|
|
|
->expects($this->any()) |
|
52
|
|
|
->method('refundCapturedReservation') |
|
53
|
|
|
->willReturnCallback(function ($val) use (&$amount, &$orderLines) { |
|
54
|
|
|
/** @var RefundCapturedReservationPayload $val */ |
|
55
|
|
|
$amount = $val->getAmount(); |
|
56
|
|
|
$orderLines = $val->getOrderLines(); |
|
57
|
|
|
|
|
58
|
|
|
$response = $this->getMockBuilder(RefundCapturedReservationResponse::class) |
|
59
|
|
|
->disableOriginalConstructor() |
|
60
|
|
|
->getMock() |
|
61
|
|
|
; |
|
62
|
|
|
|
|
63
|
|
|
return $response; |
|
64
|
|
|
}) |
|
65
|
|
|
; |
|
66
|
|
|
|
|
67
|
|
|
$payment = $this->getPayment(); |
|
68
|
|
|
$payment->setAltapayId('altapayid'); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$paymentHandler = $this->getPaymentHandler($altapayClient); |
|
71
|
|
|
$paymentHandler->refund($payment); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$this->assertSame(null, $amount); |
|
74
|
|
|
$this->assertSame([], $orderLines); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testRefund2() |
|
78
|
|
|
{ |
|
79
|
|
|
$amount = null; |
|
80
|
|
|
$orderLines = []; |
|
81
|
|
|
|
|
82
|
|
|
$altapayClient = $this->getAltapayClient(); |
|
83
|
|
|
$altapayClient |
|
|
|
|
|
|
84
|
|
|
->expects($this->any()) |
|
85
|
|
|
->method('refundCapturedReservation') |
|
86
|
|
|
->willReturnCallback(function ($val) use (&$amount, &$orderLines) { |
|
87
|
|
|
/** @var RefundCapturedReservationPayload $val */ |
|
88
|
|
|
$amount = $val->getAmount(); |
|
89
|
|
|
$orderLines = $val->getOrderLines(); |
|
90
|
|
|
|
|
91
|
|
|
$response = $this->getMockBuilder(RefundCapturedReservationResponse::class) |
|
92
|
|
|
->disableOriginalConstructor() |
|
93
|
|
|
->getMock() |
|
94
|
|
|
; |
|
95
|
|
|
$response->method('isSuccessful')->willReturn(true); |
|
96
|
|
|
|
|
97
|
|
|
$transaction = new Transaction(); |
|
98
|
|
|
$response->method('getTransactions')->willReturn([$transaction]); |
|
99
|
|
|
|
|
100
|
|
|
return $response; |
|
101
|
|
|
}) |
|
102
|
|
|
; |
|
103
|
|
|
|
|
104
|
|
|
$payment = $this->getPayment(); |
|
105
|
|
|
$payment->setAltapayId('altapayid'); |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
$paymentHandler = $this->getPaymentHandler($altapayClient); |
|
108
|
|
|
$paymentHandler->refund($payment, null, 100.55); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
$this->assertSame(100.55, $amount); |
|
111
|
|
|
$this->assertSame([], $orderLines); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
View Code Duplication |
public function testRefundAmountMatchesPaymentLineAmount() |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
$amount = null; |
|
117
|
|
|
$orderLines = []; |
|
118
|
|
|
|
|
119
|
|
|
$altapayClient = $this->getAltapayClient(); |
|
120
|
|
|
$altapayClient |
|
|
|
|
|
|
121
|
|
|
->expects($this->any()) |
|
122
|
|
|
->method('refundCapturedReservation') |
|
123
|
|
|
->willReturnCallback(function ($val) use (&$amount, &$orderLines) { |
|
124
|
|
|
/** @var RefundCapturedReservationPayload $val */ |
|
125
|
|
|
$amount = $val->getAmount(); |
|
126
|
|
|
$orderLines = $val->getOrderLines(); |
|
127
|
|
|
|
|
128
|
|
|
$response = $this->getMockBuilder(RefundCapturedReservationResponse::class) |
|
129
|
|
|
->disableOriginalConstructor() |
|
130
|
|
|
->getMock() |
|
131
|
|
|
; |
|
132
|
|
|
|
|
133
|
|
|
return $response; |
|
134
|
|
|
}) |
|
135
|
|
|
; |
|
136
|
|
|
|
|
137
|
|
|
$payment = $this->getPayment(); |
|
138
|
|
|
$payment->setAltapayId('altapayid'); |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
/** @var PaymentLine|\PHPUnit_Framework_MockObject_MockObject $paymentLine */ |
|
141
|
|
|
$paymentLine = $this->getMockForAbstractClass(PaymentLine::class); |
|
142
|
|
|
$paymentLine->setPrice(79.96) |
|
|
|
|
|
|
143
|
|
|
->setVat(25) |
|
144
|
|
|
->setQuantity(1) |
|
145
|
|
|
->setName('name') |
|
146
|
|
|
->setProductNumber('productnumber') |
|
147
|
|
|
; |
|
148
|
|
|
|
|
149
|
|
|
$paymentHandler = $this->getPaymentHandler($altapayClient); |
|
150
|
|
|
$paymentHandler->refund($payment, [$paymentLine], 99.95); |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
$this->assertSame(99.95, $amount); |
|
153
|
|
|
|
|
154
|
|
|
/** @var OrderLine $orderLine */ |
|
155
|
|
|
$orderLine = $orderLines[0]; |
|
156
|
|
|
$this->assertSame(99.95, $orderLine->getUnitPrice()); |
|
157
|
|
|
$this->assertSame(25.0, $orderLine->getTaxPercent()); |
|
158
|
|
|
$this->assertSame(1.0, $orderLine->getQuantity()); |
|
159
|
|
|
$this->assertSame('name', $orderLine->getDescription()); |
|
160
|
|
|
$this->assertSame('productnumber', $orderLine->getItemId()); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
View Code Duplication |
public function testRefundAmountDoesNotMatchPaymentLineAmount() |
|
|
|
|
|
|
164
|
|
|
{ |
|
165
|
|
|
$amount = null; |
|
166
|
|
|
$orderLines = []; |
|
167
|
|
|
|
|
168
|
|
|
$altapayClient = $this->getAltapayClient(); |
|
169
|
|
|
$altapayClient |
|
|
|
|
|
|
170
|
|
|
->expects($this->any()) |
|
171
|
|
|
->method('refundCapturedReservation') |
|
172
|
|
|
->willReturnCallback(function ($val) use (&$amount, &$orderLines) { |
|
173
|
|
|
/** @var RefundCapturedReservationPayload $val */ |
|
174
|
|
|
$amount = $val->getAmount(); |
|
175
|
|
|
$orderLines = $val->getOrderLines(); |
|
176
|
|
|
|
|
177
|
|
|
$response = $this->getMockBuilder(RefundCapturedReservationResponse::class) |
|
178
|
|
|
->disableOriginalConstructor() |
|
179
|
|
|
->getMock() |
|
180
|
|
|
; |
|
181
|
|
|
|
|
182
|
|
|
return $response; |
|
183
|
|
|
}) |
|
184
|
|
|
; |
|
185
|
|
|
|
|
186
|
|
|
$payment = $this->getPayment(); |
|
187
|
|
|
$payment->setAltapayId('altapayid'); |
|
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
/** @var PaymentLine|\PHPUnit_Framework_MockObject_MockObject $paymentLine */ |
|
190
|
|
|
$paymentLine = $this->getMockForAbstractClass(PaymentLine::class); |
|
191
|
|
|
$paymentLine->setPrice(100) |
|
|
|
|
|
|
192
|
|
|
->setVat(25) |
|
193
|
|
|
->setQuantity(1) |
|
194
|
|
|
->setName('name') |
|
195
|
|
|
->setProductNumber('productnumber') |
|
196
|
|
|
; |
|
197
|
|
|
|
|
198
|
|
|
$paymentHandler = $this->getPaymentHandler($altapayClient); |
|
199
|
|
|
$paymentHandler->refund($payment, [$paymentLine], 80); |
|
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
$this->assertSame(80.0, $amount); |
|
202
|
|
|
|
|
203
|
|
|
/** @var OrderLine $orderLine */ |
|
204
|
|
|
$orderLine = $orderLines[0]; |
|
205
|
|
|
$this->assertSame(80.0, $orderLine->getUnitPrice()); |
|
206
|
|
|
$this->assertSame(1.0, $orderLine->getQuantity()); |
|
207
|
|
|
$this->assertSame('refund', $orderLine->getDescription()); |
|
208
|
|
|
$this->assertSame('refund', $orderLine->getItemId()); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @return Payment|\PHPUnit_Framework_MockObject_MockObject |
|
213
|
|
|
*/ |
|
214
|
|
|
private function getPayment() |
|
215
|
|
|
{ |
|
216
|
|
|
return $this->getMockForAbstractClass(Payment::class); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @return Client|\PHPUnit_Framework_MockObject_MockObject |
|
221
|
|
|
*/ |
|
222
|
|
|
private function getAltapayClient() |
|
223
|
|
|
{ |
|
224
|
|
|
/** @var Client|\PHPUnit_Framework_MockObject_MockObject $altapayClient */ |
|
225
|
|
|
$altapayClient = $this->getMockBuilder(Client::class) |
|
226
|
|
|
->disableOriginalConstructor() |
|
227
|
|
|
->getMock() |
|
228
|
|
|
; |
|
229
|
|
|
|
|
230
|
|
|
return $altapayClient; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return PaymentHandler |
|
235
|
|
|
*/ |
|
236
|
|
|
private function getPaymentHandler($altapayClient): PaymentHandler |
|
237
|
|
|
{ |
|
238
|
|
|
/** @var PaymentManager|\PHPUnit_Framework_MockObject_MockObject $paymentManager */ |
|
239
|
|
|
$paymentManager = $this->getMockBuilder(PaymentManager::class) |
|
240
|
|
|
->disableOriginalConstructor() |
|
241
|
|
|
->getMock() |
|
242
|
|
|
; |
|
243
|
|
|
|
|
244
|
|
|
return new PaymentHandler($altapayClient, $paymentManager); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: