|
10 | 10 | } |
11 | 11 |
|
12 | 12 | namespace Modules\Nfse\Tests\Unit\Http\Controllers { |
| 13 | + use Illuminate\Http\JsonResponse; |
13 | 14 | use Illuminate\Http\RedirectResponse; |
14 | 15 | use Illuminate\Http\Request; |
15 | 16 | use Illuminate\Http\UploadedFile; |
|
19 | 20 |
|
20 | 21 | use function Modules\Nfse\Http\Controllers\storage_path; |
21 | 22 |
|
| 23 | + use Modules\Nfse\Support\IbgeLocalities; |
22 | 24 | use Modules\Nfse\Tests\TestCase; |
23 | 25 |
|
24 | 26 | final class SettingsControllerTest extends TestCase |
@@ -78,6 +80,133 @@ public function runPrepareNfseInput(array $nfseInput, bool $isReplacingCertifica |
78 | 80 | self::assertSame('', $prepared['bao_secret_id']); |
79 | 81 | } |
80 | 82 |
|
| 83 | + public function testUfsReturnsMappedAndSortedDataWhenIbgeRowsAreAvailable(): void |
| 84 | + { |
| 85 | + $controller = new class () extends SettingsController { |
| 86 | + protected function fetchUfsRows(): array |
| 87 | + { |
| 88 | + return [ |
| 89 | + ['sigla' => 'sp', 'nome' => 'Sao Paulo'], |
| 90 | + ['sigla' => 'rj', 'nome' => 'Rio de Janeiro'], |
| 91 | + ['sigla' => 'x', 'nome' => 'Invalido'], |
| 92 | + ]; |
| 93 | + } |
| 94 | + |
| 95 | + protected function jsonResponse(array $payload, int $status = 200): JsonResponse |
| 96 | + { |
| 97 | + return new JsonResponse($payload, $status); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + $response = $controller->ufs(new IbgeLocalities()); |
| 102 | + |
| 103 | + self::assertSame(200, $response->getStatusCode()); |
| 104 | + self::assertSame([ |
| 105 | + 'data' => [ |
| 106 | + ['uf' => 'RJ', 'name' => 'Rio de Janeiro'], |
| 107 | + ['uf' => 'SP', 'name' => 'Sao Paulo'], |
| 108 | + ], |
| 109 | + ], $response->getData(true)); |
| 110 | + } |
| 111 | + |
| 112 | + public function testUfsReturnsFallbackWhenIbgeRequestFails(): void |
| 113 | + { |
| 114 | + $controller = new class () extends SettingsController { |
| 115 | + protected function fetchUfsRows(): array |
| 116 | + { |
| 117 | + throw new \RuntimeException('ibge unavailable'); |
| 118 | + } |
| 119 | + |
| 120 | + protected function jsonResponse(array $payload, int $status = 200): JsonResponse |
| 121 | + { |
| 122 | + return new JsonResponse($payload, $status); |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + $response = $controller->ufs(new IbgeLocalities()); |
| 127 | + |
| 128 | + self::assertSame(502, $response->getStatusCode()); |
| 129 | + self::assertSame([ |
| 130 | + 'data' => [], |
| 131 | + 'message' => 'Failed to load UFs from IBGE.', |
| 132 | + ], $response->getData(true)); |
| 133 | + } |
| 134 | + |
| 135 | + public function testMunicipalitiesReturnsInvalidUfResponseWhenUfFormatIsInvalid(): void |
| 136 | + { |
| 137 | + $controller = new class () extends SettingsController { |
| 138 | + protected function jsonResponse(array $payload, int $status = 200): JsonResponse |
| 139 | + { |
| 140 | + return new JsonResponse($payload, $status); |
| 141 | + } |
| 142 | + }; |
| 143 | + |
| 144 | + $response = $controller->municipalities('r', new IbgeLocalities()); |
| 145 | + |
| 146 | + self::assertSame(422, $response->getStatusCode()); |
| 147 | + self::assertSame([ |
| 148 | + 'data' => [], |
| 149 | + 'message' => 'Invalid UF.', |
| 150 | + ], $response->getData(true)); |
| 151 | + } |
| 152 | + |
| 153 | + public function testMunicipalitiesReturnsMappedDataWhenIbgeRowsAreAvailable(): void |
| 154 | + { |
| 155 | + $controller = new class () extends SettingsController { |
| 156 | + public string $receivedUf = ''; |
| 157 | + |
| 158 | + protected function fetchMunicipalitiesRows(string $normalizedUf): array |
| 159 | + { |
| 160 | + $this->receivedUf = $normalizedUf; |
| 161 | + |
| 162 | + return [ |
| 163 | + ['id' => '3303302', 'nome' => 'Niteroi'], |
| 164 | + ['id' => '3304557', 'nome' => 'Rio de Janeiro'], |
| 165 | + ['id' => '', 'nome' => 'Invalido'], |
| 166 | + ]; |
| 167 | + } |
| 168 | + |
| 169 | + protected function jsonResponse(array $payload, int $status = 200): JsonResponse |
| 170 | + { |
| 171 | + return new JsonResponse($payload, $status); |
| 172 | + } |
| 173 | + }; |
| 174 | + |
| 175 | + $response = $controller->municipalities('rj', new IbgeLocalities()); |
| 176 | + |
| 177 | + self::assertSame('RJ', $controller->receivedUf); |
| 178 | + self::assertSame(200, $response->getStatusCode()); |
| 179 | + self::assertSame([ |
| 180 | + 'data' => [ |
| 181 | + ['ibge_code' => '3303302', 'name' => 'Niteroi'], |
| 182 | + ['ibge_code' => '3304557', 'name' => 'Rio de Janeiro'], |
| 183 | + ], |
| 184 | + ], $response->getData(true)); |
| 185 | + } |
| 186 | + |
| 187 | + public function testMunicipalitiesReturnsFallbackWhenIbgeRequestFails(): void |
| 188 | + { |
| 189 | + $controller = new class () extends SettingsController { |
| 190 | + protected function fetchMunicipalitiesRows(string $normalizedUf): array |
| 191 | + { |
| 192 | + throw new \RuntimeException('ibge unavailable'); |
| 193 | + } |
| 194 | + |
| 195 | + protected function jsonResponse(array $payload, int $status = 200): JsonResponse |
| 196 | + { |
| 197 | + return new JsonResponse($payload, $status); |
| 198 | + } |
| 199 | + }; |
| 200 | + |
| 201 | + $response = $controller->municipalities('rj', new IbgeLocalities()); |
| 202 | + |
| 203 | + self::assertSame(502, $response->getStatusCode()); |
| 204 | + self::assertSame([ |
| 205 | + 'data' => [], |
| 206 | + 'message' => 'Failed to load municipalities from IBGE.', |
| 207 | + ], $response->getData(true)); |
| 208 | + } |
| 209 | + |
81 | 210 | public function testReplaceCertificateCyclePurgesOldArtifactsClearsSettingsAndStoresNewCertificate(): void |
82 | 211 | { |
83 | 212 | ControllerIsolationState::reset(); |
|
0 commit comments