Skip to content
Open
Show file tree
Hide file tree
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
110 changes: 110 additions & 0 deletions tests/php/Unit/Checkout/Ajax/CheckoutAddressRequestGuardTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\Checkout\Ajax;

use PHPUnit\Framework\TestCase;
use PrestaShop\Module\PsOnePageCheckout\Checkout\Ajax\CheckoutAddressRequestGuard;
use PrestaShop\Module\PsOnePageCheckout\Checkout\Ajax\CheckoutCustomerContextResolver;
use PrestaShop\Module\PsOnePageCheckout\Checkout\Ajax\OpcTempAddress;

class CheckoutAddressRequestGuardTest extends TestCase
{
public function testIsOwnedCheckoutAddressRejectsNonPositiveAddressIdBeforeResolvingCustomer(): void
{
// resolveId would return a valid customer, yet a non-positive address id short-circuits first.
$resolver = $this->createConfiguredMock(CheckoutCustomerContextResolver::class, ['resolveId' => 42]);
$resolver->expects(self::never())->method('resolveId');

$guard = new CheckoutAddressRequestGuard($this->fakeContext(), $resolver);

self::assertFalse($guard->isOwnedCheckoutAddress(0));
self::assertFalse($guard->isOwnedCheckoutAddress(-5));
}

public function testIsOwnedCheckoutAddressRejectsWhenNoCustomerIsResolved(): void
{
$resolver = $this->createConfiguredMock(CheckoutCustomerContextResolver::class, ['resolveId' => 0]);

$guard = new CheckoutAddressRequestGuard($this->fakeContext(), $resolver);

self::assertFalse($guard->isOwnedCheckoutAddress(10));
}

public function testApplyTemporaryInlineInvoiceAddressSkipsWhenBillingMirrorsDelivery(): void
{
$tempAddress = $this->createMock(OpcTempAddress::class);
$tempAddress->expects(self::never())->method('createFromRequest');

$this->guard()->applyTemporaryInlineInvoiceAddress($tempAddress, [
'use_same_address' => '1',
'invoice_id_country' => 8,
]);
}

public function testApplyTemporaryInlineInvoiceAddressSkipsWhenAPersistedInvoiceAddressExists(): void
{
$tempAddress = $this->createMock(OpcTempAddress::class);
$tempAddress->expects(self::never())->method('createFromRequest');

$this->guard()->applyTemporaryInlineInvoiceAddress($tempAddress, [
'use_same_address' => '0',
'id_address_invoice' => 99,
'invoice_id_country' => 8,
]);
}

public function testApplyTemporaryInlineInvoiceAddressSkipsWithoutABillingCountry(): void
{
$tempAddress = $this->createMock(OpcTempAddress::class);
$tempAddress->expects(self::never())->method('createFromRequest');

$this->guard()->applyTemporaryInlineInvoiceAddress($tempAddress, [
'use_same_address' => '0',
'invoice_id_country' => 0,
]);
}

public function testApplyTemporaryInlineInvoiceAddressMountsTheInlineBillingCountry(): void
{
$tempAddress = $this->createMock(OpcTempAddress::class);
$tempAddress->expects(self::once())
->method('createFromRequest')
->with(
[
'use_same_address' => '0',
'invoice_id_country' => 8,
'invoice_id_state' => 3,
'invoice_postcode' => '75001',
'invoice_city' => 'Paris',
],
true
);

$this->guard()->applyTemporaryInlineInvoiceAddress($tempAddress, [
'use_same_address' => '0',
'invoice_id_country' => 8,
'invoice_id_state' => 3,
'invoice_postcode' => '75001',
'invoice_city' => 'Paris',
]);
}

private function guard(): CheckoutAddressRequestGuard
{
return new CheckoutAddressRequestGuard(
$this->fakeContext(),
$this->createMock(CheckoutCustomerContextResolver::class)
);
}

private function fakeContext(): \Context
{
return new class extends \Context {
public function __construct()
{
}
};
}
}
53 changes: 53 additions & 0 deletions tests/php/Unit/Checkout/Ajax/CheckoutAjaxResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\Checkout\Ajax;

use PHPUnit\Framework\TestCase;
use PrestaShop\Module\PsOnePageCheckout\Checkout\Ajax\CheckoutAjaxResponse;

class CheckoutAjaxResponseTest extends TestCase
{
public function testErrorWrapsTheMessageUnderTheEmptyFieldByDefault(): void
{
self::assertSame(
[
'success' => false,
'errors' => [
'' => ['Something went wrong.'],
],
],
CheckoutAjaxResponse::error('Something went wrong.')
);
}

public function testErrorAttachesTheMessageToTheGivenField(): void
{
self::assertSame(
[
'success' => false,
'errors' => [
'email' => ['Invalid email.'],
],
],
CheckoutAjaxResponse::error('Invalid email.', 'email')
);
}

public function testValidationForwardsTheErrorMapUnchanged(): void
{
$errors = [
'email' => ['Invalid email.'],
'postcode' => ['Invalid postcode.', 'Too short.'],
];

self::assertSame(
[
'success' => false,
'errors' => $errors,
],
CheckoutAjaxResponse::validation($errors)
);
}
}