summaryrefslogtreecommitdiffstats
path: root/tests/BatchTest.php
diff options
context:
space:
mode:
authorMurilo Ijanc <[email protected]>2026-07-26 14:58:55 -0300
committerMurilo Ijanc <[email protected]>2026-07-26 14:58:55 -0300
commit64764fb9fac0227f8e0443e7c7519d73aec275b9 (patch)
treecb4fc56eeb6ecce53854f9c530609cf01ab3349f /tests/BatchTest.php
downloadphp-64764fb9fac0227f8e0443e7c7519d73aec275b9.tar.gz
Initial importv0.1.0
Diffstat (limited to 'tests/BatchTest.php')
-rw-r--r--tests/BatchTest.php139
1 files changed, 139 insertions, 0 deletions
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 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Cepis\Tests;
+
+final class BatchTest extends ServerTestCase
+{
+ public function testPreservesOrderAndMarksMissing(): void
+ {
+ $this->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]);
+ }
+}