Tests de Repositories

This commit is contained in:
Juan Pablo Vial
2025-05-08 16:17:14 -04:00
parent ca5354a3ee
commit 5ad4fc4038
5 changed files with 552 additions and 0 deletions

View File

@ -0,0 +1,120 @@
<?php
namespace Tests\Unit\Repository\MediosPago\Toku;
use PDO;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\TestCase;
use Faker;
use Incoviba\Common\Define;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
class CustomerTest extends TestCase
{
protected PDO $pdo;
protected Define\Connection $connection;
protected Model\Persona $persona;
protected Service\Persona $personaService;
protected function setUp(): void
{
$dsn = "mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_DATABASE']}";
$this->pdo = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASSWORD']);
$this->connection = $this->getMockBuilder(Define\Connection::class)
->disableOriginalConstructor()
->getMock();
$this->connection->method('getPDO')->willReturn($this->pdo);
$faker = Faker\Factory::create();
$datos = $this->getMockBuilder(Model\Persona\Datos::class)
->disableOriginalConstructor()->getMock();
$datos->email = $faker->email();
$datos->telefono = $faker->randomNumber(9);
$this->persona = $this->getMockBuilder(Model\Persona::class)
->disableOriginalConstructor()->getMock();
$this->persona->rut = $faker->randomNumber(8);
$digit = $faker->numberBetween(0, 11);
$this->persona->digito = $digit === 11 ? 'k' : $digit;
$this->persona->method('datos')->willReturn($datos);
$this->personaService = $this->getMockBuilder(Service\Persona::class)
->disableOriginalConstructor()->getMock();
$this->personaService->method('getById')->willReturn($this->persona);
}
public function testCreate(): void
{
$repository = new Repository\MediosPago\Toku\Customer($this->connection, $this->personaService);
$faker = Faker\Factory::create();
$data = [
'rut' => implode('', [$this->persona->rut, $this->persona->digito]),
'toku_id' => $faker->iban(),
];
$customer = $repository->create($data);
$this->assertEquals($this->persona, $customer->persona);
$this->assertEquals($data['toku_id'], $customer->toku_id);
}
public function testSave(): Model\MediosPago\Toku\Customer
{
$repository = new Repository\MediosPago\Toku\Customer($this->connection, $this->personaService);
$faker = Faker\Factory::create();
$data = [
'rut' => implode('', [$this->persona->rut, $this->persona->digito]),
'toku_id' => $faker->iban(),
];
$customer = $repository->create($data);
$customer = $repository->save($customer);
$this->assertNotNull($customer->id);
$this->assertEquals($this->persona, $customer->persona);
$this->assertEquals($data['toku_id'], $customer->toku_id);
return $customer;
}
#[Depends('testSave')]
public function testFetch(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->fetchById($reference->id);
$this->assertInstanceOf(Model\MediosPago\Toku\Customer::class, $customer);
$this->assertEquals($reference->id, $customer->id);
$this->assertEquals($this->persona, $customer->persona);
$this->assertEquals($reference->toku_id, $customer->toku_id);
}
#[Depends('testSave')]
public function testFetchByRut(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->fetchByRut($this->persona->rut);
$this->assertEquals($reference->id, $customer->id);
$this->assertEquals($this->persona, $customer->persona);
$this->assertEquals($reference->toku_id, $customer->toku_id);
}
}

View File

@ -0,0 +1,113 @@
<?php
namespace Tests\Unit\Repository\MediosPago\Toku;
use PDO;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\TestCase;
use Faker;
use Incoviba\Common\Define;
use Incoviba\Model;
use Incoviba\Repository;
class InvoiceTest extends TestCase
{
protected Define\Connection $connection;
protected PDO $pdo;
protected Model\Venta\Cuota $cuota;
protected Repository\Venta\Cuota $cuotaRepository;
protected function setUp(): void
{
$dsn = "mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_DATABASE']}";
$this->pdo = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASSWORD']);
$this->connection = $this->getMockBuilder(Define\Connection::class)
->disableOriginalConstructor()->getMock();
$this->connection->method('getPDO')->willReturn($this->pdo);
$faker = Faker\Factory::create();
$this->cuota = $this->getMockBuilder(Model\Venta\Cuota::class)
->disableOriginalConstructor()->getMock();
$this->cuota->id = $faker->randomNumber();
$this->cuotaRepository = $this->getMockBuilder(Repository\Venta\Cuota::class)
->disableOriginalConstructor()->getMock();
$this->cuotaRepository->method('fetchById')->willReturn($this->cuota);
}
public function testCreate(): void
{
$repository = new Repository\MediosPago\Toku\Invoice($this->connection, $this->cuotaRepository);
$faker = Faker\Factory::create();
$data = [
'cuota_id' => $this->cuota->id,
'toku_id' => $faker->ean13(),
];
$invoice = $repository->create($data);
$this->assertEquals($this->cuota->id, $invoice->cuota->id);
$this->assertEquals($data['toku_id'], $invoice->toku_id);
}
public function testSave(): Model\MediosPago\Toku\Invoice
{
$repository = new Repository\MediosPago\Toku\Invoice($this->connection, $this->cuotaRepository);
$faker = Faker\Factory::create();
$data = [
'cuota_id' => $this->cuota->id,
'toku_id' => $faker->ean13(),
];
$invoice = $repository->create($data);
$invoice = $repository->save($invoice);
$this->assertNotNull($invoice->id);
$this->assertEquals($this->cuota->id, $invoice->cuota->id);
$this->assertEquals($data['toku_id'], $invoice->toku_id);
return $invoice;
}
#[Depends('testSave')]
public function testFetch(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->fetchById($reference->id);
$this->assertInstanceOf(Model\MediosPago\Toku\Invoice::class, $invoice);
$this->assertEquals($reference->id, $invoice->id);
$this->assertEquals($this->cuota->id, $invoice->cuota->id);
$this->assertEquals($reference->toku_id, $invoice->toku_id);
}
#[Depends('testSave')]
public function testFetchByCuota(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->fetchByCuota($this->cuota->id);
$this->assertEquals($reference->id, $invoice->id);
$this->assertEquals($this->cuota->id, $invoice->cuota->id);
$this->assertEquals($reference->toku_id, $invoice->toku_id);
}
}

View File

@ -0,0 +1,112 @@
<?php
namespace Tests\Unit\Repository\MediosPago\Toku;
use PDO;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\TestCase;
use Faker;
use Incoviba\Common\Define;
use Incoviba\Model;
use Incoviba\Repository;
class SubscriptionTest extends TestCase
{
protected PDO $pdo;
protected Define\Connection $connection;
protected Model\Venta $venta;
protected Repository\Venta $ventaRepository;
protected function setUp(): void
{
$dsn = "mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_DATABASE']}";
$this->pdo = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASSWORD']);
$this->connection = $this->getMockBuilder(Define\Connection::class)
->disableOriginalConstructor()
->getMock();
$this->connection->method('getPDO')->willReturn($this->pdo);
$faker = Faker\Factory::create();
$this->venta = $this->getMockBuilder(Model\Venta::class)
->disableOriginalConstructor()->getMock();
$this->venta->id = $faker->randomNumber();
$this->ventaRepository = $this->getMockBuilder(Repository\Venta::class)
->disableOriginalConstructor()->getMock();
$this->ventaRepository->method('fetchById')->willReturn($this->venta);
}
public function testCreate(): void
{
$repository = new Repository\MediosPago\Toku\Subscription($this->connection, $this->ventaRepository);
$faker = Faker\Factory::create();
$data = [
'venta_id' => $this->venta->id,
'toku_id' => $faker->ean13(),
];
$subscription = $repository->create($data);
$this->assertEquals($this->venta->id, $subscription->venta->id);
$this->assertEquals($data['toku_id'], $subscription->toku_id);
}
public function testSave(): Model\MediosPago\Toku\Subscription
{
$repository = new Repository\MediosPago\Toku\Subscription($this->connection, $this->ventaRepository);
$faker = Faker\Factory::create();
$data = [
'venta_id' => $this->venta->id,
'toku_id' => $faker->ean13(),
];
$subscription = $repository->create($data);
$subscription = $repository->save($subscription);
$this->assertNotNull($subscription->id);
$this->assertEquals($this->venta->id, $subscription->venta->id);
$this->assertEquals($data['toku_id'], $subscription->toku_id);
return $subscription;
}
#[Depends('testSave')]
public function testFetch(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->fetchById($reference->id);
$this->assertInstanceOf(Model\MediosPago\Toku\Subscription::class, $subscription);
$this->assertEquals($reference->id, $subscription->id);
$this->assertEquals($this->venta->id, $subscription->venta->id);
$this->assertEquals($reference->toku_id, $subscription->toku_id);
}
#[Depends('testSave')]
public function testFetchByVenta(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->fetchByVenta($this->venta->id);
$this->assertEquals($reference->id, $subscription->id);
$this->assertEquals($this->venta->id, $subscription->venta->id);
$this->assertEquals($reference->toku_id, $subscription->toku_id);
}
}