feature/cierres (#25)
Varios cambios Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl> Reviewed-on: #25
This commit is contained in:
15
app/tests/extension/AbstractIntegration.php
Normal file
15
app/tests/extension/AbstractIntegration.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace Tests\Extension;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
|
||||
abstract class AbstractIntegration extends TestCase
|
||||
{
|
||||
protected ClientInterface $client;
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->client = new Client(['base_uri' => $_ENV['APP_URL']]);
|
||||
}
|
||||
}
|
10
app/tests/extension/AbstractModel.php
Normal file
10
app/tests/extension/AbstractModel.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace Tests\Extension;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Incoviba\Common\Define\Model;
|
||||
|
||||
abstract class AbstractModel extends TestCase
|
||||
{
|
||||
protected Model $model;
|
||||
}
|
17
app/tests/extension/AbstractPerformance.php
Normal file
17
app/tests/extension/AbstractPerformance.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Tests\Extension;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
abstract class AbstractPerformance extends TestCase
|
||||
{
|
||||
protected float $startTime;
|
||||
protected function start(): void
|
||||
{
|
||||
$this->startTime = microtime(true);
|
||||
}
|
||||
protected function end(): float
|
||||
{
|
||||
return microtime(true) - $this->startTime;
|
||||
}
|
||||
}
|
147
app/tests/extension/AbstractSeed.php
Normal file
147
app/tests/extension/AbstractSeed.php
Normal file
@ -0,0 +1,147 @@
|
||||
<?php
|
||||
namespace Tests\Extension;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use Faker;
|
||||
use Tests\Extension\Faker\Provider\Rut;
|
||||
|
||||
abstract class AbstractSeed implements SeedInterface
|
||||
{
|
||||
public function __construct(PDO $connection)
|
||||
{
|
||||
$this->setConnection($connection);
|
||||
$this->faker = Faker\Factory::create('es_AR');
|
||||
$this->faker->addProvider(new Rut($this->faker));
|
||||
}
|
||||
|
||||
protected PDO $connection;
|
||||
protected Faker\Generator $faker;
|
||||
public function setConnection(PDO $connection): SeedInterface
|
||||
{
|
||||
$this->connection = $connection;
|
||||
return $this;
|
||||
}
|
||||
public function getConnection(): PDO
|
||||
{
|
||||
return $this->connection;
|
||||
}
|
||||
public function getDependencies(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
protected string $table;
|
||||
protected function table(string $table): self
|
||||
{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected array $queryQueue = [];
|
||||
protected function insertValues(array $valueRows): self
|
||||
{
|
||||
$columns = array_keys($valueRows[0]);
|
||||
$columnsString = implode(', ', array_map(fn($column) => "`{$column}`", $columns));
|
||||
$placeholderArray = array_map(fn($column) => ":{$column}", $columns);
|
||||
$placeholders = implode(', ', $placeholderArray);
|
||||
$query = "INSERT INTO `{$this->table}` ({$columnsString}) VALUES ({$placeholders})";
|
||||
$this->queryQueue []= ['query' => $query, 'values' => $valueRows];
|
||||
return $this;
|
||||
}
|
||||
protected function save(): self
|
||||
{
|
||||
foreach ($this->queryQueue as $entry) {
|
||||
$query = $entry['query'];
|
||||
$valueRows = $entry['values'];
|
||||
|
||||
foreach ($valueRows as $valueRow) {
|
||||
try {
|
||||
$this->connection->beginTransaction();
|
||||
$statement = $this->connection->prepare($query);
|
||||
if ($statement === false) {
|
||||
$this->connection->rollBack();
|
||||
continue;
|
||||
}
|
||||
$statement->execute($valueRow);
|
||||
$this->connection->commit();
|
||||
} catch (PDOException | \Throwable $exception) {
|
||||
$this->connection->rollBack();
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function loadValues(string $table, array $conditions = [], string|array $columns = '*'): array
|
||||
{
|
||||
$columns = $this->processColumns($columns);
|
||||
$query = "SELECT {$columns} FROM `{$table}`";
|
||||
if (count($conditions) > 0) {
|
||||
$conditionsString = $this->processConditions($conditions);
|
||||
$query = "{$query} WHERE {$conditionsString}";
|
||||
}
|
||||
try {
|
||||
$statement = $this->connection->prepare($query);
|
||||
$statement->execute();
|
||||
} catch (PDOException) {
|
||||
return [];
|
||||
}
|
||||
try {
|
||||
if ($columns !== '*' and !str_contains($columns, ',')) {
|
||||
return $statement->fetchAll(PDO::FETCH_COLUMN);
|
||||
}
|
||||
return $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
protected function processColumns(string|array $columns): string
|
||||
{
|
||||
if (is_array($columns)) {
|
||||
$columns = implode(',', array_map(fn($column) => "`{$column}`", $columns));
|
||||
}
|
||||
if ($columns === '*') {
|
||||
return $columns;
|
||||
}
|
||||
$columns = array_map(fn($column) => trim($column), explode(',', $columns));
|
||||
return implode(', ', array_map(function($column) {
|
||||
if (!str_contains($column, '`')) {
|
||||
return "`{$column}`";
|
||||
}
|
||||
return $column;
|
||||
}, $columns));
|
||||
}
|
||||
protected function processConditions(array $conditions): array
|
||||
{
|
||||
$processedConditions = [];
|
||||
$processedValues = [];
|
||||
foreach ($conditions as $condition) {
|
||||
if (is_string($condition) and (str_starts_with(strtolower($condition), 'and') or str_starts_with(strtolower($condition), 'or'))) {
|
||||
$processedConditions[] = $condition;
|
||||
continue;
|
||||
}
|
||||
$column = $condition['column'];
|
||||
$value = $condition['value'];
|
||||
$match = $condition['match'] ?? 'AND';
|
||||
$operator = $condition['operator'] ?? '=';
|
||||
$columnValue = ":{$column}";
|
||||
if (is_array($value)) {
|
||||
$columnString = [];
|
||||
foreach ($value as $idx => $val) {
|
||||
$columnValue = ":{$column}_{$idx}";
|
||||
$columnString[] = $columnValue;
|
||||
$processedValues["{$column}_{$idx}"] = $val;
|
||||
}
|
||||
$columnValue = '(' . implode(', ', $columnString) . ')';
|
||||
if (!str_contains($operator, 'IN')) {
|
||||
$operator = 'IN';
|
||||
}
|
||||
} else {
|
||||
$processedValues[$column] = $value;
|
||||
}
|
||||
$processedConditions[] = "{$match} `{$column}` {$operator} {$columnValue}";
|
||||
}
|
||||
return ['query' => implode(' ', $processedConditions), 'values' => $processedValues];
|
||||
}
|
||||
}
|
38
app/tests/extension/Faker/Provider/Rut.php
Normal file
38
app/tests/extension/Faker/Provider/Rut.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace Tests\Extension\Faker\Provider;
|
||||
|
||||
use Faker\Provider\Base;
|
||||
|
||||
class Rut extends Base
|
||||
{
|
||||
public function rut(bool $withDigito = true, bool $withDotsAndSlash = true): string
|
||||
{
|
||||
$base = self::numberBetween(1000000, 99999999);
|
||||
$rut = $base;
|
||||
if ($withDotsAndSlash) {
|
||||
$rut = number_format($rut, 0, ',', '.');
|
||||
}
|
||||
if ($withDigito) {
|
||||
$digito = $this->getDigito($base);
|
||||
if ($withDotsAndSlash) {
|
||||
return "{$digito}-{$rut}";
|
||||
}
|
||||
return "{$digito}{$rut}";
|
||||
}
|
||||
return $rut;
|
||||
}
|
||||
public function digitoVerificador(string $rut): bool|string
|
||||
{
|
||||
if ( !preg_match("/^[0-9.]+/",$rut)) return false;
|
||||
$rut = str_replace('.','',$rut);
|
||||
return $this->getDigito($rut);
|
||||
}
|
||||
|
||||
protected function getDigito(string $rut): string
|
||||
{
|
||||
$M=0;$S=1;
|
||||
for(;$rut;$rut=floor($rut/10))
|
||||
$S=($S+$rut%10*(9-$M++%6))%11;
|
||||
return $S?$S-1:'K';
|
||||
}
|
||||
}
|
10
app/tests/extension/ObjectHasMethodTrait.php
Normal file
10
app/tests/extension/ObjectHasMethodTrait.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace Tests\Extension;
|
||||
|
||||
trait ObjectHasMethodTrait
|
||||
{
|
||||
public function assertObjectHasMethod(string $method, object $object): void
|
||||
{
|
||||
$this->assertTrue(method_exists($object, $method), sprintf('The object %s does not have the method %s', get_class($object), $method));
|
||||
}
|
||||
}
|
27
app/tests/extension/SeedInterface.php
Normal file
27
app/tests/extension/SeedInterface.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace Tests\Extension;
|
||||
|
||||
use PDO;
|
||||
|
||||
interface SeedInterface
|
||||
{
|
||||
/**
|
||||
* @param PDO $connection
|
||||
* @return self
|
||||
*/
|
||||
public function setConnection(PDO $connection): self;
|
||||
/**
|
||||
* @return PDO
|
||||
*/
|
||||
public function getConnection(): PDO;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getDependencies(): array;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function run(): void;
|
||||
}
|
33
app/tests/extension/Seeds/Direcciones.php
Normal file
33
app/tests/extension/Seeds/Direcciones.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace Tests\Extension\Seeds;
|
||||
|
||||
use Tests\Extension\AbstractSeed;
|
||||
|
||||
class Direcciones extends AbstractSeed
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$comunas = $this->loadValues('comuna', columns: 'id');
|
||||
|
||||
$n = 50;
|
||||
$data = [];
|
||||
for ($i = 0; $i < $n; $i++) {
|
||||
$row = [
|
||||
'calle' => $this->faker->streetName,
|
||||
'numero' => $this->faker->randomNumber(5),
|
||||
'comuna' => $this->faker->randomElement($comunas),
|
||||
'extra' => '',
|
||||
];
|
||||
$extraRand = ((int) round(rand() / getrandmax())) === 1;
|
||||
if ($extraRand) {
|
||||
$nExtra = (int) round(rand(1, 3));
|
||||
$row['extra'] = $this->faker->words($nExtra, true);
|
||||
}
|
||||
$data[] = $row;
|
||||
}
|
||||
|
||||
$this->table('direccion')
|
||||
->insertValues($data)
|
||||
->save();
|
||||
}
|
||||
}
|
39
app/tests/extension/Seeds/Inmobiliarias.php
Normal file
39
app/tests/extension/Seeds/Inmobiliarias.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
namespace Tests\Extension\Seeds;
|
||||
|
||||
use Tests\Extension\AbstractSeed;
|
||||
|
||||
class Inmobiliarias extends AbstractSeed
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$tipos = $this->loadValues('tipo_sociedad', columns: 'id');
|
||||
$suffixes = [
|
||||
'Inmobiliaria ',
|
||||
'Administradora ',
|
||||
'Asesorías ',
|
||||
''
|
||||
];
|
||||
|
||||
$n = 5;
|
||||
$data = [];
|
||||
for ($i = 0; $i < $n; $i++) {
|
||||
$rut = $this->faker->rut(false, false);
|
||||
$abreviacion = $this->faker->streetName;
|
||||
$suffix = $this->faker->randomElement($suffixes);
|
||||
$razon = "{$suffix}{$abreviacion}";
|
||||
$sigla = strtoupper(substr($abreviacion, 0, 3));
|
||||
$data []= [
|
||||
'rut' => $rut,
|
||||
'dv' => $this->faker->digitoVerificador($rut),
|
||||
'razon' => $razon,
|
||||
'abreviacion' => $abreviacion,
|
||||
'sigla' => $sigla,
|
||||
'sociedad' => $this->faker->randomElement($tipos),
|
||||
];
|
||||
}
|
||||
$this->table('inmobiliaria')
|
||||
->insertValues($data)
|
||||
->save();
|
||||
}
|
||||
}
|
40
app/tests/extension/Seeds/Proyectos.php
Normal file
40
app/tests/extension/Seeds/Proyectos.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace Tests\Extension\Seeds;
|
||||
|
||||
use Tests\Extension\AbstractSeed;
|
||||
|
||||
class Proyectos extends AbstractSeed
|
||||
{
|
||||
public function getDependencies(): array
|
||||
{
|
||||
return [
|
||||
Inmobiliarias::class,
|
||||
Direcciones::class
|
||||
];
|
||||
}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$inmobiliarias = $this->loadValues('inmobiliaria', columns: 'rut');
|
||||
$direcciones = $this->loadValues('direccion', columns: 'id');
|
||||
|
||||
$n = 10;
|
||||
$data = [];
|
||||
for ($i = 0; $i < $n; $i++) {
|
||||
$data[] = [
|
||||
'inmobiliaria' => $this->faker->randomElement($inmobiliarias),
|
||||
'descripcion' => $this->faker->words(2, true),
|
||||
'direccion' => $this->faker->randomElement($direcciones),
|
||||
'superficie_sobre_nivel' => $this->faker->randomFloat(2, 1000, 10000),
|
||||
'superficie_bajo_nivel' => $this->faker->randomFloat(2, 0, 5000),
|
||||
'pisos' => $this->faker->randomNumber(2),
|
||||
'subterraneos' => $this->faker->randomNumber(2),
|
||||
'corredor' => $this->faker->randomFloat(4, 0, 1)
|
||||
];
|
||||
}
|
||||
|
||||
$this->table('proyecto')
|
||||
->insertValues($data)
|
||||
->save();
|
||||
}
|
||||
}
|
70
app/tests/extension/TestSeeder.php
Normal file
70
app/tests/extension/TestSeeder.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
namespace Tests\Extension;
|
||||
|
||||
use FilesystemIterator;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
class TestSeeder
|
||||
{
|
||||
public function __construct(protected PDO $connection) {}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$seedClasses = $this->getSeedClasses();
|
||||
$orderedSeeds = $this->orderedSeeds($seedClasses);
|
||||
foreach ($orderedSeeds as $seed) {
|
||||
$seed->run();
|
||||
}
|
||||
}
|
||||
|
||||
protected function getSeedClasses(): array
|
||||
{
|
||||
$seedsFolder = implode(DIRECTORY_SEPARATOR, [__DIR__, 'Seeds']);
|
||||
$files = new FilesystemIterator($seedsFolder, FilesystemIterator::SKIP_DOTS);
|
||||
$seeds = [];
|
||||
foreach ($files as $file) {
|
||||
$seeds []= $this->buildClassName($file->getBasename('.php'));
|
||||
}
|
||||
return $seeds;
|
||||
}
|
||||
protected function getSeed(string $seedClassName): SeedInterface
|
||||
{
|
||||
return new $seedClassName($this->connection);
|
||||
}
|
||||
|
||||
protected function buildClassName(string $fileBaseName): string
|
||||
{
|
||||
$namespace = implode('\\', [__NAMESPACE__, 'Seeds']);
|
||||
return implode('\\', [$namespace, $fileBaseName]);
|
||||
}
|
||||
|
||||
protected function orderedSeeds(array $seedClasses): array
|
||||
{
|
||||
$orderedSeeds = [];
|
||||
foreach ($seedClasses as $seedClassName) {
|
||||
$seed = $this->getSeed($seedClassName);
|
||||
if ($seed->getDependencies() === []) {
|
||||
$orderedSeeds[$seedClassName] = $seed;
|
||||
continue;
|
||||
}
|
||||
$orderedSeeds = array_merge($orderedSeeds, $this->orderedDependencies($orderedSeeds, $seedClasses, $seedClassName));
|
||||
}
|
||||
return $orderedSeeds;
|
||||
}
|
||||
|
||||
protected function orderedDependencies(array $orderedSeeds, array $seedClasses, string $seedClassName): array
|
||||
{
|
||||
$seed = $this->getSeed($seedClassName);
|
||||
$dependencies = $seed->getDependencies();
|
||||
foreach ($dependencies as $dependencyClass) {
|
||||
if (!array_key_exists($dependencyClass, $orderedSeeds)) {
|
||||
$orderedSeeds = array_merge($orderedSeeds, $this->orderedDependencies($orderedSeeds, $seedClasses, $dependencyClass));
|
||||
}
|
||||
}
|
||||
if (!array_key_exists($seedClassName, $orderedSeeds)) {
|
||||
$orderedSeeds[$seedClassName] = $seed;
|
||||
}
|
||||
return $orderedSeeds;
|
||||
}
|
||||
}
|
18
app/tests/extension/testMethodsTrait.php
Normal file
18
app/tests/extension/testMethodsTrait.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace Tests\Extension;
|
||||
|
||||
trait testMethodsTrait
|
||||
{
|
||||
use ObjectHasMethodTrait;
|
||||
|
||||
public function testMethods(): void
|
||||
{
|
||||
$object = $this->model;
|
||||
|
||||
foreach ($this->methods as $method) {
|
||||
$this->assertObjectHasMethod($method, $object);
|
||||
}
|
||||
}
|
||||
|
||||
protected array $methods = [];
|
||||
}
|
24
app/tests/extension/testPropertiesTrait.php
Normal file
24
app/tests/extension/testPropertiesTrait.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace Tests\Extension;
|
||||
|
||||
use Incoviba\Common\Define\Model;
|
||||
|
||||
trait testPropertiesTrait
|
||||
{
|
||||
use ObjectHasMethodTrait;
|
||||
|
||||
public function testProperties(): void
|
||||
{
|
||||
$model = $this->model;
|
||||
|
||||
$this->assertProperties($model);
|
||||
}
|
||||
|
||||
protected array $properties = [];
|
||||
protected function assertProperties(Model $model): void
|
||||
{
|
||||
foreach ($this->properties as $key) {
|
||||
$this->assertObjectHasProperty($key, $model);
|
||||
}
|
||||
}
|
||||
}
|
28
app/tests/integration/API/Ventas/MediosPago/TokuTest.php
Normal file
28
app/tests/integration/API/Ventas/MediosPago/TokuTest.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
namespace Tests\Integration\API\Ventas\MediosPago;
|
||||
|
||||
use Tests\Extension\AbstractIntegration;
|
||||
|
||||
class TokuTest extends AbstractIntegration
|
||||
{
|
||||
public function testCuotas()
|
||||
{
|
||||
|
||||
}
|
||||
public function testSuccess()
|
||||
{
|
||||
|
||||
}
|
||||
public function testTest()
|
||||
{
|
||||
|
||||
}
|
||||
public function testReset()
|
||||
{
|
||||
|
||||
}
|
||||
public function testEnqueue()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
14
app/tests/integration/HomeTest.php
Normal file
14
app/tests/integration/HomeTest.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace Test\Integration;
|
||||
|
||||
use Tests\Extension\AbstractIntegration;
|
||||
|
||||
class HomeTest extends AbstractIntegration
|
||||
{
|
||||
public function testLoad(): void
|
||||
{
|
||||
$response = $this->client->get('/');
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertStringContainsString('Incoviba', $response->getBody()->getContents());
|
||||
}
|
||||
}
|
140
app/tests/integration/QueueTest.php
Normal file
140
app/tests/integration/QueueTest.php
Normal file
@ -0,0 +1,140 @@
|
||||
<?php
|
||||
namespace Incoviba\Test\Integration;
|
||||
|
||||
use DateInterval;
|
||||
use DateTimeImmutable;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Faker;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Service;
|
||||
use Incoviba\Repository;
|
||||
use Tests\Extension\Faker\Provider\Rut;
|
||||
|
||||
class QueueTest extends TestCase
|
||||
{
|
||||
protected ContainerInterface $container;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
require_once implode(DIRECTORY_SEPARATOR, [dirname(__DIR__, 2), 'setup', 'container.php']);
|
||||
$this->container = buildContainer();
|
||||
}
|
||||
|
||||
public function testServiceWorker(): void
|
||||
{
|
||||
$faker = Faker\Factory::create();
|
||||
$faker->addProvider(new Rut($faker));
|
||||
$pagoData = [
|
||||
'fecha' => '2022-01-01',
|
||||
'valor' => 10000,
|
||||
];
|
||||
$pagoService = $this->container->get(Service\Venta\Pago::class);
|
||||
$pago = $pagoService->add($pagoData);
|
||||
|
||||
$this->assertEquals(0.0, $pago->uf);
|
||||
|
||||
$queueService = $this->container->get(Service\Queue::class);
|
||||
$queueService->run();
|
||||
|
||||
$pago = $pagoService->getById($pago->id);
|
||||
$this->assertNotEquals(0.0, $pago->uf);
|
||||
|
||||
$direccionRepository = $this->container->get(Repository\Direccion::class);
|
||||
$direcciones = $direccionRepository->fetchAll();
|
||||
$direccion = $faker->randomElement($direcciones);
|
||||
$rut = $faker->rut(false, false);
|
||||
$propietarioData = [
|
||||
'rut' => $rut,
|
||||
'dv' => $faker->digitoVerificador($rut),
|
||||
'direccion' => $direccion->id,
|
||||
'nombres' => $faker->firstName,
|
||||
'apellido_paterno' => $faker->lastName,
|
||||
'apellido_materno' => $faker->lastName,
|
||||
'email' => $faker->email,
|
||||
'telefono' => $faker->randomNumber(9),
|
||||
];
|
||||
$propietarioRepository = $this->container->get(Repository\Venta\Propietario::class);
|
||||
$propietario = $propietarioRepository->create($propietarioData);
|
||||
$propietario = $propietarioRepository->save($propietario);
|
||||
$proyectoRepository = $this->container->get(Repository\Proyecto::class);
|
||||
$proyectos = $proyectoRepository->fetchAll();
|
||||
$proyecto = $faker->randomElement($proyectos);
|
||||
$tipoUnidadRepository = $this->container->get(Repository\Proyecto\TipoUnidad::class);
|
||||
$tiposUnidades = $tipoUnidadRepository->fetchAll();
|
||||
$tipoUnidad = $faker->randomElement($tiposUnidades);
|
||||
$proyectoTipoUnidadData = [
|
||||
'proyecto' => $proyecto->id,
|
||||
'tipo' => $tipoUnidad->id,
|
||||
'nombre' => $faker->word,
|
||||
'descripcion' => $faker->sentence,
|
||||
'abreviacion' => substr($faker->word, 0, 1),
|
||||
'logia' => $faker->randomFloat(2, 1, 20),
|
||||
'terraza' => $faker->randomFloat(2, 1, 20),
|
||||
'm2' => $faker->randomFloat(2, 20, 100),
|
||||
];
|
||||
$proyectoTipoUnidadRepository = $this->container->get(Repository\Proyecto\ProyectoTipoUnidad::class);
|
||||
$proyectoTipoUnidad = $proyectoTipoUnidadRepository->create($proyectoTipoUnidadData);
|
||||
$proyectoTipoUnidad = $proyectoTipoUnidadRepository->save($proyectoTipoUnidad);
|
||||
$unidadData = [
|
||||
'proyecto' => $proyecto->id,
|
||||
'tipo' => $tipoUnidad->id,
|
||||
'piso' => $faker->numberBetween(1, 300),
|
||||
'descripcion' => "{$tipoUnidad->descripcion} {$faker->numberBetween(1, 300)}",
|
||||
'orientacion' => $faker->randomElement(['N', 'NE', 'NO', 'S', 'SE', 'SO', 'E', 'O']),
|
||||
'pt' => $proyectoTipoUnidad->id,
|
||||
];
|
||||
$unidadRepository = $this->container->get(Repository\Venta\Unidad::class);
|
||||
$unidad = $unidadRepository->create($unidadData);
|
||||
$unidad = $unidadRepository->save($unidad);
|
||||
$propiedadData = [
|
||||
'unidad_principal' => $unidad->id,
|
||||
];
|
||||
$propiedadRepository = $this->container->get(Repository\Venta\Propiedad::class);
|
||||
$propiedad = $propiedadRepository->create($propiedadData);
|
||||
$propiedad = $propiedadRepository->save($propiedad);
|
||||
$fecha = $faker->date;
|
||||
$pieData = [
|
||||
'valor' => 10000,
|
||||
'cuotas' => 12,
|
||||
'fecha' => $fecha,
|
||||
];
|
||||
$pieRepository = $this->container->get(Repository\Venta\Pie::class);
|
||||
$pie = $pieRepository->create($pieData);
|
||||
$pie = $pieRepository->save($pie);
|
||||
$ventaData = [
|
||||
'fecha' => $fecha,
|
||||
'propietario' => $propietario->rut,
|
||||
'propiedad' => $propiedad->id,
|
||||
'fecha_ingreso' => new DateTimeImmutable($fecha)->add(new DateInterval('P3D'))->format('Y-m-d'),
|
||||
'pie' => $pie->id,
|
||||
];
|
||||
$ventaRepository = $this->container->get(Repository\Venta::class);
|
||||
$venta = $ventaRepository->create($ventaData);
|
||||
$venta = $ventaRepository->save($venta);
|
||||
$bancoData = [
|
||||
'nombre' => $faker->word,
|
||||
];
|
||||
$bancoRepository = $this->container->get(Repository\Contabilidad\Banco::class);
|
||||
$banco = $bancoRepository->create($bancoData);
|
||||
$banco = $bancoRepository->save($banco);
|
||||
|
||||
$cuotaData = [
|
||||
'pie' => $pie->id,
|
||||
'fecha' => $faker->dateTimeBetween('2024-01-01')->format('Y-m-d'),
|
||||
'valor' => 10000,
|
||||
'banco' => $banco->id,
|
||||
];
|
||||
$cuotaService = $this->container->get(Service\Venta\Cuota::class);
|
||||
$cuota = $cuotaService->add($cuotaData);
|
||||
|
||||
$this->assertEquals(0.0, $cuota->pago->uf);
|
||||
|
||||
$queueService->run();
|
||||
|
||||
$cuota = $cuotaService->getById($cuota->id);
|
||||
$this->assertGreaterThan(0.0, $cuota->pago->uf);
|
||||
}
|
||||
}
|
@ -1,17 +1,22 @@
|
||||
<?php
|
||||
namespace ProVM\Performance;
|
||||
namespace Tests\Performance;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use PHPUnit\Framework;
|
||||
use Tests\Extension\AbstractPerformance;
|
||||
|
||||
class HomeTest extends Framework\TestCase
|
||||
class HomeTest extends AbstractPerformance
|
||||
{
|
||||
protected Client $client;
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->client = new Client(['base_uri' => $_ENV['APP_URL']]);
|
||||
}
|
||||
|
||||
public function testLoad(): void
|
||||
{
|
||||
$client = new Client(['base_uri' => 'http://proxy']);
|
||||
$start = microtime(true);
|
||||
$response = $client->get('');
|
||||
$end = microtime(true);
|
||||
$this->assertLessThanOrEqual(1000, $end - $start);
|
||||
$this->start();
|
||||
$this->client->get('/');
|
||||
$time = $this->end();
|
||||
$this->assertLessThanOrEqual(1000, $time);
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +1,17 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Model;
|
||||
|
||||
use Incoviba\Model\Inmobiliaria\Proveedor;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Tests\Extension\AbstractModel;
|
||||
use Tests\Extension\testPropertiesTrait;
|
||||
|
||||
class ProveedorTest extends TestCase
|
||||
class ProveedorTest extends AbstractModel
|
||||
{
|
||||
public function testCreate()
|
||||
{
|
||||
$proveedor = new Proveedor();
|
||||
$this->assertInstanceOf(Proveedor::class, $proveedor);
|
||||
}
|
||||
public function testProperties()
|
||||
{
|
||||
$proveedor = new Proveedor();
|
||||
use testPropertiesTrait;
|
||||
|
||||
$this->assertObjectHasProperty('rut', $proveedor);
|
||||
$this->assertObjectHasProperty('digito', $proveedor);
|
||||
$this->assertObjectHasProperty('nombre', $proveedor);
|
||||
$this->assertObjectHasProperty('razon', $proveedor);
|
||||
$this->assertObjectHasProperty('contacto', $proveedor);
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->model = new Proveedor();
|
||||
$this->properties = ['rut', 'digito', 'nombre', 'razon', 'contacto'];
|
||||
}
|
||||
}
|
||||
|
17
app/tests/unit/src/Model/Proyecto/Broker/ContactTest.php
Normal file
17
app/tests/unit/src/Model/Proyecto/Broker/ContactTest.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Model\Proyecto\Broker;
|
||||
|
||||
use Incoviba\Model\Proyecto\Broker\Contact;
|
||||
use Tests\Extension\AbstractModel;
|
||||
use Tests\Extension\testPropertiesTrait;
|
||||
|
||||
class ContactTest extends AbstractModel
|
||||
{
|
||||
use testPropertiesTrait;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->model = new Contact();
|
||||
$this->properties = ['name', 'email', 'phone', 'address'];
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Model\Proyecto\Broker\Contract;
|
||||
|
||||
use Incoviba\Model\Proyecto\Broker\Contract\State;
|
||||
use Tests\Extension\AbstractModel;
|
||||
use Tests\Extension\testPropertiesTrait;
|
||||
|
||||
class StateTest extends AbstractModel
|
||||
{
|
||||
use testPropertiesTrait;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->model = new State();
|
||||
$this->properties = ['contract', 'date', 'type'];
|
||||
}
|
||||
}
|
19
app/tests/unit/src/Model/Proyecto/Broker/ContractTest.php
Normal file
19
app/tests/unit/src/Model/Proyecto/Broker/ContractTest.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Model\Proyecto\Broker;
|
||||
|
||||
use Incoviba\Model\Proyecto\Broker\Contract;
|
||||
use Tests\Extension\AbstractModel;
|
||||
use Tests\Extension\testMethodsTrait;
|
||||
use Tests\Extension\testPropertiesTrait;
|
||||
|
||||
class ContractTest extends AbstractModel
|
||||
{
|
||||
use testPropertiesTrait, testMethodsTrait;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->model = new Contract();
|
||||
$this->properties = ['project', 'broker', 'commission'];
|
||||
$this->methods = ['states', 'current', 'promotions'];
|
||||
}
|
||||
}
|
17
app/tests/unit/src/Model/Proyecto/Broker/DataTest.php
Normal file
17
app/tests/unit/src/Model/Proyecto/Broker/DataTest.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Model\Proyecto\Broker;
|
||||
|
||||
use Incoviba\Model\Proyecto\Broker;
|
||||
use Tests\Extension\AbstractModel;
|
||||
use Tests\Extension\testPropertiesTrait;
|
||||
|
||||
class DataTest extends AbstractModel
|
||||
{
|
||||
use testPropertiesTrait;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->model = new Broker\Data();
|
||||
$this->properties = ['broker', 'representative', 'legalName'];
|
||||
}
|
||||
}
|
19
app/tests/unit/src/Model/Proyecto/BrokerTest.php
Normal file
19
app/tests/unit/src/Model/Proyecto/BrokerTest.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Model\Proyecto;
|
||||
|
||||
use Incoviba\Model\Proyecto\Broker;
|
||||
use Tests\Extension\AbstractModel;
|
||||
use Tests\Extension\testMethodsTrait;
|
||||
use Tests\Extension\testPropertiesTrait;
|
||||
|
||||
class BrokerTest extends AbstractModel
|
||||
{
|
||||
use testPropertiesTrait, testMethodsTrait;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->model = new Broker();
|
||||
$this->properties = ['rut', 'digit', 'name'];
|
||||
$this->methods = ['data', 'contracts'];
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
namespace Test\Unit\Model\Venta\MediosPago\Toku;
|
||||
|
||||
use Faker;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Tests\Extension\ObjectHasMethodTrait;
|
||||
use Incoviba\Model\Persona;
|
||||
use Incoviba\Model\Venta\MediosPago\Toku\Customer;
|
||||
|
||||
class CustomerTest extends TestCase
|
||||
{
|
||||
use ObjectHasMethodTrait;
|
||||
|
||||
public static function dataProperties(): array
|
||||
{
|
||||
return [
|
||||
['id'],
|
||||
['persona'],
|
||||
['toku_id']
|
||||
];
|
||||
}
|
||||
#[DataProvider('dataProperties')]
|
||||
public function testProperties(string $propertyName): void
|
||||
{
|
||||
$customer = new Customer();
|
||||
|
||||
$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();
|
||||
|
||||
$persona = $this->getMockBuilder(Persona::class)->disableOriginalConstructor()->getMock();
|
||||
$persona->rut = $faker->randomNumber(8);
|
||||
$persona->digito = 'k';
|
||||
|
||||
$toku_id = $faker->ean13();
|
||||
|
||||
$id = $faker->randomNumber(4);
|
||||
|
||||
$customer = new Customer();
|
||||
$customer->id = $id;
|
||||
$customer->persona = $persona;
|
||||
$customer->toku_id = $toku_id;
|
||||
|
||||
$expected = json_encode([
|
||||
'id' => $id,
|
||||
'rut' => implode('', [$persona->rut, strtoupper($persona->digito)]),
|
||||
'toku_id' => $toku_id
|
||||
]);
|
||||
$this->assertJsonStringEqualsJsonString($expected, json_encode($customer));
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Model\Venta\MediosPago\Toku;
|
||||
|
||||
use Faker;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Incoviba\Model;
|
||||
|
||||
class InvoiceTest extends TestCase
|
||||
{
|
||||
public static function dataProperties(): array
|
||||
{
|
||||
return [
|
||||
['id'],
|
||||
['cuota'],
|
||||
['toku_id'],
|
||||
];
|
||||
}
|
||||
#[DataProvider('dataProperties')]
|
||||
public function testProperties(string $propertyName): void
|
||||
{
|
||||
$invoice = new Model\Venta\MediosPago\Toku\Invoice();
|
||||
$this->assertObjectHasProperty($propertyName, $invoice);
|
||||
}
|
||||
|
||||
public function testJson(): void
|
||||
{
|
||||
$faker = Faker\Factory::create();
|
||||
|
||||
$cuota = $this->getMockBuilder(Model\Venta\Cuota::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$cuota->id = $faker->randomNumber();
|
||||
|
||||
$toku_id = $faker->ean13();
|
||||
|
||||
$id = $faker->randomNumber();
|
||||
|
||||
$invoice = new Model\Venta\MediosPago\Toku\Invoice();
|
||||
$invoice->id = $id;
|
||||
$invoice->cuota = $cuota;
|
||||
$invoice->toku_id = $toku_id;
|
||||
|
||||
$expected = json_encode([
|
||||
'id' => $id,
|
||||
'cuota_id' => $cuota->id,
|
||||
'toku_id' => $toku_id,
|
||||
]);
|
||||
$this->assertEquals($expected, json_encode($invoice));
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Model\Venta\MediosPago\Toku;
|
||||
|
||||
use Faker;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Incoviba\Model;
|
||||
|
||||
class SubscriptionTest extends TestCase
|
||||
{
|
||||
public static function dataProperties(): array
|
||||
{
|
||||
return [
|
||||
['id'],
|
||||
['venta'],
|
||||
['toku_id'],
|
||||
];
|
||||
}
|
||||
#[DataProvider('dataProperties')]
|
||||
public function testProperties(string $propertyName): void
|
||||
{
|
||||
$subscription = new Model\Venta\MediosPago\Toku\Subscription();
|
||||
$this->assertObjectHasProperty($propertyName, $subscription);
|
||||
}
|
||||
|
||||
public function testJson(): void
|
||||
{
|
||||
$faker = Faker\Factory::create();
|
||||
|
||||
$venta = $this->getMockBuilder(Model\Venta::class)->disableOriginalConstructor()->getMock();
|
||||
$venta->id = $faker->randomNumber();
|
||||
|
||||
$toku_id = $faker->ean13();
|
||||
|
||||
$id = $faker->randomNumber();
|
||||
|
||||
$subscription = new Model\Venta\MediosPago\Toku\Subscription();
|
||||
$subscription->id = $id;
|
||||
$subscription->venta = $venta;
|
||||
$subscription->toku_id = $toku_id;
|
||||
|
||||
$expected = json_encode([
|
||||
'id' => $id,
|
||||
'venta_id' => $venta->id,
|
||||
'toku_id' => $toku_id,
|
||||
]);
|
||||
$this->assertEquals($expected, json_encode($subscription));
|
||||
}
|
||||
}
|
19
app/tests/unit/src/Model/Venta/PromotionTest.php
Normal file
19
app/tests/unit/src/Model/Venta/PromotionTest.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Model\Venta;
|
||||
|
||||
use Incoviba\Model\Venta\Promotion;
|
||||
use Tests\Extension\AbstractModel;
|
||||
use Tests\Extension\testMethodsTrait;
|
||||
use Tests\Extension\testPropertiesTrait;
|
||||
|
||||
class PromotionTest extends AbstractModel
|
||||
{
|
||||
use testPropertiesTrait, testMethodsTrait;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->model = new Promotion();
|
||||
$this->properties = ['description', 'amount', 'startDate', 'endDate', 'validUntil', 'type', 'state'];
|
||||
$this->methods = ['projects', 'brokers', 'unitTypes', 'unitLines', 'units', 'value'];
|
||||
}
|
||||
}
|
17
app/tests/unit/src/Model/Venta/Reservation/StateTest.php
Normal file
17
app/tests/unit/src/Model/Venta/Reservation/StateTest.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Model\Venta\Reservation;
|
||||
|
||||
use Incoviba\Model\Venta\Reservation\State;
|
||||
use Tests\Extension\AbstractModel;
|
||||
use Tests\Extension\testPropertiesTrait;
|
||||
|
||||
class StateTest extends AbstractModel
|
||||
{
|
||||
use testPropertiesTrait;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->model = new State();
|
||||
$this->properties = ['reservation', 'date', 'type'];
|
||||
}
|
||||
}
|
19
app/tests/unit/src/Model/Venta/ReservationTest.php
Normal file
19
app/tests/unit/src/Model/Venta/ReservationTest.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Model\Venta;
|
||||
|
||||
use Incoviba\Model\Venta\Reservation;
|
||||
use Tests\Extension\AbstractModel;
|
||||
use Tests\Extension\testMethodsTrait;
|
||||
use Tests\Extension\testPropertiesTrait;
|
||||
|
||||
class ReservationTest extends AbstractModel
|
||||
{
|
||||
use testPropertiesTrait, testMethodsTrait;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->model = new Reservation();
|
||||
$this->properties = ['buyer', 'date', 'units', 'promotions', 'broker'];
|
||||
$this->methods = ['states', 'currentState', 'addUnit', 'removeUnit', 'findUnit', 'hasUnit'];
|
||||
}
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Repository\Venta\MediosPago\Toku;
|
||||
|
||||
use Faker;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
use PDO;
|
||||
use PHPUnit\Framework\Attributes\Depends;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
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\Venta\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\Venta\MediosPago\Toku\Customer
|
||||
{
|
||||
$repository = new Repository\Venta\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\Venta\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\Venta\MediosPago\Toku\Customer($this->connection, $this->personaService);
|
||||
|
||||
$customer = $repository->fetchById($reference->id);
|
||||
$this->assertInstanceOf(Model\Venta\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\Venta\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\Venta\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);
|
||||
}
|
||||
#[Depends('testSave')]
|
||||
public function testFetchByTokuId(Model\Venta\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\Venta\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);
|
||||
}
|
||||
}
|
@ -0,0 +1,133 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Repository\Venta\MediosPago\Toku;
|
||||
|
||||
use Faker;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use PDO;
|
||||
use PHPUnit\Framework\Attributes\Depends;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
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\Venta\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\Venta\MediosPago\Toku\Invoice
|
||||
{
|
||||
$repository = new Repository\Venta\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\Venta\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\Venta\MediosPago\Toku\Invoice($this->connection, $this->cuotaRepository);
|
||||
|
||||
$invoice = $repository->fetchById($reference->id);
|
||||
$this->assertInstanceOf(Model\Venta\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\Venta\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\Venta\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);
|
||||
}
|
||||
#[Depends('testSave')]
|
||||
public function testFetchByTokuId(Model\Venta\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\Venta\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);
|
||||
}
|
||||
}
|
@ -0,0 +1,132 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Repository\Venta\MediosPago\Toku;
|
||||
|
||||
use Faker;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use PDO;
|
||||
use PHPUnit\Framework\Attributes\Depends;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
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\Venta\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\Venta\MediosPago\Toku\Subscription
|
||||
{
|
||||
$repository = new Repository\Venta\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\Venta\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\Venta\MediosPago\Toku\Subscription($this->connection, $this->ventaRepository);
|
||||
$subscription = $repository->fetchById($reference->id);
|
||||
$this->assertInstanceOf(Model\Venta\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\Venta\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\Venta\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);
|
||||
}
|
||||
#[Depends('testSave')]
|
||||
public function testFetchByTokuId(Model\Venta\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\Venta\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);
|
||||
}
|
||||
}
|
101
app/tests/unit/src/Service/MQTT/BeanstalkdTest.php
Normal file
101
app/tests/unit/src/Service/MQTT/BeanstalkdTest.php
Normal file
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
namespace Incoviba\Test\Service\MQTT;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use xobotyi\beansclient\BeansClient;
|
||||
use xobotyi\beansclient\Connection;
|
||||
use xobotyi\beansclient\Exception\JobException;
|
||||
use xobotyi\beansclient\Job;
|
||||
use Incoviba\Exception\MQTT\MissingJob;
|
||||
use Incoviba\Service\MQTT\Beanstalkd;
|
||||
|
||||
class BeanstalkdTest extends TestCase
|
||||
{
|
||||
protected LoggerInterface $logger;
|
||||
protected BeansClient $client;
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
|
||||
$this->client = $this->getMockBuilder(BeansClient::class)->disableOriginalConstructor()->getMock();
|
||||
}
|
||||
|
||||
public function testExists(): void
|
||||
{
|
||||
$stats = ['current-jobs-ready' => 1];
|
||||
$this->client->method('watchTube')->willReturn($this->client);
|
||||
$this->client->method('statsTube')->willReturn($stats);
|
||||
$service = new Beanstalkd($this->logger, $this->client);
|
||||
$this->assertTrue($service->exists());
|
||||
}
|
||||
public function testNotExists(): void
|
||||
{
|
||||
$stats = ['current-jobs-ready' => 0];
|
||||
$this->client->method('watchTube')->willReturn($this->client);
|
||||
$this->client->method('statsTube')->willReturn($stats);
|
||||
$service = new Beanstalkd($this->logger, $this->client);
|
||||
$this->assertFalse($service->exists());
|
||||
}
|
||||
public function testGet(): void
|
||||
{
|
||||
$jobData = [
|
||||
'id' => 1,
|
||||
'configuration' => [
|
||||
'type' => 'service',
|
||||
],
|
||||
'created_at' => '2020-01-01 00:00:00',
|
||||
];
|
||||
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
|
||||
$connection->method('isActive')->willReturn(true);
|
||||
$this->client->method('getConnection')->willReturn($connection);
|
||||
$this->client->method('watchTube')->willReturn($this->client);
|
||||
$this->client->method('statsTube')->willReturn(['current-jobs-ready' => 1]);
|
||||
$job = new Job($this->client, 1, 'ready', json_encode($jobData));
|
||||
$this->client->method('reserve')->willReturn($job);
|
||||
$service = new Beanstalkd($this->logger, $this->client);
|
||||
$this->assertEquals(json_encode($jobData), $service->get());
|
||||
}
|
||||
public function testGetException(): void
|
||||
{
|
||||
$this->client->method('watchTube')->willReturn($this->client);
|
||||
$this->client->method('statsTube')->willReturn(['current-jobs-ready' => 0]);
|
||||
$service = new Beanstalkd($this->logger, $this->client);
|
||||
$this->expectException(MissingJob::class);
|
||||
$service->get();
|
||||
|
||||
$this->client->method('statsTube')->willReturn(['current-jobs-ready' => 1]);
|
||||
$exception = new JobException();
|
||||
$this->client->method('reserve')->willThrowException($exception);
|
||||
$this->expectException(MissingJob::class);
|
||||
$service->get();
|
||||
}
|
||||
public function testSet(): void
|
||||
{
|
||||
$this->client->method('useTube')->willReturn($this->client);
|
||||
$this->client->method('put');
|
||||
$service = new Beanstalkd($this->logger, $this->client);
|
||||
$service->set('test');
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
public function testSetException(): void
|
||||
{
|
||||
$this->client->method('useTube')->willReturn($this->client);
|
||||
$exception = new JobException();
|
||||
$this->client->method('put')->willThrowException($exception);
|
||||
$service = new Beanstalkd($this->logger, $this->client);
|
||||
$service->set('test');
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
public function testRemove(): void
|
||||
{
|
||||
$this->client->method('useTube')->willReturn($this->client);
|
||||
$this->client->method('delete')->willReturn(true);
|
||||
$service = new Beanstalkd($this->logger, $this->client);
|
||||
$service->remove(1);
|
||||
$this->assertTrue(true);
|
||||
|
||||
$this->client->method('delete')->willReturn(false);
|
||||
$service->remove(1);
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
69
app/tests/unit/src/Service/MQTTTest.php
Normal file
69
app/tests/unit/src/Service/MQTTTest.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
namespace Incoviba\Test\Service;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use xobotyi\beansclient\BeansClient;
|
||||
use Incoviba\Exception\MQTT\MissingClient;
|
||||
use Incoviba\Service\MQTT;
|
||||
use Incoviba\Service\MQTT\Beanstalkd;
|
||||
|
||||
class MQTTTest extends TestCase
|
||||
{
|
||||
public function testRegisterAndClientExistsAndGet(): void
|
||||
{
|
||||
$mqtt = new MQTT();
|
||||
$beanstalkd = $this->getMockBuilder(Beanstalkd::class)->disableOriginalConstructor()->getMock();
|
||||
$mqtt->register('beanstalkd', $beanstalkd);
|
||||
$this->assertTrue($mqtt->clientExists('beanstalkd'));
|
||||
}
|
||||
public function testGetClient(): void
|
||||
{
|
||||
$mqtt = new MQTT();
|
||||
$logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
|
||||
$client = $this->getMockBuilder(BeansClient::class)->disableOriginalConstructor()->getMock();
|
||||
$beanstalkd = new Beanstalkd($logger, $client);
|
||||
$mqtt->register('beanstalkd', $beanstalkd);
|
||||
$this->assertEquals($beanstalkd, $mqtt->getClient('beanstalkd'));
|
||||
}
|
||||
public function testGetClientException(): void
|
||||
{
|
||||
$mqtt = new MQTT();
|
||||
$this->expectException(MissingClient::class);
|
||||
$mqtt->getClient('test');
|
||||
}
|
||||
public function testExists(): void
|
||||
{
|
||||
$mqtt = new MQTT();
|
||||
$beanstalkd = $this->getMockBuilder(Beanstalkd::class)->disableOriginalConstructor()->getMock();
|
||||
$beanstalkd->method('exists')->willReturn(true);
|
||||
$mqtt->register('beanstalkd', $beanstalkd);
|
||||
$this->assertTrue($mqtt->exists('beanstalkd'));
|
||||
}
|
||||
public function testGet(): void
|
||||
{
|
||||
$mqtt = new MQTT();
|
||||
$beanstalkd = $this->getMockBuilder(Beanstalkd::class)->disableOriginalConstructor()->getMock();
|
||||
$beanstalkd->method('get')->willReturn('test');
|
||||
$mqtt->register('beanstalkd', $beanstalkd);
|
||||
$this->assertEquals('test', $mqtt->get('beanstalkd'));
|
||||
}
|
||||
public function testSet(): void
|
||||
{
|
||||
$mqtt = new MQTT();
|
||||
$beanstalkd = $this->getMockBuilder(Beanstalkd::class)->disableOriginalConstructor()->getMock();
|
||||
$beanstalkd->method('set');
|
||||
$mqtt->register('beanstalkd', $beanstalkd);
|
||||
$mqtt->set('test', 0, 'beanstalkd');
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
public function testRemove(): void
|
||||
{
|
||||
$mqtt = new MQTT();
|
||||
$beanstalkd = $this->getMockBuilder(Beanstalkd::class)->disableOriginalConstructor()->getMock();
|
||||
$beanstalkd->method('remove');
|
||||
$mqtt->register('beanstalkd', $beanstalkd);
|
||||
$mqtt->remove(0, 'beanstalkd');
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
66
app/tests/unit/src/Service/Money/SIITest.php
Normal file
66
app/tests/unit/src/Service/Money/SIITest.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Service\Money;
|
||||
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use GuzzleHttp\Client;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
||||
use Incoviba\Service;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
class SIITest extends TestCase
|
||||
{
|
||||
protected Client $client;
|
||||
protected Service\UF $ufService;
|
||||
protected Service\Valor $valorService;
|
||||
protected \PDO $pdo;
|
||||
protected Define\Connection $connection;
|
||||
protected \PDOStatement $statement;
|
||||
protected Repository\UF $ufRepository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->client = new Client(['base_uri' => 'https://www.sii.cl/valores_y_fechas/']);
|
||||
|
||||
$this->pdo = $this->getMockBuilder(PDO::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$this->pdo->method('beginTransaction')->willReturn(true);
|
||||
$this->pdo->method('commit')->willReturn(true);
|
||||
#$this->pdo->method('rollBack')->willReturn(null);
|
||||
|
||||
$this->statement = $this->getMockBuilder(PDOStatement::class)->getMock();
|
||||
$this->statement->method('fetchAll')->willReturn([]);
|
||||
|
||||
$this->connection = $this->getMockBuilder(Define\Connection::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$this->connection->method('getPDO')->willReturn($this->pdo);
|
||||
$this->connection->method('query')->willReturn($this->statement);
|
||||
$this->connection->method('execute')->willReturn($this->statement);
|
||||
|
||||
$this->ufRepository = $this->getMockBuilder(Repository\UF::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$this->ufRepository->method('getConnection')->willReturn($this->connection);
|
||||
$this->ufRepository->method('getTable')->willReturn('uf');
|
||||
}
|
||||
|
||||
public function testGet(): void
|
||||
{
|
||||
$provider = new Service\Money\SII($this->client, $this->ufRepository);
|
||||
|
||||
$date = new \DateTimeImmutable('2025-05-05');
|
||||
$expected = 39107.9;
|
||||
|
||||
$this->assertEquals($expected, $provider->get(Service\Money::UF, $date));
|
||||
}
|
||||
public function testGetNoValid(): void
|
||||
{
|
||||
$provider = new Service\Money\SII($this->client, $this->ufRepository);
|
||||
|
||||
$date = (new \DateTimeImmutable())->add(new \DateInterval("P1Y"));
|
||||
|
||||
$this->expectException(EmptyResponse::class);
|
||||
$provider->get(Service\Money::UF, $date);
|
||||
}
|
||||
}
|
66
app/tests/unit/src/Service/QueueTest.php
Normal file
66
app/tests/unit/src/Service/QueueTest.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
namespace Incoviba\Test\Service;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Incoviba\Service\Job;
|
||||
use Incoviba\Service\Queue;
|
||||
use Incoviba\Service\Worker;
|
||||
use Incoviba\Model;
|
||||
|
||||
class QueueTest extends TestCase
|
||||
{
|
||||
protected LoggerInterface $logger;
|
||||
protected Job $jobService;
|
||||
protected Worker $defaultWorker;
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->logger = $this->getMockBuilder(LoggerInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->jobService = $this->getMockBuilder(Job::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->defaultWorker = $this->getMockBuilder(Worker::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
}
|
||||
|
||||
public function testRegister(): void
|
||||
{
|
||||
$queue = new Queue($this->logger, $this->jobService, $this->defaultWorker);
|
||||
$worker = $this->getMockBuilder(Worker::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$queue->register('test', $worker);
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
public function testEnqueue(): void
|
||||
{
|
||||
$queue = new Queue($this->logger, $this->jobService, $this->defaultWorker);
|
||||
$jobData = ['test' => 'test'];
|
||||
$result = $queue->enqueue($jobData);
|
||||
$this->assertTrue($result);
|
||||
|
||||
$result = $queue->push($jobData);
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
public function testRun(): void
|
||||
{
|
||||
$queue = new Queue($this->logger, $this->jobService, $this->defaultWorker);
|
||||
$result = $queue->run();
|
||||
$this->assertTrue($result);
|
||||
|
||||
|
||||
$jobData = [
|
||||
'type' => 'test',
|
||||
];
|
||||
$job = new Model\Job();
|
||||
$job->id = 1;
|
||||
$job->configuration = $jobData;
|
||||
$this->jobService->method('isPending')->willReturn(true);
|
||||
$this->jobService->method('get')->willReturn($job);
|
||||
$result = $queue->run();
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
}
|
66
app/tests/unit/src/Service/UFTest.php
Normal file
66
app/tests/unit/src/Service/UFTest.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
namespace Incoviba\Test\Service;
|
||||
|
||||
use DateInterval;
|
||||
use DateTimeImmutable;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Faker;
|
||||
use Incoviba\Common\Implement\Exception\EmptyRedis;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Service;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class UFTest extends TestCase
|
||||
{
|
||||
protected array $dateMap;
|
||||
protected array $ufMap;
|
||||
protected Service\Redis $redisService;
|
||||
protected Service\Money $moneyService;
|
||||
protected Repository\UF $ufRepository;
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$faker = Faker\Factory::create();
|
||||
$today = new DateTimeImmutable($faker->dateTime->format('Y-m-1'));
|
||||
|
||||
$this->dateMap = [
|
||||
$today->sub(new DateInterval("P1M")), // Case: Date past
|
||||
$today, // Case: Date === "Today" (1st of month)
|
||||
new DateTimeImmutable($today->format('Y-m-8')), // Case: Date 8th of month (before 9th)
|
||||
new DateTimeImmutable($today->format('Y-m-15')), // Case: Date 15th of month (after 9th)
|
||||
$today->add(new DateInterval("P1M")), // Case: Date one month from now
|
||||
];
|
||||
$this->ufMap = [
|
||||
$faker->randomFloat(2, 10000, 20000), // 1st value
|
||||
$faker->randomFloat(2, 25000, 38000), // 2nd value
|
||||
$faker->randomFloat(2, 38000, 39000), // 3rd value (before 9th)
|
||||
0.0, // no value (after 9th)
|
||||
0.0 // no value
|
||||
];
|
||||
|
||||
$this->redisService = $this->getMockBuilder(Service\Redis::class)->disableOriginalConstructor()->getMock();
|
||||
$emptyRedis = $this->getMockBuilder(EmptyRedis::class)->disableOriginalConstructor()->getMock();
|
||||
$this->redisService->method('get')->willThrowException($emptyRedis);
|
||||
$this->moneyService = $this->getMockBuilder(Service\Money::class)->disableOriginalConstructor()->getMock();
|
||||
$this->moneyService->method('getUF')->willReturnCallback(function($date) {
|
||||
return $this->ufMap[array_search($date, $this->dateMap)];
|
||||
});
|
||||
$this->ufRepository = $this->getMockBuilder(Repository\UF::class)->disableOriginalConstructor()->getMock();
|
||||
$emptyResult = $this->getMockBuilder(EmptyResult::class)->disableOriginalConstructor()->getMock();
|
||||
$this->ufRepository->method('fetchByFecha')->willThrowException($emptyResult);
|
||||
$this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
$service = new Service\UF($this->redisService, $this->moneyService, $this->ufRepository, $this->logger);
|
||||
|
||||
foreach ($this->dateMap as $i => $date) {
|
||||
$uf = $service->get($date);
|
||||
|
||||
$this->assertEquals($this->ufMap[$i], $uf);
|
||||
}
|
||||
}
|
||||
}
|
81
app/tests/unit/src/Service/ValorTest.php
Normal file
81
app/tests/unit/src/Service/ValorTest.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Service;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Incoviba\Service\Valor;
|
||||
use Incoviba\Service;
|
||||
|
||||
class ValorTest extends TestCase
|
||||
{
|
||||
protected Service\UF $ufService;
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->ufService = $this->getMockBuilder(Service\UF::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->ufService->method('get')->willReturn(35000.0);
|
||||
}
|
||||
|
||||
public static function cleanDataProvider(): array
|
||||
{
|
||||
return [
|
||||
['1.003.000,01', 1003000.01],
|
||||
['4,273.84', 4273.84],
|
||||
[443.75, 443.75],
|
||||
['443.75', 443.75]
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('cleanDataProvider')]
|
||||
public function testClean($input, $expected): void
|
||||
{
|
||||
$valorService = new Valor($this->ufService);
|
||||
|
||||
$result = $valorService->clean($input);
|
||||
|
||||
$this->assertIsFloat($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public static function ufsDataProvider(): array
|
||||
{
|
||||
return [
|
||||
['1000000', 28.57143],
|
||||
[443.75, 443.75]
|
||||
];
|
||||
}
|
||||
|
||||
public function testToUF()
|
||||
{
|
||||
$date = '2025-01-01';
|
||||
$valorService = new Valor($this->ufService);
|
||||
|
||||
$input = '1000000';
|
||||
$result = $valorService->toUF($input, $date);
|
||||
|
||||
$this->assertIsFloat($result);
|
||||
$this->assertEquals($input / 35000, $result);
|
||||
}
|
||||
|
||||
public static function pesosDataProvider(): array
|
||||
{
|
||||
return [
|
||||
[1000.01, 1000.01*35000, false],
|
||||
[1000, 1000*35000, true],
|
||||
['1000', 1000, false],
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('pesosDataProvider')]
|
||||
public function testToPesos($input, $expected, $force)
|
||||
{
|
||||
$date = '2025-01-01';
|
||||
$valorService = new Valor($this->ufService);
|
||||
|
||||
$result = $valorService->toPesos($input, $date, $force);
|
||||
|
||||
$this->assertIsInt($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
@ -0,0 +1,198 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Service\Venta\MediosPago\Toku;
|
||||
|
||||
use Faker;
|
||||
use GuzzleHttp\Client;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
use PDO;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
|
||||
class CustomerTest extends TestCase
|
||||
{
|
||||
protected Client $client;
|
||||
protected Model\Venta\MediosPago\Toku\Customer $customer;
|
||||
protected Repository\Venta\MediosPago\Toku\Customer $customerRepository;
|
||||
protected array $getData;
|
||||
protected array $postData;
|
||||
|
||||
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();
|
||||
|
||||
$datos = $this->getMockBuilder(Model\Persona\Datos::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$datos->email = $faker->email();
|
||||
$datos->telefono = $faker->randomNumber(8);
|
||||
|
||||
$persona = $this->getMockBuilder(Model\Persona::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$persona->rut = $faker->randomNumber();
|
||||
$persona->digito = $faker->randomNumber();
|
||||
$persona->nombres = $faker->firstName();
|
||||
$persona->apellidoPaterno = $faker->lastName();
|
||||
$persona->apellidoMaterno = $faker->lastName();
|
||||
$persona->method('datos')->willReturn($datos);
|
||||
$persona->method('nombreCompleto')->willReturn(implode(' ', [
|
||||
$persona->nombres,
|
||||
$persona->apellidoPaterno,
|
||||
$persona->apellidoMaterno
|
||||
]));
|
||||
|
||||
$this->customer = $this->getMockBuilder(Model\Venta\MediosPago\Toku\Customer::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$this->customer->id = $faker->randomNumber();
|
||||
$this->customer->persona = $persona;
|
||||
$this->customer->toku_id = $faker->ean13();
|
||||
$this->customer->method('rut')->willReturn(implode('', [
|
||||
$persona->rut,
|
||||
strtoupper($persona->digito)
|
||||
]));
|
||||
$this->customer->method('jsonSerialize')->willReturn([
|
||||
'id' => $this->customer->id,
|
||||
'rut' => $this->customer->rut(),
|
||||
'toku_id' => $this->customer->toku_id
|
||||
]);
|
||||
|
||||
$this->customerRepository = $this->getMockBuilder(Repository\Venta\MediosPago\Toku\Customer::class)
|
||||
->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,
|
||||
'external_id' => $this->customer->rut(),
|
||||
'government_id' => $this->customer->rut(),
|
||||
'email' => $persona->datos()->email,
|
||||
'name' => $persona->nombreCompleto(),
|
||||
'phone_number' => $persona->datos()->telefono,
|
||||
'silenced_until' => null,
|
||||
'default_agent' => null,
|
||||
'agent_phone_number' => null,
|
||||
'pac_mandate_id' => null,
|
||||
'send_mail' => false,
|
||||
'default_receipt_type' => 'bill',
|
||||
'rfc' => null,
|
||||
'tax_zip_code' => null,
|
||||
'fiscal_regime' => null,
|
||||
'secondary_emails' => [],
|
||||
'metadata' => []
|
||||
];
|
||||
|
||||
$getBody = $this->getMockBuilder(StreamInterface::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$getBody->method('getContents')->willReturn(json_encode($this->getData));
|
||||
|
||||
$getResponse = $this->getMockBuilder(ResponseInterface::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$getResponse->method('getStatusCode')->willReturn(200);
|
||||
$getResponse->method('getBody')->willReturn($getBody);
|
||||
|
||||
$this->postData = [
|
||||
'id' => $this->customer->id,
|
||||
'external_id' => $this->customer->rut(),
|
||||
'government_id' => $this->customer->rut(),
|
||||
'email' => $persona->datos()->email,
|
||||
'name' => $persona->nombreCompleto(),
|
||||
'phone_number' => $persona->datos()->telefono,
|
||||
'silenced_until' => null,
|
||||
'default_agent' => null,
|
||||
'agent_phone_number' => null,
|
||||
'pac_mandate_id' => null,
|
||||
'send_mail' => false,
|
||||
'default_receipt_type' => null,
|
||||
'rfc' => null,
|
||||
'tax_zip_code' => null,
|
||||
'fiscal_regime' => null,
|
||||
'secondary_emails' => [],
|
||||
'metadata' => []
|
||||
];
|
||||
|
||||
$postBody = $this->getMockBuilder(StreamInterface::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$postBody->method('getContents')->willReturn(json_encode($this->postData));
|
||||
|
||||
$postResponse = $this->getMockBuilder(ResponseInterface::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$postResponse->method('getStatusCode')->willReturn(200);
|
||||
$postResponse->method('getBody')->willReturn($postBody);
|
||||
|
||||
$deleteBody = $this->getMockBuilder(StreamInterface::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$deleteBody->method('getContents')->willReturn('Customer Deleted');
|
||||
|
||||
$deleteResponse = $this->getMockBuilder(ResponseInterface::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$deleteResponse->method('getStatusCode')->willReturn(204);
|
||||
$deleteResponse->method('getBody')->willReturn($deleteBody);
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
public function testGetById(): void
|
||||
{
|
||||
$service = new Service\Venta\MediosPago\Toku\Customer($this->client, $this->customerRepository);
|
||||
|
||||
$this->assertEquals(json_decode(json_encode($this->customer), true), $service->getById($this->customer->id));
|
||||
}
|
||||
public function testGetByExternalId(): void
|
||||
{
|
||||
$service = new Service\Venta\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\Venta\MediosPago\Toku\Customer($this->client, $this->customerRepository);
|
||||
|
||||
$this->assertArrayIsEqualToArrayIgnoringListOfKeys($this->getData, $service->get($this->customer->toku_id), []);
|
||||
}
|
||||
public function testAdd(): void
|
||||
{
|
||||
$service = new Service\Venta\MediosPago\Toku\Customer($this->client, $this->customerRepository);
|
||||
|
||||
$sendData = [
|
||||
'rut' => $this->postData['government_id'],
|
||||
'nombreCompleto' => $this->postData['name'],
|
||||
'email' => $this->postData['email'],
|
||||
'telefono' => $this->postData['phone_number']
|
||||
];
|
||||
$this->assertTrue($service->add($sendData));
|
||||
}
|
||||
public function testEdit(): void
|
||||
{
|
||||
$service = new Service\Venta\MediosPago\Toku\Customer($this->client, $this->customerRepository);
|
||||
|
||||
$sendData = [
|
||||
'rut' => $this->postData['government_id'],
|
||||
'nombreCompleto' => $this->postData['name'],
|
||||
'email' => $this->postData['email'],
|
||||
'telefono' => $this->postData['phone_number']
|
||||
];
|
||||
$this->assertTrue($service->edit($this->customer->toku_id, $sendData));
|
||||
}
|
||||
public function testDelete(): void
|
||||
{
|
||||
$service = new Service\Venta\MediosPago\Toku\Customer($this->client, $this->customerRepository);
|
||||
|
||||
$this->expectNotToPerformAssertions();
|
||||
$service->delete($this->customer->toku_id);
|
||||
}
|
||||
}
|
265
app/tests/unit/src/Service/Venta/MediosPago/Toku/InvoiceTest.php
Normal file
265
app/tests/unit/src/Service/Venta/MediosPago/Toku/InvoiceTest.php
Normal file
@ -0,0 +1,265 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Service\Venta\MediosPago\Toku;
|
||||
|
||||
use Faker;
|
||||
use GuzzleHttp\Client;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
use PDO;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
|
||||
class InvoiceTest extends TestCase
|
||||
{
|
||||
protected Client $client;
|
||||
protected Model\Venta\MediosPago\Toku\Invoice $invoice;
|
||||
protected Repository\Venta\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\Venta\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\Venta\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\Venta\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\Venta\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\Venta\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\Venta\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\Venta\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\Venta\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\Venta\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\Venta\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\Venta\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository, $this->pagoService, $this->ufService);
|
||||
|
||||
$this->assertTrue($service->update($request['payment_intent']['invoice'], $request['payment_intent']));
|
||||
}
|
||||
}
|
@ -0,0 +1,190 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Service\Venta\MediosPago\Toku;
|
||||
|
||||
use Faker;
|
||||
use GuzzleHttp\Client;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
use PDO;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
|
||||
class SubscriptionTest extends TestCase
|
||||
{
|
||||
protected Client $client;
|
||||
protected Model\Venta\MediosPago\Toku\Subscription $subscription;
|
||||
protected Repository\Venta\MediosPago\Toku\Subscription $subscriptionRepository;
|
||||
protected Model\Venta $venta;
|
||||
protected array $getData;
|
||||
protected array $postData;
|
||||
|
||||
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\Venta\MediosPago\Toku\Customer::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$customer->id = $faker->randomNumber();
|
||||
|
||||
$pie = $this->getMockBuilder(Model\Venta\Pie::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$pie->valor = $faker->randomFloat();
|
||||
$formaPago = $this->getMockBuilder(Model\Venta\FormaPago::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$formaPago->pie = $pie;
|
||||
$proyecto = $this->getMockBuilder(Model\Proyecto::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$proyecto->descripcion = $faker->sentence();
|
||||
$summary = implode(' - ', [
|
||||
$faker->randomNumber(4),
|
||||
'E' . $faker->randomNumber(3),
|
||||
'B' . $faker->randomNumber(3),
|
||||
]);
|
||||
$propiedad = $this->getMockBuilder(Model\Venta\Propiedad::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$propiedad->method('summary')->willReturn($summary);
|
||||
$this->venta = $this->getMockBuilder(Model\Venta::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$this->venta->id = $faker->randomNumber();
|
||||
$this->venta->method('formaPago')->willReturn($formaPago);
|
||||
$this->venta->method('proyecto')->willReturn($proyecto);
|
||||
$this->venta->method('propiedad')->willReturn($propiedad);
|
||||
|
||||
$this->subscription = $this->getMockBuilder(Model\Venta\MediosPago\Toku\Subscription::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$this->subscription->id = $faker->randomNumber();
|
||||
$this->subscription->venta = $this->venta;
|
||||
$this->subscription->toku_id = $faker->ean13();
|
||||
$this->subscription->method('jsonSerialize')->willReturn([
|
||||
'id' => $this->subscription->id,
|
||||
'venta_id' => $this->venta->id,
|
||||
'toku_id' => $this->subscription->toku_id,
|
||||
]);
|
||||
|
||||
$this->subscriptionRepository = $this->getMockBuilder(Repository\Venta\MediosPago\Toku\Subscription::class)
|
||||
->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,
|
||||
'customer' => $customer->id,
|
||||
'product_id' => $this->venta->id,
|
||||
'pac_mandate_id' => null,
|
||||
'is_recurring' => false,
|
||||
'amount' => $pie->valor,
|
||||
'due_day' => null,
|
||||
'receipt_product_code' => null,
|
||||
'metadata' => [
|
||||
'proyecto' => $proyecto->descripcion,
|
||||
'propiedad' => $propiedad->summary(),
|
||||
]
|
||||
];
|
||||
|
||||
$getBody = $this->getMockBuilder(StreamInterface::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$getBody->method('getContents')->willReturn(json_encode($this->getData));
|
||||
|
||||
$getResponse = $this->getMockBuilder(ResponseInterface::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$getResponse->method('getStatusCode')->willReturn(200);
|
||||
$getResponse->method('getBody')->willReturn($getBody);
|
||||
|
||||
$this->postData = [
|
||||
'id' => $this->subscription->toku_id,
|
||||
'customer' => $customer->id,
|
||||
'product_id' => $this->venta->id,
|
||||
'pac_mandate_id' => null,
|
||||
'is_recurring' => false,
|
||||
'amount' => $pie->valor,
|
||||
'due_day' => null,
|
||||
'metadata' => [
|
||||
'proyecto' => $proyecto->descripcion,
|
||||
'propiedad' => $propiedad->summary(),
|
||||
]
|
||||
];
|
||||
|
||||
$postBody = $this->getMockBuilder(StreamInterface::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$postBody->method('getContents')->willReturn(json_encode($this->postData));
|
||||
|
||||
$postResponse = $this->getMockBuilder(ResponseInterface::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$postResponse->method('getStatusCode')->willReturn(200);
|
||||
$postResponse->method('getBody')->willReturn($postBody);
|
||||
|
||||
$deleteBody = $this->getMockBuilder(StreamInterface::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$deleteBody->method('getContents')->willReturn('Subscription deleted');
|
||||
$deleteResponse = $this->getMockBuilder(ResponseInterface::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$deleteResponse->method('getStatusCode')->willReturn(204);
|
||||
$deleteResponse->method('getBody')->willReturn($deleteBody);
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
public function testGetById(): void
|
||||
{
|
||||
$service = new Service\Venta\MediosPago\Toku\Subscription($this->client, $this->subscriptionRepository);
|
||||
|
||||
$this->assertEquals(json_decode(json_encode($this->subscription), true), $service->getById($this->subscription->toku_id));
|
||||
}
|
||||
public function testGetByExternalId(): void
|
||||
{
|
||||
$service = new Service\Venta\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\Venta\MediosPago\Toku\Subscription($this->client, $this->subscriptionRepository);
|
||||
|
||||
$this->assertArrayIsEqualToArrayIgnoringListOfKeys($this->getData, $service->get($this->subscription->toku_id), []);
|
||||
}
|
||||
public function testAdd(): void
|
||||
{
|
||||
$service = new Service\Venta\MediosPago\Toku\Subscription($this->client, $this->subscriptionRepository);
|
||||
|
||||
$sendData = [
|
||||
'customer' => $this->postData['customer'],
|
||||
'product_id' => $this->postData['product_id'],
|
||||
'venta' => $this->venta
|
||||
];
|
||||
$this->assertTrue($service->add($sendData));
|
||||
}
|
||||
public function testEdit(): void
|
||||
{
|
||||
$service = new Service\Venta\MediosPago\Toku\Subscription($this->client, $this->subscriptionRepository);
|
||||
|
||||
$sendData = [
|
||||
'customer' => $this->postData['customer'],
|
||||
'product_id' => $this->postData['product_id'],
|
||||
'venta' => $this->venta
|
||||
];
|
||||
$this->assertTrue($service->edit($this->subscription->toku_id, $sendData));
|
||||
}
|
||||
public function testDelete(): void
|
||||
{
|
||||
$service = new Service\Venta\MediosPago\Toku\Subscription($this->client, $this->subscriptionRepository);
|
||||
|
||||
$this->expectNotToPerformAssertions();
|
||||
$service->delete($this->subscription->toku_id);
|
||||
}
|
||||
}
|
339
app/tests/unit/src/Service/Venta/MediosPago/TokuTest.php
Normal file
339
app/tests/unit/src/Service/Venta/MediosPago/TokuTest.php
Normal file
@ -0,0 +1,339 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Service\Venta\MediosPago;
|
||||
|
||||
use Faker;
|
||||
use Incoviba\Exception\InvalidResult;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Service;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class TokuTest extends TestCase
|
||||
{
|
||||
protected LoggerInterface $logger;
|
||||
protected Service\Venta\MediosPago\Toku\Customer $customerService;
|
||||
protected Service\Venta\MediosPago\Toku\Subscription $subscriptionService;
|
||||
protected Service\Venta\MediosPago\Toku\Invoice $invoiceService;
|
||||
protected Model\Venta\MediosPago\Toku\Customer $customer;
|
||||
protected Model\Venta\MediosPago\Toku\Subscription $subscription;
|
||||
protected Model\Venta\MediosPago\Toku\Invoice $invoice;
|
||||
protected Model\Persona $persona;
|
||||
protected Model\Venta $venta;
|
||||
protected Model\Venta\Cuota $cuota;
|
||||
protected Model\Venta\Pago $pago;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$faker = Faker\Factory::create();
|
||||
|
||||
$this->logger = $this->getMockBuilder(LoggerInterface::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
|
||||
$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();
|
||||
$this->persona->digito = 'K';
|
||||
$this->persona->nombres = $faker->firstName();
|
||||
$this->persona->apellidoPaterno = $faker->lastName();
|
||||
$this->persona->apellidoMaterno = $faker->lastName();
|
||||
$this->persona->method('datos')->willReturn($datos);
|
||||
$this->persona->method('__get')->with('dv')->willReturn($this->persona->digito);
|
||||
$this->customer = $this->getMockBuilder(Model\Venta\MediosPago\Toku\Customer::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$this->customer->id = $faker->randomNumber();
|
||||
$this->customer->persona = $this->persona;
|
||||
$this->customer->toku_id = $faker->ean13();
|
||||
$this->customer->method('rut')->willReturn(implode('', [
|
||||
$this->persona->rut,
|
||||
strtoupper($this->persona->digito)
|
||||
]));
|
||||
|
||||
$pie = $this->getMockBuilder(Model\Venta\Pie::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$pie->valor = $faker->randomFloat();
|
||||
$formaPago = $this->getMockBuilder(Model\Venta\FormaPago::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$formaPago->pie = $pie;
|
||||
$proyecto = $this->getMockBuilder(Model\Proyecto::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$proyecto->descripcion = $faker->sentence();
|
||||
$summary = implode(' - ', [
|
||||
$faker->randomNumber(4),
|
||||
'E' . $faker->randomNumber(3),
|
||||
'B' . $faker->randomNumber(3),
|
||||
]);
|
||||
$propiedad = $this->getMockBuilder(Model\Venta\Propiedad::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$propiedad->method('summary')->willReturn($summary);
|
||||
$datos2 = $this->getMockBuilder(Model\Venta\Datos::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$datos2->email = $datos->email;
|
||||
$datos2->telefono = $datos->telefono;
|
||||
$propietario = $this->getMockBuilder(Model\Venta\Propietario::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$propietario->rut = $this->persona->rut;
|
||||
$propietario->dv = $this->persona->dv;
|
||||
$propietario->nombres = $this->persona->nombres;
|
||||
$propietario->apellidos = ['paterno' => $this->persona->apellidoPaterno, 'materno' => $this->persona->apellidoMaterno];
|
||||
$propietario->datos = $datos2;
|
||||
$this->venta = $this->getMockBuilder(Model\Venta::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$this->venta->id = $faker->randomNumber();
|
||||
$this->venta->method('formaPago')->willReturn($formaPago);
|
||||
$this->venta->method('proyecto')->willReturn($proyecto);
|
||||
$this->venta->method('propiedad')->willReturn($propiedad);
|
||||
$this->venta->method('propietario')->willReturn($propietario);
|
||||
$this->subscription = $this->getMockBuilder(Model\Venta\MediosPago\Toku\Subscription::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$this->subscription->id = $faker->randomNumber();
|
||||
$this->subscription->venta = $this->venta;
|
||||
$this->subscription->toku_id = $faker->ean13();
|
||||
|
||||
$this->pago = $this->getMockBuilder(Model\Venta\Pago::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$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 = $this->pago;
|
||||
$pie->method('cuotas')->willReturn([$this->cuota]);
|
||||
$this->invoice = $this->getMockBuilder(Model\Venta\MediosPago\Toku\Invoice::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$this->invoice->id = $faker->randomNumber();
|
||||
$this->invoice->cuota = $this->cuota;
|
||||
$this->invoice->toku_id = $faker->ean13();
|
||||
|
||||
$customerArray = [
|
||||
'id' => $this->customer->id,
|
||||
'rut' => $this->customer->rut(),
|
||||
'toku_id' => $this->customer->toku_id
|
||||
];
|
||||
$this->customerService = $this->getMockBuilder(Service\Venta\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(),
|
||||
'external_id' => $this->customer->rut(),
|
||||
'name' => implode(' ', [$this->persona->nombres, $this->persona->apellidoPaterno, $this->persona->apellidoMaterno]),
|
||||
'mail' => $this->persona->datos()->email,
|
||||
'phone_number' => $this->persona->datos()->telefono,
|
||||
'silenced_until' => null,
|
||||
'default_agent' => 'contacto@incoviba.cl',
|
||||
'agent_phone_number' => null,
|
||||
'pac_mandate_id' => null,
|
||||
'send_mail' => false,
|
||||
'default_receipt_type' => null,
|
||||
'rfc' => null,
|
||||
'tax_zip_code' => null,
|
||||
'fiscal_regime' => null,
|
||||
'secondary_emails' => [],
|
||||
'metadata' => []
|
||||
]);
|
||||
$this->customerService->method('add')->willReturn(true);
|
||||
$this->customerService->method('edit')->willReturn(true);
|
||||
$this->customerService->method('delete');
|
||||
|
||||
$this->subscriptionService = $this->getMockBuilder(Service\Venta\MediosPago\Toku\Subscription::class)
|
||||
->disableOriginalConstructor()->getMock();
|
||||
$this->subscriptionService->method('getById')->willReturn([
|
||||
'id' => $this->subscription->id,
|
||||
'venta_id' => $this->venta->id,
|
||||
'toku_id' => $this->subscription->toku_id,
|
||||
]);
|
||||
$this->subscriptionService->method('get')->willReturn([
|
||||
'id' => $this->subscription->toku_id,
|
||||
'customer' => $this->customer->toku_id,
|
||||
'product_id' => $this->venta->id,
|
||||
'pac_mandate_id' => null,
|
||||
'is_recurring' => false,
|
||||
'amount' => $pie->valor,
|
||||
'due_day' => null,
|
||||
'receipt_product_code' => null,
|
||||
'metadata' => [
|
||||
'proyecto' => $proyecto->descripcion,
|
||||
'propiedad' => $propiedad->summary(),
|
||||
]
|
||||
]);
|
||||
$this->subscriptionService->method('add')->willReturn(true);
|
||||
$this->subscriptionService->method('edit')->willReturn(true);
|
||||
$this->subscriptionService->method('delete');
|
||||
|
||||
$invoiceArray = [
|
||||
'id' => $this->invoice->id,
|
||||
'cuota_id' => $this->cuota->id,
|
||||
'toku_id' => $this->invoice->toku_id,
|
||||
];
|
||||
$this->invoiceService = $this->getMockBuilder(Service\Venta\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' => $this->pago->fecha->format('Y-m-d'),
|
||||
'is_void' => false,
|
||||
'amount' => $this->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,
|
||||
]);
|
||||
$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
|
||||
{
|
||||
$service = new Service\Venta\MediosPago\Toku($this->logger);
|
||||
$service->register(Service\Venta\MediosPago\Toku::CUSTOMER, $this->customerService);
|
||||
|
||||
$expected = [
|
||||
'id' => $this->customer->id,
|
||||
'rut' => $this->customer->rut(),
|
||||
'toku_id' => $this->customer->toku_id
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $service->sendPersona($this->persona));
|
||||
}
|
||||
public function testSendSubscription(): void
|
||||
{
|
||||
$service = new Service\Venta\MediosPago\Toku($this->logger);
|
||||
$service->register(Service\Venta\MediosPago\Toku::CUSTOMER, $this->customerService);
|
||||
$service->register(Service\Venta\MediosPago\Toku::SUBSCRIPTION, $this->subscriptionService);
|
||||
|
||||
$expected = [
|
||||
'id' => $this->subscription->id,
|
||||
'venta_id' => $this->venta->id,
|
||||
'toku_id' => $this->subscription->toku_id,
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $service->sendVenta($this->venta));
|
||||
}
|
||||
public function testSendInvoice(): void
|
||||
{
|
||||
$service = new Service\Venta\MediosPago\Toku($this->logger);
|
||||
$service->register(Service\Venta\MediosPago\Toku::CUSTOMER, $this->customerService);
|
||||
$service->register(Service\Venta\MediosPago\Toku::SUBSCRIPTION, $this->subscriptionService);
|
||||
$service->register(Service\Venta\MediosPago\Toku::INVOICE, $this->invoiceService);
|
||||
|
||||
$expected = [
|
||||
[
|
||||
'id' => $this->invoice->id,
|
||||
'cuota_id' => $this->cuota->id,
|
||||
'toku_id' => $this->invoice->toku_id,
|
||||
]
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $service->sendCuotas($this->venta));
|
||||
}
|
||||
public function testUpdatePago(): void
|
||||
{
|
||||
$service = new Service\Venta\MediosPago\Toku($this->logger);
|
||||
$service->register(Service\Venta\MediosPago\Toku::CUSTOMER, $this->customerService);
|
||||
$service->register(Service\Venta\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\Venta\MediosPago\Toku($this->logger);
|
||||
$service->register(Service\Venta\MediosPago\Toku::CUSTOMER, $customerService);
|
||||
$service->register(Service\Venta\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\Venta\MediosPago\Toku($this->logger);
|
||||
$service->register(Service\Venta\MediosPago\Toku::CUSTOMER, $this->customerService);
|
||||
$service->register(Service\Venta\MediosPago\Toku::INVOICE, $invoiceService);
|
||||
|
||||
$this->expectException(InvalidResult::class);
|
||||
$service->updatePago($request['payment_intent']);
|
||||
}
|
||||
}
|
73
app/tests/unit/src/Service/Venta/PagoTest.php
Normal file
73
app/tests/unit/src/Service/Venta/PagoTest.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
namespace Inventario\Service\Venta;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
|
||||
class PagoTest extends TestCase
|
||||
{
|
||||
protected float $uf;
|
||||
protected Model\Venta\Pago $pago;
|
||||
protected Repository\Venta\Pago $pagoRepository;
|
||||
protected Repository\Venta\EstadoPago $estadoPagoRepository;
|
||||
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository;
|
||||
protected Service\UF $ufService;
|
||||
protected Service\Valor $valorService;
|
||||
protected Service\Queue $queueService;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$fecha = new DateTimeImmutable();
|
||||
$this->uf = 37568.84;
|
||||
|
||||
$tipoEstadoPago = $this->getMockBuilder(Model\Venta\TipoEstadoPago::class)->disableOriginalConstructor()->getMock();
|
||||
$tipoEstadoPago->id = 1;
|
||||
$tipoEstadoPago->descripcion = 'depositado';
|
||||
$estadoPago = $this->getMockBuilder(Model\Venta\EstadoPago::class)->disableOriginalConstructor()->getMock();
|
||||
$estadoPago->id = 1;
|
||||
$estadoPago->tipoEstadoPago = $tipoEstadoPago;
|
||||
$estadoPago->fecha = $fecha;
|
||||
|
||||
$this->pago = $this->getMockBuilder(Model\Venta\Pago::class)->disableOriginalConstructor()->getMock();
|
||||
$this->pago->id = 1;
|
||||
$this->pago->fecha = $fecha;
|
||||
$this->pago->uf = $this->uf;
|
||||
$this->pago->currentEstado = $estadoPago;
|
||||
$this->pagoRepository = $this->getMockBuilder(Repository\Venta\Pago::class)->disableOriginalConstructor()->getMock();
|
||||
$this->pagoRepository->method('fetchById')->willReturn($this->pago);
|
||||
$this->pagoRepository->method('edit')->willReturnCallback(function() {
|
||||
$this->pago->uf = $this->uf;
|
||||
return $this->pago;
|
||||
});
|
||||
$this->estadoPagoRepository = $this->getMockBuilder(Repository\Venta\EstadoPago::class)->disableOriginalConstructor()->getMock();
|
||||
$this->estadoPagoRepository->method('fetchCurrentByPago')->with($this->pago->id)->willReturn($estadoPago);
|
||||
$this->tipoEstadoPagoRepository = $this->getMockBuilder(Repository\Venta\TipoEstadoPago::class)->disableOriginalConstructor()->getMock();
|
||||
$this->ufService = $this->getMockBuilder(Service\UF::class)->disableOriginalConstructor()->getMock();
|
||||
$this->ufService->method('get')->with($fecha)->willReturn($this->uf);
|
||||
$this->valorService = $this->getMockBuilder(Service\Valor::class)->disableOriginalConstructor()->getMock();
|
||||
$this->queueService = $this->getMockBuilder(Service\Queue::class)->disableOriginalConstructor()->getMock();
|
||||
}
|
||||
|
||||
public function testUpdateUF(): void
|
||||
{
|
||||
$this->pago->uf = null;
|
||||
|
||||
$pagoService = new Service\Venta\Pago(
|
||||
$this->pagoRepository,
|
||||
$this->estadoPagoRepository,
|
||||
$this->tipoEstadoPagoRepository,
|
||||
$this->ufService,
|
||||
$this->valorService,
|
||||
$this->queueService
|
||||
);
|
||||
|
||||
$status = $pagoService->updateUF($this->pago->id);
|
||||
$pago = $pagoService->getById($this->pago->id);
|
||||
|
||||
$this->assertTrue($status);
|
||||
$this->assertEquals($pago->uf, $this->uf);
|
||||
}
|
||||
}
|
46
app/tests/unit/src/Service/Worker/ServiceTest.php
Normal file
46
app/tests/unit/src/Service/Worker/ServiceTest.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
namespace Tests\Unit\Service\Worker;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Incoviba\Service;
|
||||
use Incoviba\Model;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class ServiceTest extends TestCase
|
||||
{
|
||||
protected Model\Job $job;
|
||||
protected ContainerInterface $container;
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$pagoService = $this->getMockBuilder(Service\Venta\Pago::class)->disableOriginalConstructor()->getMock();
|
||||
$pagoService->method('updateUF')->willReturn(true);
|
||||
$this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
|
||||
$this->container = $this->getMockBuilder(ContainerInterface::class)->disableOriginalConstructor()->getMock();
|
||||
$this->container->method('get')->willReturnMap([
|
||||
[LoggerInterface::class, $this->logger],
|
||||
[Service\Queue::class, $this->getMockBuilder(Service\Queue::class)->disableOriginalConstructor()->getMock()],
|
||||
[Service\UF::class, $this->getMockBuilder(Service\UF::class)->disableOriginalConstructor()->getMock()],
|
||||
[Service\Venta\Pago::class, $pagoService],
|
||||
]);
|
||||
}
|
||||
|
||||
public function testExecute(): void
|
||||
{
|
||||
$configuration = [
|
||||
'type' => 'service',
|
||||
'service' => Service\Venta\Pago::class,
|
||||
'method' => 'updateUF',
|
||||
'params' => ['pago_id' => 1]
|
||||
];
|
||||
$job = $this->getMockBuilder(Model\Job::class)->disableOriginalConstructor()->getMock();
|
||||
$job->configuration = $configuration;
|
||||
|
||||
$service = new Service\Worker\Service($this->container, $this->logger);
|
||||
$result = $service->execute($job);
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user