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/BatchTest.php | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 tests/BatchTest.php (limited to 'tests/BatchTest.php') diff --git a/tests/BatchTest.php b/tests/BatchTest.php new file mode 100644 index 0000000..95b25ff --- /dev/null +++ b/tests/BatchTest.php @@ -0,0 +1,139 @@ +server->respond('/csv?ceps=01001000%2C99999999%2C20040020', [self::csv([ + '01001000,Praça da Sé,lado ímpar,,Sé,São Paulo,SP,São Paulo,Sudeste,3550308,1004,11,7107', + '99999999,,,,,,,,,,,,', + '20040020,Avenida Rio Branco,,,Centro,Rio de Janeiro,RJ,Rio de Janeiro,Sudeste,3304557,,21,6001', + ])]); + + $result = $this->client->lookupMany(['01001000', '99999999', '20040020']); + + self::assertCount(3, $result); + self::assertNotNull($result[0]); + self::assertSame('São Paulo', $result[0]->localidade); + self::assertNull($result[1]); + self::assertNotNull($result[2]); + self::assertSame('RJ', $result[2]->uf); + self::assertSame('Avenida Rio Branco', $result[2]->logradouro); + } + + public function testMissingEntryDoesNotTruncateTheArray(): void + { + $this->server->respond('/csv?ceps=99999999%2C01001000', [self::csv([ + '99999999,,,,,,,,,,,,', + '01001000,Praça da Sé,,,Sé,São Paulo,SP,São Paulo,Sudeste,1,,11,1', + ])]); + + $result = $this->client->lookupMany(['99999999', '01001000']); + + self::assertCount(2, $result); + self::assertNull($result[0]); + self::assertNotNull($result[1]); + self::assertSame([0, 1], array_keys($result)); + } + + public function testBatchCarriesNoCoordinate(): void + { + $this->server->respond('/csv?ceps=01001000', [self::csv([ + '01001000,Praça da Sé,,,Sé,São Paulo,SP,São Paulo,Sudeste,1,,11,1', + ])]); + + $first = $this->client->lookupMany(['01001000'])[0]; + self::assertNotNull($first); + self::assertNull($first->geo); + } + + public function testFieldWithCommaIsParsed(): void + { + $this->server->respond('/csv?ceps=01001000', [self::csv([ + '01001000,"Rua Um, Dois",,,Centro,São Paulo,SP,São Paulo,Sudeste,1,,11,1', + ])]); + + $first = $this->client->lookupMany(['01001000'])[0]; + self::assertNotNull($first); + self::assertSame('Rua Um, Dois', $first->logradouro); + } + + public function testEscapedQuoteIsParsed(): void + { + $this->server->respond('/csv?ceps=01001000', [self::csv([ + '01001000,"Rua ""Dom"" Pedro",,,Centro,São Paulo,SP,São Paulo,Sudeste,1,,11,1', + ])]); + + $first = $this->client->lookupMany(['01001000'])[0]; + self::assertNotNull($first); + self::assertSame('Rua "Dom" Pedro', $first->logradouro); + } + + public function testTrailingBackslashDoesNotSwallowTheQuote(): void + { + $this->server->respond('/csv?ceps=01001000', [self::csv([ + '01001000,"Rua Barra\\",,,Centro,São Paulo,SP,São Paulo,Sudeste,1,,11,1', + ])]); + + $first = $this->client->lookupMany(['01001000'])[0]; + self::assertNotNull($first); + self::assertSame('Rua Barra\\', $first->logradouro); + self::assertSame('São Paulo', $first->localidade); + } + + public function testEmptyListMakesNoRequest(): void + { + self::assertSame([], $this->client->lookupMany([])); + self::assertSame([], $this->server->calls()); + } + + public function testChunksAboveTheApiLimit(): void + { + $ceps = []; + for ($i = 0; $i < 501; $i++) { + $ceps[] = sprintf('%08d', $i); + } + $this->server->respond('*', [['kind' => 'csv_echo']]); + + $result = $this->client->lookupMany($ceps); + + self::assertCount(501, $result); + self::assertSame(2, count($this->server->calls())); + self::assertNotNull($result[0]); + self::assertSame('00000000', $result[0]->cep); + self::assertNotNull($result[500]); + self::assertSame('00000500', $result[500]->cep); + } + + public function testTruncatedResponseDoesNotShift(): void + { + $this->server->respond('/csv?ceps=01001000%2C20040020', [self::csv([ + '01001000,,,,,São Paulo,SP,São Paulo,Sudeste,1,,11,1', + ])]); + + $result = $this->client->lookupMany(['01001000', '20040020']); + + self::assertCount(2, $result); + self::assertNotNull($result[0]); + self::assertSame('01001000', $result[0]->cep); + self::assertNull($result[1]); + } + + public function testMalformedCepIsNullNotAnException(): void + { + $this->server->respond('/csv?ceps=01001000%2Cnot-a-cep', [self::csv([ + '01001000,Praça da Sé,,,Sé,São Paulo,SP,São Paulo,Sudeste,1,,11,1', + 'not-a-cep,,,,,,,,,,,,', + ])]); + + $result = $this->client->lookupMany(['01001000', 'not-a-cep']); + + self::assertCount(2, $result); + self::assertNotNull($result[0]); + self::assertNull($result[1]); + } +} -- cgit v1.3