-
Notifications
You must be signed in to change notification settings - Fork 116
Add test for JSON serialisation #777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| use Adyen\Service\Checkout\PaymentLinksApi; | ||
| use Adyen\Service\Checkout\PaymentsApi; | ||
| use Adyen\Service\Checkout\RecurringApi; | ||
| use Adyen\Model\Checkout\LineItem; | ||
|
|
||
| class ModelBasedCheckoutTest extends TestCaseMock | ||
| { | ||
|
|
@@ -380,4 +381,56 @@ public function testNonNullableSettersCanBeNulled() | |
| // Assert nulled value is not in serialised string | ||
| $this->assertFalse(strpos($jsonString, 'billingAddress') !== false); | ||
| } | ||
|
|
||
| // test request JSON payload serialization | ||
| public function testJsonSerializationMatchesExpected() | ||
| { | ||
| $amount = new Amount(); | ||
| $amount->setCurrency('EUR')->setValue(10000); | ||
|
|
||
| $lineItem1 = new LineItem(); | ||
| $lineItem1->setQuantity(1)->setAmountIncludingTax(5000)->setDescription('Sunglasses'); | ||
| $lineItem2 = new LineItem(); | ||
| $lineItem2->setQuantity(1)->setAmountIncludingTax(5000)->setDescription('Headphones'); | ||
|
|
||
| $request = new CreateCheckoutSessionRequest(); | ||
| $request | ||
| ->setChannel('Web') | ||
| ->setAmount($amount) | ||
| ->setCountryCode('NL') | ||
| ->setMerchantAccount('YOUR_MERCHANT_ACCOUNT') | ||
| ->setReference('YOUR_PAYMENT_REFERENCE') | ||
| ->setReturnUrl('https://mycompany.example.org/redirect?orderRef=YOUR_PAYMENT_REFERENCE') | ||
| ->setLineItems([$lineItem1, $lineItem2]); | ||
|
Comment on lines
+401
to
+404
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider extracting the common parameters used in the $merchantAccount = 'YOUR_MERCHANT_ACCOUNT';
$paymentReference = 'YOUR_PAYMENT_REFERENCE';
$request = new CreateCheckoutSessionRequest();
$request
->setChannel('Web')
->setAmount($amount)
->setCountryCode('NL')
->setMerchantAccount($merchantAccount)
->setReference($paymentReference)
->setReturnUrl('https://mycompany.example.org/redirect?orderRef=' . $paymentReference)
->setLineItems([$lineItem1, $lineItem2]); |
||
|
|
||
| $expectedJson = <<<JSON | ||
| { | ||
| "channel": "Web", | ||
| "amount": { | ||
| "currency": "EUR", | ||
| "value": 10000 | ||
| }, | ||
| "countryCode": "NL", | ||
| "merchantAccount": "YOUR_MERCHANT_ACCOUNT", | ||
| "reference": "YOUR_PAYMENT_REFERENCE", | ||
| "returnUrl": "https://mycompany.example.org/redirect?orderRef=YOUR_PAYMENT_REFERENCE", | ||
| "lineItems": [ | ||
| { | ||
| "quantity": 1, | ||
| "amountIncludingTax": 5000, | ||
| "description": "Sunglasses" | ||
| }, | ||
| { | ||
| "quantity": 1, | ||
| "amountIncludingTax": 5000, | ||
| "description": "Headphones" | ||
| } | ||
| ] | ||
| } | ||
| JSON; | ||
|
Comment on lines
+406
to
+430
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The $expectedJson = json_encode([
"channel" => "Web",
"amount" => [
"currency" => "EUR",
"value" => 10000
],
"countryCode" => "NL",
"merchantAccount" => 'YOUR_MERCHANT_ACCOUNT',
"reference" => 'YOUR_PAYMENT_REFERENCE',
"returnUrl" => "https://mycompany.example.org/redirect?orderRef=YOUR_PAYMENT_REFERENCE",
"lineItems" => [
[
"quantity" => 1,
"amountIncludingTax" => 5000,
"description" => "Sunglasses"
],
[
"quantity" => 1,
"amountIncludingTax" => 5000,
"description" => "Headphones"
]
]
], JSON_PRETTY_PRINT); |
||
|
|
||
| $actualJson = json_encode($request, JSON_PRETTY_PRINT); | ||
|
|
||
| $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's good to see a test for JSON serialization. Consider adding a comment explaining the purpose of this test, specifically that it verifies attributes with default values are not included in the JSON output. This enhances readability and maintainability.