Skip to content
This repository was archived by the owner on Nov 15, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<!-- Indicando qual é o diretório onde as classes de teste se encontram -->
<testsuites>
<testsuite name="Orçamentos Coderockr">
<directory suffix=".php">src/</directory>
<directory suffix=".php">tests/</directory>
</testsuite>
</testsuites>

Expand Down
37 changes: 37 additions & 0 deletions src/Orcamentos/Filter/CNPJMask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Orcamentos\Filter;

/**
* Class CNPJMask
* @package Orcamentos\Filter
*/
class CNPJMask
{
/**
* @param $cnpj
* @return int
*/
public function removeMask($cnpj)
{
return (int)preg_replace("/\D/", '', $cnpj);
}

/**
* @param $cnpj
* @return string
*/
public function applyMask($cnpj)
{
// asserting that only digits will be present
$cnpj = $this->removeMask($cnpj);

// applying 14 digits
$cnpj = str_pad($cnpj, 14, '0', STR_PAD_LEFT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argument 1 (input) is int but \str_pad() takes string


$pattern = "/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/";
$replacement = "\$1.\$2.\$3/\$4-\$5";

return (string)preg_replace($pattern, $replacement, $cnpj);
}
}
7 changes: 6 additions & 1 deletion src/Orcamentos/Model/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Doctrine\ORM\Mapping as ORM;
use DateTime;
use Orcamentos\Filter\CNPJMask;

/**
* @ORM\Entity
Expand Down Expand Up @@ -91,11 +92,15 @@ public function setName($name)

public function getCnpj()
{
return $this->cnpj;
$cnpjMask = new CNPJMask();
return $cnpjMask->applyMask($this->cnpj);
}

public function setCnpj($cnpj)
{
$cnpjMask = new CNPJMask();
$cnpj = $cnpjMask->removeMask($cnpj);

return $this->cnpj = $cnpj;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Orcamentos/Test/ApplicationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Orcamentos\Test;

use Mockery as m;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;

class ApplicationTestCase extends PHPUnit_Framework_TestCase
class ApplicationTestCase extends TestCase
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class extends undeclared class \PHPUnit\Framework\TestCase

{
protected $repositoryMock;
protected $emMock;
Expand Down
69 changes: 69 additions & 0 deletions tests/src/Orcamentos/Filter/CNPJMaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Orcamentos\Service;

use Orcamentos\Filter\CNPJMask;
use PHPUnit\Framework\TestCase;

/**
* Class CNPJMaskTest
* @package Orcamentos\Service
*/
class CNPJMaskTest extends TestCase
{
public function dataForAddMask()
{
return array(
array(
'expected' => '18.915.219/0001-86',
'actual' => '18915219000186'
),
array(
'expected' => '09.376.674/0001-60',
'actual' => '09376674000160'
),
array(
'expected' => '09.376.674/0001-60',
'actual' => 9376674000160
)
);
}

public function dataForRemoveMask()
{
return array(
array(
'actual' => '18.915.219/0001-86',
'expected' => 18915219000186
),
array(
'actual' => '09.376.674/0001-60',
'expected' => 9376674000160
)
);
}

/**
* @dataProvider dataForRemoveMask
*/
public function testRemoveMask($actual, $expected)
{
$filter = new CNPJMask();

$cnpj = $filter->removeMask($actual);

$this->assertEquals($expected, $cnpj);
}

/**
* @dataProvider dataForRemoveMask
*/
public function testApplyMask($expected, $actual)
{
$filter = new CNPJMask();

$cnpj = $filter->applyMask($actual);

$this->assertEquals($expected, $cnpj);
}
}