feature/cierres (#30)
Reservas agregar y aprobar Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl> Reviewed-on: #30
This commit is contained in:
@ -13,7 +13,7 @@ class ReservationTest extends AbstractModel
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->model = new Reservation();
|
||||
$this->properties = ['buyer', 'date', 'units', 'promotions', 'broker'];
|
||||
$this->properties = ['project', 'buyer', 'date', 'units', 'promotions', 'broker'];
|
||||
$this->methods = ['states', 'currentState', 'addUnit', 'removeUnit', 'findUnit', 'hasUnit'];
|
||||
}
|
||||
}
|
||||
|
64
app/tests/unit/src/Repository/DireccionTest.php
Normal file
64
app/tests/unit/src/Repository/DireccionTest.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
namespace Incoviba\Test\Repository;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Faker;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class DireccionTest extends TestCase
|
||||
{
|
||||
protected Define\Connection $connection;
|
||||
protected Repository\Comuna $comunaRepository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->connection = $this->getMockBuilder(Define\Connection::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->comunaRepository = $this->getMockBuilder(Repository\Comuna::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->comunaRepository->method('fetchById')->willReturnCallback(function ($id) {
|
||||
$comuna = new Model\Comuna();
|
||||
$comuna->id = $id;
|
||||
return $comuna;
|
||||
});
|
||||
}
|
||||
|
||||
public function testCreate(): void
|
||||
{
|
||||
$direccionRepository = new Repository\Direccion($this->connection, $this->comunaRepository);
|
||||
|
||||
$faker = Faker\Factory::create();
|
||||
$data = [
|
||||
'calle' => $faker->streetName,
|
||||
'numero' => $faker->randomNumber(),
|
||||
'extra' => $faker->streetSuffix,
|
||||
'comuna' => $faker->randomNumber(),
|
||||
];
|
||||
$direccion = $direccionRepository->create($data);
|
||||
|
||||
$this->assertTrue(!isset($direccion->id));
|
||||
$this->assertEquals($data['calle'], $direccion->calle);
|
||||
$this->assertEquals($data['numero'], $direccion->numero);
|
||||
$this->assertEquals($data['extra'], $direccion->extra);
|
||||
$this->assertEquals($data['comuna'], $direccion->comuna->id);
|
||||
}
|
||||
public function testSave(): void
|
||||
{
|
||||
$direccionRepository = new Repository\Direccion($this->connection, $this->comunaRepository);
|
||||
$faker = Faker\Factory::create();
|
||||
|
||||
$direccion = new Model\Direccion();
|
||||
$direccion->calle = $faker->streetName;
|
||||
$direccion->numero = $faker->randomNumber(3);
|
||||
$direccion->extra = $faker->streetSuffix;
|
||||
$direccion->comuna = $this->getMockBuilder(Model\Comuna::class)->getMock();
|
||||
$direccion->comuna->id = $faker->numberBetween(10000, 18000);
|
||||
$direccion = $direccionRepository->save($direccion);
|
||||
|
||||
$this->assertTrue(isset($direccion->id));
|
||||
}
|
||||
}
|
62
app/tests/unit/src/Repository/PersonaTest.php
Normal file
62
app/tests/unit/src/Repository/PersonaTest.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
namespace Incoviba\Test\Repository;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Faker;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class PersonaTest extends TestCase
|
||||
{
|
||||
protected Define\Connection $connection;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->connection = $this->getMockBuilder(Define\Connection::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
}
|
||||
|
||||
public function testCreate(): void
|
||||
{
|
||||
$personaRepository = new Repository\Persona($this->connection);
|
||||
$faker = Faker\Factory::create();
|
||||
|
||||
$data = [
|
||||
'rut' => $faker->randomNumber(8),
|
||||
'digito' => $faker->boolean(100 - round(1 / 11 * 100)) ? $faker->randomNumber(1) : 'K',
|
||||
'nombres' => $faker->name(),
|
||||
'apellido_paterno' => $faker->lastName(),
|
||||
'apellido_materno' => $faker->lastName()
|
||||
];
|
||||
$persona = $personaRepository->create($data);
|
||||
|
||||
$this->assertEquals($data['rut'], $persona->rut);
|
||||
$this->assertEquals($data['digito'], $persona->digito);
|
||||
$this->assertEquals($data['nombres'], $persona->nombres);
|
||||
$this->assertEquals($data['apellido_paterno'], $persona->apellidoPaterno);
|
||||
$this->assertEquals($data['apellido_materno'], $persona->apellidoMaterno);
|
||||
}
|
||||
public function testSave(): void
|
||||
{
|
||||
$personaRepository = new Repository\Persona($this->connection);
|
||||
$faker = Faker\Factory::create();
|
||||
|
||||
$data = [
|
||||
'rut' => $faker->randomNumber(8),
|
||||
'digito' => $faker->boolean(100 - round(1 / 11 * 100)) ? $faker->randomNumber(1) : 'K',
|
||||
'nombres' => $faker->name(),
|
||||
'apellido_paterno' => $faker->lastName(),
|
||||
'apellido_materno' => $faker->lastName()
|
||||
];
|
||||
$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'];
|
||||
$persona = $personaRepository->save($persona);
|
||||
$this->assertEquals($data['rut'], $persona->rut);
|
||||
}
|
||||
}
|
102
app/tests/unit/src/Repository/Venta/ReservationTest.php
Normal file
102
app/tests/unit/src/Repository/Venta/ReservationTest.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
namespace Incoviba\Test\Repository\Venta;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Faker;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class ReservationTest extends TestCase
|
||||
{
|
||||
protected Define\Connection $connection;
|
||||
protected Repository\Proyecto $proyectoRepository;
|
||||
protected Repository\Persona $personaRepository;
|
||||
protected Repository\Proyecto\Broker $brokerRepository;
|
||||
protected Repository\Venta\Unidad $unitRepository;
|
||||
protected Repository\Venta\Promotion $promotionRepository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->connection = $this->getMockBuilder(Define\Connection::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->proyectoRepository = $this->getMockBuilder(Repository\Proyecto::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->proyectoRepository->method('fetchById')->willReturnCallback(function ($id) {
|
||||
$proyecto = new Model\Proyecto();
|
||||
$proyecto->id = $id;
|
||||
return $proyecto;
|
||||
});
|
||||
$this->personaRepository = $this->getMockBuilder(Repository\Persona::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->personaRepository->method('fetchById')->willReturnCallback(function ($rut) {
|
||||
$persona = new Model\Persona();
|
||||
$persona->rut = $rut;
|
||||
return $persona;
|
||||
});
|
||||
$this->brokerRepository = $this->getMockBuilder(Repository\Proyecto\Broker::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->brokerRepository->method('fetchById')->willReturnCallback(function ($rut) {
|
||||
$broker = new Model\Proyecto\Broker();
|
||||
$broker->rut = $rut;
|
||||
return $broker;
|
||||
});
|
||||
$this->unitRepository = $this->getMockBuilder(Repository\Venta\Unidad::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->unitRepository->method('fetchById')->willReturnCallback(function ($id) {
|
||||
$unidad = new Model\Venta\Unidad();
|
||||
$unidad->id = $id;
|
||||
return $unidad;
|
||||
});
|
||||
$this->promotionRepository = $this->getMockBuilder(Repository\Venta\Promotion::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->promotionRepository->method('fetchById')->willReturnCallback(function ($id) {
|
||||
$promotion = new Model\Venta\Promotion();
|
||||
$promotion->id = $id;
|
||||
return $promotion;
|
||||
});
|
||||
}
|
||||
|
||||
public function testCreate(): void
|
||||
{
|
||||
$reservationRepository = new Repository\Venta\Reservation($this->connection, $this->proyectoRepository,
|
||||
$this->personaRepository, $this->brokerRepository, $this->unitRepository, $this->promotionRepository);
|
||||
$faker = Faker\Factory::create();
|
||||
$data = [
|
||||
'project_id' => $faker->numberBetween(1, 10),
|
||||
'buyer_rut' => $faker->randomNumber(8),
|
||||
'date' => $faker->dateTimeBetween('-2 years', 'now')->format('Y-m-d'),
|
||||
];
|
||||
$reservation = $reservationRepository->create($data);
|
||||
|
||||
$this->assertTrue(!isset($reservation->id));
|
||||
$this->assertEquals($data['project_id'], $reservation->project->id);
|
||||
$this->assertEquals($data['buyer_rut'], $reservation->buyer->rut);
|
||||
$this->assertEquals($data['date'], $reservation->date->format('Y-m-d'));
|
||||
}
|
||||
public function testSave(): void
|
||||
{
|
||||
$reservationRepository = new Repository\Venta\Reservation($this->connection, $this->proyectoRepository,
|
||||
$this->personaRepository, $this->brokerRepository, $this->unitRepository, $this->promotionRepository);
|
||||
$faker = Faker\Factory::create();
|
||||
$data = [
|
||||
'project_id' => $faker->numberBetween(1, 10),
|
||||
'buyer_rut' => $faker->randomNumber(8),
|
||||
'date' => $faker->dateTimeBetween('-2 years', 'now')->format('Y-m-d'),
|
||||
];
|
||||
$reservation = new Model\Venta\Reservation();
|
||||
$reservation->project = $this->proyectoRepository->fetchById($data['project_id']);
|
||||
$reservation->buyer = $this->personaRepository->fetchById($data['buyer_rut']);
|
||||
$reservation->date = new DateTimeImmutable($data['date']);
|
||||
$reservation = $reservationRepository->save($reservation);
|
||||
|
||||
$this->assertTrue(isset($reservation->id));
|
||||
}
|
||||
}
|
64
app/tests/unit/src/Service/DireccionTest.php
Normal file
64
app/tests/unit/src/Service/DireccionTest.php
Normal 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);
|
||||
}
|
||||
}
|
65
app/tests/unit/src/Service/PersonaTest.php
Normal file
65
app/tests/unit/src/Service/PersonaTest.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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],
|
||||
];
|
||||
}
|
||||
|
65
app/tests/unit/src/Service/Venta/BonoPieTest.php
Normal file
65
app/tests/unit/src/Service/Venta/BonoPieTest.php
Normal 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));
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
139
app/tests/unit/src/Service/Venta/ReservationTest.php
Normal file
139
app/tests/unit/src/Service/Venta/ReservationTest.php
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user