|
| 1 | +<?php |
| 2 | + |
| 3 | +// SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 4 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 5 | + |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace LibreCodeCoop\NfsePHP\Tests\Unit\Http; |
| 9 | + |
| 10 | +use donatj\MockWebServer\MockWebServer; |
| 11 | +use donatj\MockWebServer\Response; |
| 12 | +use LibreCodeCoop\NfsePHP\Dto\DpsData; |
| 13 | +use LibreCodeCoop\NfsePHP\Http\NfseClient; |
| 14 | +use LibreCodeCoop\NfsePHP\SecretStore\NoOpSecretStore; |
| 15 | +use LibreCodeCoop\NfsePHP\Tests\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * Tests for NfseClient using donatj/mock-webserver (tier-1 — no real cert required). |
| 19 | + * |
| 20 | + * @covers \LibreCodeCoop\NfsePHP\Http\NfseClient |
| 21 | + */ |
| 22 | +class NfseClientTest extends TestCase |
| 23 | +{ |
| 24 | + private static MockWebServer $server; |
| 25 | + |
| 26 | + public static function setUpBeforeClass(): void |
| 27 | + { |
| 28 | + self::$server = new MockWebServer(); |
| 29 | + self::$server->start(); |
| 30 | + } |
| 31 | + |
| 32 | + public static function tearDownAfterClass(): void |
| 33 | + { |
| 34 | + self::$server->stop(); |
| 35 | + } |
| 36 | + |
| 37 | + public function testEmitReturnsReceiptDataOnSuccess(): void |
| 38 | + { |
| 39 | + $payload = json_encode([ |
| 40 | + 'nNFSe' => '42', |
| 41 | + 'chaveAcesso' => 'abc-123', |
| 42 | + 'dhEmi' => '2026-01-01T12:00:00', |
| 43 | + ], JSON_THROW_ON_ERROR); |
| 44 | + |
| 45 | + self::$server->setResponseOfPath( |
| 46 | + '/NFS-e/api/v1/dps', |
| 47 | + new Response($payload, ['Content-Type' => 'application/json'], 200) |
| 48 | + ); |
| 49 | + |
| 50 | + $store = new NoOpSecretStore(); |
| 51 | + $client = new NfseClient( |
| 52 | + secretStore: $store, |
| 53 | + sandboxMode: false, |
| 54 | + baseUrlOverride: self::$server->getServerRoot() . '/NFS-e/api/v1', |
| 55 | + ); |
| 56 | + |
| 57 | + $dps = $this->makeDps(); |
| 58 | + $receipt = $client->emit($dps); |
| 59 | + |
| 60 | + self::assertSame('42', $receipt->nfseNumber); |
| 61 | + self::assertSame('abc-123', $receipt->chaveAcesso); |
| 62 | + self::assertSame('2026-01-01T12:00:00', $receipt->dataEmissao); |
| 63 | + } |
| 64 | + |
| 65 | + public function testQueryReturnsReceiptDataOnSuccess(): void |
| 66 | + { |
| 67 | + $payload = json_encode([ |
| 68 | + 'nNFSe' => '99', |
| 69 | + 'chaveAcesso' => 'xyz-456', |
| 70 | + 'dhEmi' => '2026-06-01T10:00:00', |
| 71 | + ], JSON_THROW_ON_ERROR); |
| 72 | + |
| 73 | + self::$server->setResponseOfPath( |
| 74 | + '/NFS-e/api/v1/dps/xyz-456', |
| 75 | + new Response($payload, ['Content-Type' => 'application/json'], 200) |
| 76 | + ); |
| 77 | + |
| 78 | + $store = new NoOpSecretStore(); |
| 79 | + $client = new NfseClient( |
| 80 | + secretStore: $store, |
| 81 | + baseUrlOverride: self::$server->getServerRoot() . '/NFS-e/api/v1', |
| 82 | + ); |
| 83 | + |
| 84 | + $receipt = $client->query('xyz-456'); |
| 85 | + |
| 86 | + self::assertSame('99', $receipt->nfseNumber); |
| 87 | + } |
| 88 | + |
| 89 | + public function testCancelReturnsTrueOnSuccess(): void |
| 90 | + { |
| 91 | + self::$server->setResponseOfPath( |
| 92 | + '/NFS-e/api/v1/dps/abc-123', |
| 93 | + new Response('{}', ['Content-Type' => 'application/json'], 200) |
| 94 | + ); |
| 95 | + |
| 96 | + $store = new NoOpSecretStore(); |
| 97 | + $client = new NfseClient( |
| 98 | + secretStore: $store, |
| 99 | + baseUrlOverride: self::$server->getServerRoot() . '/NFS-e/api/v1', |
| 100 | + ); |
| 101 | + |
| 102 | + self::assertTrue($client->cancel('abc-123', 'Cancelamento a pedido do tomador')); |
| 103 | + } |
| 104 | + |
| 105 | + // ------------------------------------------------------------------------- |
| 106 | + |
| 107 | + private function makeDps(): DpsData |
| 108 | + { |
| 109 | + return new DpsData( |
| 110 | + cnpjPrestador: '29842527000145', |
| 111 | + municipioIbge: '3303302', |
| 112 | + itemListaServico: '0107', |
| 113 | + valorServico: '1000.00', |
| 114 | + aliquota: '5.00', |
| 115 | + discriminacao: 'Consultoria em TI', |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments