From 64764fb9fac0227f8e0443e7c7519d73aec275b9 Mon Sep 17 00:00:00 2001 From: Murilo Ijanc Date: Sun, 26 Jul 2026 14:58:55 -0300 Subject: Initial import --- tests/LookupTest.php | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 tests/LookupTest.php (limited to 'tests/LookupTest.php') diff --git a/tests/LookupTest.php b/tests/LookupTest.php new file mode 100644 index 0000000..9483948 --- /dev/null +++ b/tests/LookupTest.php @@ -0,0 +1,154 @@ +server->respond('/01001000', [self::json(self::SE_ADDRESS)]); + $address = $this->client->lookup('01001-000'); + + self::assertSame('Praça da Sé', $address->logradouro); + self::assertSame('São Paulo', $address->localidade); + self::assertSame('SP', $address->uf); + self::assertSame('01001000', $address->cep); + self::assertSame('01001-000', $address->formattedCep()); + self::assertNull($address->geo); + } + + public function testReadsTheCoordinateWhenPresent(): void + { + $body = self::SE_ADDRESS + []; + $body['geo'] = ['lat' => -23.567202, 'lon' => -46.649659, 'raio_m' => 1084, 'precisao' => 'logradouro']; + $this->server->respond('/01310100', [self::json($body)]); + $address = $this->client->lookup('01310100'); + + self::assertNotNull($address->geo); + self::assertEqualsWithDelta(-23.567202, $address->geo->lat, 1e-9); + self::assertEqualsWithDelta(-46.649659, $address->geo->lon, 1e-9); + self::assertSame(1084, $address->geo->raio_m); + self::assertSame('logradouro', $address->geo->precisao); + self::assertTrue($address->geo->isPrecise()); + } + + public function testMunicipalityCoordinateIsNotPrecise(): void + { + $body = self::SE_ADDRESS; + $body['geo'] = ['lat' => -23.5, 'lon' => -46.6, 'raio_m' => 22000, 'precisao' => 'localidade']; + $this->server->respond('/01001000', [self::json($body)]); + + $geo = $this->client->lookup('01001000')->geo; + self::assertNotNull($geo); + self::assertFalse($geo->isPrecise()); + } + + public function testMalformedGeoDoesNotLoseTheAddress(): void + { + $body = self::SE_ADDRESS; + $body['geo'] = ['lat' => 'not a number']; + $this->server->respond('/01001000', [self::json($body)]); + $address = $this->client->lookup('01001000'); + + self::assertNull($address->geo); + self::assertSame('São Paulo', $address->localidade); + } + + public function testNewApiFieldDoesNotBreak(): void + { + $body = self::SE_ADDRESS; + $body['campo_do_futuro'] = 'value'; + $this->server->respond('/01001000', [self::json($body)]); + $address = $this->client->lookup('01001000'); + + self::assertSame('value', $address->raw['campo_do_futuro']); + self::assertSame('value', $address->field('campo_do_futuro')); + self::assertNull($address->field('nao_existe')); + self::assertSame('São Paulo', $address->localidade); + } + + public function testMissingFieldsBecomeEmptyStringsNotNull(): void + { + $this->server->respond('/01001000', [self::json(['cep' => '01001000'])]); + $address = $this->client->lookup('01001000'); + + self::assertSame('', $address->logradouro); + self::assertSame('', $address->uf); + } + + public function testNotFound(): void + { + $this->server->respond('/99999999', [self::json( + ['erro' => 'cep_nao_encontrado', 'mensagem' => 'CEP não existe na base do DNE'], + 404, + )]); + + try { + $this->client->lookup('99999999'); + self::fail('expected CepNotFoundException'); + } catch (CepNotFoundException $exception) { + self::assertStringContainsString('99999999', $exception->getMessage()); + } + } + + public function testNotFoundIsNotRetried(): void + { + $this->server->respond('/99999999', [self::json(['erro' => 'cep_nao_encontrado'], 404)]); + + $this->expectException(CepNotFoundException::class); + + try { + $this->client->lookup('99999999'); + } finally { + self::assertSame(1, $this->server->countCalls('/99999999')); + } + } + + public function testInvalidCepNeverLeavesTheMachine(): void + { + try { + $this->client->lookup('123'); + self::fail('expected InvalidCepException'); + } catch (InvalidCepException) { + self::assertSame([], $this->server->calls()); + } + } + + public function testApi400BecomesInvalidCep(): void + { + $this->server->respond('/00000000', [self::json( + ['erro' => 'cep_invalido', 'mensagem' => 'rejected by the API'], + 400, + )]); + + try { + $this->client->lookup('00000000'); + self::fail('expected InvalidCepException'); + } catch (InvalidCepException $exception) { + self::assertSame('rejected by the API', $exception->getMessage()); + self::assertSame(1, $this->server->countCalls('/00000000')); + } + } + + public function testResponseThatIsNotJson(): void + { + $this->server->respond('/01001000', [self::raw(200, 'captive portal')]); + + $this->expectException(InvalidResponseException::class); + $this->client->lookup('01001000'); + } + + public function testJsonArrayIsNotAnObject(): void + { + $this->server->respond('/01001000', [self::raw(200, '[1, 2, 3]')]); + + $this->expectException(InvalidResponseException::class); + $this->client->lookup('01001000'); + } +} -- cgit v1.3