Skip to content

Commit

Permalink
Merge pull request #1057 from nfephp-org/testes
Browse files Browse the repository at this point in the history
Testes dos métodos sefazEPP, sefazECPP, sefazCancela e sefazCancelaPorSubstituicao
  • Loading branch information
gersonfs committed Jun 3, 2024
2 parents 1108719 + 07353bd commit 4597985
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 27 deletions.
99 changes: 72 additions & 27 deletions src/Tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,9 @@ public function sefazEPP(
string $nProt,
array $itens = [],
int $tipo = 1,
int $nSeqEvento = 1
int $nSeqEvento = 1,
?\DateTimeInterface $dhEvento = null,
?string $lote = null
): string {
$uf = UFList::getUFByCode((int)substr($chNFe, 0, 2));
//pedido de prorrogação primero prazo
Expand All @@ -531,7 +533,9 @@ public function sefazEPP(
$chNFe,
$tpEvento,
$nSeqEvento,
$tagAdic
$tagAdic,
$dhEvento,
$lote
);
}

Expand All @@ -546,8 +550,14 @@ public function sefazEPP(
* @return string
* @throws InvalidArgumentException
*/
public function sefazECPP(string $chave, string $nProt, int $tipo, int $nSeqEvento = 1): string
{
public function sefazECPP(
string $chave,
string $nProt,
int $tipo,
int $nSeqEvento = 1,
?\DateTimeInterface $dhEvento = null,
?string $lote = null
): string {
if (empty($chave) || empty($nProt)) {
throw new InvalidArgumentException('A chave ou o numero do protocolo estão vazios!');
}
Expand All @@ -565,7 +575,7 @@ public function sefazECPP(string $chave, string $nProt, int $tipo, int $nSeqEven
. "$idPedidoCancelado"
. "</idPedidoCancelado>"
. "<nProt>$nProt</nProt>";
return $this->sefazEvento($uf, $chave, $tpEvento, $nSeqEvento, $tagAdic);
return $this->sefazEvento($uf, $chave, $tpEvento, $nSeqEvento, $tagAdic, $dhEvento, $lote);
}

/**
Expand All @@ -576,16 +586,21 @@ public function sefazECPP(string $chave, string $nProt, int $tipo, int $nSeqEven
* @return string
* @throws InvalidArgumentException
*/
public function sefazCancela(string $chave, string $xJust, string $nProt): string
{
public function sefazCancela(
string $chave,
string $xJust,
string $nProt,
?\DateTimeInterface $dhEvento = null,
?string $lote = null
): string {
if (empty($chave) || empty($xJust) || empty($nProt)) {
throw new InvalidArgumentException('Cancelamento: chave, just ou numprot vazio!');
}
$uf = $this->validKeyByUF($chave);
$xJust = Strings::replaceUnacceptableCharacters(substr(trim($xJust), 0, 255));
$nSeqEvento = 1;
$tagAdic = "<nProt>$nProt</nProt><xJust>$xJust</xJust>";
return $this->sefazEvento($uf, $chave, self::EVT_CANCELA, $nSeqEvento, $tagAdic);
return $this->sefazEvento($uf, $chave, self::EVT_CANCELA, $nSeqEvento, $tagAdic, $dhEvento, $lote);
}

/**
Expand All @@ -603,7 +618,9 @@ public function sefazCancelaPorSubstituicao(
string $xJust,
string $nProt,
string $chNFeRef,
string $verAplic = null
string $verAplic = null,
?\DateTimeInterface $dhEvento = null,
?string $lote = null
): string {
if ($this->modelo != 65) {
throw new InvalidArgumentException(
Expand Down Expand Up @@ -634,7 +651,7 @@ public function sefazCancelaPorSubstituicao(
. "<nProt>$nProt</nProt>"
. "<xJust>$xJust</xJust>"
. "<chNFeRef>$chNFeRef</chNFeRef>";
return $this->sefazEvento($uf, $chave, self::EVT_CANCELASUBSTITUICAO, $nSeqEvento, $tagAdic);
return $this->sefazEvento($uf, $chave, self::EVT_CANCELASUBSTITUICAO, $nSeqEvento, $tagAdic, $dhEvento, $lote);
}

/**
Expand All @@ -646,8 +663,14 @@ public function sefazCancelaPorSubstituicao(
* @return string
* @throws InvalidArgumentException
*/
public function sefazManifesta(string $chave, int $tpEvento, string $xJust = '', int $nSeqEvento = 1): string
{
public function sefazManifesta(
string $chave,
int $tpEvento,
string $xJust = '',
int $nSeqEvento = 1,
?\DateTimeInterface $dhEvento = null,
?string $lote = null
): string {
if (empty($chave) || empty($tpEvento)) {
throw new InvalidArgumentException('Manifestacao: chave ou tipo de evento vazio!');
}
Expand All @@ -656,7 +679,7 @@ public function sefazManifesta(string $chave, int $tpEvento, string $xJust = '',
$xJust = Strings::replaceUnacceptableCharacters(substr(trim($xJust), 0, 255));
$tagAdic = "<xJust>$xJust</xJust>";
}
return $this->sefazEvento('AN', $chave, $tpEvento, $nSeqEvento, $tagAdic);
return $this->sefazEvento('AN', $chave, $tpEvento, $nSeqEvento, $tagAdic, $dhEvento, $lote);
}

/**
Expand All @@ -666,8 +689,11 @@ public function sefazManifesta(string $chave, int $tpEvento, string $xJust = '',
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function sefazManifestaLote(\stdClass $std): string
{
public function sefazManifestaLote(
\stdClass $std,
?\DateTimeInterface $dhEvento = null,
?string $lote = null
): string {
$allowed = [
self::EVT_CONFIRMACAO,
self::EVT_CIENCIA,
Expand Down Expand Up @@ -698,16 +724,19 @@ public function sefazManifestaLote(\stdClass $std): string
$evt->evento[$i]->tagAdic = $tagAdic;
$i++;
}
return $this->sefazEventoLote('AN', $evt);
return $this->sefazEventoLote('AN', $evt, $dhEvento, $lote);
}

/**
* Send event for delivery receipt
* @param \stdClass $std
* @return string
*/
public function sefazComprovanteEntrega(\stdClass $std): string
{
public function sefazComprovanteEntrega(
\stdClass $std,
?\DateTimeInterface $dhEvento = null,
?string $lote = null
): string {
if (empty($std->verAplic) && !empty($this->verAplic)) {
$std->verAplic = $this->verAplic;
}
Expand Down Expand Up @@ -743,7 +772,9 @@ public function sefazComprovanteEntrega(\stdClass $std): string
$std->chNFe,
$tpEvento,
$std->nSeqEvento,
$tagAdic
$tagAdic,
$dhEvento,
$lote
);
}

Expand All @@ -753,8 +784,11 @@ public function sefazComprovanteEntrega(\stdClass $std): string
* @return string
* @throws \Exception
*/
public function sefazInsucessoEntrega(\stdClass $std): string
{
public function sefazInsucessoEntrega(
\stdClass $std,
?\DateTimeInterface $dhEvento = null,
?string $lote = null
): string {
if (empty($std->verAplic) && !empty($this->verAplic)) {
$std->verAplic = $this->verAplic;
}
Expand Down Expand Up @@ -800,7 +834,9 @@ public function sefazInsucessoEntrega(\stdClass $std): string
$std->chNFe,
$tpEvento,
$std->nSeqEvento,
$tagAdic
$tagAdic,
$dhEvento,
$lote
);
}

Expand All @@ -812,8 +848,12 @@ public function sefazInsucessoEntrega(\stdClass $std): string
* @throws RuntimeException
* @throws InvalidArgumentException
*/
public function sefazEventoLote(string $uf, \stdClass $std): string
{
public function sefazEventoLote(
string $uf,
\stdClass $std,
?\DateTimeInterface $dhEvento = null,
?string $lote = null
): string {
if (empty($uf)) {
throw new InvalidArgumentException('Evento Lote: UF ou parametro "std" vazio!');
}
Expand All @@ -833,7 +873,10 @@ public function sefazEventoLote(string $uf, \stdClass $std): string
$cnpj = $this->config->cnpj;
$dt = new \DateTime('now', new \DateTimeZone($this->timezone));
$dt->setTimezone(new \DateTimeZone($this->timezone));
$dhEvento = $dt->format('Y-m-d\TH:i:sP');
$dhEventoString = $dt->format('Y-m-d\TH:i:sP');
if ($dhEvento != null) {
$dhEventoString = $dhEvento->format('Y-m-d\TH:i:sP');
}
$sSeqEvento = str_pad($evt->nSeqEvento, 2, "0", STR_PAD_LEFT);
$eventId = "ID" . $evt->tpEvento . $evt->chave . $sSeqEvento;
$cOrgao = UFList::getCodeByUF($uf);
Expand All @@ -847,7 +890,7 @@ public function sefazEventoLote(string $uf, \stdClass $std): string
$request .= "<CPF>$cnpj</CPF>";
}
$request .= "<chNFe>$evt->chave</chNFe>"
. "<dhEvento>$dhEvento</dhEvento>"
. "<dhEvento>$dhEventoString</dhEvento>"
. "<tpEvento>$evt->tpEvento</tpEvento>"
. "<nSeqEvento>$evt->nSeqEvento</nSeqEvento>"
. "<verEvento>$this->urlVersion</verEvento>"
Expand All @@ -870,7 +913,9 @@ public function sefazEventoLote(string $uf, \stdClass $std): string
}
$dt = new \DateTime('now', new \DateTimeZone($this->timezone));
$dt->setTimezone(new \DateTimeZone($this->timezone));
$lote = $dt->format('YmdHis') . random_int(0, 9);
if ($lote == null) {
$lote = $dt->format('YmdHis') . random_int(0, 9);
}
$request = "<envEvento xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">"
. "<idLote>$lote</idLote>"
. $batchRequest
Expand Down
152 changes: 152 additions & 0 deletions tests/ToolsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,158 @@ public function test_sefazAtorInteressado(): void
$this->assertSame($esperado, $request);
}

public function test_sefazEPP(): void
{
$chNFe = '35150300822602000124550010009923461099234656';
$nProt = '135150001686732';
$itens = [
[1, 111],
[2, 222],
[3, 333]
];
$dhEvento = new \DateTime('2024-02-01 14:07:05 -03:00');
$retorno = $this->tools->sefazEPP($chNFe, $nProt, $itens, 1, 1, $dhEvento, '123');
//@todo fazer mock do retorno
$request = $this->tools->getRequest();
$esperado = $this->getCleanXml(__DIR__ . '/fixtures/xml/exemplo_xml_request_sefazEPP.xml');
$this->assertSame($esperado, $request);
}

public function test_sefazECPP(): void
{
$chNFe = '35150300822602000124550010009923461099234656';
$nProt = '135150001686732';
$dhEvento = new \DateTime('2024-02-01 14:07:05 -03:00');
$retorno = $this->tools->sefazECPP($chNFe, $nProt, 1, 1, $dhEvento, '123');
//@todo fazer mock do retorno
$request = $this->tools->getRequest();
$esperado = $this->getCleanXml(__DIR__ . '/fixtures/xml/exemplo_xml_request_sefazECPP.xml');
$this->assertSame($esperado, $request);
}

public function test_sefazCancela(): void
{
$chNFe = '35150300822602000124550010009923461099234656';
$xJust = 'Preenchimento incorreto dos dados';
$dhEvento = new \DateTime('2024-02-01 14:07:05 -03:00');
$nProt = '123456789101234';
$retorno = $this->tools->sefazCancela($chNFe, $xJust, $nProt, $dhEvento, '123');
//@todo fazer mock do retorno
$request = $this->tools->getRequest();
$esperado = $this->getCleanXml(__DIR__ . '/fixtures/xml/exemplo_xml_sefazCancela.xml');
$this->assertSame($esperado, $request);
}

public function test_sefazCancelaPorSubstituicaoErroChave(): void
{
$msg = 'Cancelamento pro Substituição deve ser usado apenas para operações com modelo 65 NFCe';
$this->expectExceptionMessage($msg);
$chNFe = '35150300822602000124550010009923461099234656';
$chReferenciada = '35170705248891000181550010000011831339972127';
$xJust = 'Preenchimento incorreto dos dados';
$dhEvento = new \DateTime('2024-02-01 14:07:05 -03:00');
$nProt = '123456789101234';
$this->tools->sefazCancelaPorSubstituicao($chNFe, $xJust, $nProt, $chReferenciada, "1", $dhEvento, '123');
}

public function test_sefazCancelaPorSubstituicao(): void
{
$chNFe = '35240305730928000145650010000001421071400478';
$chReferenciada = '35240305730928000145650010000001421071400478';
$xJust = 'Preenchimento incorreto dos dados';
$dhEvento = new \DateTime('2024-02-01 14:07:05 -03:00');
$nProt = '123456789101234';
$this->tools->model(65);
$retorno = $this->tools->sefazCancelaPorSubstituicao(
$chNFe,
$xJust,
$nProt,
$chReferenciada,
"1",
$dhEvento,
'123'
);
//@todo fazer mock do retorno
$request = $this->tools->getRequest();
$esperado = $this->getCleanXml(__DIR__ . '/fixtures/xml/exemplo_xml_request_sefazCancelaPorSubstituicao.xml');
$this->assertSame($esperado, $request);
}

public function test_sefazManifesta(): void
{
$chNFe = '35240305730928000145650010000001421071400478';
$xJust = 'Preenchimento incorreto dos dados';
$dhEvento = new \DateTime('2024-02-01 14:07:05 -03:00');
$tpEvento = 210210; //ciencia da operação
$retorno = $this->tools->sefazManifesta(
$chNFe,
$tpEvento,
$xJust,
1,
$dhEvento,
'123'
);
//@todo fazer mock do retorno
$request = $this->tools->getRequest();
$esperado = $this->getCleanXml(__DIR__ . '/fixtures/xml/exemplo_xml_request_sefazManifesta.xml');
$this->assertSame($esperado, $request);
}

/*public function test_sefazManifestaLote(): void
{
$chNFe = '35240305730928000145650010000001421071400478';
$xJust = 'Preenchimento incorreto dos dados';
$dhEvento = new \DateTime('2024-02-01 14:07:05 -03:00');
$tpEvento = 210210; //ciencia da operação
$retorno = $this->tools->sefazManifestaLote(
$chNFe,
$tpEvento,
$xJust,
1,
$dhEvento,
'123'
);
//@todo fazer mock do retorno
$request = $this->tools->getRequest();
$esperado = $this->getCleanXml(__DIR__ . '/fixtures/xml/exemplo_xml_request_sefazManifesta.xml');
$this->assertSame($esperado, $request);
}
/*public function test_sefazComprovanteEntrega(): void
{
$retorno = $this->tools->sefazComprovanteEntrega();
}
public function test_sefazInsucessoEntrega(): void
{
$retorno = $this->tools->sefazInsucessoEntrega();
}
public function test_sefazEventoLote(): void
{
$retorno = $this->tools->sefazEventoLote();
}
public function test_sefazEPEC(): void
{
$retorno = $this->tools->sefazEPEC();
}
public function test_sefazDownload(): void
{
$retorno = $this->tools->sefazDownload();
}
public function test_sefazCsc(): void
{
$retorno = $this->tools->sefazCsc();
}
public function test_sefazValidate(): void
{
$retorno = $this->tools->sefazValidate();
}*/

/**
* @param string $xml
* @param int|string $idLote
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeRecepcaoEvento4"><envEvento xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.00"><idLote>123</idLote><evento xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.00"><infEvento Id="ID1101123524030573092800014565001000000142107140047801"><cOrgao>35</cOrgao><tpAmb>2</tpAmb><CNPJ>93623057000128</CNPJ><chNFe>35240305730928000145650010000001421071400478</chNFe><dhEvento>2024-02-01T14:07:05-03:00</dhEvento><tpEvento>110112</tpEvento><nSeqEvento>1</nSeqEvento><verEvento>1.00</verEvento><detEvento versao="1.00"><descEvento>Cancelamento por substituicao</descEvento><cOrgaoAutor>35</cOrgaoAutor><tpAutor>1</tpAutor><verAplic>1</verAplic><nProt>123456789101234</nProt><xJust>Preenchimento incorreto dos dados</xJust><chNFeRef>35240305730928000145650010000001421071400478</chNFeRef></detEvento></infEvento><Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/><Reference URI="#ID1101123524030573092800014565001000000142107140047801"><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><DigestValue>4jU1RfQjf5ZVhzeOTPA9RijlL4g=</DigestValue></Reference></SignedInfo><SignatureValue>XCQTGJU3yMsOGheAMalafm/Iuwx9u/OX+ffElNxCGJntAY73sGZoGB5gETnBF+ymrgFrG3cMkzPa/Lu8xpzl51ruvkaa5w2Q2t/9/fzS4guzJfkcxtgyTWI2a4M/UClOlXIFGeWu85JOQuwtsrcrk7B+g+Xt56u38KFrzQQAPr2ReF3Rtxo6F40e+aijN9rDh5PtWPdTWZuejcIG2hi4L7tp8EVkpyA1SbFgyX6zdMEMellGeiSAwMiGkcCLwB6mE1PPYNBlbalbk5hUKp9MYMbdJuVgrVuVqhymCCbiuA1IKf6uik+2+rzqjCf+zl8/T/vSDOQvGAEqxdegsxmj/tfJD+P5nrwtFz50qidquxuL/U8+PMtdBE6hACspjHryOsk4AKbbZulHwzUO8XGkKgded4eN8OJm5Bq5UZKjoJbXnt7kod+58fvKRzXVW/NDSdVW75myUZ4al7foJwvWJ7WL04uzNx5ID/XsV01zvwSTNCotxIf2SU4NGgczQSqFT07U1ERA0VMLQG8fhZsZf6FUTW/rxqgczVbEhMMuilitrsnQikXNFrkJFWvMZju52QOqt3KT3+c30wjQ6sswIvCSKFkMuL35KhH8BVEHwH8jOPoYmWH4clo923d+tHmXNSgf1tPiT50FYobO/CEHFkEvz/B7da5d0oP8z60RpGo=</SignatureValue><KeyInfo><X509Data><X509Certificate>MIIFkDCCA3gCCQD1479OxEO9njANBgkqhkiG9w0BAQsFADCBiTELMAkGA1UEBhMCQlIxGjAYBgNVBAgMEVJpbyBHcmFuZGUgZG8gU3VsMRUwEwYDVQQHDAxQb3J0byBBbGVncmUxEzARBgNVBAoMCm5mZXBocC1vcmcxEzARBgNVBAsMCm5mZXBocC1vcmcxHTAbBgNVBAMMFFNQRUQgUHJvamVjdHMgaW4gUEhQMB4XDTE4MDg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WBW00qEY6+WL2i3+gd52KUYuOhDBksi4aHaSSBVv+HyqbliDGeKDKiGtEcWr3QP1efLVV2HyqwrlL3Vy1LhhAwDjVladkr7HVchFJ7CQg53q7yZI8Rwcaf/ab9pqAwI90/EUx6dwpDjT8YbFr2xorOfKzCKUIIjzHX8WrpT7N6LEGNN17SKrQB2xULQ0aAvnuk8snnJNav/fQr21DUm0T17zgsBUKMB4JtRx3ux+S8VhKd8gwXme3wuDI+5aE1jAg5w5viFDo8nHbW/rDLHoRkt7NoE+tgbJN6Dq/+DbJV/eyA6sNYmpA/bp+mCnTsjLjfS5Vj3UZP8s+OcLirjW1QYjND4gIvOtv2W4aTcjxHdH63F6FlpgzU8vJvYIUQ2y5SqjIywoDTIxn8GDzyyhbgRYkhAk3PvHlgixiq8Ip/hGs27wbO19j/y2IRxu6m1Ag/8v1KB/5WHdvVdyEhQfeYfMH/aiq41lsFGERbBwpugFfOy0it97sUDbZ8z5elrJ+bLE3V7/1kBAfSLPn8lBuPrSQpucS+DgghYlN4JzCytRAJs+qAmF5gwtvIJNRvGpY</X509Certificate></X509Data></KeyInfo></Signature></evento></envEvento></nfeDadosMsg>
Loading

0 comments on commit 4597985

Please sign in to comment.