Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions tests/Unit/ModelBasedCheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()
{
Comment on lines +386 to +387

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

    /**
     * Test request JSON payload serialization
     * Verifies that attributes with default values are not included in the JSON output.
     */
    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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider extracting the common parameters used in the CreateCheckoutSessionRequest to constants. This improves readability and maintainability, especially if these values are used in other tests as well.

        $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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The expectedJson is defined as a HEREDOC string. While this is valid, consider using json_encode with the JSON_PRETTY_PRINT option to generate the expected JSON string from a PHP array. This approach can improve readability and reduce the risk of syntax errors in the JSON string.

        $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);
}
}