-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBillingFeatureTest.php
More file actions
152 lines (133 loc) · 5.25 KB
/
Copy pathBillingFeatureTest.php
File metadata and controls
152 lines (133 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
declare(strict_types=1);
use Basement\AbacatePay\Billing\BillingResource;
use Basement\AbacatePay\Billing\Enum\BillingFrequencyEnum;
use Basement\AbacatePay\Billing\Enum\BillingMethodEnum;
use Basement\AbacatePay\Billing\Enum\BillingStatusEnum;
use Basement\AbacatePay\Billing\Http\Request\CreateBillingRequest;
use Basement\AbacatePay\Billing\Http\Request\ProductRequest;
use Basement\AbacatePay\Customer\Http\Request\CustomerRequest;
use Basement\AbacatePay\Exception\AbacatePayException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
it('creates billing completely', function () {
$responseData = [
'data' => [
'id' => 'bill_123456',
'url' => 'https://pay.abacatepay.com/bill-5678',
'amount' => 4000,
'status' => 'PENDING',
'devMode' => true,
'methods' => ['PIX'],
'products' => [['id' => 'prod_123456', 'externalId' => 'prod-1234', 'quantity' => 2]],
'frequency' => 'ONE_TIME',
'nextBilling' => 'null',
'customer' => [
'id' => 'cust_123456',
'metadata' => [
'name' => 'Daniel Lima',
'cellphone' => '(11) 4002-8922',
'email' => 'daniel_lima@abacatepay.com',
'taxId' => '123.456.789-01',
],
],
'allowCoupons' => false,
'coupons' => [],
],
];
$handler = new MockHandler([
new Response(200, [], json_encode($responseData)),
]);
$client = new Client(['handler' => $handler]);
$billingResource = new BillingResource(client: $client);
$request = new CreateBillingRequest(
frequency: BillingFrequencyEnum::OneTime,
methods: [BillingMethodEnum::Pix],
products: [
new ProductRequest(
externalId: 'prod-1234',
name: 'Assinatura de Programa Fitness',
description: 'Acesso ao programa fitness premium por 1 mês.',
quantity: 2,
price: 2000
),
],
return_url: 'https://example.com/billing',
completion_url: 'https://example.com/completion',
allow_coupons: false,
coupons: ['ABKT10', 'ABKT5', 'PROMO10'],
customerId: 'cust_abcdefghij',
customer: new CustomerRequest(
id: 'id123',
name: 'Daniel Lima',
cellphone: '(11) 4002-8922',
email: 'daniel_lima@abacatepay.com',
tax_id: '123.456.789-01'
),
externalId: 'seu_id_123'
);
$response = $billingResource->create($request);
expect($response->data->id)->toBe('bill_123456')
->and($response->data->url)->toBe('https://pay.abacatepay.com/bill-5678')
->and($response->data->amount)->toBe(4000)
->and($response->data->status)->toBe(BillingStatusEnum::Pending)
->and($response->data->dev_mode)->toBeTrue()
->and($response->data->methods[0])->toBe(BillingMethodEnum::Pix)
->and($response->data->frequency)->toBe(BillingFrequencyEnum::OneTime)
->and($response->data->next_billing)->toBeNull()
->and($response->data->allow_coupons)->toBeFalse()
->and($response->data->customer->name)->toBe('Daniel Lima');
});
it('throws exception on unauthorized', function () {
$handler = new MockHandler([
new ClientException(
'Unauthorized',
new Request('GET', 'test-abacatepay'),
new Response(401, [], json_encode(['error' => 'Unauthorized'], JSON_THROW_ON_ERROR))
),
]);
$client = new Client(['handler' => $handler]);
$billingResource = new BillingResource(client: $client);
$request = new CreateBillingRequest(
frequency: BillingFrequencyEnum::OneTime,
methods: [BillingMethodEnum::Pix],
products: [],
return_url: 'https://example.com/return',
completion_url: 'https://example.com/complete',
allow_coupons: false,
coupons: [],
customerId: null,
customer: null,
externalId: null
);
expect(fn () => $billingResource->create($request))->toThrow(AbacatePayException::class);
});
it('throws internal server error exception', function () {
$handler = new MockHandler([
new ServerException(
'Internal Server Error',
new Request('POST', 'test-abacatepay'),
new Response(500, [], json_encode(['error' => 'server crashed'], JSON_THROW_ON_ERROR))
),
]);
$client = new Client(['handler' => $handler]);
$billingResource = new BillingResource(client: $client);
$request = new CreateBillingRequest(
frequency: BillingFrequencyEnum::OneTime,
methods: [BillingMethodEnum::Pix],
products: [],
return_url: 'https://example.com/return',
completion_url: 'https://example.com/complete',
allow_coupons: false,
coupons: [],
customerId: null,
customer: null,
externalId: null
);
expect(fn () => $billingResource->create($request))
->toThrow(AbacatePayException::class, 'Internal Server Error');
});