Skip to content

Commit 57ba046

Browse files
authored
Merge pull request #28 from LibreCodeCoop/test/update-replace-without-previous-cnpj
test: cover replace flow without previous cnpj
2 parents be3d5bb + a6af046 commit 57ba046

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

tests/Unit/Http/Controllers/SettingsControllerTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,72 @@ protected function storeCertificate(string $cnpj, string $pfxContent, string $pa
544544
self::assertSame('value', ControllerIsolationState::$settings['other.key'] ?? null);
545545
}
546546

547+
public function testUpdateReplacingCertificateDoesNotPurgeOrClearWhenPreviousCnpjIsMissing(): void
548+
{
549+
ControllerIsolationState::reset();
550+
ControllerIsolationState::$settings = [
551+
'nfse.uf' => 'RJ',
552+
'other.key' => 'value',
553+
];
554+
555+
$request = new Request(
556+
inputs: [
557+
'nfse' => [
558+
'cnpj_prestador' => '33333333000133',
559+
'uf' => 'rj',
560+
'municipio_nome' => 'Niteroi',
561+
'municipio_ibge' => '3303302',
562+
'item_lista_servico' => '0123',
563+
'bao_addr' => 'http://openbao:8200',
564+
'bao_mount' => '/nfse',
565+
],
566+
'pfx_password' => 'valid-password',
567+
],
568+
files: [
569+
'pfx_file' => new UploadedFile('/tmp/cert-replace-no-prev.pfx'),
570+
],
571+
);
572+
573+
$controller = new class () extends SettingsController {
574+
public ?string $purgedCnpj = null;
575+
public bool $clearCalled = false;
576+
577+
protected function readUploadedCertificate(UploadedFile $file): string
578+
{
579+
return 'fake-pfx-content';
580+
}
581+
582+
protected function validateCertificatePayload(string $pfxContent, string $password): void
583+
{
584+
}
585+
586+
protected function purgeCertificateArtifacts(string $cnpj): void
587+
{
588+
$this->purgedCnpj = $cnpj;
589+
}
590+
591+
protected function clearNfseSettings(): void
592+
{
593+
$this->clearCalled = true;
594+
parent::clearNfseSettings();
595+
}
596+
597+
protected function storeCertificate(string $cnpj, string $pfxContent, string $password): void
598+
{
599+
}
600+
};
601+
602+
$response = $controller->update($request);
603+
604+
self::assertInstanceOf(RedirectResponse::class, $response);
605+
self::assertSame('route', $response->target);
606+
self::assertSame('nfse.settings.edit', $response->route);
607+
self::assertSame('nfse::general.saved_and_certificate_uploaded', $response->flash['success'] ?? null);
608+
self::assertNull($controller->purgedCnpj);
609+
self::assertFalse($controller->clearCalled);
610+
self::assertSame('value', ControllerIsolationState::$settings['other.key'] ?? null);
611+
}
612+
547613
public function testUpdateReturnsStoreFailureMessageAfterSettingsSaveWhenCertificateStoreThrows(): void
548614
{
549615
ControllerIsolationState::reset();

0 commit comments

Comments
 (0)