🚀 مُصادَق: تحديث برمجي جديد 2026-05-03 13:45
This commit is contained in:
44
tests/Unit/HmacTest.php
Normal file
44
tests/Unit/HmacTest.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use App\Services\Security\HmacService;
|
||||
|
||||
final class HmacTest extends TestCase
|
||||
{
|
||||
private HmacService $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->service = new HmacService();
|
||||
}
|
||||
|
||||
public function test_it_verifies_valid_signature(): void
|
||||
{
|
||||
$secret = 'test-secret';
|
||||
$nonce = 'nonce-123';
|
||||
$timestamp = (string)time();
|
||||
$payload = json_encode(['foo' => 'bar']);
|
||||
|
||||
$signature = $this->service->sign($payload, $secret, $nonce, $timestamp);
|
||||
|
||||
$this->assertTrue($this->service->verify($payload, $signature, $secret, $nonce, $timestamp));
|
||||
}
|
||||
|
||||
public function test_it_rejects_tampered_payload(): void
|
||||
{
|
||||
$secret = 'test-secret';
|
||||
$nonce = 'nonce-123';
|
||||
$timestamp = (string)time();
|
||||
$payload = json_encode(['foo' => 'bar']);
|
||||
|
||||
$signature = $this->service->sign($payload, $secret, $nonce, $timestamp);
|
||||
|
||||
$tamperedPayload = json_encode(['foo' => 'baz']);
|
||||
|
||||
$this->assertFalse($this->service->verify($tamperedPayload, $signature, $secret, $nonce, $timestamp));
|
||||
}
|
||||
}
|
||||
51
tests/Unit/TaxValidationTest.php
Normal file
51
tests/Unit/TaxValidationTest.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use App\Services\TaxValidationService;
|
||||
|
||||
final class TaxValidationTest extends TestCase
|
||||
{
|
||||
private TaxValidationService $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->service = new TaxValidationService();
|
||||
}
|
||||
|
||||
public function test_it_validates_standard_invoice(): void
|
||||
{
|
||||
$data = [
|
||||
'invoice_type' => '001', // Standard
|
||||
'total_tax_exclusive_amount' => 100,
|
||||
'total_tax_amount' => 16,
|
||||
'grand_total' => 116,
|
||||
'tax_items' => [
|
||||
['tax_percent' => 16, 'tax_amount' => 16, 'taxable_amount' => 100]
|
||||
]
|
||||
];
|
||||
|
||||
$result = $this->service->validate($data);
|
||||
$this->assertTrue($result['is_valid']);
|
||||
}
|
||||
|
||||
public function test_it_detects_mismatching_totals(): void
|
||||
{
|
||||
$data = [
|
||||
'invoice_type' => '001',
|
||||
'total_tax_exclusive_amount' => 100,
|
||||
'total_tax_amount' => 16,
|
||||
'grand_total' => 110, // Error: should be 116
|
||||
'tax_items' => [
|
||||
['tax_percent' => 16, 'tax_amount' => 16, 'taxable_amount' => 100]
|
||||
]
|
||||
];
|
||||
|
||||
$result = $this->service->validate($data);
|
||||
$this->assertFalse($result['is_valid']);
|
||||
$this->assertContains('Grand total mismatch', $result['errors']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user