|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Subscribe; |
| 4 | + |
| 5 | +use PaymeUz\HttpClient\SubscribeApiHttpClient; |
| 6 | +use PaymeUz\Util\Core; |
| 7 | +use PHPUnit\Framework\MockObject\Exception; |
| 8 | +use PHPUnit\Framework\MockObject\MockObject; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use PaymeUz\Api\SubscribeApi; |
| 11 | +use ReflectionClass; |
| 12 | + |
| 13 | +class ReceiptMethodsTest extends TestCase |
| 14 | +{ |
| 15 | + private SubscribeApi $api; |
| 16 | + private SubscribeApiHttpClient|MockObject $clientMock; |
| 17 | + |
| 18 | + /** |
| 19 | + * @throws Exception |
| 20 | + */ |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + // Create a mock for the SubscribeApiHttpClient |
| 24 | + $this->clientMock = $this->createMock(SubscribeApiHttpClient::class); |
| 25 | + |
| 26 | + // Inject the mock client into the SubscribeApi instance |
| 27 | + $this->api = new SubscribeApi( |
| 28 | + id: 'test_merchant_id', |
| 29 | + key: 'test_api_key', |
| 30 | + isTest: true |
| 31 | + ); |
| 32 | + |
| 33 | + // Override the private client property with the mock |
| 34 | + $reflection = new ReflectionClass($this->api); |
| 35 | + $clientProperty = $reflection->getProperty('client'); |
| 36 | + $clientProperty->setValue($this->api, $this->clientMock); |
| 37 | + } |
| 38 | + |
| 39 | + public function testCreateCard(): void |
| 40 | + { |
| 41 | + $cardNumber = '8600069195406311'; |
| 42 | + $expiryDate = '03/99'; |
| 43 | + $save = true; |
| 44 | + |
| 45 | + // Define the expected response |
| 46 | + $expectedResponse = (object)[ |
| 47 | + 'status' => 'success', |
| 48 | + 'body' => (object)[ |
| 49 | + 'result' => (object)[ |
| 50 | + 'card' => (object)[ |
| 51 | + 'token' => 'mocked_token' |
| 52 | + ] |
| 53 | + ] |
| 54 | + ] |
| 55 | + ]; |
| 56 | + |
| 57 | + // Set the expectation for the client mock |
| 58 | + $this->clientMock->expects($this->once()) |
| 59 | + ->method('sendRequest') |
| 60 | + ->with('cards.create', [ |
| 61 | + 'card' => [ |
| 62 | + 'number' => $cardNumber, |
| 63 | + 'expire' => $expiryDate |
| 64 | + ], |
| 65 | + 'save' => $save |
| 66 | + ]) |
| 67 | + ->willReturn($expectedResponse); |
| 68 | + |
| 69 | + // Call the method and assert the result |
| 70 | + $result = $this->api->createCard($cardNumber, $expiryDate, $save); |
| 71 | + $this->assertEquals($expectedResponse, $result); |
| 72 | + } |
| 73 | + |
| 74 | + public function testCreateReceipt(): void |
| 75 | + { |
| 76 | + $amount = 500000; // unit amount 5000.00 SUM |
| 77 | + $orderId = 443; |
| 78 | + $account = ['order_id' => $orderId]; |
| 79 | + $description = "Payment for order #$orderId"; |
| 80 | + $productItem = [ |
| 81 | + 'discount' => 0, |
| 82 | + 'title' => 'Tomatoes', |
| 83 | + 'price' => 505000, |
| 84 | + 'count' => 2, |
| 85 | + 'code' => '00702001001000001', |
| 86 | + 'units' => 241092, |
| 87 | + 'vat_percent' => 15, |
| 88 | + 'package_code' => '123456' |
| 89 | + ]; |
| 90 | + $detail = [ |
| 91 | + 'receipt_type' => 0, |
| 92 | + 'items' => [$productItem] |
| 93 | + ]; |
| 94 | + |
| 95 | + // Define the expected response |
| 96 | + $expectedResponse = (object)[ |
| 97 | + 'status' => 'success', |
| 98 | + 'body' => (object)[ |
| 99 | + 'result' => (object)[ |
| 100 | + 'receipt_id' => 'mocked_receipt_id' |
| 101 | + ] |
| 102 | + ] |
| 103 | + ]; |
| 104 | + |
| 105 | + // Set the expectation for the client mock |
| 106 | + $this->clientMock->expects($this->once()) |
| 107 | + ->method('sendRequest') |
| 108 | + ->with('receipts.create', [ |
| 109 | + 'amount' => $amount, |
| 110 | + 'account' => $account, |
| 111 | + 'description' => $description, |
| 112 | + 'detail' => $detail |
| 113 | + ]) |
| 114 | + ->willReturn($expectedResponse); |
| 115 | + |
| 116 | + // Call the method and assert the result |
| 117 | + $result = $this->api->createReceipt($amount, $account, $description, $detail); |
| 118 | + $this->assertEquals($expectedResponse, $result); |
| 119 | + } |
| 120 | + |
| 121 | + public function testPayReceipt(): void |
| 122 | + { |
| 123 | + $receiptId = 'mocked_receipt_id'; |
| 124 | + $token = 'mocked_token'; |
| 125 | + $payer = ['payer_id' => 'test_payer']; |
| 126 | + |
| 127 | + // Define the expected response |
| 128 | + $expectedResponse = (object)[ |
| 129 | + 'status' => 'success', |
| 130 | + 'body' => (object)[ |
| 131 | + 'result' => (object)[ |
| 132 | + 'payment_id' => 'mocked_payment_id' |
| 133 | + ] |
| 134 | + ] |
| 135 | + ]; |
| 136 | + |
| 137 | + // Set the expectation for the client mock |
| 138 | + $this->clientMock->expects($this->once()) |
| 139 | + ->method('sendRequest') |
| 140 | + ->with('receipts.pay', [ |
| 141 | + 'id' => $receiptId, |
| 142 | + 'token' => $token, |
| 143 | + 'payer' => $payer |
| 144 | + ]) |
| 145 | + ->willReturn($expectedResponse); |
| 146 | + |
| 147 | + // Call the method and assert the result |
| 148 | + $result = $this->api->payReceipt($receiptId, $token, $payer); |
| 149 | + $this->assertEquals($expectedResponse, $result); |
| 150 | + } |
| 151 | + |
| 152 | + public function testCancelReceipt(): void |
| 153 | + { |
| 154 | + $receiptId = 'mocked_receipt_id'; |
| 155 | + |
| 156 | + // Define the expected response |
| 157 | + $expectedResponse = (object)[ |
| 158 | + 'status' => 'success' |
| 159 | + ]; |
| 160 | + |
| 161 | + // Set the expectation for the client mock |
| 162 | + $this->clientMock->expects($this->once()) |
| 163 | + ->method('sendRequest') |
| 164 | + ->with('receipts.cancel', ['id' => $receiptId]) |
| 165 | + ->willReturn($expectedResponse); |
| 166 | + |
| 167 | + // Call the method and assert the result |
| 168 | + $result = $this->api->cancelReceipt($receiptId); |
| 169 | + $this->assertEquals($expectedResponse, $result); |
| 170 | + } |
| 171 | +} |
0 commit comments