|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Unit; |
| 6 | + |
| 7 | +use PayNL\Sdk\Model\Amount; |
| 8 | +use PayNL\Sdk\Model\Pay\PayOrder; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class PayOrderTest extends TestCase |
| 12 | +{ |
| 13 | + public function testConstructorMapsPayloadObject(): void |
| 14 | + { |
| 15 | + $payload = [ |
| 16 | + 'object' => [ |
| 17 | + 'id' => 'PO-123', |
| 18 | + 'description' => 'Test order', |
| 19 | + 'reference' => 'REF-999', |
| 20 | + 'status' => ['code' => 200, 'action' => 'PAID'], |
| 21 | + 'amount' => ['value' => 1000, 'currency' => 'EUR'], |
| 22 | + 'capturedAmount' => ['value' => 500, 'currency' => 'EUR'], |
| 23 | + 'authorizedAmount'=> ['value' => 1000, 'currency' => 'EUR'], |
| 24 | + ], |
| 25 | + ]; |
| 26 | + |
| 27 | + $order = new PayOrder($payload); |
| 28 | + |
| 29 | + $this->assertSame('PO-123', $order->getId()); |
| 30 | + $this->assertSame('Test order', $order->getDescription()); |
| 31 | + $this->assertSame('REF-999', $order->getReference()); |
| 32 | + $this->assertSame(200, $order->getStatusCode()); |
| 33 | + $this->assertSame('PAID', $order->getStatusName()); |
| 34 | + } |
| 35 | + |
| 36 | + public function testAmountRefundedIsNullWhenNotSet(): void |
| 37 | + { |
| 38 | + $order = new PayOrder(); |
| 39 | + |
| 40 | + $this->assertNull($order->getAmountRefunded()); |
| 41 | + } |
| 42 | + |
| 43 | + public function testAmountRefundedReturnsValueInUnits(): void |
| 44 | + { |
| 45 | + $order = new PayOrder(); |
| 46 | + $amount = new Amount(1234, 'EUR'); |
| 47 | + |
| 48 | + $order->setAmountRefunded($amount); |
| 49 | + |
| 50 | + $this->assertSame(12.34, $order->getAmountRefunded()); |
| 51 | + } |
| 52 | + |
| 53 | + public function testIsFastCheckoutTrueForPaymentBasedCheckout(): void |
| 54 | + { |
| 55 | + $order = new PayOrder(); |
| 56 | + $order->setType('payment_based_checkout'); |
| 57 | + |
| 58 | + $this->assertTrue($order->isFastCheckout()); |
| 59 | + } |
| 60 | + |
| 61 | + public function testIsFastCheckoutFalseForOtherType(): void |
| 62 | + { |
| 63 | + $order = new PayOrder(); |
| 64 | + $order->setType('something_else'); |
| 65 | + |
| 66 | + $this->assertFalse($order->isFastCheckout()); |
| 67 | + } |
| 68 | + |
| 69 | + public function testFastCheckoutDataDelegatesToCheckoutData(): void |
| 70 | + { |
| 71 | + $order = new PayOrder(); |
| 72 | + $data = ['foo' => 'bar', 'baz' => 123]; |
| 73 | + |
| 74 | + $order->setCheckoutData($data); |
| 75 | + |
| 76 | + $this->assertSame($data, $order->getFastCheckoutData()); |
| 77 | + } |
| 78 | + |
| 79 | + public function testStatusHelpersWithSetStatusCodeName(): void |
| 80 | + { |
| 81 | + $order = new PayOrder(); |
| 82 | + |
| 83 | + $order->setStatusCodeName(321, 'PENDING'); |
| 84 | + |
| 85 | + $this->assertSame(321, $order->getStatusCode()); |
| 86 | + $this->assertSame('PENDING', $order->getStatusName()); |
| 87 | + } |
| 88 | + |
| 89 | + public function testStatusHelpersWithSetStatus(): void |
| 90 | + { |
| 91 | + $order = new PayOrder(); |
| 92 | + $status = ['code' => 999, 'action' => 'SOMETHING']; |
| 93 | + |
| 94 | + $order->setStatus($status); |
| 95 | + |
| 96 | + $this->assertSame(999, $order->getStatusCode()); |
| 97 | + $this->assertSame('SOMETHING', $order->getStatusName()); |
| 98 | + $this->assertSame($status, $order->getStatus()); |
| 99 | + } |
| 100 | + |
| 101 | + public function testPaymentHelpersReturnFirstPaymentData(): void |
| 102 | + { |
| 103 | + $order = new PayOrder(); |
| 104 | + |
| 105 | + $payments = [ |
| 106 | + [ |
| 107 | + 'paymentMethod' => ['id' => 'IDEAL'], |
| 108 | + 'customerId' => 'CUST-1', |
| 109 | + 'customerName' => 'John Doe', |
| 110 | + ], |
| 111 | + ]; |
| 112 | + |
| 113 | + $order->setPayments($payments); |
| 114 | + |
| 115 | + $this->assertSame('IDEAL', $order->getPaymentMethod()); |
| 116 | + $this->assertSame('CUST-1', $order->getCustomerId()); |
| 117 | + $this->assertSame('John Doe', $order->getCustomerName()); |
| 118 | + } |
| 119 | + |
| 120 | + public function testPaymentHelpersReturnNullWhenNoPayments(): void |
| 121 | + { |
| 122 | + $order = new PayOrder(); |
| 123 | + |
| 124 | + $this->assertNull($order->getPaymentMethod()); |
| 125 | + $this->assertNull($order->getCustomerId()); |
| 126 | + $this->assertNull($order->getCustomerName()); |
| 127 | + } |
| 128 | + |
| 129 | + public function testIsTestmodeTrueWhenIntegrationTestFlagTrue(): void |
| 130 | + { |
| 131 | + $order = new PayOrder(); |
| 132 | + |
| 133 | + $order->setIntegration(['test' => true]); |
| 134 | + |
| 135 | + $this->assertTrue($order->isTestmode()); |
| 136 | + } |
| 137 | + |
| 138 | + public function testIsTestmodeFalseWhenIntegrationTestFlagMissingOrFalse(): void |
| 139 | + { |
| 140 | + $order = new PayOrder(); |
| 141 | + |
| 142 | + $order->setIntegration(['foo' => 'bar']); |
| 143 | + $this->assertFalse($order->isTestmode()); |
| 144 | + |
| 145 | + $order->setIntegration(['test' => false]); |
| 146 | + $this->assertFalse($order->isTestmode()); |
| 147 | + } |
| 148 | + |
| 149 | + public function testAmountAndCurrency(): void |
| 150 | + { |
| 151 | + $order = new PayOrder(); |
| 152 | + $amount = new Amount(2500, 'EUR'); // 25.00 |
| 153 | + |
| 154 | + $order->setAmount($amount); |
| 155 | + |
| 156 | + $this->assertEquals(25.00, $order->getAmount()); |
| 157 | + $this->assertSame('EUR', $order->getCurrency()); |
| 158 | + } |
| 159 | + |
| 160 | + |
| 161 | + public function testPaymentAndStatusUrls(): void |
| 162 | + { |
| 163 | + $order = new PayOrder(); |
| 164 | + |
| 165 | + $links = [ |
| 166 | + 'redirect' => 'https://pay.nl/redirect/123', |
| 167 | + 'status' => 'https://pay.nl/status/123', |
| 168 | + ]; |
| 169 | + |
| 170 | + $order->setLinks($links); |
| 171 | + |
| 172 | + $this->assertSame($links, $order->getLinks()); |
| 173 | + $this->assertSame('https://pay.nl/redirect/123', $order->getPaymentUrl()); |
| 174 | + $this->assertSame('https://pay.nl/status/123', $order->getStatusUrl()); |
| 175 | + } |
| 176 | + |
| 177 | + public function testChargebackDetection(): void |
| 178 | + { |
| 179 | + $order = new PayOrder(); |
| 180 | + |
| 181 | + $order->setStatus(['code' => 123, 'action' => 'CHARGEBACK']); |
| 182 | + $this->assertTrue($order->isChargeBack()); |
| 183 | + |
| 184 | + $order->setStatus(['code' => 123, 'action' => 'PAID']); |
| 185 | + $this->assertFalse($order->isChargeBack()); |
| 186 | + } |
| 187 | + |
| 188 | + public function testTransferDataGetterAndSetter(): void |
| 189 | + { |
| 190 | + $order = new PayOrder(); |
| 191 | + |
| 192 | + $data = ['some' => 'value', 'foo' => 'bar']; |
| 193 | + |
| 194 | + $order->setTransferData($data); |
| 195 | + |
| 196 | + $this->assertSame($data, $order->getTransferData()); |
| 197 | + } |
| 198 | + |
| 199 | +} |
0 commit comments