Skip to content

Commit b404050

Browse files
committed
Add initial tests
1 parent c0980a6 commit b404050

File tree

2 files changed

+249
-0
lines changed

2 files changed

+249
-0
lines changed

tests/Subscribe/CardMethodsTest.php

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Subscribe;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
use PaymeUz\Api\SubscribeApi;
8+
9+
class CardMethodsTest extends TestCase
10+
{
11+
protected SubscribeApi $api;
12+
13+
protected function setUp(): void
14+
{
15+
$this->api = $this->getMockBuilder(SubscribeApi::class)
16+
->disableOriginalConstructor()
17+
->onlyMethods(['createCard', 'getVerifyCode', 'verifyCard', 'checkCard', 'removeCard'])
18+
->getMock();
19+
}
20+
21+
public function testCreateCard()
22+
{
23+
$cardNumber = '8600069195406311';
24+
$expiryDate = '03/99';
25+
$isTest = true;
26+
27+
$this->api->method('createCard')
28+
->willReturn((object)[
29+
'status' => true,
30+
'body' => (object)[
31+
'result' => (object)[
32+
'card' => (object)[
33+
'token' => 'mock_token'
34+
]
35+
]
36+
]
37+
]);
38+
39+
$result = $this->api->createCard($cardNumber, $expiryDate, $isTest);
40+
$this->assertTrue($result->status);
41+
$this->assertEquals('mock_token', $result->body->result->card->token);
42+
}
43+
44+
public function testVerifyCard()
45+
{
46+
$token = 'mock_token';
47+
$otpCode = '666666';
48+
49+
$this->api->method('verifyCard')
50+
->willReturn((object)[
51+
'status' => true,
52+
'body' => (object)[
53+
'result' => 'verified'
54+
]
55+
]);
56+
57+
$result = $this->api->verifyCard($token, $otpCode);
58+
$this->assertTrue($result->status);
59+
$this->assertEquals('verified', $result->body->result);
60+
}
61+
62+
public function testRemoveCard()
63+
{
64+
$token = 'mock_token';
65+
66+
$this->api->method('removeCard')
67+
->willReturn((object)[
68+
'status' => true,
69+
'body' => (object)[
70+
'result' => 'removed'
71+
]
72+
]);
73+
74+
$result = $this->api->removeCard($token);
75+
$this->assertTrue($result->status);
76+
$this->assertEquals('removed', $result->body->result);
77+
}
78+
}
+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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

Comments
 (0)