103 lines
4.4 KiB
PHP
103 lines
4.4 KiB
PHP
<?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));
|
|
}
|
|
}
|