|
6 | 6 |
|
7 | 7 | use NFePHP\Common\Certificate; |
8 | 8 | use NFePHP\Common\Strings; |
| 9 | +use NFePHP\NFe\Factories\Contingency; |
9 | 10 | use NFePHP\NFe\Common\Standardize; |
10 | 11 | use NFePHP\NFe\Tests\Common\ToolsFake; |
11 | 12 | use NFePHP\NFe\Tools; |
@@ -260,6 +261,141 @@ public function test_sefaz_dist_dfe(): void |
260 | 261 | $this->assertSame($esperado, $request); |
261 | 262 | } |
262 | 263 |
|
| 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 | + |
263 | 399 | public function test_sefazCCe(): void |
264 | 400 | { |
265 | 401 | $chave = '35220605730928000145550010000048661583302923'; |
@@ -849,6 +985,46 @@ protected function getCleanXml($filePath) |
849 | 985 | return $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement); |
850 | 986 | } |
851 | 987 |
|
| 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 | + |
852 | 1028 | public function ufProvider(): array |
853 | 1029 | { |
854 | 1030 | return [ |
|
0 commit comments