266 lines
11 KiB
PHP
266 lines
11 KiB
PHP
<?php
|
|
namespace Tests\Unit\Service\MediosPago\Toku;
|
|
|
|
use PDO;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\StreamInterface;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Faker;
|
|
use GuzzleHttp\Client;
|
|
use Incoviba\Common\Define;
|
|
use Incoviba\Repository;
|
|
use Incoviba\Model;
|
|
use Incoviba\Service;
|
|
|
|
class InvoiceTest extends TestCase
|
|
{
|
|
protected Client $client;
|
|
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
|
|
{
|
|
$dsn = "mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_DATABASE']}";
|
|
$pdo = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASSWORD']);
|
|
|
|
$connection = $this->getMockBuilder(Define\Connection::class)
|
|
->disableOriginalConstructor()->getMock();
|
|
$connection->method('getPDO')->willReturn($pdo);
|
|
|
|
$faker = Faker\Factory::create();
|
|
|
|
$customer = $this->getMockBuilder(Model\MediosPago\Toku\Customer::class)
|
|
->disableOriginalConstructor()->getMock();
|
|
$customer->id = $faker->randomNumber();
|
|
$customer->toku_id = $faker->ean13();
|
|
$venta = $this->getMockBuilder(Model\Venta::class)
|
|
->disableOriginalConstructor()->getMock();
|
|
$venta->id = $faker->randomNumber();
|
|
$subscription = $this->getMockBuilder(Model\MediosPago\Toku\Subscription::class)
|
|
->disableOriginalConstructor()->getMock();
|
|
$subscription->id = $faker->randomNumber();
|
|
$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 = $fechaUF1;
|
|
$pago->valor = $faker->randomNumber(6, 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();
|
|
$this->cuota->numero = $faker->randomNumber(2);
|
|
$this->cuota->pago = $pago;
|
|
|
|
$this->invoice = $this->getMockBuilder(Model\MediosPago\Toku\Invoice::class)
|
|
->disableOriginalConstructor()->getMock();
|
|
$this->invoice->id = $faker->randomNumber();
|
|
$this->invoice->cuota = $this->cuota;
|
|
$this->invoice->toku_id = $faker->ean13();
|
|
$this->invoice->method('jsonSerialize')->willReturn([
|
|
'id' => $this->invoice->id,
|
|
'cuota_id' => $this->cuota->id,
|
|
'toku_id' => $this->invoice->toku_id
|
|
]);
|
|
|
|
$this->invoiceRepository = $this->getMockBuilder(Repository\MediosPago\Toku\Invoice::class)
|
|
->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,
|
|
'product_id' => $subscription->venta->id,
|
|
'subscription' => $subscription->toku_id,
|
|
'is_paid' => false,
|
|
'due_date' => $this->cuota->pago->fecha->format('Y-m-d'),
|
|
'is_void' => false,
|
|
'amount' => $this->cuota->pago->valor,
|
|
'link_payment' => '',
|
|
'metadata' => [
|
|
'numero' => $this->cuota->numero,
|
|
'valor' => $this->cuota->pago->valor,
|
|
'UF' => $this->cuota->pago->valor()
|
|
],
|
|
'receipt_type' => null,
|
|
'id_receipt' => null,
|
|
'source' => null,
|
|
'disable_automatic_payment' => false,
|
|
'currency_code' => 'CLP',
|
|
'invoice_external_id' => $this->cuota->id,
|
|
'id' => $this->invoice->toku_id,
|
|
];
|
|
|
|
$getBody = $this->getMockBuilder(StreamInterface::class)
|
|
->disableOriginalConstructor()->getMock();
|
|
$getBody->method('getContents')->willReturn(json_encode($this->getData));
|
|
|
|
$getResponse = $this->getMockBuilder(ResponseInterface::class)
|
|
->disableOriginalConstructor()->getMock();
|
|
$getResponse->method('getBody')->willReturn($getBody);
|
|
$getResponse->method('getStatusCode')->willReturn(200);
|
|
|
|
$this->postData = [
|
|
'customer' => $customer->toku_id,
|
|
'product_id' => $subscription->venta->id,
|
|
'subscription' => $subscription->toku_id,
|
|
'is_paid' => false,
|
|
'due_date' => $this->cuota->pago->fecha->format('Y-m-d'),
|
|
'is_void' => false,
|
|
'amount' => $this->cuota->pago->valor,
|
|
'link_payment' => '',
|
|
'metadata' => [
|
|
'numero' => $this->cuota->numero,
|
|
'valor' => $this->cuota->pago->valor,
|
|
'UF' => $this->cuota->pago->valor()
|
|
],
|
|
'receipt_type' => null,
|
|
'id_receipt' => null,
|
|
'source' => null,
|
|
'disable_automatic_payment' => false,
|
|
'currency_code' => 'CLF',
|
|
'invoice_external_id' => $this->cuota->id,
|
|
'id' => $this->invoice->toku_id,
|
|
];
|
|
|
|
$postBody = $this->getMockBuilder(StreamInterface::class)
|
|
->disableOriginalConstructor()->getMock();
|
|
$postBody->method('getContents')->willReturn(json_encode($this->postData));
|
|
|
|
$postResponse = $this->getMockBuilder(ResponseInterface::class)
|
|
->disableOriginalConstructor()->getMock();
|
|
$postResponse->method('getBody')->willReturn($postBody);
|
|
$postResponse->method('getStatusCode')->willReturn(200);
|
|
|
|
$deleteBody = $this->getMockBuilder(StreamInterface::class)
|
|
->disableOriginalConstructor()->getMock();
|
|
$deleteBody->method('getContents')->willReturn("Invoice deleted");
|
|
|
|
$deleteResponse = $this->getMockBuilder(ResponseInterface::class)
|
|
->disableOriginalConstructor()->getMock();
|
|
$deleteResponse->method('getBody')->willReturn($deleteBody);
|
|
$deleteResponse->method('getStatusCode')->willReturn(204);
|
|
|
|
$this->client = $this->getMockBuilder(Client::class)
|
|
->disableOriginalConstructor()->getMock();
|
|
$this->client->method('get')->willReturn($getResponse);
|
|
$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->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, $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, $this->pagoService, $this->ufService);
|
|
|
|
$sendData = [
|
|
'customer' => $this->postData['customer'],
|
|
'product_id' => $this->postData['product_id'],
|
|
'subscription' => $this->postData['subscription'],
|
|
'cuota' => $this->cuota
|
|
];
|
|
|
|
$this->assertTrue($service->add($sendData));
|
|
}
|
|
public function testEdit(): void
|
|
{
|
|
$service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository, $this->pagoService, $this->ufService);
|
|
|
|
$sendData = [
|
|
'customer' => $this->postData['customer'],
|
|
'product_id' => $this->postData['product_id'],
|
|
'subscription' => $this->postData['subscription'],
|
|
'cuota' => $this->cuota
|
|
];
|
|
|
|
$this->assertTrue($service->edit($this->invoice->toku_id, $sendData));
|
|
}
|
|
public function testDelete(): void
|
|
{
|
|
$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']));
|
|
}
|
|
}
|