feat: new v4 version (#5)
This commit is contained in:
@@ -1,87 +1,107 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the MNC\ChileanRut library.
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @project Chilean RUT
|
||||
* @link https://github.com/mnavarrocarter/chilean-rut
|
||||
* @package castor/log
|
||||
* @author Matias Navarro-Carter mnavarrocarter@gmail.com
|
||||
* @license MIT
|
||||
* @copyright 2024 Matias Navarro-Carter
|
||||
*
|
||||
* (c) Matías Navarro Carter <mnavarrocarter@gmail.com>
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace MNC\ChileanRut;
|
||||
namespace MNC;
|
||||
|
||||
use MNC\Rut\IsInvalid;
|
||||
use MNC\Rut\Verifier;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Class RutTest
|
||||
* @package MNC\ChileanRut\Tests\Rut
|
||||
*/
|
||||
#[CoversClass(Rut::class)]
|
||||
#[CoversClass(IsInvalid::class)]
|
||||
#[CoversClass(Verifier::class)]
|
||||
class RutTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getRutDataset
|
||||
* @param string $raw
|
||||
* @param int $expectedNumber
|
||||
* @param string $expectedVerifier
|
||||
*/
|
||||
public function testItParsesRuts(string $raw, int $expectedNumber, string $expectedVerifier): void
|
||||
#[Test]
|
||||
#[DataProvider('getParseData')]
|
||||
public function it_parses_ruts(string $raw, int $expectedNumber, Verifier $expectedVerifier): void
|
||||
{
|
||||
$rut = Rut::parse($raw);
|
||||
self::assertSame($expectedNumber, $rut->getNumber());
|
||||
self::assertSame($expectedVerifier, $rut->getVerifier());
|
||||
$this->assertSame($expectedNumber, $rut->number);
|
||||
$this->assertSame($expectedVerifier, $rut->verifier);
|
||||
}
|
||||
|
||||
public function testItDetectsOutOfRangeVerifier(): void
|
||||
#[Test]
|
||||
#[DataProvider('getParseWithErrorData')]
|
||||
public function it_parses_with_error(string $raw, string $expectedError): void
|
||||
{
|
||||
$this->expectException(InvalidRut::class);
|
||||
Rut::parse('16894365F');
|
||||
$this->expectException(IsInvalid::class);
|
||||
$this->expectExceptionMessage($expectedError);
|
||||
Rut::parse($raw);
|
||||
}
|
||||
|
||||
public function testItDetectsInvalidVerifier(): void
|
||||
#[Test]
|
||||
public function it_cannot_be_negative(): void
|
||||
{
|
||||
$this->expectException(InvalidRut::class);
|
||||
Rut::parse('16894365K');
|
||||
$this->expectException(IsInvalid::class);
|
||||
$this->expectExceptionMessage('El RUT numero -22224525 es menor a cero');
|
||||
Rut::create(-22_224_525);
|
||||
}
|
||||
|
||||
public function testItDetectsRutTooBig(): void
|
||||
{
|
||||
$this->expectException(InvalidRut::class);
|
||||
Rut::create(3_355_535_353);
|
||||
}
|
||||
|
||||
public function testItComparesToEqual(): void
|
||||
#[Test]
|
||||
public function it_checks_for_equality(): void
|
||||
{
|
||||
$rut1 = Rut::parse('168943652');
|
||||
$rut2 = Rut::parse('16.894.365-2');
|
||||
$rut3 = Rut::create(22_224_525);
|
||||
self::assertTrue($rut1->equals($rut2));
|
||||
self::assertFalse($rut1->equals($rut3));
|
||||
$this->assertTrue($rut1->equals($rut2));
|
||||
$this->assertFalse($rut1->equals($rut3));
|
||||
}
|
||||
|
||||
public static function testItCreatesARut(): void
|
||||
#[Test]
|
||||
public function it_creates_with_no_verifier(): void
|
||||
{
|
||||
$rut = Rut::create(22_457_309);
|
||||
self::assertSame(22_457_309, $rut->getNumber());
|
||||
$this->assertSame(22_457_309, $rut->number);
|
||||
}
|
||||
|
||||
public static function testItCanFormatRut(): void
|
||||
#[Test]
|
||||
public function it_formats(): void
|
||||
{
|
||||
$rut = (string) Rut::create(22_457_309)->format();
|
||||
self::assertSame('22457309K', $rut);
|
||||
$rut = Rut::create(22_457_309);
|
||||
$this->assertSame('22457309K', $rut->toString());
|
||||
$this->assertSame('22457309-K', $rut->toSimple());
|
||||
$this->assertSame('22.457.309-K', $rut->toHuman());
|
||||
$this->assertSame('7309', $rut->last(4));
|
||||
$this->assertSame('****7309', $rut->last(4, '*'));
|
||||
$this->assertSame('2245', $rut->first(4));
|
||||
$this->assertSame('2245****', $rut->first(4, '*'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array[]
|
||||
*/
|
||||
public function getRutDataset(): array
|
||||
public static function getParseData(): array
|
||||
{
|
||||
return [
|
||||
['16.894.365-2', 16_894_365, '2'],
|
||||
['24 736.7322', 24_736_732, '2'],
|
||||
[' 24 232.. 442 -- 0', 24_232_442, '0'],
|
||||
['35323325', 3_532_332, '5'],
|
||||
['22.457.309K', 22_457_309, 'K'],
|
||||
['15450088K', 15450088, 'K']
|
||||
['16.894.365-2', 16_894_365, Verifier::Two],
|
||||
['24 736.7322', 24_736_732, Verifier::Two],
|
||||
[' 24 232.. 442 -- 0', 24_232_442, Verifier::Zero],
|
||||
['35323325', 3_532_332, Verifier::Five],
|
||||
['22.457.309K', 22_457_309, Verifier::K],
|
||||
['15450088K', 15_450_088, Verifier::K],
|
||||
];
|
||||
}
|
||||
|
||||
public static function getParseWithErrorData(): array
|
||||
{
|
||||
return [
|
||||
['212321312321-1', 'El RUT numero 212321312321 es mayor a 99.999.999'],
|
||||
['23.232.123-K', 'El digito verificador K no es valido para el rut 23232123'],
|
||||
['12.2324.232-P', 'Encontrado un digito verificador invalido con valor P'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user