feat: new v4 version (#5)
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace MNC\ChileanRut;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Class FormattedRutTest
|
||||
* @package MNC\ChileanRut
|
||||
*/
|
||||
class FormattedRutTest extends TestCase
|
||||
{
|
||||
public function testItFormatsHyphened(): void
|
||||
{
|
||||
$formatted = (string) Rut::parse('168943652')->format()->hyphened();
|
||||
self::assertSame('16894365-2', $formatted);
|
||||
}
|
||||
|
||||
public function testItFormatsDotted(): void
|
||||
{
|
||||
$formatted = (string) Rut::parse('168943652')->format()->dotted();
|
||||
self::assertSame('16.894.3652', $formatted);
|
||||
}
|
||||
|
||||
public function testItFormatsHyphenedAndDotted(): void
|
||||
{
|
||||
$formatted = (string) Rut::parse('168943652')->format()->hyphened()->dotted();
|
||||
self::assertSame('16.894.365-2', $formatted);
|
||||
}
|
||||
|
||||
public function testItFormatsObfuscated(): void
|
||||
{
|
||||
$formatted = (string) Rut::parse('168943652')->format()->obfuscated();
|
||||
self::assertSame('*****3652', $formatted);
|
||||
}
|
||||
|
||||
public function testItFormatsObfuscatedAndHyphened(): void
|
||||
{
|
||||
$formatted = (string) Rut::parse('168943652')->format()->hyphened()->obfuscated();
|
||||
self::assertSame('*****365-2', $formatted);
|
||||
}
|
||||
|
||||
public function testItFormatsObfuscatedAndDotted(): void
|
||||
{
|
||||
$formatted = (string) Rut::parse('168943652')->format()->obfuscated()->dotted();
|
||||
self::assertSame('**.***.3652', $formatted);
|
||||
}
|
||||
|
||||
public function testItFormatsWithAll(): void
|
||||
{
|
||||
$formatted = (string) Rut::parse('168943652')->format()->hyphened()->dotted()->obfuscated();
|
||||
self::assertSame('**.***.365-2', $formatted);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace MNC\ChileanRut\Doctrine;
|
||||
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
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace MNC\Rut\Doctrine;
|
||||
|
||||
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
||||
use Doctrine\DBAL\Types\ConversionException;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use MNC\ChileanRut\Rut;
|
||||
use MNC\Rut;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[CoversClass(NumericRutType::class)]
|
||||
#[CoversClass(Rut::class)]
|
||||
#[CoversClass(Rut\Verifier::class)]
|
||||
class NumericRutTypeTest extends TestCase
|
||||
{
|
||||
public static function setUpBeforeClass(): void
|
||||
@@ -54,4 +73,11 @@ class NumericRutTypeTest extends TestCase
|
||||
$this->expectException(ConversionException::class);
|
||||
$type->convertToDatabaseValue(new \DateTime(), $platform);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_true_for_comment(): void
|
||||
{
|
||||
$platform = $this->createMock(AbstractPlatform::class);
|
||||
$this->assertTrue(Type::getType(NumericRutType::NAME)->requiresSQLCommentHint($platform));
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace MNC\ChileanRut\Doctrine;
|
||||
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
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace MNC\Rut\Doctrine;
|
||||
|
||||
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
||||
use Doctrine\DBAL\Types\ConversionException;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use MNC\ChileanRut\Rut;
|
||||
use MNC\Rut;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[CoversClass(RutType::class)]
|
||||
#[CoversClass(Rut::class)]
|
||||
#[CoversClass(Rut\Verifier::class)]
|
||||
class RutTypeTest extends TestCase
|
||||
{
|
||||
public static function setUpBeforeClass(): void
|
||||
@@ -62,4 +81,11 @@ class RutTypeTest extends TestCase
|
||||
$this->expectException(ConversionException::class);
|
||||
$type->convertToDatabaseValue(new \DateTime(), $platform);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_true_for_comment(): void
|
||||
{
|
||||
$platform = $this->createMock(AbstractPlatform::class);
|
||||
$this->assertTrue(Type::getType(RutType::NAME)->requiresSQLCommentHint($platform));
|
||||
}
|
||||
}
|
||||
@@ -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