Skip to content

Commit 5d95541

Browse files
committed
Refactor tests: restructure, simplify, and relocate for better organization
1 parent 290ed03 commit 5d95541

13 files changed

Lines changed: 338 additions & 286 deletions
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Lloricode\Paymaya\DataTransferObjects\Checkout\CheckoutDto;
6+
7+
beforeEach(function () {
8+
fakeCredentials();
9+
});
10+
11+
it('set items invalid', function () {
12+
$this->expectException(ErrorException::class);
13+
$this->expectExceptionMessage('Lloricode\Paymaya\DataTransferObjects\Checkout\CheckoutDto::setItems() not found.');
14+
15+
(new CheckoutDto)
16+
->setItems([]);
17+
});
18+
19+
it('invalid getter', function () {
20+
$this->expectException(ErrorException::class);
21+
$this->expectExceptionMessage('Lloricode\Paymaya\DataTransferObjects\Checkout\CheckoutDto::setBlah() not found.');
22+
23+
(new CheckoutDto)
24+
->setBlah('xxx');
25+
});
26+
27+
it('only 1 parameter', function () {
28+
$this->expectException(ErrorException::class);
29+
$this->expectExceptionMessage(
30+
'Argument of Lloricode\Paymaya\Request\Checkout\Checkout::setId() is 1 expected.'
31+
);
32+
33+
(new CheckoutDto)
34+
->setId(1);
35+
})->skip();

tests/Helpers.php

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,26 @@
33
declare(strict_types=1);
44

55
use Lloricode\Paymaya\Constant;
6-
use Lloricode\Paymaya\DataTransferObjects\Checkout\CheckoutDto;
76
use Lloricode\Paymaya\Enums\Environment;
8-
use Lloricode\Paymaya\Request\Checkout\Checkout;
9-
use Lloricode\Paymaya\Test\TestHelper;
7+
use Lloricode\Paymaya\Enums\Webhook;
108
use Saloon\Http\Faking\MockClient;
119

12-
use function PHPUnit\Framework\assertEquals;
13-
1410
function fakeCredentials(): void
1511
{
16-
1712
Constant::$environment = Environment::testing;
1813
Constant::$secretKey = 'sk-....';
1914
Constant::$publicKey = 'pk-.....';
2015

2116
MockClient::destroyGlobal();
2217
}
2318

24-
function buildCheckout(): CheckoutDto
25-
{
26-
return TestHelper::buildCheckout();
27-
}
28-
29-
/**
30-
* https://hackmd.io/@paymaya-pg/Checkout
31-
*/
32-
function jsonCheckoutDataFromDocs(): string
33-
{
34-
return TestHelper::jsonCheckoutDataFromDocs();
35-
}
36-
37-
/**
38-
* https://stackoverflow.com/a/19989922
39-
*/
40-
function assertUUID($value): void
19+
function sampleWebhookData(array $override = []): array
4120
{
42-
$UUIDv4 = '/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i';
43-
assertEquals(1, preg_match($UUIDv4, (string) $value), 'Not a valid uuid.');
21+
return $override + [
22+
'name' => Webhook::CHECKOUT_SUCCESS,
23+
'id' => 'test-generated-id',
24+
'callbackUrl' => 'https://web.test/test/success',
25+
'createdAt' => '2020-01-05T02:30:57.000Z',
26+
'updatedAt' => '2021-02-05T02:30:57.000Z',
27+
];
4428
}

tests/CheckoutTest.php renamed to tests/Requests/Checkout/RetrieveCheckoutRequestTest.php

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22

33
declare(strict_types=1);
44

5-
use GuzzleHttp\Exception\ClientException;
6-
use GuzzleHttp\Exception\GuzzleException;
75
use Lloricode\Paymaya\DataTransferObjects\Checkout\Buyer\BillingAddressDto;
86
use Lloricode\Paymaya\DataTransferObjects\Checkout\Buyer\ShippingAddressDto;
97
use Lloricode\Paymaya\DataTransferObjects\Checkout\CheckoutDto;
108
use Lloricode\Paymaya\Requests\Checkout\RetrieveCheckoutRequest;
11-
use Lloricode\Paymaya\Requests\Checkout\SubmitCheckoutRequest;
12-
use Lloricode\Paymaya\Response\Checkout\CheckoutResponse;
139
use Lloricode\Paymaya\Response\Checkout\PaymentDetail\PaymentDetail;
1410
use Saloon\Http\Faking\MockClient;
1511
use Saloon\Http\Faking\MockResponse;
@@ -22,44 +18,6 @@
2218
fakeCredentials();
2319
});
2420

25-
test('json check exact from docs', function () {
26-
assertJsonStringEqualsJsonString(
27-
jsonCheckoutDataFromDocs(),
28-
json_encode(buildCheckout())
29-
);
30-
});
31-
32-
it('check via sandbox', function () {
33-
34-
$id = 'test-generated-id';
35-
$url = 'https://test';
36-
37-
MockClient::global([
38-
SubmitCheckoutRequest::class => MockResponse::make(
39-
body: [
40-
'checkoutId' => $id,
41-
'redirectUrl' => $url,
42-
],
43-
),
44-
]);
45-
46-
$checkoutResponse = null;
47-
48-
try {
49-
/** @var CheckoutResponse $checkoutResponse */
50-
$checkoutResponse = (new SubmitCheckoutRequest(buildCheckout()))->send()->dto();
51-
} catch (ErrorException) {
52-
$this->fail('ErrorException');
53-
} catch (ClientException $e) {
54-
$this->fail('ClientException: '.$e->getMessage().$e->getResponse()->getBody());
55-
} catch (GuzzleException) {
56-
$this->fail('GuzzleException');
57-
}
58-
59-
assertEquals($id, $checkoutResponse->checkoutId);
60-
assertEquals($url, $checkoutResponse->redirectUrl);
61-
});
62-
6321
it('show with id success', function () {
6422
$responseData = '{
6523
"id": "4ef96167-b8f2-4400-912e-5bd2f4289cfb",
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use GuzzleHttp\Exception\ClientException;
6+
use GuzzleHttp\Exception\GuzzleException;
7+
use Lloricode\Paymaya\Requests\Checkout\SubmitCheckoutRequest;
8+
use Lloricode\Paymaya\Response\Checkout\CheckoutResponse;
9+
use Lloricode\Paymaya\Test\TestHelper;
10+
use Saloon\Http\Faking\MockClient;
11+
use Saloon\Http\Faking\MockResponse;
12+
13+
use function PHPUnit\Framework\assertEquals;
14+
15+
beforeEach(function () {
16+
fakeCredentials();
17+
});
18+
19+
it('check via sandbox', function () {
20+
21+
$id = 'test-generated-id';
22+
$url = 'https://test';
23+
24+
MockClient::global([
25+
SubmitCheckoutRequest::class => MockResponse::make(
26+
body: [
27+
'checkoutId' => $id,
28+
'redirectUrl' => $url,
29+
],
30+
),
31+
]);
32+
33+
$checkoutResponse = null;
34+
35+
try {
36+
/** @var CheckoutResponse $checkoutResponse */
37+
$checkoutResponse = (new SubmitCheckoutRequest(TestHelper::buildCheckout()))->send()->dto();
38+
} catch (ErrorException) {
39+
$this->fail('ErrorException');
40+
} catch (ClientException $e) {
41+
$this->fail('ClientException: '.$e->getMessage().$e->getResponse()->getBody());
42+
} catch (GuzzleException) {
43+
$this->fail('GuzzleException');
44+
}
45+
46+
assertEquals($id, $checkoutResponse->checkoutId);
47+
assertEquals($url, $checkoutResponse->redirectUrl);
48+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Lloricode\Paymaya\Requests\Customization\DeleteCustomizationRequest;
6+
use Saloon\Http\Faking\MockClient;
7+
use Saloon\Http\Faking\MockResponse;
8+
9+
use function PHPUnit\Framework\assertEquals;
10+
11+
beforeEach(function () {
12+
fakeCredentials();
13+
});
14+
15+
it('delete data', function () {
16+
$mockClient = MockClient::global([
17+
DeleteCustomizationRequest::class => MockResponse::make(
18+
status: 204,
19+
),
20+
]);
21+
22+
$response = (new DeleteCustomizationRequest)
23+
->send();
24+
25+
$mockClient->assertSentCount(1);
26+
assertEquals(204, $response->status());
27+
});

tests/CustomizationTest.php renamed to tests/Requests/Customization/RegisterCustomizationRequestTest.php

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
use GuzzleHttp\Exception\ClientException;
66
use GuzzleHttp\Exception\GuzzleException;
77
use Lloricode\Paymaya\DataTransferObjects\Checkout\Customization\CustomizationDto;
8-
use Lloricode\Paymaya\Requests\Customization\DeleteCustomizationRequest;
98
use Lloricode\Paymaya\Requests\Customization\RegisterCustomizationRequest;
10-
use Lloricode\Paymaya\Requests\Customization\RetrieveCustomizationRequest;
119
use Saloon\Http\Faking\MockClient;
1210
use Saloon\Http\Faking\MockResponse;
1311

14-
use function PHPUnit\Framework\assertEquals;
1512
use function PHPUnit\Framework\assertSame;
1613

1714
beforeEach(function () {
@@ -61,63 +58,3 @@
6158
json_encode($response->toArray(), JSON_PRETTY_PRINT)
6259
);
6360
});
64-
65-
it('retrieve', function () {
66-
$data = '{
67-
"logoUrl": "https://image-logo.png",
68-
"iconUrl": "https://image-icon.png",
69-
"appleTouchIconUrl": "https://image-apple.png",
70-
"customTitle": "Test Title Mock",
71-
"colorScheme": "#e01c44",
72-
"redirectTimer": 3,
73-
"hideReceiptInput": true,
74-
"skipResultPage": false,
75-
"showMerchantName": true
76-
}';
77-
78-
MockClient::global([
79-
RetrieveCustomizationRequest::class => MockResponse::make(
80-
body: $data,
81-
),
82-
]);
83-
84-
$response = (new RetrieveCustomizationRequest)
85-
->send()
86-
->dto();
87-
88-
assertSame(
89-
json_encode(json_decode($data), JSON_PRETTY_PRINT),
90-
json_encode($response->toArray(), JSON_PRETTY_PRINT)
91-
);
92-
});
93-
94-
it('retrieve no data', function () {
95-
MockClient::global([
96-
RetrieveCustomizationRequest::class => MockResponse::make(
97-
status: 404,
98-
),
99-
]);
100-
101-
$response = (new RetrieveCustomizationRequest)
102-
->send()
103-
->dto();
104-
105-
assertSame(
106-
json_encode(json_decode(json_encode(new CustomizationDto)), JSON_PRETTY_PRINT),
107-
json_encode($response->toArray(), JSON_PRETTY_PRINT)
108-
);
109-
})->todo('handle 404');
110-
111-
it('delete data', function () {
112-
$mockClient = MockClient::global([
113-
DeleteCustomizationRequest::class => MockResponse::make(
114-
status: 204,
115-
),
116-
]);
117-
118-
$response = (new DeleteCustomizationRequest)
119-
->send();
120-
121-
$mockClient->assertSentCount(1);
122-
assertEquals(204, $response->status());
123-
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Lloricode\Paymaya\DataTransferObjects\Checkout\Customization\CustomizationDto;
6+
use Lloricode\Paymaya\Requests\Customization\RetrieveCustomizationRequest;
7+
use Saloon\Http\Faking\MockClient;
8+
use Saloon\Http\Faking\MockResponse;
9+
10+
use function PHPUnit\Framework\assertSame;
11+
12+
beforeEach(function () {
13+
fakeCredentials();
14+
});
15+
16+
it('retrieve', function () {
17+
$data = '{
18+
"logoUrl": "https://image-logo.png",
19+
"iconUrl": "https://image-icon.png",
20+
"appleTouchIconUrl": "https://image-apple.png",
21+
"customTitle": "Test Title Mock",
22+
"colorScheme": "#e01c44",
23+
"redirectTimer": 3,
24+
"hideReceiptInput": true,
25+
"skipResultPage": false,
26+
"showMerchantName": true
27+
}';
28+
29+
MockClient::global([
30+
RetrieveCustomizationRequest::class => MockResponse::make(
31+
body: $data,
32+
),
33+
]);
34+
35+
$response = (new RetrieveCustomizationRequest)
36+
->send()
37+
->dto();
38+
39+
assertSame(
40+
json_encode(json_decode($data), JSON_PRETTY_PRINT),
41+
json_encode($response->toArray(), JSON_PRETTY_PRINT)
42+
);
43+
});
44+
45+
it('retrieve no data', function () {
46+
MockClient::global([
47+
RetrieveCustomizationRequest::class => MockResponse::make(
48+
status: 404,
49+
),
50+
]);
51+
52+
$response = (new RetrieveCustomizationRequest)
53+
->send()
54+
->dto();
55+
56+
assertSame(
57+
json_encode(json_decode(json_encode(new CustomizationDto)), JSON_PRETTY_PRINT),
58+
json_encode($response->toArray(), JSON_PRETTY_PRINT)
59+
);
60+
})
61+
->todo('handle 404');

0 commit comments

Comments
 (0)