[BC] New Version
This version simplifies the api a lot, eliminating unnecesary complexity and reducing the library to a few classes only.
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the MNC\ChileanRut library.
|
||||
*
|
||||
* (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\Tests\Bridge\Symfony\Form;
|
||||
|
||||
use MNC\ChileanRut\Bridge\Symfony\Form\RutType;
|
||||
use MNC\ChileanRut\Rut;
|
||||
use Symfony\Component\Form\Test\TypeTestCase;
|
||||
|
||||
class RutTypeTest extends TypeTestCase
|
||||
{
|
||||
public function testSubmitValidData()
|
||||
{
|
||||
$objectToCompare = new Rut('16.894.365-2');
|
||||
|
||||
// $objectToCompare will retrieve data from the form submission; pass it as the second argument
|
||||
$form = $this->factory->create(RutType::class);
|
||||
|
||||
// submit the data to the form directly
|
||||
$form->submit('16.894.365-2');
|
||||
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
|
||||
$formData = $form->getData();
|
||||
|
||||
// check that $objectToCompare was modified as expected when the form was submitted
|
||||
$this->assertInstanceOf(Rut::class, $formData);
|
||||
$this->assertTrue($formData->isEqualTo($objectToCompare));
|
||||
|
||||
$view = $form->createView();
|
||||
|
||||
$this->assertSame('16.894.365-2', $view->vars['value']);
|
||||
}
|
||||
}
|
||||
51
tests/Doctrine/NumericRutTypeTest.php
Normal file
51
tests/Doctrine/NumericRutTypeTest.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace MNC\ChileanRut\Doctrine;
|
||||
|
||||
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
||||
use Doctrine\DBAL\Types\ConversionException;
|
||||
use MNC\ChileanRut\Rut;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class NumericRutTypeTest extends TestCase
|
||||
{
|
||||
public function testItConvertsNullFromDatabaseValue(): void
|
||||
{
|
||||
$platform = $this->createMock(AbstractPlatform::class);
|
||||
$type = new NumericRutType();
|
||||
$result = $type->convertToPHPValue(null, $platform);
|
||||
self::assertNull($result);
|
||||
}
|
||||
|
||||
public function testItConvertsStringFromDatabaseValue(): void
|
||||
{
|
||||
$platform = $this->createMock(AbstractPlatform::class);
|
||||
$type = new NumericRutType();
|
||||
$result = $type->convertToPHPValue(16894365, $platform);
|
||||
self::assertInstanceOf(Rut::class, $result);
|
||||
}
|
||||
|
||||
public function testItConvertsNullToDatabaseValue(): void
|
||||
{
|
||||
$platform = $this->createMock(AbstractPlatform::class);
|
||||
$type = new NumericRutType();
|
||||
$result = $type->convertToDatabaseValue(null, $platform);
|
||||
self::assertNull($result);
|
||||
}
|
||||
|
||||
public function testItConvertsRutToDatabaseValue(): void
|
||||
{
|
||||
$platform = $this->createMock(AbstractPlatform::class);
|
||||
$type = new NumericRutType();
|
||||
$result = $type->convertToDatabaseValue(Rut::parse('168943652'), $platform);
|
||||
self::assertSame('16894365', $result);
|
||||
}
|
||||
|
||||
public function testItCannotConvertToDatabaseValue(): void
|
||||
{
|
||||
$platform = $this->createMock(AbstractPlatform::class);
|
||||
$type = new NumericRutType();
|
||||
$this->expectException(ConversionException::class);
|
||||
$type->convertToDatabaseValue(new \DateTime(), $platform);
|
||||
}
|
||||
}
|
||||
59
tests/Doctrine/RutTypeTest.php
Normal file
59
tests/Doctrine/RutTypeTest.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace MNC\ChileanRut\Doctrine;
|
||||
|
||||
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
||||
use Doctrine\DBAL\Types\ConversionException;
|
||||
use MNC\ChileanRut\Rut;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RutTypeTest extends TestCase
|
||||
{
|
||||
public function testItConvertsNullFromDatabaseValue(): void
|
||||
{
|
||||
$platform = $this->createMock(AbstractPlatform::class);
|
||||
$type = new RutType();
|
||||
$result = $type->convertToPHPValue(null, $platform);
|
||||
self::assertNull($result);
|
||||
}
|
||||
|
||||
public function testItConvertsStringFromDatabaseValue(): void
|
||||
{
|
||||
$platform = $this->createMock(AbstractPlatform::class);
|
||||
$type = new RutType();
|
||||
$result = $type->convertToPHPValue('16.894.365-2', $platform);
|
||||
self::assertInstanceOf(Rut::class, $result);
|
||||
}
|
||||
|
||||
public function testItCannotConvertFromDatabaseValue(): void
|
||||
{
|
||||
$platform = $this->createMock(AbstractPlatform::class);
|
||||
$type = new RutType();
|
||||
$this->expectException(ConversionException::class);
|
||||
$type->convertToPHPValue(true, $platform);
|
||||
}
|
||||
|
||||
public function testItConvertsNullToDatabaseValue(): void
|
||||
{
|
||||
$platform = $this->createMock(AbstractPlatform::class);
|
||||
$type = new RutType();
|
||||
$result = $type->convertToDatabaseValue(null, $platform);
|
||||
self::assertNull($result);
|
||||
}
|
||||
|
||||
public function testItConvertsRutToDatabaseValue(): void
|
||||
{
|
||||
$platform = $this->createMock(AbstractPlatform::class);
|
||||
$type = new RutType();
|
||||
$result = $type->convertToDatabaseValue(Rut::parse('168943652'), $platform);
|
||||
self::assertSame('16.894.365-2', $result);
|
||||
}
|
||||
|
||||
public function testItCannotConvertToDatabaseValue(): void
|
||||
{
|
||||
$platform = $this->createMock(AbstractPlatform::class);
|
||||
$type = new RutType();
|
||||
$this->expectException(ConversionException::class);
|
||||
$type->convertToDatabaseValue(new \DateTime(), $platform);
|
||||
}
|
||||
}
|
||||
54
tests/FormattedRutTest.php
Normal file
54
tests/FormattedRutTest.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?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,82 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the MNC\ChileanRut library.
|
||||
*
|
||||
* (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\Tests\Rut;
|
||||
|
||||
use MNC\ChileanRut\Exception\InvalidRutException;
|
||||
use MNC\ChileanRut\Rut;
|
||||
use MNC\ChileanRut\Validator\Module11RutValidator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RutTest extends TestCase
|
||||
{
|
||||
public function testThatRutIsSanitizedProperlyOnInstantiation()
|
||||
{
|
||||
$rut = Rut::fromString('16.894.365-2');
|
||||
|
||||
$this->assertSame('16894365', $rut->getCorrelative());
|
||||
$this->assertSame('2', $rut->getVerifierDigit());
|
||||
}
|
||||
|
||||
public function testThatRutsInstantiatedDifferentFormatButWithEqualValueAreIndeedEqual()
|
||||
{
|
||||
$rut1 = new Rut('16.894.365-2');
|
||||
$rut2 = new Rut('16894365-2');
|
||||
$this->assertTrue($rut1->isEqualTo($rut2));
|
||||
}
|
||||
|
||||
public function testThatFormatClearWorks()
|
||||
{
|
||||
$rut = new Rut('16.894.365-2');
|
||||
$this->assertSame('168943652', $rut->format(Rut::FORMAT_CLEAR));
|
||||
}
|
||||
|
||||
public function testThatFormatWithHyphenWorks()
|
||||
{
|
||||
$rut = new Rut('16.894.365-2');
|
||||
$this->assertSame('16894365-2', $rut->format(Rut::FORMAT_HYPHENED));
|
||||
}
|
||||
|
||||
public function testThatFormatReadableWorks()
|
||||
{
|
||||
$rut = new Rut('168943652');
|
||||
$this->assertSame('16.894.365-2', $rut->format(Rut::FORMAT_READABLE));
|
||||
}
|
||||
|
||||
public function testThatFormatHiddenWorks()
|
||||
{
|
||||
$rut = new Rut('168943652');
|
||||
$this->assertSame('16.***.***-2', $rut->format(Rut::FORMAT_HIDDEN));
|
||||
}
|
||||
|
||||
public function testThatIntegratedValidationThrowsExceptionOnInvalidRut()
|
||||
{
|
||||
$this->expectException(InvalidRutException::class);
|
||||
|
||||
$validator = new Module11RutValidator();
|
||||
$rut = new Rut('4444444-2', $validator);
|
||||
}
|
||||
|
||||
public function testThatIntegratedValidationDoesNotThrowExceptionOnValidRut()
|
||||
{
|
||||
$validator = new Module11RutValidator();
|
||||
$rut = new Rut('16.894.365-2', $validator);
|
||||
|
||||
$this->assertInstanceOf(Rut::class, $rut);
|
||||
}
|
||||
|
||||
public function testInvalidFormatValueRaisesException()
|
||||
{
|
||||
$rut = new Rut('16.894.365-2');
|
||||
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$rut->format(23);
|
||||
}
|
||||
}
|
||||
86
tests/RutTest.php
Normal file
86
tests/RutTest.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the MNC\ChileanRut library.
|
||||
*
|
||||
* (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;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Class RutTest
|
||||
* @package MNC\ChileanRut\Tests\Rut
|
||||
*/
|
||||
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
|
||||
{
|
||||
$rut = Rut::parse($raw);
|
||||
self::assertSame($expectedNumber, $rut->getNumber());
|
||||
self::assertSame($expectedVerifier, $rut->getVerifier());
|
||||
}
|
||||
|
||||
public function testItDetectsOutOfRangeVerifier(): void
|
||||
{
|
||||
$this->expectException(InvalidRut::class);
|
||||
Rut::parse('16894365F');
|
||||
}
|
||||
|
||||
public function testItDetectsInvalidVerifier(): void
|
||||
{
|
||||
$this->expectException(InvalidRut::class);
|
||||
Rut::parse('16894365K');
|
||||
}
|
||||
|
||||
public function testItDetectsRutTooBig(): void
|
||||
{
|
||||
$this->expectException(InvalidRut::class);
|
||||
Rut::create(3_355_535_353);
|
||||
}
|
||||
|
||||
public function testItComparesToEqual(): 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));
|
||||
}
|
||||
|
||||
public static function testItCreatesARut(): void
|
||||
{
|
||||
$rut = Rut::create(22_457_309);
|
||||
self::assertSame(22_457_309, $rut->getNumber());
|
||||
}
|
||||
|
||||
public static function testItCanFormatRut(): void
|
||||
{
|
||||
$rut = (string) Rut::create(22_457_309)->format();
|
||||
self::assertSame('22457309K', $rut);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array[]
|
||||
*/
|
||||
public function getRutDataset(): 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']
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the MNC\ChileanRut library.
|
||||
*
|
||||
* (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\Tests\Validator;
|
||||
|
||||
use MNC\ChileanRut\Exception\InvalidRutException;
|
||||
use MNC\ChileanRut\Util\CorrelativeUtils;
|
||||
use MNC\ChileanRut\Validator\ChainRutValidator;
|
||||
use MNC\ChileanRut\Validator\Module11RutValidator;
|
||||
use MNC\ChileanRut\Validator\RutValidator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Class ChainRutValidatorTest.
|
||||
*/
|
||||
class ChainRutValidatorTest extends TestCase
|
||||
{
|
||||
public function testThatChainValidatorFails(): void
|
||||
{
|
||||
$rut = CorrelativeUtils::autoGenerateValidRut();
|
||||
|
||||
$normalValidator = new Module11RutValidator();
|
||||
$mockValidator = $this->createMock(RutValidator::class);
|
||||
$mockValidator->expects($this->once())
|
||||
->method('validate')
|
||||
->willThrowException(new InvalidRutException($rut));
|
||||
|
||||
$chainValidator = new ChainRutValidator($normalValidator, $mockValidator);
|
||||
|
||||
$this->expectException(InvalidRutException::class);
|
||||
$chainValidator->validate($rut);
|
||||
}
|
||||
|
||||
public function testThatChainValidatorPasses(): void
|
||||
{
|
||||
$rut = CorrelativeUtils::autoGenerateValidRut();
|
||||
|
||||
$normalValidator = new Module11RutValidator();
|
||||
$mockValidator = $this->createMock(RutValidator::class);
|
||||
$mockValidator->expects($this->once())
|
||||
->method('validate')
|
||||
->willReturn(null);
|
||||
|
||||
$chainValidator = new ChainRutValidator($normalValidator, $mockValidator);
|
||||
|
||||
$chainValidator->validate($rut);
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the MNC\ChileanRut library.
|
||||
*
|
||||
* (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\Tests\Validator;
|
||||
|
||||
use MNC\ChileanRut\Exception\InvalidRutException;
|
||||
use MNC\ChileanRut\Rut;
|
||||
use MNC\ChileanRut\Validator\Module11RutValidator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SimpleRutValidatorTest extends TestCase
|
||||
{
|
||||
public function testValidationPassesOnValidRut()
|
||||
{
|
||||
$rut = new Rut('16.894.365-2');
|
||||
$validator = new Module11RutValidator();
|
||||
|
||||
$validator->validate($rut);
|
||||
|
||||
$this->assertInstanceOf(Rut::class, $rut);
|
||||
}
|
||||
|
||||
public function testValidationFailsOnInvalidRut()
|
||||
{
|
||||
$this->expectException(InvalidRutException::class);
|
||||
|
||||
$rut = new Rut('34.4534.353-1');
|
||||
$validator = new Module11RutValidator();
|
||||
|
||||
$validator->validate($rut);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user