Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Http/MunicipalParametersClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 7 additions & 6 deletions tests/Unit/Http/MunicipalParametersClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,28 @@ 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(),
);
}

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(),
);

Expand Down Expand Up @@ -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) {
Expand Down
Loading