Skip to content

Commit 9e035e8

Browse files
authored
Merge pull request #1325 from gersonfs/testes-adicionais
Expand tools coverage and remove unused adjusters
2 parents df60417 + f0e9c82 commit 9e035e8

3 files changed

Lines changed: 176 additions & 40 deletions

File tree

src/Make.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,26 +1372,6 @@ protected static function propertiesToBack(stdClass $data, array $possible): std
13721372
return $new;
13731373
}
13741374

1375-
/**
1376-
* Adjust the text size to the maximum acceptable size
1377-
* @param string|null $string
1378-
* @param int $max
1379-
* @return string|null
1380-
*/
1381-
protected function adjustingStrings($string, $max = 0): ?string
1382-
{
1383-
if (is_null($string)) {
1384-
return null;
1385-
}
1386-
if (empty($string)) {
1387-
return '';
1388-
}
1389-
if ($max === 0) {
1390-
return $string;
1391-
}
1392-
return substr($string, 0, $max);
1393-
}
1394-
13951375
/**
13961376
* Formatação numerica condicional
13971377
* @param string|float|int|null $value

src/MakeDev.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,26 +1328,6 @@ protected static function propertiesToBack(stdClass $data, array $possible): std
13281328
return $new;
13291329
}
13301330

1331-
/**
1332-
* Adjust the text size to the maximum acceptable size
1333-
* @param string|null $string
1334-
* @param int $max
1335-
* @return string|null
1336-
*/
1337-
protected function adjustingStrings($string, $max = 0): ?string
1338-
{
1339-
if (is_null($string)) {
1340-
return null;
1341-
}
1342-
if (empty($string)) {
1343-
return '';
1344-
}
1345-
if ($max === 0) {
1346-
return $string;
1347-
}
1348-
return substr($string, 0, $max);
1349-
}
1350-
13511331
/**
13521332
* Formatação numerica condicional
13531333
* @param string|float|int|null $value

tests/ToolsTest.php

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use NFePHP\Common\Certificate;
88
use NFePHP\Common\Strings;
9+
use NFePHP\NFe\Factories\Contingency;
910
use NFePHP\NFe\Common\Standardize;
1011
use NFePHP\NFe\Tests\Common\ToolsFake;
1112
use NFePHP\NFe\Tools;
@@ -260,6 +261,141 @@ public function test_sefaz_dist_dfe(): void
260261
$this->assertSame($esperado, $request);
261262
}
262263

264+
public function test_sefaz_epec_throws_when_xml_is_empty(): void
265+
{
266+
$tools = $this->buildToolsForEpec('RS');
267+
$xml = '';
268+
269+
$this->expectException(\InvalidArgumentException::class);
270+
$this->expectExceptionMessage('EPEC: parâmetro xml esta vazio!');
271+
272+
$tools->sefazEPEC($xml);
273+
}
274+
275+
public function test_sefaz_epec_throws_when_contingency_is_not_epec(): void
276+
{
277+
$xml = file_get_contents(__DIR__ . '/fixtures/xml/signNFe_modelo_55.xml');
278+
$tools = $this->tools;
279+
$tools->model(55);
280+
281+
$this->expectException(\RuntimeException::class);
282+
$this->expectExceptionMessage('A contingencia EPEC deve estar ativada!');
283+
284+
$tools->sefazEPEC($xml);
285+
}
286+
287+
public function test_sefaz_epec_throws_when_author_uf_differs_from_key_uf(): void
288+
{
289+
$xml = file_get_contents(__DIR__ . '/fixtures/xml/signNFe_modelo_55.xml');
290+
$tools = $this->buildToolsForEpec('SP');
291+
$tools->model(55);
292+
293+
$this->expectException(\RuntimeException::class);
294+
$this->expectExceptionMessage('não é da mesma UF');
295+
296+
$tools->sefazEPEC($xml);
297+
}
298+
299+
public function test_sefaz_epec_builds_request_and_updates_xml_by_reference(): void
300+
{
301+
$xml = file_get_contents(__DIR__ . '/fixtures/xml/signNFe_modelo_55.xml');
302+
$tools = $this->buildToolsForEpec('RS');
303+
$tools->model(55);
304+
$tools->getSoap()->setReturnValue('<retEnvEvento/>');
305+
306+
$response = $tools->sefazEPEC($xml);
307+
308+
$this->assertSame('<retEnvEvento/>', $response);
309+
$this->assertStringContainsString('<Signature', $xml);
310+
$this->assertStringContainsString('<tpEmis>4</tpEmis>', $xml);
311+
$this->assertStringContainsString('<xJust>Falha tecnica para testes EPEC</xJust>', $xml);
312+
313+
$request = $tools->getRequest();
314+
$this->assertStringContainsString('<tpEvento>110140</tpEvento>', $request);
315+
$this->assertStringContainsString('<descEvento>EPEC</descEvento>', $request);
316+
$this->assertStringContainsString('<cOrgaoAutor>43</cOrgaoAutor>', $request);
317+
$this->assertStringContainsString('<CNPJ>58716523000119</CNPJ>', $request);
318+
$this->assertStringContainsString('<IE>112006603110</IE>', $request);
319+
$this->assertStringContainsString('<verAplic>fernando</verAplic>', $request);
320+
$this->assertStringContainsString('<vNF>500.00</vNF>', $request);
321+
$this->assertStringContainsString('<vICMS>0.00</vICMS>', $request);
322+
$this->assertStringContainsString('<vST>0.00</vST>', $request);
323+
}
324+
325+
public function test_sefaz_epec_accepts_custom_ver_aplic(): void
326+
{
327+
$xml = file_get_contents(__DIR__ . '/fixtures/xml/signNFe_modelo_55.xml');
328+
$tools = $this->buildToolsForEpec('RS');
329+
$tools->model(55);
330+
$tools->getSoap()->setReturnValue('<retEnvEvento/>');
331+
332+
$tools->sefazEPEC($xml, 'CustomApp_9.9');
333+
334+
$request = $tools->getRequest();
335+
$this->assertStringContainsString('<verAplic>CustomApp_9.9</verAplic>', $request);
336+
}
337+
338+
public function test_sefaz_validate_returns_true_when_protocol_matches(): void
339+
{
340+
$nfe = file_get_contents(__DIR__ . '/fixtures/xml/nfe_layout4_com_prot.xml');
341+
$response = file_get_contents(__DIR__ . '/fixtures/xml/nfe_layout4_com_prot.xml');
342+
343+
$tools = $this->tools;
344+
$tools->model(55);
345+
$tools->getSoap()->setReturnValue($response);
346+
347+
$this->assertTrue($tools->sefazValidate($nfe));
348+
}
349+
350+
public function test_sefaz_validate_returns_false_when_protocol_data_differs(): void
351+
{
352+
$nfe = file_get_contents(__DIR__ . '/fixtures/xml/nfe_layout4_com_prot.xml');
353+
$response = '<?xml version="1.0" encoding="UTF-8"?>'
354+
. '<retConsSitNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">'
355+
. '<protNFe versao="4.00"><infProt>'
356+
. '<chNFe>43180906929383000163550010000000261000010301</chNFe>'
357+
. '<nProt>143180006932433</nProt>'
358+
. '<digVal>digest-diferente</digVal>'
359+
. '</infProt></protNFe>'
360+
. '</retConsSitNFe>';
361+
362+
$tools = $this->tools;
363+
$tools->model(55);
364+
$tools->getSoap()->setReturnValue($response);
365+
366+
$this->assertFalse($tools->sefazValidate($nfe));
367+
}
368+
369+
public function test_sefaz_validate_throws_with_xmotivo_when_response_has_no_protocol(): void
370+
{
371+
$nfe = file_get_contents(__DIR__ . '/fixtures/xml/nfe_layout4_com_prot.xml');
372+
$response = '<retConsSitNFe><xMotivo>Documento nao localizado</xMotivo></retConsSitNFe>';
373+
374+
$tools = $this->tools;
375+
$tools->model(55);
376+
$tools->getSoap()->setReturnValue($response);
377+
378+
$this->expectException(\InvalidArgumentException::class);
379+
$this->expectExceptionMessage('Validacao NF-e: Documento nao localizado');
380+
381+
$tools->sefazValidate($nfe);
382+
}
383+
384+
public function test_sefaz_validate_throws_when_response_has_no_protocol_and_no_xmotivo(): void
385+
{
386+
$nfe = file_get_contents(__DIR__ . '/fixtures/xml/nfe_layout4_com_prot.xml');
387+
$response = '<retConsSitNFe/>';
388+
389+
$tools = $this->tools;
390+
$tools->model(55);
391+
$tools->getSoap()->setReturnValue($response);
392+
393+
$this->expectException(\InvalidArgumentException::class);
394+
$this->expectExceptionMessage('O documento de resposta nao contem o node "protNFe".');
395+
396+
$tools->sefazValidate($nfe);
397+
}
398+
263399
public function test_sefazCCe(): void
264400
{
265401
$chave = '35220605730928000145550010000048661583302923';
@@ -849,6 +985,46 @@ protected function getCleanXml($filePath)
849985
return $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement);
850986
}
851987

988+
protected function buildToolsForUfAndContingency(string $uf, ?string $contingencyJson = null): ToolsFake
989+
{
990+
$config = json_decode($this->configJson, true);
991+
$config['siglaUF'] = $uf;
992+
993+
return new ToolsFake(
994+
json_encode($config),
995+
Certificate::readPfx($this->contentpfx, $this->passwordpfx),
996+
$contingencyJson ? new Contingency($contingencyJson) : null
997+
);
998+
}
999+
1000+
protected function buildEpecContingencyJson(): string
1001+
{
1002+
return json_encode([
1003+
'motive' => 'Falha tecnica para testes EPEC',
1004+
'timestamp' => 1715600000,
1005+
'type' => 'EPEC',
1006+
'tpEmis' => 4,
1007+
]);
1008+
}
1009+
1010+
protected function buildToolsForEpec(string $uf): ToolsFake
1011+
{
1012+
$config = json_decode($this->configJson, true);
1013+
$config['siglaUF'] = $uf;
1014+
$certificate = Certificate::readPfx($this->contentpfx, $this->passwordpfx);
1015+
$contingency = new Contingency($this->buildEpecContingencyJson());
1016+
1017+
return new class (json_encode($config), $certificate, $contingency) extends ToolsFake {
1018+
protected function checkContingencyForWebServices(string $service)
1019+
{
1020+
if (($this->contingency->type ?? '') === 'EPEC') {
1021+
return;
1022+
}
1023+
parent::checkContingencyForWebServices($service);
1024+
}
1025+
};
1026+
}
1027+
8521028
public function ufProvider(): array
8531029
{
8541030
return [

0 commit comments

Comments
 (0)