Prueba UF

This commit is contained in:
Juan Pablo Vial
2025-06-24 11:16:19 -04:00
parent a687743762
commit 4053854410

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