Completed
Pull Request — master (#11)
by Laurens
02:02
created

PaymentBuilderTest::getNonImplementedPaymentData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 26
nc 1
nop 0
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\PaymentBuilder;
11
use Money\Currency;
12
use PHPUnit\Framework\TestCase;
13
use Ramsey\Uuid\Uuid;
14
15
/**
16
 * @small
17
 */
18
final class PaymentBuilderTest extends TestCase
19
{
20
    /**
21
     * @test
22
     */
23
    public function parseArray(): void
24
    {
25
        $paymentData = [
26
            [
27
                "uuid" => (string) Uuid::uuid1(),
28
                "amount" => 100,
29
                "type" => PaymentBuilder::CASH,
30
                "attributes" => [
31
                    "handedAmount" => 500,
32
                ]
33
            ],
34
            [
35
                "uuid" => (string) Uuid::uuid1(),
36
                "amount" => 200,
37
                "type" => PaymentBuilder::CASH,
38
                "attributes" => [
39
                    "handedAmount" => 500,
40
                ]
41
            ],
42
        ];
43
44
        $builder = new PaymentBuilder();
45
        $payments = $builder->buildFromArray($paymentData, new Currency('EUR'));
46
47
        foreach ($payments as $payment) {
48
            self::assertInstanceOf(AbstractPayment::class, $payment);
49
        }
50
    }
51
52
    /**
53
     * @test
54
     * @dataProvider getPaymentData
55
     */
56
    public function build($paymentData, $expectedClass): void
57
    {
58
        $builder = new PaymentBuilder();
59
        $payment = $builder->build($paymentData, new Currency('EUR'));
60
61
        self::assertInstanceOf(AbstractPayment::class, $payment);
62
        self::assertInstanceOf($expectedClass, $payment);
63
        self::assertSame($paymentData['uuid'], (string) $payment->getUuid());
64
        self::assertSame($paymentData['amount'], (int) $payment->getAmount()->getAmount());
65
66
        if ($payment instanceof CashPayment) {
67
            $this->cashPaymentTests($payment, $paymentData);
68
        }
69
        if ($payment instanceof CardPayment) {
70
            $this->cardPaymentTests($payment, $paymentData);
71
        }
72
    }
73
74
    private function cashPaymentTests(CashPayment $payment, $paymentData)
75
    {
76
        self::assertSame($paymentData['attributes']['handedAmount'], (int) $payment->getHandedAmount()->getAmount());
77
    }
78
79
    private function cardPaymentTests(CardPayment $payment, $paymentData)
80
    {
81
        self::assertSame($paymentData['attributes']['cardPaymentEntryMode'], $payment->getCardPaymentEntryMode());
82
        self::assertSame($paymentData['attributes']['maskedPan'], $payment->getMaskedPan());
83
        self::assertSame($paymentData['attributes']['referenceNumber'], $payment->getReferenceNumber());
84
        self::assertSame($paymentData['attributes']['nrOfInstallments'], $payment->getNrOfInstallments());
85
        self::assertSame($paymentData['attributes']['cardType'], $payment->getCardType());
86
        self::assertSame($paymentData['attributes']['terminalVerificationResults'], $payment->getTerminalVerificationResults());
87
        if (array_key_exists('applicationIdentifier', $paymentData['attributes'])) {
88
            self::assertSame($paymentData['attributes']['applicationIdentifier'], $payment->getApplicationIdentifier());
89
        }
90
        if (array_key_exists('applicationName', $paymentData['attributes'])) {
91
            self::assertSame($paymentData['attributes']['applicationName'], $payment->getApplicationName());
92
        }
93
    }
94
95
    public function getPaymentData(): array
96
    {
97
        return [
98
            PaymentBuilder::CARD . 1 => [
99
                [
100
                    "uuid" => (string) Uuid::uuid1(),
101
                    "amount" => 100,
102
                    "type" => PaymentBuilder::CARD,
103
                    "attributes" => [
104
                        "cardPaymentEntryMode" => "CONTACTLESS_EMV",
105
                        "maskedPan" => "123456*********1234",
106
                        "referenceNumber" => "XH3WXTAUFB",
107
                        "nrOfInstallments" => 0,
108
                        "cardType" => "MAESTRO",
109
                        "terminalVerificationResults" => "0000001234",
110
                        "applicationIdentifier" => "A0000000012345",
111
                        "applicationName" => "MAESTRO",
112
                    ]
113
                ],
114
                CardPayment::class
115
            ],
116
            PaymentBuilder::CARD . 2 => [
117
                [
118
                    "uuid" => (string) Uuid::uuid1(),
119
                    "amount" => 100,
120
                    "type" => PaymentBuilder::CARD,
121
                    "attributes" => [
122
                        "cardPaymentEntryMode" => "CONTACTLESS_EMV",
123
                        "maskedPan" => "123456*********1234",
124
                        "referenceNumber" => "XH3WXTAUFB",
125
                        "nrOfInstallments" => 0,
126
                        "cardType" => "MAESTRO",
127
                        "terminalVerificationResults" => "0000001234",
128
                    ]
129
                ],
130
                CardPayment::class
131
            ],
132
            PaymentBuilder::CASH => [
133
                [
134
                    "uuid" => (string) Uuid::uuid1(),
135
                    "amount" => 200,
136
                    "type" => PaymentBuilder::CASH,
137
                    "attributes" => [
138
                        "handedAmount" => 500,
139
                    ]
140
                ],
141
                CashPayment::class
142
            ],
143
        ];
144
    }
145
146
    /**
147
     * @test
148
     * @expectedException \LauLamanApps\IzettleApi\Client\Exceptions\PaymentTypeNotConfiguredException
149
     */
150
    public function parseNonConfiguredPaymentType(): void
151
    {
152
        $builder = new PaymentBuilder();
153
        $builder->build(['type' => 'IZETTLE_NON_EXISTENCE'], new Currency('EUR'));
154
    }
155
156
    /**
157
     * @test
158
     * @dataProvider getNonImplementedPaymentData
159
     * @expectedException \Exception
160
     * @expectedExceptionMessage('Payment type not implemented')
161
     *
162
     * We have a test for the non implemented payment types so when someone opens
163
     * a PR to Add a payment type this test fails and we can write a correct test.
164
     */
165
    public function parseNonImplemented($paymentData): void
166
    {
167
        $builder = new PaymentBuilder();
168
        $builder->build($paymentData, new Currency('EUR'));
169
    }
170
171
    public function getNonImplementedPaymentData(): array
172
    {
173
        return [
174
            PaymentBuilder::INVOICE => [
175
                [
176
                    "uuid" => (string) Uuid::uuid1(),
177
                    "amount" => 300,
178
                    "type" => PaymentBuilder::INVOICE,
179
                    "attributes" => [
180
                        "unknownFields" => true
181
                    ]
182
                ]
183
            ],
184
            PaymentBuilder::MOBILE => [
185
                [
186
                    "uuid" => (string) Uuid::uuid1(),
187
                    "amount" => 400,
188
                    "type" => PaymentBuilder::MOBILE,
189
                    "attributes" => [
190
                        "unknownFields" => true
191
                    ]
192
                ]
193
            ],
194
            PaymentBuilder::SWISH => [
195
                [
196
                    "uuid" => (string) Uuid::uuid1(),
197
                    "amount" => 500,
198
                    "type" => PaymentBuilder::SWISH,
199
                    "attributes" => [
200
                        "unknownFields" => true
201
                    ]
202
                ],
203
            ],
204
            PaymentBuilder::VIPPS => [
205
                [
206
                    "uuid" => (string) Uuid::uuid1(),
207
                    "amount" => 600,
208
                    "type" => PaymentBuilder::VIPPS,
209
                    "attributes" => [
210
                        "unknownFields" => true
211
                    ]
212
                ],
213
            ],
214
        ];
215
    }
216
}
217