-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ComprobanteMetodoPago Validator with tests. (#12)
* Added ComprobanteMetodoPago Validator with tests. * Corrected order imports
- Loading branch information
1 parent
d9d87f6
commit e6164fc
Showing
2 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/CfdiUtils/Validate/Cfdi33/Standard/ComprobanteMetodoPago.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
namespace CfdiUtils\Validate\Cfdi33\Standard; | ||
|
||
use CfdiUtils\Nodes\NodeInterface; | ||
use CfdiUtils\Validate\Asserts; | ||
use CfdiUtils\Validate\Cfdi33\Abstracts\AbstractDiscoverableVersion33; | ||
use CfdiUtils\Validate\Status; | ||
|
||
/** | ||
* ComprobanteMetodoPago | ||
* | ||
* Valida que: | ||
* - METPAG01: Si el tipo de documento es T, P ó N, el metodo de pago no debe existir(CFDI33123, CFDI33124) | ||
* - METPAG02: Si el tipo de documento es I ó E, el metodo de pago debe ser "PUE" o "PPD (CFDI33121, CFDI33122) | ||
*/ | ||
class ComprobanteMetodoPago extends AbstractDiscoverableVersion33 | ||
{ | ||
private function registerAsserts(Asserts $asserts) | ||
{ | ||
$assertDescriptions = [ | ||
'METPAG01' => 'Si el tipo de documento es T, P ó N, el metodo de pago' | ||
. ' no debe existir(CFDI33123, CFDI33124)', | ||
'METPAG02' => 'Si el tipo de documento es I ó E, el metodo de pago' | ||
. 'debe ser "PUE" o "PPD" (CFDI33121, CFDI33122)', | ||
]; | ||
foreach ($assertDescriptions as $code => $title) { | ||
$asserts->put($code, $title); | ||
} | ||
} | ||
|
||
public function validate(NodeInterface $comprobante, Asserts $asserts) | ||
{ | ||
$this->registerAsserts($asserts); | ||
$tipoDeComprobante = $comprobante['TipoDeComprobante']; | ||
|
||
if ('T' === $tipoDeComprobante || 'P' === $tipoDeComprobante || 'N' === $tipoDeComprobante) { | ||
$asserts->putStatus( | ||
'METPAG01', | ||
Status::when(! isset($comprobante['MetodoPago'])) | ||
); | ||
} | ||
|
||
if ('I' === $tipoDeComprobante || 'E' === $tipoDeComprobante) { | ||
$allowedMetodoPago = ['PUE', 'PPD']; | ||
$asserts->putStatus( | ||
'METPAG02', | ||
Status::when(in_array($comprobante['MetodoPago'], $allowedMetodoPago, true)) | ||
); | ||
} | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
tests/CfdiUtilsTests/Validate/Cfdi33/Standard/ComprobanteMetodoPagoTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
namespace CfdiUtilsTests\Validate\Cfdi33\Standard; | ||
|
||
use CfdiUtils\Validate\Cfdi33\Standard\ComprobanteMetodoPago; | ||
use CfdiUtils\Validate\Status; | ||
use CfdiUtilsTests\Validate\ValidateTestCase; | ||
|
||
class ComprobanteMetodoPagoTest extends ValidateTestCase | ||
{ | ||
/** @var ComprobanteMetodoPago */ | ||
protected $validator; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->validator = new ComprobanteMetodoPago(); | ||
} | ||
|
||
public function providerValidCases() | ||
{ | ||
return[ | ||
['T', null, 'METPAG01'], | ||
['P', null, 'METPAG01'], | ||
['N', null, 'METPAG01'], | ||
['I', 'PUE', 'METPAG02'], | ||
['I', 'PPD', 'METPAG02'], | ||
['E', 'PUE', 'METPAG02'], | ||
['I', 'PPD', 'METPAG02'], | ||
]; | ||
} | ||
|
||
/** | ||
* @param $tipoDeComprobante | ||
* @param $metodoDePago | ||
* @param $ok | ||
* @dataProvider providerValidCases | ||
*/ | ||
public function testValidCases($tipoDeComprobante, $metodoDePago, $ok) | ||
{ | ||
$this->comprobante->addAttributes([ | ||
'TipoDeComprobante' => $tipoDeComprobante, | ||
'MetodoPago' => $metodoDePago, | ||
]); | ||
$this->runValidate(); | ||
$this->assertFalse($this->asserts->hasErrors()); | ||
$this->assertStatusEqualsCode(Status::ok(), $ok); | ||
} | ||
|
||
public function providerInvalidCases() | ||
{ | ||
return[ | ||
['T', 'PUE', 'METPAG01'], | ||
['T', '', 'METPAG01'], | ||
['P', 'PUE', 'METPAG01'], | ||
['P', '', 'METPAG01'], | ||
['N', 'PUE', 'METPAG01'], | ||
['N', '', 'METPAG01'], | ||
['I', null, 'METPAG02'], | ||
['I', null, 'METPAG02'], | ||
['E', 'XXX', 'METPAG02'], | ||
['I', 'XXX', 'METPAG02'], | ||
]; | ||
} | ||
|
||
/** | ||
* @param $tipoDeComprobante | ||
* @param $metodoDePago | ||
* @param $error | ||
* @dataProvider providerInvalidCases | ||
*/ | ||
public function testInvalidCases($tipoDeComprobante, $metodoDePago, $error) | ||
{ | ||
$this->comprobante->addAttributes([ | ||
'TipoDeComprobante' => $tipoDeComprobante, | ||
'MetodoPago' => $metodoDePago, | ||
]); | ||
$this->runValidate(); | ||
$this->assertTrue($this->asserts->hasErrors()); | ||
$this->assertStatusEqualsCode(Status::error(), $error); | ||
} | ||
|
||
public function providerNoneCases() | ||
{ | ||
return [ | ||
[null, ''], | ||
['', ''], | ||
['X', ''], | ||
]; | ||
} | ||
/** | ||
* @param $tipoDeComprobante | ||
* @param $metodoDePago | ||
* @dataProvider providerNoneCases | ||
*/ | ||
public function testNoneCases($tipoDeComprobante, $metodoDePago) | ||
{ | ||
$this->comprobante->addAttributes([ | ||
'TipoDeComprobante' => $tipoDeComprobante, | ||
'MetodoPago' => $metodoDePago, | ||
]); | ||
$this->runValidate(); | ||
$this->assertFalse($this->asserts->hasErrors()); | ||
$this->assertCount(2, $this->asserts->byStatus(Status::none())); | ||
} | ||
} |