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,165 @@
<?php
namespace Incoviba\Service\MediosPago;
use HttpException;
use PDOException;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\ResponseInterface;
use Incoviba\Common\Define\Repository;
use Incoviba\Common\Implement\Exception\{EmptyResponse, EmptyResult};
use Incoviba\Common\Ideal\LoggerEnabled;
abstract class AbstractEndPoint extends LoggerEnabled implements EndPoint
{
public function __construct(protected ClientInterface $client) {}
/**
* @param ResponseInterface $response
* @param string $request_uri
* @param array $validStatus
* @param array $invalidStatus
* @return void
* @throws EmptyResponse
*/
protected function validateResponse(ResponseInterface $response, string $request_uri, array $validStatus, array $invalidStatus): void
{
$status = $response->getStatusCode();
$reason = $response->getReasonPhrase();
$contents = $response->getBody()->getContents();
if (in_array($status, $invalidStatus)) {
$exception = new HttpException("{$reason}\n{$contents}", $status);
throw new EmptyResponse($request_uri, $exception);
}
if (!in_array($status, $validStatus)) {
$exception = new HttpException("{$reason}\n{$contents}", $status);
throw new EmptyResponse($request_uri, $exception);
}
}
/**
* @param string $request_uri
* @param array $validStatus
* @param array $invalidStatus
* @return string
* @throws EmptyResponse
*/
protected function sendGet(string $request_uri, array $validStatus, array $invalidStatus): array
{
try {
$response = $this->client->get($request_uri);
} catch (ClientExceptionInterface $exception) {
throw new EmptyResponse($request_uri, $exception);
}
$this->validateResponse($response, $request_uri, $validStatus, $invalidStatus);
return json_decode($response->getBody()->getContents(), true);
}
/**
* @param string $request_uri
* @param array $data
* @param array $validStatus
* @param array $invalidStatus
* @return string
* @throws EmptyResponse
*/
protected function sendAdd(string $request_uri, array $data, array $validStatus, array $invalidStatus): bool
{
$params = $this->mapParams($data);
try {
$response = $this->client->post($request_uri, ['json' => $params]);
} catch (ClientExceptionInterface $exception) {
throw new EmptyResponse($request_uri, $exception);
}
$this->validateResponse($response, $request_uri, $validStatus, $invalidStatus);
$contents = $response->getBody()->getContents();
$json = json_decode($contents, true);
return $this->save($json);
}
/**
* @param string $request_uri
* @param array $data
* @param array $validStatus
* @param array $invalidStatus
* @return string
* @throws EmptyResponse
*/
protected function sendEdit(string $request_uri, array $data, array $validStatus, array $invalidStatus): bool
{
$params = $this->mapParams($data);
try {
$response = $this->client->put($request_uri, ['json' => $params]);
} catch (ClientExceptionInterface $exception) {
throw new EmptyResponse($request_uri, $exception);
}
$this->validateResponse($response, $request_uri, $validStatus, $invalidStatus);
$contents = $response->getBody()->getContents();
$json = json_decode($contents, true);
return $this->save($json);
}
/**
* @param string $request_uri
* @param array $validStatus
* @param array $invalidStatus
* @return void
* @throws EmptyResponse
*/
protected function sendDelete(string $request_uri, array $validStatus, array $invalidStatus): void
{
try {
$response = $this->client->delete($request_uri);
} catch (ClientExceptionInterface $exception) {
throw new EmptyResponse($request_uri, $exception);
}
$this->validateResponse($response, $request_uri, $validStatus, $invalidStatus);
}
protected function doSave(Repository $repository, array $data): bool
{
try {
$repository->fetchById($data['id']);
return true;
} catch (EmptyResult) {
$mappedData = $this->mapSave($data);
$filteredData = $repository->filterData($mappedData);
$model = $repository->create($filteredData);
try {
$repository->save($model);
return true;
} catch (PDOException $exception) {
$this->logger->warning($exception);
return false;
}
}
}
/**
* @param callable $callable
* @param string $id
* @param string $message
* @return array
* @throws EmptyResponse
*/
protected function doGetById(callable $callable, string $id, string $message): array
{
try {
$model = $callable($id);
return json_decode(json_encode($model), true);
} catch (EmptyResult $exception) {
throw new EmptyResponse($message, $exception);
}
}
abstract protected function mapParams(array $data): array;
abstract protected function mapSave(array $data): array;
abstract protected function save(array $data): bool;
}

View File

@ -0,0 +1,42 @@
<?php
namespace Incoviba\Service\MediosPago;
use Incoviba\Common\Implement\Exception\EmptyResponse;
interface EndPoint
{
/**
* @param string $id
* @return array
* @throws EmptyResponse
*/
public function getById(string $id): array;
/**
* @param string $id
* @return array
* @throws EmptyResponse
*/
public function get(string $id): array;
/**
* @param array $data
* @return bool
* @throws EmptyResponse
*/
public function add(array $data): bool;
/**
* @param string $id
* @param array $data
* @return bool
* @throws EmptyResponse
*/
public function edit(string $id, array $data): bool;
/**
* @param string $id
* @return void
* @throws EmptyResponse
*/
public function delete(string $id): void;
}

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);
}
}