Valor con pruebas
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
<phpunit
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd"
|
||||
bootstrap="test.bootstrap.php"
|
||||
bootstrap="vendor/autoload.php"
|
||||
cacheDirectory="/code/cache/tests"
|
||||
executionOrder="depends,defects"
|
||||
requireCoverageMetadata="false"
|
||||
@ -30,14 +30,14 @@
|
||||
</source>
|
||||
<coverage pathCoverage="false" ignoreDeprecatedCodeUnits="true" disableCodeCoverageIgnore="true">
|
||||
<report>
|
||||
<html outputDirectory="/code/public/coverage/html"/>
|
||||
<!--<html outputDirectory="/code/public/coverage/html"/>-->
|
||||
<php outputFile="/code/public/coverage/coverage.php"/>
|
||||
</report>
|
||||
</coverage>
|
||||
<logging>
|
||||
<!--<logging>
|
||||
<junit outputFile="/code/cache/tests/junit.xml"/>
|
||||
<teamcity outputFile="/code/cache/tests/teamcity.txt"/>
|
||||
<testdoxHtml outputFile="/code/cache/tests/testdox.html"/>
|
||||
<testdoxText outputFile="/code/cache/tests/testdox.txt"/>
|
||||
</logging>
|
||||
</logging>-->
|
||||
</phpunit>
|
||||
|
@ -4,6 +4,7 @@ namespace Incoviba\Service;
|
||||
use DateTimeInterface;
|
||||
use DateTimeImmutable;
|
||||
use DateMalformedStringException;
|
||||
use function PHPUnit\Framework\countOf;
|
||||
|
||||
class Valor
|
||||
{
|
||||
@ -11,15 +12,22 @@ class Valor
|
||||
|
||||
public function clean(string|float|int $value): float
|
||||
{
|
||||
if ((float) $value == $value) {
|
||||
if (!is_string($value)) {
|
||||
return (float) $value;
|
||||
}
|
||||
return (float) str_replace(['.', ','], ['', '.'], $value);
|
||||
if ((int) $value == $value) {
|
||||
return (float) $value;
|
||||
}
|
||||
|
||||
if ($this->isUS($value)) {
|
||||
return $this->formatUS($value);
|
||||
}
|
||||
return $this->formatCL($value);
|
||||
}
|
||||
public function toPesos(string $value, null|string|DateTimeInterface $date = null, bool $force = false): int
|
||||
{
|
||||
$date = $this->getDateTime($date);
|
||||
if (abs((float) $value - (int) $value) > 0 or $force) {
|
||||
if ($this->isFloat($value) or $force) {
|
||||
return round($value * $this->ufService->get($date));
|
||||
}
|
||||
return (int) $value;
|
||||
@ -27,7 +35,7 @@ class Valor
|
||||
public function toUF(string $value, null|string|DateTimeInterface $date = null, bool $force = false): float
|
||||
{
|
||||
$date = $this->getDateTime($date);
|
||||
if (abs((float) $value - (int) $value) > 0 and !$force) {
|
||||
if ($this->isFloat($value) and !$force) {
|
||||
return (float) $value;
|
||||
}
|
||||
return $value / $this->ufService->get($date);
|
||||
@ -47,4 +55,58 @@ class Valor
|
||||
}
|
||||
return $date;
|
||||
}
|
||||
|
||||
protected function isUS(string $value): bool
|
||||
{
|
||||
/*
|
||||
* Chile
|
||||
* 1.000.000,00
|
||||
* 10000,000
|
||||
* 10,53
|
||||
* 1.000,00
|
||||
* 1.000 imposible! se asume US si # antes de . < 10
|
||||
*
|
||||
* 1,000,000.00
|
||||
* 10000.00
|
||||
* 1,000.000
|
||||
* 10.53
|
||||
* 1,000 imposible! se asume CL
|
||||
*/
|
||||
if (str_contains($value, '.')) {
|
||||
$parts = explode('.', $value);
|
||||
if (count($parts) > 2) { // 1.000.000 || 1.000.000,00
|
||||
return false;
|
||||
}
|
||||
if (strlen($parts[0]) > 3) { // 1000.000 || 1,000.000
|
||||
return true;
|
||||
}
|
||||
if (str_contains($value, ',')) {
|
||||
if (strpos($value, ',') > strpos($value, '.')) { // 1.000,000
|
||||
return false;
|
||||
}
|
||||
return true; // 1,000.000
|
||||
}
|
||||
if ((int) $parts[0] < 10) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
protected function formatCL(string $value): float
|
||||
{
|
||||
return (float) str_replace(',', '.', (str_replace('.', '', $value)));
|
||||
}
|
||||
protected function formatUS(string $value): float
|
||||
{
|
||||
return (float) str_replace(',', '', $value);
|
||||
}
|
||||
protected function isFloat(string|int|float $value): bool
|
||||
{
|
||||
if (!is_string($value)) {
|
||||
return is_float($value);
|
||||
}
|
||||
$cleaned = $this->clean($value);
|
||||
return round($cleaned) !== $cleaned;
|
||||
}
|
||||
}
|
||||
|
69
app/tests/unit/src/Service/ValorTest.php
Normal file
69
app/tests/unit/src/Service/ValorTest.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
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],
|
||||
];
|
||||
}
|
||||
|
||||
#[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 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user