1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainAltapayBundle\Tests\Handler; |
4
|
|
|
|
5
|
|
|
use Loevgaard\AltaPay\Client\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\Entity\PaymentRepository; |
14
|
|
|
use Loevgaard\DandomainAltapayBundle\Handler\PaymentHandler; |
15
|
|
|
use Money\Currency; |
16
|
|
|
use Money\Money; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
|
19
|
|
|
final class PaymentHandlerTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function testCapture() |
22
|
|
|
{ |
23
|
|
|
$payment = $this->getPayment(); |
24
|
|
|
$payment->setAltapayId('altapayid'); |
|
|
|
|
25
|
|
|
|
26
|
|
|
$altapayClient = $this->getAltapayClient(); |
27
|
|
|
$altapayClient |
28
|
|
|
->method('captureReservation') |
29
|
|
|
->willReturnCallback(function () { |
30
|
|
|
$response = $this->getMockBuilder(CaptureReservationResponse::class) |
31
|
|
|
->disableOriginalConstructor() |
32
|
|
|
->getMock() |
33
|
|
|
; |
34
|
|
|
$response->method('isSuccessful')->willReturn(true); |
35
|
|
|
|
36
|
|
|
return $response; |
37
|
|
|
}) |
38
|
|
|
; |
39
|
|
|
|
40
|
|
|
$paymentHandler = $this->getPaymentHandler($altapayClient); |
41
|
|
|
$res = $paymentHandler->capture($payment, new Money(9995, new Currency('DKK'))); |
|
|
|
|
42
|
|
|
|
43
|
|
|
$this->assertInstanceOf(CaptureReservationResponse::class, $res); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testRefund1() |
47
|
|
|
{ |
48
|
|
|
$amount = null; |
49
|
|
|
$orderLines = []; |
50
|
|
|
|
51
|
|
|
$altapayClient = $this->getAltapayClient(); |
52
|
|
|
$altapayClient |
|
|
|
|
53
|
|
|
->expects($this->any()) |
54
|
|
|
->method('refundCapturedReservation') |
55
|
|
|
->willReturnCallback(function ($val) use (&$amount, &$orderLines) { |
56
|
|
|
/** @var RefundCapturedReservationPayload $val */ |
57
|
|
|
$amount = $val->getAmount(); |
58
|
|
|
$orderLines = $val->getOrderLines(); |
59
|
|
|
|
60
|
|
|
$response = $this->getMockBuilder(RefundCapturedReservationResponse::class) |
61
|
|
|
->disableOriginalConstructor() |
62
|
|
|
->getMock() |
63
|
|
|
; |
64
|
|
|
|
65
|
|
|
return $response; |
66
|
|
|
}) |
67
|
|
|
; |
68
|
|
|
|
69
|
|
|
$payment = $this->getPayment(); |
70
|
|
|
$payment->setAltapayId('altapayid'); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$paymentHandler = $this->getPaymentHandler($altapayClient); |
73
|
|
|
$paymentHandler->refund($payment); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$this->assertSame(null, $amount); |
76
|
|
|
$this->assertSame([], $orderLines); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testRefund2() |
80
|
|
|
{ |
81
|
|
|
$amount = null; |
82
|
|
|
$orderLines = []; |
83
|
|
|
|
84
|
|
|
$altapayClient = $this->getAltapayClient(); |
85
|
|
|
$altapayClient |
|
|
|
|
86
|
|
|
->expects($this->any()) |
87
|
|
|
->method('refundCapturedReservation') |
88
|
|
|
->willReturnCallback(function ($val) use (&$amount, &$orderLines) { |
89
|
|
|
/** @var RefundCapturedReservationPayload $val */ |
90
|
|
|
$amount = $val->getAmount(); |
91
|
|
|
$orderLines = $val->getOrderLines(); |
92
|
|
|
|
93
|
|
|
$response = $this->getMockBuilder(RefundCapturedReservationResponse::class) |
94
|
|
|
->disableOriginalConstructor() |
95
|
|
|
->getMock() |
96
|
|
|
; |
97
|
|
|
$response->method('isSuccessful')->willReturn(true); |
98
|
|
|
|
99
|
|
|
$transaction = new Transaction(); |
100
|
|
|
$response->method('getTransactions')->willReturn([$transaction]); |
101
|
|
|
|
102
|
|
|
return $response; |
103
|
|
|
}) |
104
|
|
|
; |
105
|
|
|
|
106
|
|
|
$payment = $this->getPayment(); |
107
|
|
|
$payment->setAltapayId('altapayid'); |
|
|
|
|
108
|
|
|
|
109
|
|
|
$paymentHandler = $this->getPaymentHandler($altapayClient); |
110
|
|
|
$paymentHandler->refund($payment, new Money(10055, new Currency('DKK'))); |
|
|
|
|
111
|
|
|
|
112
|
|
|
$this->assertEquals(new Money(10055, new Currency('DKK')), $amount); |
113
|
|
|
$this->assertSame([], $orderLines); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function testRefundAmountMatchesPaymentLineAmount() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$amount = null; |
119
|
|
|
$orderLines = []; |
120
|
|
|
|
121
|
|
|
$altapayClient = $this->getAltapayClient(); |
122
|
|
|
$altapayClient |
|
|
|
|
123
|
|
|
->expects($this->any()) |
124
|
|
|
->method('refundCapturedReservation') |
125
|
|
|
->willReturnCallback(function ($val) use (&$amount, &$orderLines) { |
126
|
|
|
/** @var RefundCapturedReservationPayload $val */ |
127
|
|
|
$amount = $val->getAmount(); |
128
|
|
|
$orderLines = $val->getOrderLines(); |
129
|
|
|
|
130
|
|
|
$response = $this->getMockBuilder(RefundCapturedReservationResponse::class) |
131
|
|
|
->disableOriginalConstructor() |
132
|
|
|
->getMock() |
133
|
|
|
; |
134
|
|
|
|
135
|
|
|
return $response; |
136
|
|
|
}) |
137
|
|
|
; |
138
|
|
|
|
139
|
|
|
$payment = $this->getPayment(); |
140
|
|
|
$payment->setAltapayId('altapayid'); |
|
|
|
|
141
|
|
|
|
142
|
|
|
/** @var PaymentLine|\PHPUnit_Framework_MockObject_MockObject $paymentLine */ |
143
|
|
|
$paymentLine = new PaymentLine('productnumber', 'name', 1, new Money('7996', new Currency('DKK')), 25); |
144
|
|
|
|
145
|
|
|
$paymentHandler = $this->getPaymentHandler($altapayClient); |
146
|
|
|
$paymentHandler->refund($payment, new Money(9995, new Currency('DKK')), [$paymentLine]); |
|
|
|
|
147
|
|
|
|
148
|
|
|
$this->assertEquals(new Money(9995, new Currency('DKK')), $amount); |
149
|
|
|
|
150
|
|
|
/** @var OrderLine $orderLine */ |
151
|
|
|
$orderLine = $orderLines[0]; |
152
|
|
|
$this->assertEquals(new Money(9995, new Currency('DKK')), $orderLine->getUnitPrice()); |
153
|
|
|
$this->assertSame(25.0, $orderLine->getTaxPercent()); |
154
|
|
|
$this->assertSame(1.0, $orderLine->getQuantity()); |
155
|
|
|
$this->assertSame('name', $orderLine->getDescription()); |
156
|
|
|
$this->assertSame('productnumber', $orderLine->getItemId()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
View Code Duplication |
public function testRefundAmountDoesNotMatchPaymentLineAmount() |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$amount = null; |
162
|
|
|
$orderLines = []; |
163
|
|
|
|
164
|
|
|
$altapayClient = $this->getAltapayClient(); |
165
|
|
|
$altapayClient |
|
|
|
|
166
|
|
|
->expects($this->any()) |
167
|
|
|
->method('refundCapturedReservation') |
168
|
|
|
->willReturnCallback(function ($val) use (&$amount, &$orderLines) { |
169
|
|
|
/** @var RefundCapturedReservationPayload $val */ |
170
|
|
|
$amount = $val->getAmount(); |
171
|
|
|
$orderLines = $val->getOrderLines(); |
172
|
|
|
|
173
|
|
|
$response = $this->getMockBuilder(RefundCapturedReservationResponse::class) |
174
|
|
|
->disableOriginalConstructor() |
175
|
|
|
->getMock() |
176
|
|
|
; |
177
|
|
|
|
178
|
|
|
return $response; |
179
|
|
|
}) |
180
|
|
|
; |
181
|
|
|
|
182
|
|
|
$payment = $this->getPayment(); |
183
|
|
|
$payment->setAltapayId('altapayid'); |
|
|
|
|
184
|
|
|
|
185
|
|
|
$paymentLine = new PaymentLine('productnumber', 'name', 1, new Money('10000', new Currency('DKK')), 25); |
186
|
|
|
|
187
|
|
|
$paymentHandler = $this->getPaymentHandler($altapayClient); |
188
|
|
|
$paymentHandler->refund($payment, new Money(8000, new Currency('DKK')), [$paymentLine]); |
|
|
|
|
189
|
|
|
|
190
|
|
|
$this->assertEquals(new Money(8000, new Currency('DKK')), $amount); |
191
|
|
|
|
192
|
|
|
/** @var OrderLine $orderLine */ |
193
|
|
|
$orderLine = $orderLines[0]; |
194
|
|
|
$this->assertEquals(new Money(8000, new Currency('DKK')), $orderLine->getUnitPrice()); |
195
|
|
|
$this->assertSame(1.0, $orderLine->getQuantity()); |
196
|
|
|
$this->assertSame('refund', $orderLine->getDescription()); |
197
|
|
|
$this->assertSame('refund', $orderLine->getItemId()); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return Payment|\PHPUnit_Framework_MockObject_MockObject |
202
|
|
|
*/ |
203
|
|
|
private function getPayment() |
204
|
|
|
{ |
205
|
|
|
return $this->getMockForAbstractClass(Payment::class); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return Client|\PHPUnit_Framework_MockObject_MockObject |
210
|
|
|
*/ |
211
|
|
|
private function getAltapayClient() |
212
|
|
|
{ |
213
|
|
|
/** @var Client|\PHPUnit_Framework_MockObject_MockObject $altapayClient */ |
214
|
|
|
$altapayClient = $this->getMockBuilder(Client::class) |
215
|
|
|
->disableOriginalConstructor() |
216
|
|
|
->getMock() |
217
|
|
|
; |
218
|
|
|
|
219
|
|
|
return $altapayClient; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param $altapayClient |
224
|
|
|
* |
225
|
|
|
* @return PaymentHandler |
226
|
|
|
*/ |
227
|
|
|
private function getPaymentHandler($altapayClient): PaymentHandler |
228
|
|
|
{ |
229
|
|
|
/** @var PaymentRepository|\PHPUnit_Framework_MockObject_MockObject $paymentRepository */ |
230
|
|
|
$paymentRepository = $this->getMockBuilder(PaymentRepository::class) |
231
|
|
|
->disableOriginalConstructor() |
232
|
|
|
->getMock() |
233
|
|
|
; |
234
|
|
|
|
235
|
|
|
return new PaymentHandler($altapayClient, $paymentRepository); |
|
|
|
|
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
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: