Skip to content
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

PIMAG-750 | Bugfix: Check if the shipping address isset #841 #852

Merged
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
24 changes: 12 additions & 12 deletions Service/Order/TransactionPart/LimitStreetLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,8 @@ class LimitStreetLength implements TransactionPartInterface

public function process(OrderInterface $order, $apiMethod, array $transaction): array
{
if ($apiMethod == Orders::CHECKOUT_TYPE) {
$transaction['billingAddress']['streetAndNumber'] = $this->limitStreetLength($transaction['billingAddress']['streetAndNumber']);
$transaction['shippingAddress']['streetAndNumber'] = $this->limitStreetLength($transaction['shippingAddress']['streetAndNumber']);
}

if ($apiMethod == Payments::CHECKOUT_TYPE) {
$transaction['billingAddress']['streetAndNumber'] = $this->limitStreetLength($transaction['billingAddress']['streetAndNumber']);
}

if ($apiMethod == Payments::CHECKOUT_TYPE && array_key_exists('shippingAddress', $transaction)) {
$transaction['shippingAddress']['streetAndNumber'] = $this->limitStreetLength($transaction['shippingAddress']['streetAndNumber']);
}
$transaction = $this->limitAddress('billingAddress', $transaction);
$transaction = $this->limitAddress('shippingAddress', $transaction);

if ($this->streetTruncated) {
$transaction['metadata']['street_truncated'] = true;
Expand All @@ -48,4 +38,14 @@ private function limitStreetLength(string $street): string
$this->streetTruncated = true;
return mb_substr($street, 0, 100);
}

private function limitAddress(string $type, array $transaction): array
{
if (array_key_exists($type, $transaction)) {
$limited = $this->limitStreetLength($transaction[$type]['streetAndNumber']);
$transaction[$type]['streetAndNumber'] = $limited;
}

return $transaction;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Test\Integration\Service\Order\TransactionPart;

use Magento\Sales\Api\Data\OrderInterface;
use Mollie\Payment\Model\Client\Orders;
use Mollie\Payment\Service\Order\TransactionPart\LimitStreetLength;
use Mollie\Payment\Test\Integration\IntegrationTestCase;

class LimitStreetLengthTest extends IntegrationTestCase
{
public function testLimitsTheAddressForOrdersApi(): void
{
/** @var LimitStreetLength $instance */
$instance = $this->objectManager->create(LimitStreetLength::class);

$transaction = [
'billingAddress' => [
'streetAndNumber' => 'a super long steet name that exceeds the maximum of 100 characters and should be truncated when its too long',
],
'shippingAddress' => [
'streetAndNumber' => 'a super long steet name that exceeds the maximum of 100 characters and should be truncated when its too long',
],
];

$result = $instance->process(
$this->objectManager->create(OrderInterface::class),
Orders::CHECKOUT_TYPE,
$transaction
);

$this->assertEquals(
'a super long steet name that exceeds the maximum of 100 characters and should be truncated when its ',
$result['billingAddress']['streetAndNumber']
);

$this->assertEquals(
'a super long steet name that exceeds the maximum of 100 characters and should be truncated when its ',
$result['shippingAddress']['streetAndNumber']
);

$this->assertArrayHasKey('metadata', $result);
$this->assertArrayHasKey('street_truncated', $result['metadata']);
$this->assertTrue($result['metadata']['street_truncated']);
}

public function testHandlesVirtualProducts(): void
{
/** @var LimitStreetLength $instance */
$instance = $this->objectManager->create(LimitStreetLength::class);

$transaction = [
'billingAddress' => [
'streetAndNumber' => 'a super long steet name that exceeds the maximum of 100 characters and should be truncated when its too long',
],
// Omit the shipping address as that's not relevant for virtual products
];

$result = $instance->process(
$this->objectManager->create(OrderInterface::class),
Orders::CHECKOUT_TYPE,
$transaction
);

$this->assertEquals(
'a super long steet name that exceeds the maximum of 100 characters and should be truncated when its ',
$result['billingAddress']['streetAndNumber']
);

$this->assertArrayHasKey('metadata', $result);
$this->assertArrayHasKey('street_truncated', $result['metadata']);
$this->assertTrue($result['metadata']['street_truncated']);
}

public function testDoesNotMarkAsTruncatedWhenNotTruncated(): void
{
/** @var LimitStreetLength $instance */
$instance = $this->objectManager->create(LimitStreetLength::class);

$transaction = [
'metadata' => [],
'billingAddress' => [
'streetAndNumber' => 'a short street name',
],
];

$result = $instance->process(
$this->objectManager->create(OrderInterface::class),
Orders::CHECKOUT_TYPE,
$transaction
);

$this->assertEquals(
'a short street name',
$result['billingAddress']['streetAndNumber']
);

$this->assertArrayNotHasKey('street_truncated', $result['metadata']);
}
}
Loading