You are here

class BrazilianIdsServiceTest in Brazilian IDs 8

@coversDefaultClass \Drupal\brazilian_ids\BrazilianIdsService @group brazilian_ids

Hierarchy

Expanded class hierarchy of BrazilianIdsServiceTest

File

tests/src/Unit/BrazilianIdsServiceTest.php, line 12

Namespace

Drupal\Tests\brazilian_ids\Unit
View source
class BrazilianIdsServiceTest extends UnitTestCase {
  private $service;

  /**
   * Tests cleaning a CPF numbers.
   */
  public function testCleanCpf() {
    $this
      ->assertEquals('29979245883', $this->service
      ->clean('299.792.458-83'));
  }

  /**
   * Tests cleaning a CNPJ numbers.
   */
  public function testCleanCnpj() {
    $this
      ->assertEquals('27491687000161', $this->service
      ->clean('27.491.687/0001-61'));
  }

  /**
   * Tests cleaning a RG numbers.
   */
  public function testCleanRg() {
    $this
      ->assertEquals('275353369', $this->service
      ->clean('27.535.336-9'));
    $this
      ->assertEquals('27535336X', $this->service
      ->clean('27.535.336-X'));
    $this
      ->assertEquals('27535336x', $this->service
      ->clean('27.535.336-x'));
  }

  /**
   * Tests CPF validation.
   */
  public function testValidateCpf() {

    // Valid CPF numbers.
    $this
      ->assertTrue($this->service
      ->validateCpf('29979245883'));
    $this
      ->assertTrue($this->service
      ->validateCpf('81684990009'));
    $this
      ->assertTrue($this->service
      ->validateCpf('82523167000'));
    $this
      ->assertTrue($this->service
      ->validateCpf('08219249072'));

    // Invalid varification digits.
    $this
      ->assertFalse($this->service
      ->validateCpf('29979245873'));
    $this
      ->assertFalse($this->service
      ->validateCpf('29979245881'));
    $this
      ->assertFalse($this->service
      ->validateCpf('29979245803'));
    $this
      ->assertFalse($this->service
      ->validateCpf('29979245880'));
    $this
      ->assertFalse($this->service
      ->validateCpf('29979245800'));

    // Less than 11 digits.
    $this
      ->assertFalse($this->service
      ->validateCpf('2997924588'));

    // More than 11 digits.
    $this
      ->assertFalse($this->service
      ->validateCpf('299792458833'));

    // CPF numbers using just one digit must be false.
    $this
      ->assertFalse($this->service
      ->validateCpf('00000000000'));
    $this
      ->assertFalse($this->service
      ->validateCpf('11111111111'));
    $this
      ->assertFalse($this->service
      ->validateCpf('22222222222'));
    $this
      ->assertFalse($this->service
      ->validateCpf('33333333333'));
    $this
      ->assertFalse($this->service
      ->validateCpf('44444444444'));
    $this
      ->assertFalse($this->service
      ->validateCpf('55555555555'));
    $this
      ->assertFalse($this->service
      ->validateCpf('66666666666'));
    $this
      ->assertFalse($this->service
      ->validateCpf('77777777777'));
    $this
      ->assertFalse($this->service
      ->validateCpf('88888888888'));
    $this
      ->assertFalse($this->service
      ->validateCpf('99999999999'));
  }

  /**
   * Tests CNPJ validation.
   */
  public function testValidateCnpj() {

    // Valid CNPJ numbers.
    $this
      ->assertTrue($this->service
      ->validateCnpj('45598438000151'));
    $this
      ->assertTrue($this->service
      ->validateCnpj('46264168000105'));
    $this
      ->assertTrue($this->service
      ->validateCnpj('50232337000100'));
    $this
      ->assertTrue($this->service
      ->validateCnpj('09753778000146'));

    // Invalid varification digits.
    $this
      ->assertFalse($this->service
      ->validateCnpj('45598438000161'));
    $this
      ->assertFalse($this->service
      ->validateCnpj('45598438000152'));
    $this
      ->assertFalse($this->service
      ->validateCnpj('45598438000101'));
    $this
      ->assertFalse($this->service
      ->validateCnpj('45598438000150'));
    $this
      ->assertFalse($this->service
      ->validateCnpj('45598438000100'));

    // Less than 14 digits.
    $this
      ->assertFalse($this->service
      ->validateCnpj('4559843800015'));

    // More than 14 digits.
    $this
      ->assertFalse($this->service
      ->validateCnpj('455984380001511'));

    // CNPJ numbers using just one digit must be false.
    $this
      ->assertFalse($this->service
      ->validateCnpj('00000000000000'));
    $this
      ->assertFalse($this->service
      ->validateCnpj('11111111111111'));
    $this
      ->assertFalse($this->service
      ->validateCnpj('22222222222222'));
    $this
      ->assertFalse($this->service
      ->validateCnpj('33333333333333'));
    $this
      ->assertFalse($this->service
      ->validateCnpj('44444444444444'));
    $this
      ->assertFalse($this->service
      ->validateCnpj('55555555555555'));
    $this
      ->assertFalse($this->service
      ->validateCnpj('66666666666666'));
    $this
      ->assertFalse($this->service
      ->validateCnpj('77777777777777'));
    $this
      ->assertFalse($this->service
      ->validateCnpj('88888888888888'));
    $this
      ->assertFalse($this->service
      ->validateCnpj('99999999999999'));
  }

  /**
   * Tests validation of CPF or CNPJ depending on value's length.
   */
  public function testValidateCpfCnpj() {

    // Valid CPF number.
    $this
      ->assertTrue($this->service
      ->validateCpfCnpj('29979245883'));

    // Valid CNPJ number.
    $this
      ->assertTrue($this->service
      ->validateCpfCnpj('45598438000151'));

    // Invalid CPF number.
    $this
      ->assertFalse($this->service
      ->validateCpfCnpj('29979245881'));

    // Invalid CNPJ number.
    $this
      ->assertFalse($this->service
      ->validateCpfCnpj('45598438000150'));

    // Numbers that do not have either 11 or 14 digits.
    $this
      ->assertFalse($this->service
      ->validateCpfCnpj('2997924588'));
    $this
      ->assertFalse($this->service
      ->validateCpfCnpj('299792458833'));
    $this
      ->assertFalse($this->service
      ->validateCpfCnpj('455984380001512'));
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $this->service = new BrazilianIdsService();
    $string_translation = $this
      ->getStringTranslationStub();
    $this->service
      ->setStringTranslation($string_translation);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BrazilianIdsServiceTest::$service private property
BrazilianIdsServiceTest::setUp public function Overrides UnitTestCase::setUp
BrazilianIdsServiceTest::testCleanCnpj public function Tests cleaning a CNPJ numbers.
BrazilianIdsServiceTest::testCleanCpf public function Tests cleaning a CPF numbers.
BrazilianIdsServiceTest::testCleanRg public function Tests cleaning a RG numbers.
BrazilianIdsServiceTest::testValidateCnpj public function Tests CNPJ validation.
BrazilianIdsServiceTest::testValidateCpf public function Tests CPF validation.
BrazilianIdsServiceTest::testValidateCpfCnpj public function Tests validation of CPF or CNPJ depending on value's length.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.