Skip to content

Commit 0932f18

Browse files
committed
test(http): assert cancellation eventos payload contract
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 4d39411 commit 0932f18

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

tests/Unit/Http/NfseClientTest.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function testQueryReturnsReceiptDataOnSuccess(): void
136136
new Response($payload, ['Content-Type' => 'application/json'], 200)
137137
);
138138

139-
$client = $this->makeClient();
139+
$client = $this->makeClient($this->signer);
140140

141141
$receipt = $client->query('xyz-456');
142142

@@ -146,13 +146,34 @@ public function testQueryReturnsReceiptDataOnSuccess(): void
146146
public function testCancelReturnsTrueOnSuccess(): void
147147
{
148148
self::$server->setResponseOfPath(
149-
'/SefinNacional/dps/abc-123',
149+
'/SefinNacional/nfse/abc-123/eventos',
150150
new Response('{}', ['Content-Type' => 'application/json'], 200)
151151
);
152152

153-
$client = $this->makeClient();
153+
$client = $this->makeClient($this->signer);
154154

155155
self::assertTrue($client->cancel('abc-123', 'Cancelamento a pedido do tomador'));
156+
157+
$request = self::$server->getLastRequest();
158+
self::assertNotNull($request);
159+
self::assertSame('POST', $request->getRequestMethod());
160+
self::assertSame('/SefinNacional/nfse/abc-123/eventos', $request->getRequestUri());
161+
162+
$payload = json_decode($request->getInput(), true, 512, JSON_THROW_ON_ERROR);
163+
self::assertIsArray($payload);
164+
self::assertArrayHasKey('pedidoRegistroEventoXmlGZipB64', $payload);
165+
166+
$compressedXml = base64_decode((string) $payload['pedidoRegistroEventoXmlGZipB64'], true);
167+
self::assertNotFalse($compressedXml);
168+
169+
$eventoXml = gzdecode($compressedXml);
170+
self::assertNotFalse($eventoXml);
171+
self::assertStringContainsString('<pedRegEvento', $eventoXml);
172+
self::assertStringContainsString('<chNFSe>abc-123</chNFSe>', $eventoXml);
173+
self::assertStringContainsString('<e101101>', $eventoXml);
174+
self::assertStringContainsString('<xDesc>Cancelamento de NFS-e</xDesc>', $eventoXml);
175+
self::assertStringContainsString('<cMotivo>1</cMotivo>', $eventoXml);
176+
self::assertStringContainsString('<xMotivo>Cancelamento a pedido do tomador</xMotivo>', $eventoXml);
156177
}
157178

158179
// -------------------------------------------------------------------------
@@ -202,7 +223,7 @@ public function testQueryThrowsQueryExceptionWhenGatewayReturnsError(): void
202223
new Response('{"error":"not found"}', ['Content-Type' => 'application/json'], 404),
203224
);
204225

205-
$client = $this->makeClient();
226+
$client = $this->makeClient($this->signer);
206227

207228
$this->expectException(QueryException::class);
208229
$client->query('missing-key');
@@ -215,7 +236,7 @@ public function testQueryExceptionCarriesErrorCodeAndHttpStatus(): void
215236
new Response('{"error":"not found"}', ['Content-Type' => 'application/json'], 404),
216237
);
217238

218-
$client = $this->makeClient();
239+
$client = $this->makeClient($this->signer);
219240

220241
try {
221242
$client->query('missing-key');
@@ -229,11 +250,11 @@ public function testQueryExceptionCarriesErrorCodeAndHttpStatus(): void
229250
public function testCancelThrowsCancellationExceptionWhenGatewayReturnsError(): void
230251
{
231252
self::$server->setResponseOfPath(
232-
'/SefinNacional/dps/blocked-key',
253+
'/SefinNacional/nfse/blocked-key/eventos',
233254
new Response('{"error":"cannot cancel"}', ['Content-Type' => 'application/json'], 409),
234255
);
235256

236-
$client = $this->makeClient();
257+
$client = $this->makeClient($this->signer);
237258

238259
$this->expectException(CancellationException::class);
239260
$client->cancel('blocked-key', 'a pedido do tomador');
@@ -242,11 +263,11 @@ public function testCancelThrowsCancellationExceptionWhenGatewayReturnsError():
242263
public function testCancellationExceptionCarriesErrorCodeAndHttpStatus(): void
243264
{
244265
self::$server->setResponseOfPath(
245-
'/SefinNacional/dps/blocked-key',
266+
'/SefinNacional/nfse/blocked-key/eventos',
246267
new Response('{"error":"cannot cancel"}', ['Content-Type' => 'application/json'], 409),
247268
);
248269

249-
$client = $this->makeClient();
270+
$client = $this->makeClient($this->signer);
250271

251272
try {
252273
$client->cancel('blocked-key', 'a pedido do tomador');

0 commit comments

Comments
 (0)