Skip to content

Commit cb0bb5e

Browse files
committed
test(client): update NfseClient tests to use EnvironmentConfig and CertConfig
Signed-off-by: Vitor Mattos <vitor@php.rio>
1 parent 0bcf832 commit cb0bb5e

File tree

1 file changed

+27
-43
lines changed

1 file changed

+27
-43
lines changed

tests/Unit/Http/NfseClientTest.php

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
use donatj\MockWebServer\MockWebServer;
1111
use donatj\MockWebServer\Response;
12+
use LibreCodeCoop\NfsePHP\Config\CertConfig;
13+
use LibreCodeCoop\NfsePHP\Config\EnvironmentConfig;
1214
use LibreCodeCoop\NfsePHP\Contracts\XmlSignerInterface;
1315
use LibreCodeCoop\NfsePHP\Dto\DpsData;
1416
use LibreCodeCoop\NfsePHP\Exception\CancellationException;
@@ -65,13 +67,7 @@ public function testEmitReturnsReceiptDataOnSuccess(): void
6567
new Response($payload, ['Content-Type' => 'application/json'], 200)
6668
);
6769

68-
$store = new NoOpSecretStore();
69-
$client = new NfseClient(
70-
secretStore: $store,
71-
sandboxMode: false,
72-
baseUrlOverride: self::$server->getServerRoot() . '/NFS-e/api/v1',
73-
signer: $this->signer,
74-
);
70+
$client = $this->makeClient($this->signer);
7571

7672
$dps = $this->makeDps();
7773
$receipt = $client->emit($dps);
@@ -94,11 +90,7 @@ public function testQueryReturnsReceiptDataOnSuccess(): void
9490
new Response($payload, ['Content-Type' => 'application/json'], 200)
9591
);
9692

97-
$store = new NoOpSecretStore();
98-
$client = new NfseClient(
99-
secretStore: $store,
100-
baseUrlOverride: self::$server->getServerRoot() . '/NFS-e/api/v1',
101-
);
93+
$client = $this->makeClient();
10294

10395
$receipt = $client->query('xyz-456');
10496

@@ -112,11 +104,7 @@ public function testCancelReturnsTrueOnSuccess(): void
112104
new Response('{}', ['Content-Type' => 'application/json'], 200)
113105
);
114106

115-
$store = new NoOpSecretStore();
116-
$client = new NfseClient(
117-
secretStore: $store,
118-
baseUrlOverride: self::$server->getServerRoot() . '/NFS-e/api/v1',
119-
);
107+
$client = $this->makeClient();
120108

121109
self::assertTrue($client->cancel('abc-123', 'Cancelamento a pedido do tomador'));
122110
}
@@ -134,11 +122,7 @@ public function testEmitThrowsIssuanceExceptionWhenGatewayRejects(): void
134122
new Response($payload, ['Content-Type' => 'application/json'], 422),
135123
);
136124

137-
$client = new NfseClient(
138-
secretStore: new NoOpSecretStore(),
139-
baseUrlOverride: self::$server->getServerRoot() . '/NFS-e/api/v1',
140-
signer: $this->signer,
141-
);
125+
$client = $this->makeClient($this->signer);
142126

143127
$this->expectException(IssuanceException::class);
144128
$client->emit($this->makeDps());
@@ -153,11 +137,7 @@ public function testIssuanceExceptionCarriesErrorCodeHttpStatusAndUpstreamPayloa
153137
new Response(json_encode($errorData, JSON_THROW_ON_ERROR), ['Content-Type' => 'application/json'], 422),
154138
);
155139

156-
$client = new NfseClient(
157-
secretStore: new NoOpSecretStore(),
158-
baseUrlOverride: self::$server->getServerRoot() . '/NFS-e/api/v1',
159-
signer: $this->signer,
160-
);
140+
$client = $this->makeClient($this->signer);
161141

162142
try {
163143
$client->emit($this->makeDps());
@@ -176,10 +156,7 @@ public function testQueryThrowsQueryExceptionWhenGatewayReturnsError(): void
176156
new Response('{"error":"not found"}', ['Content-Type' => 'application/json'], 404),
177157
);
178158

179-
$client = new NfseClient(
180-
secretStore: new NoOpSecretStore(),
181-
baseUrlOverride: self::$server->getServerRoot() . '/NFS-e/api/v1',
182-
);
159+
$client = $this->makeClient();
183160

184161
$this->expectException(QueryException::class);
185162
$client->query('missing-key');
@@ -192,10 +169,7 @@ public function testQueryExceptionCarriesErrorCodeAndHttpStatus(): void
192169
new Response('{"error":"not found"}', ['Content-Type' => 'application/json'], 404),
193170
);
194171

195-
$client = new NfseClient(
196-
secretStore: new NoOpSecretStore(),
197-
baseUrlOverride: self::$server->getServerRoot() . '/NFS-e/api/v1',
198-
);
172+
$client = $this->makeClient();
199173

200174
try {
201175
$client->query('missing-key');
@@ -213,10 +187,7 @@ public function testCancelThrowsCancellationExceptionWhenGatewayReturnsError():
213187
new Response('{"error":"cannot cancel"}', ['Content-Type' => 'application/json'], 409),
214188
);
215189

216-
$client = new NfseClient(
217-
secretStore: new NoOpSecretStore(),
218-
baseUrlOverride: self::$server->getServerRoot() . '/NFS-e/api/v1',
219-
);
190+
$client = $this->makeClient();
220191

221192
$this->expectException(CancellationException::class);
222193
$client->cancel('blocked-key', 'a pedido do tomador');
@@ -229,10 +200,7 @@ public function testCancellationExceptionCarriesErrorCodeAndHttpStatus(): void
229200
new Response('{"error":"cannot cancel"}', ['Content-Type' => 'application/json'], 409),
230201
);
231202

232-
$client = new NfseClient(
233-
secretStore: new NoOpSecretStore(),
234-
baseUrlOverride: self::$server->getServerRoot() . '/NFS-e/api/v1',
235-
);
203+
$client = $this->makeClient();
236204

237205
try {
238206
$client->cancel('blocked-key', 'a pedido do tomador');
@@ -245,6 +213,22 @@ public function testCancellationExceptionCarriesErrorCodeAndHttpStatus(): void
245213

246214
// -------------------------------------------------------------------------
247215

216+
private function makeClient(?XmlSignerInterface $signer = null): NfseClient
217+
{
218+
return new NfseClient(
219+
environment: new EnvironmentConfig(
220+
baseUrl: self::$server->getServerRoot() . '/NFS-e/api/v1',
221+
),
222+
cert: new CertConfig(
223+
cnpj: '29842527000145',
224+
pfxPath: '/dev/null',
225+
vaultPath: 'secret/nfse/29842527000145',
226+
),
227+
secretStore: new NoOpSecretStore(),
228+
signer: $signer,
229+
);
230+
}
231+
248232
private function makeDps(): DpsData
249233
{
250234
return new DpsData(

0 commit comments

Comments
 (0)