feat(payments): filter candidates by DNI distance
This commit is contained in:
@@ -351,6 +351,8 @@ class TelepagosWebhookTest extends TestCase
|
||||
|
||||
$nearAmountVariant = $this->createVariantForTenant('candidates', 10, '52.00', 'near');
|
||||
$exactAmountVariant = $this->createVariantForTenant('candidates', 10, '50.00', 'exact');
|
||||
$distanceTwoExactAmountVariant = $this->createVariantForTenant('candidates', 10, '50.00', 'distance-two');
|
||||
$farExactAmountVariant = $this->createVariantForTenant('candidates', 10, '50.00', 'far-exact');
|
||||
$nearAmountDifferentDniVariant = $this->createVariantForTenant('candidates', 10, '51.00', 'near-other-dni');
|
||||
$outsideRangeVariant = $this->createVariantForTenant('candidates', 10, '100.00', 'outside');
|
||||
|
||||
@@ -366,8 +368,22 @@ class TelepagosWebhookTest extends TestCase
|
||||
User::factory()->create()->id,
|
||||
$exactAmountVariant->id,
|
||||
1,
|
||||
'12345687',
|
||||
);
|
||||
$farExactAmountDifferentDniPurchase = $this->createPendingTransferPurchase(
|
||||
$tenant,
|
||||
User::factory()->create()->id,
|
||||
$farExactAmountVariant->id,
|
||||
1,
|
||||
'87654321',
|
||||
);
|
||||
$distanceTwoExactAmountPurchase = $this->createPendingTransferPurchase(
|
||||
$tenant,
|
||||
User::factory()->create()->id,
|
||||
$distanceTwoExactAmountVariant->id,
|
||||
1,
|
||||
'12345087',
|
||||
);
|
||||
$nearAmountDifferentDniPurchase = $this->createPendingTransferPurchase(
|
||||
$tenant,
|
||||
User::factory()->create()->id,
|
||||
@@ -412,11 +428,15 @@ class TelepagosWebhookTest extends TestCase
|
||||
$this->assertEqualsCanonicalizing([
|
||||
$nearAmountPurchase->id,
|
||||
$exactAmountDifferentDniPurchase->id,
|
||||
$distanceTwoExactAmountPurchase->id,
|
||||
], $payment->candidates()->pluck('compra_id')->all());
|
||||
$this->assertDatabaseHas('telepagos_payment_candidates', [
|
||||
'telepagos_payment_id' => $payment->id,
|
||||
'compra_id' => $nearAmountPurchase->id,
|
||||
'dni_matches' => true,
|
||||
'dni_distance' => 0,
|
||||
'payment_dni' => '12345678',
|
||||
'purchase_dni' => '12345678',
|
||||
'amount_matches' => false,
|
||||
'payment_amount' => 50,
|
||||
'purchase_amount' => 52,
|
||||
@@ -428,6 +448,9 @@ class TelepagosWebhookTest extends TestCase
|
||||
'telepagos_payment_id' => $payment->id,
|
||||
'compra_id' => $exactAmountDifferentDniPurchase->id,
|
||||
'dni_matches' => false,
|
||||
'dni_distance' => 1,
|
||||
'payment_dni' => '12345678',
|
||||
'purchase_dni' => '12345687',
|
||||
'amount_matches' => true,
|
||||
'payment_amount' => 50,
|
||||
'purchase_amount' => 50,
|
||||
@@ -435,6 +458,20 @@ class TelepagosWebhookTest extends TestCase
|
||||
'match_reason' => 'exact_amount_different_dni',
|
||||
'confidence' => 'medium',
|
||||
]);
|
||||
$this->assertDatabaseHas('telepagos_payment_candidates', [
|
||||
'telepagos_payment_id' => $payment->id,
|
||||
'compra_id' => $distanceTwoExactAmountPurchase->id,
|
||||
'dni_matches' => false,
|
||||
'dni_distance' => 2,
|
||||
'payment_dni' => '12345678',
|
||||
'purchase_dni' => '12345087',
|
||||
'amount_matches' => true,
|
||||
'match_reason' => 'exact_amount_different_dni',
|
||||
]);
|
||||
$this->assertDatabaseMissing('telepagos_payment_candidates', [
|
||||
'telepagos_payment_id' => $payment->id,
|
||||
'compra_id' => $farExactAmountDifferentDniPurchase->id,
|
||||
]);
|
||||
$this->assertDatabaseMissing('telepagos_payment_candidates', [
|
||||
'telepagos_payment_id' => $payment->id,
|
||||
'compra_id' => $nearAmountDifferentDniPurchase->id,
|
||||
@@ -455,6 +492,9 @@ class TelepagosWebhookTest extends TestCase
|
||||
$higherPriorityPayment->candidates()->create([
|
||||
'compra_id' => $nearAmountPurchase->id,
|
||||
'dni_matches' => true,
|
||||
'dni_distance' => 0,
|
||||
'payment_dni' => '12345678',
|
||||
'purchase_dni' => '12345678',
|
||||
'amount_matches' => true,
|
||||
'payment_amount' => 52,
|
||||
'purchase_amount' => 52,
|
||||
|
||||
31
tests/Unit/Purchase/DniDistanceServiceTest.php
Normal file
31
tests/Unit/Purchase/DniDistanceServiceTest.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Purchase;
|
||||
|
||||
use App\Domains\Purchase\Services\DniDistanceService;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DniDistanceServiceTest extends TestCase
|
||||
{
|
||||
/** @return iterable<string, array{string, string, int}> */
|
||||
public static function distances(): iterable
|
||||
{
|
||||
yield 'exact' => ['40123456', '40123456', 0];
|
||||
yield 'leading transposition' => ['40123456', '04123456', 1];
|
||||
yield 'trailing transposition' => ['40123456', '40123465', 1];
|
||||
yield 'single substitution' => ['40123456', '40123856', 1];
|
||||
yield 'substitution and transposition' => ['12345678', '12345087', 2];
|
||||
yield 'seven digits normalized with leading zero' => ['04123456', '4123456', 0];
|
||||
yield 'more than two edits' => ['40123456', '87654321', 7];
|
||||
}
|
||||
|
||||
#[DataProvider('distances')]
|
||||
public function test_it_calculates_damerau_levenshtein_distance(
|
||||
string $left,
|
||||
string $right,
|
||||
int $expected,
|
||||
): void {
|
||||
$this->assertSame($expected, (new DniDistanceService)->distance($left, $right));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user