1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LauLamanApps\IzettleApi\Tests\Unit\Client\Purchase; |
6
|
|
|
|
7
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\AbstractPayment; |
8
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\Payment\CardPayment; |
9
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\Payment\CashPayment; |
10
|
|
|
use LauLamanApps\IzettleApi\Client\Purchase\PaymentParser; |
11
|
|
|
use Money\Currency; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Ramsey\Uuid\Uuid; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @small |
17
|
|
|
*/ |
18
|
|
|
final class PaymentParserTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @test |
23
|
|
|
*/ |
24
|
|
|
public function parseArray(): void |
25
|
|
|
{ |
26
|
|
|
$paymentData = [ |
27
|
|
|
[ |
28
|
|
|
"uuid" => (string) Uuid::uuid1(), |
29
|
|
|
"amount" => 100, |
30
|
|
|
"type" => PaymentParser::CASH, |
31
|
|
|
"attributes" => [ |
32
|
|
|
"handedAmount" => 500, |
33
|
|
|
] |
34
|
|
|
], |
35
|
|
|
[ |
36
|
|
|
"uuid" => (string) Uuid::uuid1(), |
37
|
|
|
"amount" => 200, |
38
|
|
|
"type" => PaymentParser::CASH, |
39
|
|
|
"attributes" => [ |
40
|
|
|
"handedAmount" => 500, |
41
|
|
|
] |
42
|
|
|
], |
43
|
|
|
]; |
44
|
|
|
$payments = PaymentParser::parseArray($paymentData, new Currency('EUR')); |
45
|
|
|
|
46
|
|
|
foreach ($payments as $payment) { |
47
|
|
|
self::assertInstanceOf(AbstractPayment::class, $payment); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
* @dataProvider getPaymentData |
54
|
|
|
*/ |
55
|
|
|
public function parse($paymentData, $expectedClass): void |
56
|
|
|
{ |
57
|
|
|
$payment = PaymentParser::parse($paymentData, new Currency('EUR')); |
58
|
|
|
|
59
|
|
|
self::assertInstanceOf(AbstractPayment::class, $payment); |
60
|
|
|
self::assertInstanceOf($expectedClass, $payment); |
61
|
|
|
self::assertSame($paymentData['uuid'], (string) $payment->getUuid()); |
62
|
|
|
self::assertSame($paymentData['amount'], (int) $payment->getAmount()->getAmount()); |
63
|
|
|
|
64
|
|
|
if ($payment instanceof CashPayment) { |
65
|
|
|
$this->cashPaymentTests($payment, $paymentData); |
66
|
|
|
} |
67
|
|
|
if ($payment instanceof CardPayment) { |
68
|
|
|
$this->cardPaymentTests($payment, $paymentData); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function cashPaymentTests(CashPayment $payment, $paymentData) |
73
|
|
|
{ |
74
|
|
|
self::assertSame($paymentData['attributes']['handedAmount'], (int) $payment->getHandedAmount()->getAmount()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function cardPaymentTests(CardPayment $payment, $paymentData) |
78
|
|
|
{ |
79
|
|
|
self::assertSame($paymentData['attributes']['cardPaymentEntryMode'], $payment->getCardPaymentEntryMode()); |
80
|
|
|
self::assertSame($paymentData['attributes']['maskedPan'], $payment->getMaskedPan()); |
81
|
|
|
self::assertSame($paymentData['attributes']['referenceNumber'], $payment->getReferenceNumber()); |
82
|
|
|
self::assertSame($paymentData['attributes']['nrOfInstallments'], $payment->getNrOfInstallments()); |
83
|
|
|
self::assertSame($paymentData['attributes']['cardType'], $payment->getCardType()); |
84
|
|
|
self::assertSame($paymentData['attributes']['terminalVerificationResults'], $payment->getTerminalVerificationResults()); |
85
|
|
|
self::assertSame($paymentData['attributes']['applicationIdentifier'], $payment->getApplicationIdentifier()); |
86
|
|
|
self::assertSame($paymentData['attributes']['applicationName'], $payment->getApplicationName()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getPaymentData(): array |
90
|
|
|
{ |
91
|
|
|
return [ |
92
|
|
|
PaymentParser::CARD => [ |
93
|
|
|
[ |
94
|
|
|
"uuid" => (string) Uuid::uuid1(), |
95
|
|
|
"amount" => 100, |
96
|
|
|
"type" => PaymentParser::CARD, |
97
|
|
|
"attributes" => [ |
98
|
|
|
"cardPaymentEntryMode" => "CONTACTLESS_EMV", |
99
|
|
|
"maskedPan" => "123456*********1234", |
100
|
|
|
"referenceNumber" => "XH3WXTAUFB", |
101
|
|
|
"nrOfInstallments" => 0, |
102
|
|
|
"cardType" => "MAESTRO", |
103
|
|
|
"terminalVerificationResults" => "0000001234", |
104
|
|
|
"applicationIdentifier" => "A0000000012345", |
105
|
|
|
"applicationName" => "MAESTRO", |
106
|
|
|
] |
107
|
|
|
], |
108
|
|
|
CardPayment::class |
109
|
|
|
], |
110
|
|
|
PaymentParser::CASH => [ |
111
|
|
|
[ |
112
|
|
|
"uuid" => (string) Uuid::uuid1(), |
113
|
|
|
"amount" => 200, |
114
|
|
|
"type" => PaymentParser::CASH, |
115
|
|
|
"attributes" => [ |
116
|
|
|
"handedAmount" => 500, |
117
|
|
|
] |
118
|
|
|
], |
119
|
|
|
CashPayment::class |
120
|
|
|
], |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @test |
126
|
|
|
* @expectedException \LauLamanApps\IzettleApi\Client\Exceptions\PaymentTypeNotConfiguredException |
127
|
|
|
*/ |
128
|
|
|
public function parseNonConfiguredPaymentType(): void |
129
|
|
|
{ |
130
|
|
|
$payment = PaymentParser::parse(['type' => 'IZETTLE_NON_EXISTENCE'], new Currency('EUR')); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @test |
135
|
|
|
* @dataProvider getNonImplementedPaymentData |
136
|
|
|
* @expectedException \Exception |
137
|
|
|
* @expectedExceptionMessage('Payment type not implemented') |
138
|
|
|
* |
139
|
|
|
* We have a test for the non implemented payment types so when someone opens |
140
|
|
|
* a PR to Add a payment type this test fails and we can write a correct test. |
141
|
|
|
*/ |
142
|
|
|
public function parseNonImplemented($paymentData): void |
143
|
|
|
{ |
144
|
|
|
$payment = PaymentParser::parse($paymentData, new Currency('EUR')); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function getNonImplementedPaymentData(): array |
148
|
|
|
{ |
149
|
|
|
return [ |
150
|
|
|
PaymentParser::INVOICE => [ |
151
|
|
|
[ |
152
|
|
|
"uuid" => (string) Uuid::uuid1(), |
153
|
|
|
"amount" => 300, |
154
|
|
|
"type" => PaymentParser::INVOICE, |
155
|
|
|
"attributes" => [ |
156
|
|
|
"unknownFields" => true |
157
|
|
|
] |
158
|
|
|
] |
159
|
|
|
], |
160
|
|
|
PaymentParser::MOBILE => [ |
161
|
|
|
[ |
162
|
|
|
"uuid" => (string) Uuid::uuid1(), |
163
|
|
|
"amount" => 400, |
164
|
|
|
"type" => PaymentParser::MOBILE, |
165
|
|
|
"attributes" => [ |
166
|
|
|
"unknownFields" => true |
167
|
|
|
] |
168
|
|
|
] |
169
|
|
|
], |
170
|
|
|
PaymentParser::SWISH => [ |
171
|
|
|
[ |
172
|
|
|
"uuid" => (string) Uuid::uuid1(), |
173
|
|
|
"amount" => 500, |
174
|
|
|
"type" => PaymentParser::SWISH, |
175
|
|
|
"attributes" => [ |
176
|
|
|
"unknownFields" => true |
177
|
|
|
] |
178
|
|
|
], |
179
|
|
|
], |
180
|
|
|
PaymentParser::VIPPS => [ |
181
|
|
|
[ |
182
|
|
|
"uuid" => (string) Uuid::uuid1(), |
183
|
|
|
"amount" => 600, |
184
|
|
|
"type" => PaymentParser::VIPPS, |
185
|
|
|
"attributes" => [ |
186
|
|
|
"unknownFields" => true |
187
|
|
|
] |
188
|
|
|
], |
189
|
|
|
], |
190
|
|
|
]; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.