@@ -50,38 +50,177 @@ public function test_to_authorize_inut_cnpj(): void
5050
5151 public function testToAuthorizeEvent ()
5252 {
53+ $ request = '<?xml version="1.0" encoding="UTF-8"?> '
54+ . '<envEvento xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.00"> '
55+ . '<idLote>123456</idLote> '
56+ . '<evento xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.00"> '
57+ . '<infEvento Id="ID1101113512345678901234550010000099230100009923"> '
58+ . '<cOrgao>35</cOrgao><tpAmb>2</tpAmb><CNPJ>12345678901234</CNPJ> '
59+ . '<chNFe>35123456789012345500100000992301000099230</chNFe> '
60+ . '<dhEvento>2017-11-17T08:26:54-02:00</dhEvento> '
61+ . '<tpEvento>110110</tpEvento><nSeqEvento>1</nSeqEvento> '
62+ . '<verEvento>1.00</verEvento> '
63+ . '<detEvento versao="1.00"><descEvento>Carta de Correcao</descEvento> '
64+ . '<xCorrecao>Correcao de teste</xCorrecao> '
65+ . '<xCondUso>A Carta de Correcao e disciplinada pelo paragrafo 1o-A do art. 7o do Convenio S/N</xCondUso> '
66+ . '</detEvento></infEvento></evento></envEvento> ' ;
67+
68+ $ response = '<?xml version="1.0" encoding="UTF-8"?> '
69+ . '<retEnvEvento xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.00"> '
70+ . '<idLote>123456</idLote><tpAmb>2</tpAmb><verAplic>SP_EVENTOS_PL_100</verAplic> '
71+ . '<cOrgao>35</cOrgao><cStat>128</cStat><xMotivo>Lote de Evento Processado</xMotivo> '
72+ . '<retEvento versao="1.00"><infEvento Id="ID1101103512345678901234"> '
73+ . '<tpAmb>2</tpAmb><verAplic>SP_EVENTOS_PL_100</verAplic> '
74+ . '<cOrgao>35</cOrgao><cStat>135</cStat> '
75+ . '<xMotivo>Evento registrado e vinculado a NF-e</xMotivo> '
76+ . '<chNFe>35123456789012345500100000992301000099230</chNFe> '
77+ . '<tpEvento>110110</tpEvento><xEvento>Carta de Correcao</xEvento> '
78+ . '<nSeqEvento>1</nSeqEvento> '
79+ . '<dhRegEvento>2017-11-17T08:27:00-02:00</dhRegEvento> '
80+ . '<nProt>135170000000001</nProt> '
81+ . '</infEvento></retEvento></retEnvEvento> ' ;
82+
83+ $ result = Complements::toAuthorize ($ request , $ response );
84+ $ this ->assertStringContainsString ('procEventoNFe ' , $ result );
85+ $ this ->assertStringContainsString ('135170000000001 ' , $ result );
5386 }
5487
5588 public function testToAuthorizeFailWrongDocument ()
5689 {
90+ $ this ->expectException (DocumentsException::class);
91+ $ request = '<?xml version="1.0" encoding="UTF-8"?> '
92+ . '<consStatServ xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00"> '
93+ . '<tpAmb>2</tpAmb><cUF>35</cUF><xServ>STATUS</xServ></consStatServ> ' ;
94+ $ response = '<retConsStatServ xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00"> '
95+ . '<tpAmb>2</tpAmb><verAplic>SP</verAplic><cStat>107</cStat> '
96+ . '<xMotivo>Servico em Operacao</xMotivo><cUF>35</cUF> '
97+ . '<dhRecbto>2017-11-17T08:26:54-02:00</dhRecbto><tMed>1</tMed> '
98+ . '</retConsStatServ> ' ;
99+ Complements::toAuthorize ($ request , $ response );
57100 }
58101
59102 public function testToAuthorizeFailNotXML ()
60103 {
104+ $ this ->expectException (\Throwable::class);
105+ Complements::toAuthorize ('not xml ' , 'not xml ' );
61106 }
62107
63108 public function testToAuthorizeFailWrongNode ()
64109 {
110+ $ this ->expectException (\Throwable::class);
111+ Complements::toAuthorize ('' , '<response/> ' );
65112 }
66113
67114 public function testCancelRegister ()
68115 {
116+ $ nfe = file_get_contents (__DIR__ . '/fixtures/xml/nfe_layout4_com_prot.xml ' );
117+
118+ $ dom = new \DOMDocument ();
119+ $ dom ->loadXML ($ nfe );
120+ $ chNFe = $ dom ->getElementsByTagName ('chNFe ' )->item (0 )->nodeValue ;
121+
122+ $ cancelamento = '<?xml version="1.0" encoding="UTF-8"?> '
123+ . '<retEnvEvento xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.00"> '
124+ . '<idLote>1</idLote><tpAmb>2</tpAmb><verAplic>SP</verAplic> '
125+ . '<cOrgao>43</cOrgao><cStat>128</cStat><xMotivo>Lote Processado</xMotivo> '
126+ . '<retEvento versao="1.00"><infEvento> '
127+ . '<tpAmb>2</tpAmb><verAplic>SP</verAplic><cOrgao>43</cOrgao> '
128+ . '<cStat>135</cStat><xMotivo>Evento registrado</xMotivo> '
129+ . '<chNFe> ' . $ chNFe . '</chNFe> '
130+ . '<tpEvento>110111</tpEvento> '
131+ . '<xEvento>Cancelamento</xEvento><nSeqEvento>1</nSeqEvento> '
132+ . '<dhRegEvento>2018-09-25T16:00:00-03:00</dhRegEvento> '
133+ . '<nProt>143180006932433</nProt> '
134+ . '</infEvento></retEvento></retEnvEvento> ' ;
135+
136+ $ result = Complements::cancelRegister ($ nfe , $ cancelamento );
137+ $ this ->assertStringContainsString ('retEvento ' , $ result );
138+ $ this ->assertStringContainsString ('110111 ' , $ result );
69139 }
70140
71141 public function testCancelRegisterFailNotNFe ()
72142 {
143+ $ this ->expectException (DocumentsException::class);
144+ $ nfe = '<?xml version="1.0" encoding="UTF-8"?><NFe xmlns="http://www.portalfiscal.inf.br/nfe"> '
145+ . '<infNFe Id="NFe35170358716523000119550010000000301000000300" versao="4.00"> '
146+ . '</infNFe></NFe> ' ;
147+ $ cancelamento = '<retEnvEvento xmlns="http://www.portalfiscal.inf.br/nfe"><retEvento> '
148+ . '<infEvento><cStat>135</cStat><nProt>123</nProt><chNFe>123</chNFe> '
149+ . '<tpEvento>110111</tpEvento></infEvento></retEvento></retEnvEvento> ' ;
150+ Complements::cancelRegister ($ nfe , $ cancelamento );
73151 }
74152
75153 public function testB2B ()
76154 {
155+ $ nfe = file_get_contents (__DIR__ . '/fixtures/xml/nfe_layout4_com_prot.xml ' );
156+ $ b2b = '<?xml version="1.0" encoding="UTF-8"?> '
157+ . '<NFeB2BFin xmlns="http://www.portalfiscal.inf.br/nfe"> '
158+ . '<infB2BFin><indPag>0</indPag><vOrigPag>100.00</vOrigPag></infB2BFin> '
159+ . '</NFeB2BFin> ' ;
160+
161+ $ result = Complements::b2bTag ($ nfe , $ b2b );
162+ $ this ->assertStringContainsString ('nfeProcB2B ' , $ result );
163+ $ this ->assertStringContainsString ('NFeB2BFin ' , $ result );
164+ $ this ->assertStringContainsString ('nfeProc ' , $ result );
77165 }
78166
79167 public function testB2BFailNotNFe ()
80168 {
169+ $ this ->expectException (DocumentsException::class);
170+ $ nfe = '<?xml version="1.0" encoding="UTF-8"?><NFe xmlns="http://www.portalfiscal.inf.br/nfe"> '
171+ . '<infNFe Id="NFe123" versao="4.00"></infNFe></NFe> ' ;
172+ $ b2b = '<NFeB2BFin><infB2BFin/></NFeB2BFin> ' ;
173+ Complements::b2bTag ($ nfe , $ b2b );
81174 }
82175
83176 public function testB2BFailWrongNode ()
84177 {
178+ $ this ->expectException (DocumentsException::class);
179+ $ nfe = file_get_contents (__DIR__ . '/fixtures/xml/nfe_layout4_com_prot.xml ' );
180+ $ b2b = '<?xml version="1.0" encoding="UTF-8"?><WrongTag><data/></WrongTag> ' ;
181+ Complements::b2bTag ($ nfe , $ b2b );
182+ }
183+
184+ public function testToAuthorizeFailEmptyRequest ()
185+ {
186+ $ this ->expectException (DocumentsException::class);
187+ $ this ->expectExceptionMessage ('protocolar ' );
188+ Complements::toAuthorize ('' , '<retorno/> ' );
189+ }
190+
191+ public function testToAuthorizeFailEmptyResponse ()
192+ {
193+ $ this ->expectException (DocumentsException::class);
194+ $ this ->expectExceptionMessage ('retorno ' );
195+ $ request = file_get_contents (__DIR__ . '/fixtures/xml/exemplo_xml_envia_lote_modelo_55.xml ' );
196+ Complements::toAuthorize ($ request , '' );
85197 }
86- }
87198
199+ public function testCancelRegisterNonMatchingChave ()
200+ {
201+ $ nfe = file_get_contents (__DIR__ . '/fixtures/xml/nfe_layout4_com_prot.xml ' );
202+
203+ $ cancelamento = '<?xml version="1.0" encoding="UTF-8"?> '
204+ . '<retEnvEvento xmlns="http://www.portalfiscal.inf.br/nfe" versao="1.00"> '
205+ . '<idLote>1</idLote><tpAmb>2</tpAmb><verAplic>SP</verAplic> '
206+ . '<cOrgao>43</cOrgao><cStat>128</cStat><xMotivo>Lote Processado</xMotivo> '
207+ . '<retEvento versao="1.00"><infEvento> '
208+ . '<tpAmb>2</tpAmb><verAplic>SP</verAplic><cOrgao>43</cOrgao> '
209+ . '<cStat>135</cStat><xMotivo>Evento registrado</xMotivo> '
210+ . '<chNFe>99999999999999999999999999999999999999999999</chNFe> '
211+ . '<tpEvento>110111</tpEvento> '
212+ . '<xEvento>Cancelamento</xEvento><nSeqEvento>1</nSeqEvento> '
213+ . '<dhRegEvento>2018-09-25T16:00:00-03:00</dhRegEvento> '
214+ . '<nProt>143180006932433</nProt> '
215+ . '</infEvento></retEvento></retEnvEvento> ' ;
216+
217+ // Non-matching chave should return the NFe without appending retEvento
218+ $ result = Complements::cancelRegister ($ nfe , $ cancelamento );
219+ $ this ->assertStringContainsString ('nfeProc ' , $ result );
220+ // retEvento should NOT be appended since chNFe doesn't match
221+ $ resultDom = new \DOMDocument ();
222+ $ resultDom ->loadXML ($ result );
223+ $ retEventos = $ resultDom ->getElementsByTagName ('retEvento ' );
224+ $ this ->assertEquals (0 , $ retEventos ->length );
225+ }
226+ }
0 commit comments