From db84187461ef5967a4fdac2473796f77a01d5415 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Fri, 9 May 2025 18:04:57 -0400 Subject: [PATCH] Pruebas de Toku --- .../Model/MediosPago/Toku/CustomerTest.php | 18 ++- .../MediosPago/Toku/CustomerTest.php | 19 +++ .../MediosPago/Toku/InvoiceTest.php | 20 +++ .../MediosPago/Toku/SubscriptionTest.php | 20 +++ .../Service/MediosPago/Toku/CustomerTest.php | 7 + .../Service/MediosPago/Toku/InvoiceTest.php | 81 +++++++++-- .../MediosPago/Toku/SubscriptionTest.php | 7 + .../unit/src/Service/MediosPago/TokuTest.php | 126 +++++++++++++++--- 8 files changed, 274 insertions(+), 24 deletions(-) diff --git a/app/tests/unit/src/Model/MediosPago/Toku/CustomerTest.php b/app/tests/unit/src/Model/MediosPago/Toku/CustomerTest.php index a1fd389..beb16c3 100644 --- a/app/tests/unit/src/Model/MediosPago/Toku/CustomerTest.php +++ b/app/tests/unit/src/Model/MediosPago/Toku/CustomerTest.php @@ -6,9 +6,12 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Incoviba\Model\MediosPago\Toku\Customer; use Incoviba\Model\Persona; +use Tests\Extension\ObjectHasMethodTrait; class CustomerTest extends TestCase { + use ObjectHasMethodTrait; + public static function dataProperties(): array { return [ @@ -17,7 +20,6 @@ class CustomerTest extends TestCase ['toku_id'] ]; } - #[DataProvider('dataProperties')] public function testProperties(string $propertyName): void { @@ -26,6 +28,20 @@ class CustomerTest extends TestCase $this->assertObjectHasProperty($propertyName, $customer); } + public static function dataMethods(): array + { + return [ + ['rut'] + ]; + } + #[DataProvider('dataMethods')] + public function testMethods(string $methodName): void + { + $customer = new Customer(); + + $this->assertObjectHasMethod($methodName, $customer); + } + public function testJson(): void { $faker = Faker\Factory::create(); diff --git a/app/tests/unit/src/Repository/MediosPago/Toku/CustomerTest.php b/app/tests/unit/src/Repository/MediosPago/Toku/CustomerTest.php index 82df5e2..b0429cd 100644 --- a/app/tests/unit/src/Repository/MediosPago/Toku/CustomerTest.php +++ b/app/tests/unit/src/Repository/MediosPago/Toku/CustomerTest.php @@ -117,4 +117,23 @@ class CustomerTest extends TestCase $this->assertEquals($this->persona, $customer->persona); $this->assertEquals($reference->toku_id, $customer->toku_id); } + #[Depends('testSave')] + public function testFetchByTokuId(Model\MediosPago\Toku\Customer $reference): void + { + $result = [ + 'id' => $reference->id, + 'rut' => implode('', [$this->persona->rut, $this->persona->digito]), + 'toku_id' => $reference->toku_id, + ]; + $resultSet = $this->getMockBuilder(\PDOStatement::class) + ->disableOriginalConstructor()->getMock(); + $resultSet->method('fetch')->willReturn($result); + $this->connection->method('execute')->willReturn($resultSet); + $repository = new Repository\MediosPago\Toku\Customer($this->connection, $this->personaService); + + $customer = $repository->fetchByTokuId($reference->toku_id); + $this->assertEquals($reference->id, $customer->id); + $this->assertEquals($this->persona, $customer->persona); + $this->assertEquals($reference->toku_id, $customer->toku_id); + } } diff --git a/app/tests/unit/src/Repository/MediosPago/Toku/InvoiceTest.php b/app/tests/unit/src/Repository/MediosPago/Toku/InvoiceTest.php index 5eda917..ee1083d 100644 --- a/app/tests/unit/src/Repository/MediosPago/Toku/InvoiceTest.php +++ b/app/tests/unit/src/Repository/MediosPago/Toku/InvoiceTest.php @@ -110,4 +110,24 @@ class InvoiceTest extends TestCase $this->assertEquals($this->cuota->id, $invoice->cuota->id); $this->assertEquals($reference->toku_id, $invoice->toku_id); } + #[Depends('testSave')] + public function testFetchByTokuId(Model\MediosPago\Toku\Invoice $reference): void + { + $result = [ + 'id' => $reference->id, + 'cuota_id' => $this->cuota->id, + 'toku_id' => $reference->toku_id, + ]; + $resultSet = $this->getMockBuilder(\PDOStatement::class) + ->disableOriginalConstructor()->getMock(); + $resultSet->method('fetch')->willReturn($result); + $this->connection->method('execute')->willReturn($resultSet); + + $repository = new Repository\MediosPago\Toku\Invoice($this->connection, $this->cuotaRepository); + + $invoice = $repository->fetchByTokuId($reference->toku_id); + $this->assertEquals($reference->id, $invoice->id); + $this->assertEquals($this->cuota->id, $invoice->cuota->id); + $this->assertEquals($reference->toku_id, $invoice->toku_id); + } } diff --git a/app/tests/unit/src/Repository/MediosPago/Toku/SubscriptionTest.php b/app/tests/unit/src/Repository/MediosPago/Toku/SubscriptionTest.php index d14f7be..fee22f2 100644 --- a/app/tests/unit/src/Repository/MediosPago/Toku/SubscriptionTest.php +++ b/app/tests/unit/src/Repository/MediosPago/Toku/SubscriptionTest.php @@ -109,4 +109,24 @@ class SubscriptionTest extends TestCase $this->assertEquals($this->venta->id, $subscription->venta->id); $this->assertEquals($reference->toku_id, $subscription->toku_id); } + #[Depends('testSave')] + public function testFetchByTokuId(Model\MediosPago\Toku\Subscription $reference): void + { + $result = [ + 'id' => $reference->id, + 'venta_id' => $this->venta->id, + 'toku_id' => $reference->toku_id, + ]; + + $resultSet = $this->getMockBuilder(\PDOStatement::class) + ->disableOriginalConstructor()->getMock(); + $resultSet->method('fetch')->willReturn($result); + $this->connection->method('execute')->willReturn($resultSet); + + $repository = new Repository\MediosPago\Toku\Subscription($this->connection, $this->ventaRepository); + $subscription = $repository->fetchByTokuId($reference->toku_id); + $this->assertEquals($reference->id, $subscription->id); + $this->assertEquals($this->venta->id, $subscription->venta->id); + $this->assertEquals($reference->toku_id, $subscription->toku_id); + } } diff --git a/app/tests/unit/src/Service/MediosPago/Toku/CustomerTest.php b/app/tests/unit/src/Service/MediosPago/Toku/CustomerTest.php index b369da6..a1bc021 100644 --- a/app/tests/unit/src/Service/MediosPago/Toku/CustomerTest.php +++ b/app/tests/unit/src/Service/MediosPago/Toku/CustomerTest.php @@ -69,6 +69,7 @@ class CustomerTest extends TestCase ->disableOriginalConstructor()->getMock(); $this->customerRepository->method('fetchById')->willReturn($this->customer); $this->customerRepository->method('fetchByRut')->willReturn($this->customer); + $this->customerRepository->method('fetchByTokuId')->willReturn($this->customer); $this->getData = [ 'id' => $this->customer->id, @@ -151,6 +152,12 @@ class CustomerTest extends TestCase $this->assertEquals(json_decode(json_encode($this->customer), true), $service->getById($this->customer->id)); } + public function testGetByExternalId(): void + { + $service = new Service\MediosPago\Toku\Customer($this->client, $this->customerRepository); + + $this->assertEquals(json_decode(json_encode($this->customer), true), $service->getByExternalId($this->customer->rut())); + } public function testGet(): void { $service = new Service\MediosPago\Toku\Customer($this->client, $this->customerRepository); diff --git a/app/tests/unit/src/Service/MediosPago/Toku/InvoiceTest.php b/app/tests/unit/src/Service/MediosPago/Toku/InvoiceTest.php index 60bed83..4c12dfa 100644 --- a/app/tests/unit/src/Service/MediosPago/Toku/InvoiceTest.php +++ b/app/tests/unit/src/Service/MediosPago/Toku/InvoiceTest.php @@ -18,8 +18,11 @@ class InvoiceTest extends TestCase protected Model\MediosPago\Toku\Invoice $invoice; protected Repository\MediosPago\Toku\Invoice $invoiceRepository; protected Model\Venta\Cuota $cuota; + protected Service\Venta\Pago $pagoService; + protected Service\UF $ufService; protected array $getData; protected array $saveData; + protected \DateTimeInterface $fechaUF2; protected function setUp(): void { @@ -45,11 +48,27 @@ class InvoiceTest extends TestCase $subscription->venta = $venta; $subscription->toku_id = $faker->ean13(); + $fechaUF1 = $faker->dateTimeInInterval('-3 years', '-1 weeks'); + $this->fechaUF2 = $faker->dateTimeInInterval('-1 month', 'now'); + $uf1 = $faker->randomFloat(2, 30000, 40000); + $uf2 = $faker->randomFloat(2, 30000, 40000); + + $this->ufService = $this->getMockBuilder(Service\UF::class) + ->disableOriginalConstructor()->getMock(); + $this->ufService->method('get')->willReturnCallback(function(\DateTimeInterface $dateTime) use ($fechaUF1, $uf1, $uf2, $faker) { + return match ($dateTime->format('Y-m-d H:i:s')) { + $fechaUF1->format('Y-m-d H:i:s') => $uf1, + $this->fechaUF2->format('Y-m-d H:i:s') => $uf2, + default => $faker->randomFloat(2, 30000, 40000) + }; + }); + $pago = $this->getMockBuilder(Model\Venta\Pago::class) ->disableOriginalConstructor()->getMock(); - $pago->fecha = $faker->dateTime(); + $pago->fecha = $fechaUF1; $pago->valor = $faker->randomNumber(6, true); - $pago->method('valor')->willReturn($faker->randomFloat(3, true)); + $pago->uf = $uf1; + $pago->method('valor')->willReturn($pago->valor / $uf1); $this->cuota = $this->getMockBuilder(Model\Venta\Cuota::class) ->disableOriginalConstructor()->getMock(); $this->cuota->id = $faker->randomNumber(); @@ -71,6 +90,7 @@ class InvoiceTest extends TestCase ->disableOriginalConstructor()->getMock(); $this->invoiceRepository->method('fetchById')->willReturn($this->invoice); $this->invoiceRepository->method('fetchByCuota')->willReturn($this->invoice); + $this->invoiceRepository->method('fetchByTokuId')->willReturn($this->invoice); $this->getData = [ 'customer' => $customer->toku_id, @@ -151,22 +171,33 @@ class InvoiceTest extends TestCase $this->client->method('post')->willReturn($postResponse); $this->client->method('put')->willReturn($postResponse); $this->client->method('delete')->willReturn($deleteResponse); + + $this->pagoService = $this->getMockBuilder(Service\Venta\Pago::class) + ->disableOriginalConstructor()->getMock(); + $this->pagoService->method('depositar')->willReturn(true); } public function testGetById(): void { - $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository); - $this->assertEquals(json_decode(json_encode($this->invoice), true), $service->getById($this->invoice->toku_id)); + $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository, $this->pagoService, $this->ufService); + + $this->assertEquals(json_decode(json_encode($this->invoice), true), $service->getById($this->cuota->id)); + } + public function testGetByExternalId(): void + { + $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository, $this->pagoService, $this->ufService); + + $this->assertEquals(json_decode(json_encode($this->invoice), true), $service->getByExternalId($this->invoice->toku_id)); } public function testGet(): void { - $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository); + $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository, $this->pagoService, $this->ufService); $this->assertEquals($this->getData, $service->get($this->invoice->toku_id)); } public function testAdd(): void { - $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository); + $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository, $this->pagoService, $this->ufService); $sendData = [ 'customer' => $this->postData['customer'], @@ -179,7 +210,7 @@ class InvoiceTest extends TestCase } public function testEdit(): void { - $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository); + $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository, $this->pagoService, $this->ufService); $sendData = [ 'customer' => $this->postData['customer'], @@ -192,9 +223,43 @@ class InvoiceTest extends TestCase } public function testDelete(): void { - $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository); + $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository, $this->pagoService, $this->ufService); $this->expectNotToPerformAssertions(); $service->delete($this->invoice->toku_id); } + public function testUpdate(): void + { + $faker = Faker\Factory::create(); + $uf2 = $this->ufService->get($this->fechaUF2); + $valor = round($this->cuota->pago->valor() * $uf2); + $request = [ + 'id' => $faker->ean13(), + 'event_type' => 'payment_intent.succeeded', + 'payment_intent' => [ + 'id' => $faker->ean13(), + 'invoice' => $this->invoice->toku_id, + 'customer' => $this->getData['customer'], + 'id_account' => $faker->ean13(), + 'gateway' => 'khipu_transfer', + 'amount' => $valor, + 'status' => 'AUTHORIZED', + 'payment_method' => $faker->ean13(), + 'transaction_date' => $this->fechaUF2->format('Y-m-d H:i:s.u'), + 'card_detail' => null, + 'buy_order' => $faker->ean8(), + 'child_buy_order' => null, + 'authorization_code' => null, + 'payment_type_code' => null, + 'response_code' => null, + 'toku_response_code' => null, + 'installments_number' => null, + 'mc_order_id' => null, + ] + ]; + + $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository, $this->pagoService, $this->ufService); + + $this->assertTrue($service->update($request['payment_intent']['invoice'], $request['payment_intent'])); + } } diff --git a/app/tests/unit/src/Service/MediosPago/Toku/SubscriptionTest.php b/app/tests/unit/src/Service/MediosPago/Toku/SubscriptionTest.php index f8974f7..12dff1b 100644 --- a/app/tests/unit/src/Service/MediosPago/Toku/SubscriptionTest.php +++ b/app/tests/unit/src/Service/MediosPago/Toku/SubscriptionTest.php @@ -75,6 +75,7 @@ class SubscriptionTest extends TestCase ->disableOriginalConstructor()->getMock(); $this->subscriptionRepository->method('fetchById')->willReturn($this->subscription); $this->subscriptionRepository->method('fetchByVenta')->willReturn($this->subscription); + $this->subscriptionRepository->method('fetchByTokuId')->willReturn($this->subscription); $this->getData = [ 'id' => $this->subscription->toku_id, @@ -145,6 +146,12 @@ class SubscriptionTest extends TestCase $this->assertEquals(json_decode(json_encode($this->subscription), true), $service->getById($this->subscription->toku_id)); } + public function testGetByExternalId(): void + { + $service = new Service\MediosPago\Toku\Subscription($this->client, $this->subscriptionRepository); + + $this->assertEquals(json_decode(json_encode($this->subscription), true), $service->getByExternalId($this->subscription->toku_id)); + } public function testGet(): void { $service = new Service\MediosPago\Toku\Subscription($this->client, $this->subscriptionRepository); diff --git a/app/tests/unit/src/Service/MediosPago/TokuTest.php b/app/tests/unit/src/Service/MediosPago/TokuTest.php index 1d42b18..26faad4 100644 --- a/app/tests/unit/src/Service/MediosPago/TokuTest.php +++ b/app/tests/unit/src/Service/MediosPago/TokuTest.php @@ -1,6 +1,8 @@ subscription->venta = $this->venta; $this->subscription->toku_id = $faker->ean13(); - $pago = $this->getMockBuilder(Model\Venta\Pago::class) + $this->pago = $this->getMockBuilder(Model\Venta\Pago::class) ->disableOriginalConstructor()->getMock(); - $pago->fecha = $faker->dateTime(); - $pago->valor = $faker->randomNumber(6); - $pago->method('valor')->willReturn($faker->randomFloat(3, 10, 500)); + $this->pago->fecha = $faker->dateTime(); + $this->pago->valor = $faker->randomNumber(6); + $this->pago->method('valor')->willReturn($faker->randomFloat(3, 10, 500)); $this->cuota = $this->getMockBuilder(Model\Venta\Cuota::class) ->disableOriginalConstructor()->getMock(); $this->cuota->id = $faker->randomNumber(); $this->cuota->numero = $faker->randomNumber(2); - $this->cuota->pago = $pago; + $this->cuota->pago = $this->pago; $pie->method('cuotas')->willReturn([$this->cuota]); $this->invoice = $this->getMockBuilder(Model\MediosPago\Toku\Invoice::class) ->disableOriginalConstructor()->getMock(); @@ -108,13 +111,15 @@ class TokuTest extends TestCase $this->invoice->cuota = $this->cuota; $this->invoice->toku_id = $faker->ean13(); - $this->customerService = $this->getMockBuilder(Service\MediosPago\Toku\Customer::class) - ->disableOriginalConstructor()->getMock(); - $this->customerService->method('getById')->willReturn([ + $customerArray = [ 'id' => $this->customer->id, 'rut' => $this->customer->rut(), 'toku_id' => $this->customer->toku_id - ]); + ]; + $this->customerService = $this->getMockBuilder(Service\MediosPago\Toku\Customer::class) + ->disableOriginalConstructor()->getMock(); + $this->customerService->method('getById')->willReturn($customerArray); + $this->customerService->method('getByExternalId')->willReturn($customerArray); $this->customerService->method('get')->willReturn([ 'id' => $this->customer->toku_id, 'goverment_id' => $this->customer->rut(), @@ -163,22 +168,24 @@ class TokuTest extends TestCase $this->subscriptionService->method('edit')->willReturn(true); $this->subscriptionService->method('delete'); - $this->invoiceService = $this->getMockBuilder(Service\MediosPago\Toku\Invoice::class) - ->disableOriginalConstructor()->getMock(); - $this->invoiceService->method('getById')->willReturn([ + $invoiceArray = [ 'id' => $this->invoice->id, 'cuota_id' => $this->cuota->id, 'toku_id' => $this->invoice->toku_id, - ]); + ]; + $this->invoiceService = $this->getMockBuilder(Service\MediosPago\Toku\Invoice::class) + ->disableOriginalConstructor()->getMock(); + $this->invoiceService->method('getById')->willReturn($invoiceArray); + $this->invoiceService->method('getByExternalId')->willReturn($invoiceArray); $this->invoiceService->method('get')->willReturn([ 'id' => $this->invoice->toku_id, 'customer' => $this->customer->toku_id, 'product_id' => $this->venta->id, 'subscription' => $this->subscription->toku_id, 'is_paid' => false, - 'due_date' => $pago->fecha->format('Y-m-d'), + 'due_date' => $this->pago->fecha->format('Y-m-d'), 'is_void' => false, - 'amount' => $pago->valor(), + 'amount' => $this->pago->valor(), 'link_payment' => '', 'metadata' => [ 'numero' => $this->cuota->numero, @@ -195,6 +202,7 @@ class TokuTest extends TestCase $this->invoiceService->method('add')->willReturn(true); $this->invoiceService->method('edit')->willReturn(true); $this->invoiceService->method('delete'); + $this->invoiceService->method('update')->willReturn(true); } public function testSendCustomer(): void @@ -241,4 +249,92 @@ class TokuTest extends TestCase $this->assertEquals($expected, $service->sendCuotas($this->venta)); } + public function testUpdatePago(): void + { + $service = new Service\MediosPago\Toku($this->logger); + $service->register(Service\MediosPago\Toku::CUSTOMER, $this->customerService); + $service->register(Service\MediosPago\Toku::INVOICE, $this->invoiceService); + + $faker = Faker\Factory::create(); + $request = [ + 'id' => $faker->ean13(), + 'event_type' => 'payment_intent.succeeded', + 'payment_intent' => [ + 'id' => $faker->ean13(), + 'invoice' => $this->invoice->toku_id, + 'customer' => $this->customer->toku_id, + 'id_account' => $faker->ean13(), + 'gateway' => 'khipu_transfer', + 'amount' => $this->pago->valor, + 'status' => 'AUTHORIZED', + 'payment_method' => $faker->ean13(), + 'transaction_date' => $faker->dateTime(), + 'card_detail' => null, + 'buy_order' => $faker->ean8(), + 'child_buy_order' => null, + 'authorization_code' => null, + 'payment_type_code' => null, + 'response_code' => null, + 'toku_response_code' => null, + 'installments_number' => null, + 'mc_order_id' => null, + ] + ]; + + $this->assertTrue($service->updatePago($request['payment_intent'])); + } + public function testUpdatePagoException(): void + { + + $emptyResponse = $this->getMockBuilder(InvalidResult::class) + ->setConstructorArgs(['message' => "No existe Customer para toku_id {$this->customer->toku_id}"])->getMock(); + + $customerService = clone($this->customerService); + $customerService->method('getByExternalId')->willThrowException($emptyResponse); + + $service = new Service\MediosPago\Toku($this->logger); + $service->register(Service\MediosPago\Toku::CUSTOMER, $customerService); + $service->register(Service\MediosPago\Toku::INVOICE, $this->invoiceService); + + $faker = Faker\Factory::create(); + $request = [ + 'id' => $faker->ean13(), + 'event_type' => 'payment_intent.succeeded', + 'payment_intent' => [ + 'id' => $faker->ean13(), + 'invoice' => $this->invoice->toku_id, + 'customer' => $this->customer->toku_id, + 'id_account' => $faker->ean13(), + 'gateway' => 'khipu_transfer', + 'amount' => $this->pago->valor, + 'status' => 'AUTHORIZED', + 'payment_method' => $faker->ean13(), + 'transaction_date' => $faker->dateTime(), + 'card_detail' => null, + 'buy_order' => $faker->ean8(), + 'child_buy_order' => null, + 'authorization_code' => null, + 'payment_type_code' => null, + 'response_code' => null, + 'toku_response_code' => null, + 'installments_number' => null, + 'mc_order_id' => null, + ] + ]; + + $this->expectException(InvalidResult::class); + $service->updatePago($request['payment_intent']); + + $emptyResponse = $this->getMockBuilder(InvalidResult::class) + ->setConstructorArgs(['message' => "No existe Invoice para toku_id {$this->invoice->toku_id}"])->getMock(); + $invoiceService = clone($this->invoiceService); + $invoiceService->method('getByExternalId')->willThrowException($emptyResponse); + + $service = new Service\MediosPago\Toku($this->logger); + $service->register(Service\MediosPago\Toku::CUSTOMER, $this->customerService); + $service->register(Service\MediosPago\Toku::INVOICE, $invoiceService); + + $this->expectException(InvalidResult::class); + $service->updatePago($request['payment_intent']); + } }