From 61da411c911e45a2f9d71392d5f54466bfc3c85b Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 6 Oct 2026 23:13:57 +0000 Subject: [PATCH] fix: require NBS code for municipal parameters Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- src/Http/MunicipalParametersClient.php | 6 ++++-- tests/Unit/Http/MunicipalParametersClientTest.php | 13 +++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Http/MunicipalParametersClient.php b/src/Http/MunicipalParametersClient.php index 3add4c4..ad838d9 100644 --- a/src/Http/MunicipalParametersClient.php +++ b/src/Http/MunicipalParametersClient.php @@ -146,8 +146,10 @@ private function serviceCode(string $codigoServico): string { $value = preg_replace('/[^0-9]/', '', $codigoServico) ?? ''; - if ($value === '') { - throw new \InvalidArgumentException('Service code cannot be empty.'); + if (preg_match('/^\\d{9}$/', $value) !== 1) { + throw new \InvalidArgumentException( + 'Municipal parameters service code must be a 9-digit NBS code.', + ); } return $value; diff --git a/tests/Unit/Http/MunicipalParametersClientTest.php b/tests/Unit/Http/MunicipalParametersClientTest.php index d019e2e..4a3b15a 100644 --- a/tests/Unit/Http/MunicipalParametersClientTest.php +++ b/tests/Unit/Http/MunicipalParametersClientTest.php @@ -50,15 +50,15 @@ public function testConvenioUsesOfficialPath(): void public function testAliquotaNormalizesServiceCodeAndPreservesCompetence(): void { self::$server->setResponseOfPath( - '/parametrizacao/3303302/010701/2026-10-03/aliquota', + '/parametrizacao/3303302/115022000/2026-10-03/aliquota', new Response('{"aliquotas":[{"Aliq":5.0}]}', ['Content-Type' => 'application/json'], 200), ); - $result = $this->client()->aliquota('3303302', '01.07.01', '2026-10-03'); + $result = $this->client()->aliquota('3303302', '1.1502.20.00', '2026-10-03'); self::assertSame(5.0, $result['aliquotas'][0]['Aliq'] ?? null); self::assertSame( - '/parametrizacao/3303302/010701/2026-10-03/aliquota', + '/parametrizacao/3303302/115022000/2026-10-03/aliquota', self::$server->getLastRequest()?->getRequestUri(), ); } @@ -66,12 +66,12 @@ public function testAliquotaNormalizesServiceCodeAndPreservesCompetence(): void public function testRegimesRetencoesAndBenefitUseOfficialPaths(): void { self::$server->setResponseOfPath( - '/parametrizacao/3303302/010701/2026-10-03/regimes_especiais', + '/parametrizacao/3303302/115022000/2026-10-03/regimes_especiais', new Response('{"regimes":[]}', ['Content-Type' => 'application/json'], 200), ); - $this->client()->regimesEspeciais('3303302', '010701', '2026-10-03'); + $this->client()->regimesEspeciais('3303302', '115022000', '2026-10-03'); self::assertSame( - '/parametrizacao/3303302/010701/2026-10-03/regimes_especiais', + '/parametrizacao/3303302/115022000/2026-10-03/regimes_especiais', self::$server->getLastRequest()?->getRequestUri(), ); @@ -101,6 +101,7 @@ public function testRejectsInvalidInputsBeforeHttpRequest(): void foreach ([ fn () => $client->convenio('123'), fn () => $client->aliquota('3303302', '', '2026-10-03'), + fn () => $client->aliquota('3303302', '010101', '2026-10-03'), fn () => $client->retencoes('3303302', 'not-a-date'), fn () => $client->beneficio('3303302', '', '2026-10-03'), ] as $operation) {