1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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]);
}
}
|