This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Esun\OceanpaymentKlarna\Test\Unit\Model;
use Esun\OceanpaymentKlarna\Model\Config;
use Esun\OceanpaymentKlarna\Model\SecretValueDecoder;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class EnvironmentCredentialsTest extends TestCase
{
public static function environmentProvider(): array
{
return [
'sandbox' => [true, 'sandbox_account', 'sandbox-value'],
'production' => [false, 'production_account', 'production-value'],
];
}
#[DataProvider('environmentProvider')]
public function testSelectsCredentialsForTheActiveEnvironment(
bool $sandbox,
string $field,
string $expected
): void {
$scopeConfig = $this->createMock(ScopeConfigInterface::class);
$scopeConfig->method('isSetFlag')
->with(
'payment/' . Config::METHOD_CODE . '/sandbox',
ScopeInterface::SCOPE_STORE,
1
)
->willReturn($sandbox);
$scopeConfig->method('getValue')
->with(
'payment/' . Config::METHOD_CODE . '/' . $field,
ScopeInterface::SCOPE_STORE,
1
)
->willReturn($expected);
$config = new Config(
$scopeConfig,
new SecretValueDecoder($this->createMock(\Magento\Framework\Encryption\EncryptorInterface::class))
);
self::assertSame($expected, $config->getSecret('account', 1));
}
}
@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace Esun\OceanpaymentKlarna\Test\Unit\Model\Gateway;
use Esun\OceanpaymentKlarna\Model\Gateway\ItemListBuilder;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Item;
use PHPUnit\Framework\TestCase;
class ItemListBuilderTest extends TestCase
{
public function testBuildsOceanpaymentCompatibleFixedNodes(): void
{
$item = $this->createMock(Item::class);
$item->method('getName')->willReturn('PLA Basic');
$item->method('getSku')->willReturn('PLA-BASIC');
$order = $this->createMock(Order::class);
$order->method('getAllVisibleItems')->willReturn([$item]);
$order->method('getShippingAmount')->willReturn(5.0);
$order->method('getTaxAmount')->willReturn(2.0);
$order->method('getDiscountAmount')->willReturn(-3.0);
$order->method('getGrandTotal')->willReturn(24.0);
$payload = json_decode((new ItemListBuilder())->build($order), true, 512, JSON_THROW_ON_ERROR);
self::assertCount(4, $payload);
self::assertSame('1', $payload[0]['type']);
self::assertSame('17.00', $payload[0]['price']);
self::assertSame('17.00', $payload[0]['total_amount']);
self::assertSame('0.12', $payload[0]['taxRate']);
self::assertSame('2.00', $payload[0]['taxPrice']);
self::assertArrayHasKey('image_url', $payload[0]);
self::assertArrayHasKey('product_url', $payload[0]);
self::assertArrayHasKey('remark', $payload[0]);
self::assertSame('3', $payload[1]['type']);
self::assertSame('4', $payload[2]['type']);
self::assertSame('5', $payload[3]['type']);
}
public function testAggregatesProductsAndZeroValueGiftIntoOneNetMerchandiseNode(): void
{
$item = $this->createMock(Item::class);
$item->method('getName')->willReturn('Product');
$item->method('getSku')->willReturn('SKU');
$gift = $this->createMock(Item::class);
$gift->method('getName')->willReturn('Free Gift');
$gift->method('getSku')->willReturn('GIFT');
$order = $this->createMock(Order::class);
$order->method('getAllVisibleItems')->willReturn([$item, $gift]);
$order->method('getShippingAmount')->willReturn(0.0);
$order->method('getTaxAmount')->willReturn(0.0);
$order->method('getDiscountAmount')->willReturn(0.0);
$order->method('getGrandTotal')->willReturn(11.5);
$payload = json_decode((new ItemListBuilder())->build($order), true, 512, JSON_THROW_ON_ERROR);
self::assertCount(4, $payload);
self::assertSame('Product;Free Gift', $payload[0]['title']);
self::assertSame('SKU;GIFT', $payload[0]['sku']);
self::assertSame('1', $payload[0]['quantity']);
self::assertSame('11.50', $payload[0]['total_amount']);
self::assertSame('0.00', $payload[1]['total_amount']);
self::assertSame('0.00', $payload[2]['total_amount']);
self::assertSame('0.00', $payload[3]['total_amount']);
}
}
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Esun\OceanpaymentKlarna\Test\Unit\Model;
use Esun\OceanpaymentKlarna\Model\SecretValueDecoder;
use Magento\Framework\Encryption\EncryptorInterface;
use PHPUnit\Framework\TestCase;
class SecretValueDecoderTest extends TestCase
{
public function testLeavesPlainTextValueUnchanged(): void
{
$encryptor = $this->createMock(EncryptorInterface::class);
$encryptor->expects(self::never())->method('decrypt');
self::assertSame('214127', (new SecretValueDecoder($encryptor))->decode('214127'));
}
public function testDecodesSingleEncryptedValue(): void
{
$encryptor = $this->createMock(EncryptorInterface::class);
$encryptor->expects(self::once())
->method('decrypt')
->with('0:3:encrypted')
->willReturn('214127');
self::assertSame('214127', (new SecretValueDecoder($encryptor))->decode('0:3:encrypted'));
}
public function testRecoversDoubleEncryptedValue(): void
{
$encryptor = $this->createMock(EncryptorInterface::class);
$encryptor->expects(self::exactly(2))
->method('decrypt')
->willReturnMap([
['0:3:outer', '0:3:inner'],
['0:3:inner', '214127'],
]);
self::assertSame('214127', (new SecretValueDecoder($encryptor))->decode('0:3:outer'));
}
}
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Esun\OceanpaymentKlarna\Test\Unit\Model;
use Esun\OceanpaymentKlarna\Model\Signature;
use PHPUnit\Framework\TestCase;
class SignatureTest extends TestCase
{
public function testCreatesHostedRequestSignatureInOceanpaymentFieldOrder(): void
{
$data = [
'account' => 'account',
'terminal' => 'terminal',
'backUrl' => 'https://example.com/back',
'order_number' => 'ORDER-1',
'order_currency' => 'USD',
'order_amount' => '10.00',
'billing_firstName' => 'Jane',
'billing_lastName' => 'Doe',
'billing_email' => 'jane@example.com',
];
$plain = implode('', array_values($data)) . 'secure-code';
self::assertSame(
hash('sha256', $plain),
(new Signature())->createRequest($data, 'secure-code')
);
}
}