feature/cierres (#30)

Reservas agregar y aprobar

Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl>
Reviewed-on: #30
This commit is contained in:
2025-09-11 15:16:12 -03:00
parent cc4cb17f1a
commit 61c813fc08
61 changed files with 3806 additions and 190 deletions

View File

@ -0,0 +1,64 @@
<?php
namespace Incoviba\Test\Service;
use Psr\Log\LoggerInterface;
use PHPUnit\Framework\TestCase;
use Faker;
use Incoviba\Common\Implement;
use Incoviba\Exception\ServiceAction;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
class DireccionTest extends TestCase
{
protected LoggerInterface $logger;
protected Repository\Direccion $direccionRepository;
protected function setUp(): void
{
$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
$this->direccionRepository = $this->getMockBuilder(Repository\Direccion::class)
->disableOriginalConstructor()
->getMock();
$this->direccionRepository->method('fetchByCalleAndNumeroAndExtraAndComuna')
->willThrowException(new Implement\Exception\EmptyResult(''));
$this->direccionRepository->method('filterData')->willReturnArgument(0);
$this->direccionRepository->method('create')->willReturnCallback(function($data) {
$direccion = new Model\Direccion();
$direccion->calle = $data['calle'];
$direccion->numero = $data['numero'];
$direccion->extra = $data['extra'];
$direccion->comuna = $this->getMockBuilder(Model\Comuna::class)
->disableOriginalConstructor()->getMock();
$direccion->comuna->id = $data['comuna'];
return $direccion;
});
$this->direccionRepository->method('save')->willReturnCallback(function($direccion) {
$direccion->id = 1;
return $direccion;
});
}
public function testAdd(): void
{
$direccionService = new Service\Direccion($this->logger, $this->direccionRepository);
$faker = Faker\Factory::create('es_ES');
$data = [
'street' => $faker->streetName,
'number' => $faker->randomNumber(3),
'extra' => $faker->word,
'comuna_id' => $faker->numberBetween(10000, 18000),
];
$direccion = $direccionService->add($data);
$this->assertEquals($data['street'], $direccion->calle);
$this->assertEquals($data['number'], $direccion->numero);
$this->assertEquals($data['extra'], $direccion->extra);
$this->assertEquals($data['comuna_id'], $direccion->comuna->id);
}
}

View File

@ -0,0 +1,65 @@
<?php
namespace Incoviba\Test\Service;
use Psr\Log\LoggerInterface;
use PHPUnit\Framework\TestCase;
use Faker;
use Incoviba\Common\Implement;
use Incoviba\Exception\ServiceAction;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
class PersonaTest extends TestCase
{
protected LoggerInterface $logger;
protected Repository\Persona $personaRepository;
protected Repository\Persona\Datos $datosPersonaRepository;
protected Repository\Venta\Propietario $propietarioRepository;
protected Service\Direccion $direccionService;
protected function setUp(): void
{
$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
$this->personaRepository = $this->getMockBuilder(Repository\Persona::class)
->disableOriginalConstructor()->getMock();
$this->personaRepository->method('fetchById')->willThrowException(new Implement\Exception\EmptyResult(''));
$this->personaRepository->method('filterData')->willReturnArgument(0);
$this->personaRepository->method('create')->willReturnCallback(function($data) {
$persona = new Model\Persona();
$persona->rut = $data['rut'];
$persona->digito = $data['digito'];
$persona->nombres = $data['nombres'];
$persona->apellidoPaterno = $data['apellido_paterno'];
$persona->apellidoMaterno = $data['apellido_materno'];
return $persona;
});
$this->personaRepository->method('save')->willReturnArgument(0);
$this->datosPersonaRepository = $this->getMockBuilder(Repository\Persona\Datos::class)
->disableOriginalConstructor()->getMock();
$this->propietarioRepository = $this->getMockBuilder(Repository\Venta\Propietario::class)
->disableOriginalConstructor()->getMock();
$this->propietarioRepository->method('fetchById')->willThrowException(new Implement\Exception\EmptyResult(''));
$this->direccionService = $this->getMockBuilder(Service\Direccion::class)
->disableOriginalConstructor()->getMock();
}
public function testAdd(): void
{
$personaService = new Service\Persona($this->logger, $this->personaRepository, $this->datosPersonaRepository,
$this->propietarioRepository, $this->direccionService);
$faker = Faker\Factory::create('es_ES');
$digit = $faker->boolean(100-round(1/11*100)) ? $faker->randomNumber(1) : 'K';
$data = [
'rut' => $faker->randomNumber(8),
'digito' => $digit,
'nombres' => $faker->name(),
'apellido_paterno' => $faker->lastName(),
'apellido_materno' => $faker->lastName(),
];
$persona = $personaService->add($data);
$this->assertEquals($data['rut'], $persona->rut);
}
}

View File

@ -3,18 +3,22 @@ namespace Tests\Unit\Service;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Faker\Factory;
use Incoviba\Service\Valor;
use Incoviba\Service;
class ValorTest extends TestCase
{
protected Service\UF $ufService;
protected static float $staticUF = 35498.76;
protected function setUp(): void
{
$this->ufService = $this->getMockBuilder(Service\UF::class)
->disableOriginalConstructor()
->getMock();
$this->ufService->method('get')->willReturn(35000.0);
$faker = Factory::create();
$this->ufService->method('get')->willReturn(self::$staticUF);
}
public static function cleanDataProvider(): array
@ -55,14 +59,14 @@ class ValorTest extends TestCase
$result = $valorService->toUF($input, $date);
$this->assertIsFloat($result);
$this->assertEquals($input / 35000, $result);
$this->assertEquals($input / self::$staticUF, $result);
}
public static function pesosDataProvider(): array
{
return [
[1000.01, 1000.01*35000, false],
[1000, 1000*35000, true],
[1000.01, round(1000.01*self::$staticUF), false],
[1000, round(1000*self::$staticUF), true],
['1000', 1000, false],
];
}

View File

@ -0,0 +1,65 @@
<?php
namespace Incoviba\Test\Unit\Service\Venta;
use Psr\Log\LoggerInterface;
use PHPUnit\Framework\TestCase;
use Faker;
use Incoviba\Common\Implement;
use Incoviba\Service;
use Incoviba\Repository;
use Incoviba\Model;
class BonoPieTest extends TestCase
{
public function testAdd(): void
{
$faker = Faker\factory::create();
$fecha = $faker->dateTimeBetween('-1 week');
$data = [
'fecha' => $fecha->format('Y-m-d'),
'valor' => $faker->randomFloat(2, 100, 1000),
];
$uf = $faker->randomFloat(2, 20000, 40000);
$pago = new Model\Venta\Pago();
$pago->id = $faker->randomNumber();
$pago->fecha = $fecha;
$pago->valor = $data['valor'] * $uf;
$pago->uf = $uf;
$bonoPie = new Model\Venta\BonoPie();
$bonoPie->valor = $data['valor'];
$bonoPie->pago = $pago;
$logger = $this->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->getMock();
$bonoPieRepository = $this->getMockBuilder(Repository\Venta\BonoPie::class)
->disableOriginalConstructor()
->getMock();
$bonoPieRepository->method('fetchByPago')->willThrowException(new Implement\Exception\EmptyResult('fetchByPago'));
$bonoPieRepository->method('create')->willReturn($bonoPie);
$bonoPieRepository->method('save')->willReturnCallback(function($bonoPie) use ($faker) {
$bonoPie->id = $faker->randomNumber();
return $bonoPie;
});
$bonoPieRepository->method('filterData')->willReturnCallback(function($data) {
return array_intersect_key($data, array_flip(['valor', 'pago']));
});
$valorService = $this->getMockBuilder(Service\Valor::class)
->disableOriginalConstructor()
->getMock();
$valorService->method('toUF')->willReturn($data['valor']);
$ufService = $this->getMockBuilder(Service\UF::class)
->disableOriginalConstructor()
->getMock();
$ufService->method('get')->with($fecha)->willReturn($uf);
$pagoService = $this->getMockBuilder(Service\Venta\Pago::class)
->disableOriginalConstructor()
->getMock();
$pagoService->method('add')->willReturn($pago);
$bonoPieService = new Service\Venta\BonoPie($logger, $bonoPieRepository, $valorService, $ufService, $pagoService);
$this->assertEquals($bonoPie, $bonoPieService->add($data));
}
}

View File

@ -1,5 +1,5 @@
<?php
namespace Inventario\Service\Venta;
namespace Incoviba\Test\Service\Venta;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
@ -70,4 +70,4 @@ class PagoTest extends TestCase
$this->assertTrue($status);
$this->assertEquals($pago->uf, $this->uf);
}
}
}

View File

@ -0,0 +1,139 @@
<?php
namespace Incoviba\Tests\Unit\Service\Venta;
use DateTimeImmutable;
use Psr\Log\LoggerInterface;
use PHPUnit\Framework\TestCase;
use Faker;
use Incoviba\Common\Implement;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
class ReservationTest extends TestCase
{
protected LoggerInterface $logger;
protected Repository\Venta\Reservation $reservationRepository;
protected Repository\Venta\Reservation\State $stateRepository;
protected Service\Persona $personaService;
protected Service\Proyecto\Broker $brokerService;
protected Service\Venta\Promotion $promotionService;
protected Service\Venta\Unidad $unitService;
protected function setUp(): void
{
$this->logger = $this->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()->getMock();
$this->reservationRepository = $this->getMockBuilder(Repository\Venta\Reservation::class)
->disableOriginalConstructor()->getMock();
$this->reservationRepository->method('fetchByBuyerAndDate')->willThrowException(new Implement\Exception\EmptyResult(''));
$this->reservationRepository->method('filterData')->willReturnArgument(0);
$this->reservationRepository->method('create')->willReturnCallback(function($data) {
$reservation = new Model\Venta\Reservation();
$reservation->buyer = $this->getMockBuilder(Model\Persona::class)->disableOriginalConstructor()->getMock();
$reservation->buyer->rut = $data['buyer_rut'];
$reservation->project = $this->getMockBuilder(Model\Proyecto::class)->disableOriginalConstructor()->getMock();
$reservation->project->id = $data['project_id'];
$reservation->date = new DateTimeImmutable($data['date']);
if (array_key_exists('broker_rut', $data) and !empty($data['broker_rut'])) {
$reservation->broker = $this->getMockBuilder(Model\Proyecto\Broker::class)->disableOriginalConstructor()->getMock();
$reservation->broker->rut = $data['broker_rut'];
}
return $reservation;
});
$this->reservationRepository->method('save')->willReturnCallback(function($reservation) {
$reservation->id = 1;
return $reservation;
});
$this->stateRepository = $this->getMockBuilder(Repository\Venta\Reservation\State::class)
->disableOriginalConstructor()->getMock();
$this->stateRepository->method('fetchByReservation')->willReturnCallback(function($reservation_id) {
$state = new Model\Venta\Reservation\State();
$state->reservation = new Model\Venta\Reservation();
$state->reservation->id = $reservation_id;
return $state;
});
$this->personaService = $this->getMockBuilder(Service\Persona::class)
->disableOriginalConstructor()->getMock();
$this->personaService->method('add')->willReturnCallback(function($data) {
$persona = new Model\Persona();
$persona->rut = $data['rut'];
return $persona;
});
$this->personaService->method('getById')->willReturnCallback(function($id) {
$persona = new Model\Persona();
$persona->rut = $id;
return $persona;
});
$this->brokerService = $this->getMockBuilder(Service\Proyecto\Broker::class)
->disableOriginalConstructor()->getMock();
$this->brokerService->method('get')->willReturnCallback(function($id) {
$broker = new Model\Proyecto\Broker();
$broker->rut = $id;
return $broker;
});
$this->promotionService = $this->getMockBuilder(Service\Venta\Promotion::class)
->disableOriginalConstructor()->getMock();
$this->promotionService->method('getById')->willReturnCallback(function($id) {
$promotion = new Model\Venta\Promotion();
$promotion->id = $id;
return $promotion;
});
$this->unitService = $this->getMockBuilder(Service\Venta\Unidad::class)
->disableOriginalConstructor()->getMock();
$this->unitService->method('getById')->willReturnCallback(function($id) {
$unit = new Model\Venta\Unidad();
$unit->id = $id;
return $unit;
});
}
public function testAdd(): void
{
$faker = Faker\Factory::create();
$reservationService = new Service\Venta\Reservation($this->logger, $this->reservationRepository,
$this->stateRepository, $this->personaService, $this->brokerService, $this->promotionService,
$this->unitService);
$units = [];
$unitsValue = [];
$unitsCount = $faker->numberBetween(1, 10);
for ($i = 0; $i < $unitsCount; $i++) {
$units[] = $faker->unique()->numberBetween(1, 100);
$unitsValue[] = $faker->randomFloat(2, 1000, 10000);
}
$digit = $faker->boolean(100-round(1/11*100)) ? $faker->randomNumber(1) : 'K';
$broker = $faker->boolean(10) ? $faker->randomNumber(8, true) : '';
$promotions = [];
$promotionsCount = $faker->numberBetween(0, 3);
for ($i = 0; $i < $promotionsCount; $i++) {
$promotions[] = $faker->numberBetween(1, 100);
}
$data = [
'project_id' => $faker->numberBetween(1, 100),
'date' => $faker->dateTimeBetween('-1 year', 'now')->format('Y-m-d'),
'buyer_rut' => $faker->randomNumber(8, true),
'buyer_digit' => $digit,
'buyer_names' => $faker->firstName(),
'buyer_last_name' => $faker->lastName(),
'buyer_last_name2' => $faker->lastName(),
'buyer_address_street' => $faker->streetName(),
'buyer_address_number' => $faker->randomNumber(3),
'buyer_address_comuna' => $faker->numberBetween(10000, 18000),
'buyer_marital_status' => $faker->word(),
'buyer_email' => $faker->email(),
'buyer_phone' => $faker->randomNumber(9),
'buyer_profession' => $faker->word(),
'buyer_birthdate' => $faker->dateTimeBetween('-80 year', '-18 year')->format('Y-m-d'),
'units' => $units,
'units_value' => $unitsValue,
'broker_rut' => $broker,
'promotions' => $promotions
];
$reservation = $reservationService->add($data);
$this->assertEquals($data['project_id'], $reservation->project->id);
$this->assertEquals($data['buyer_rut'], $reservation->buyer->rut);
$this->assertCount($unitsCount, $reservation->units);
}
}