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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
<?php
declare(strict_types=1);
namespace Cepis\Tests;
use Cepis\CepNotFoundException;
use Cepis\InvalidCepException;
use Cepis\InvalidResponseException;
final class LookupTest extends ServerTestCase
{
public function testSuccessfulLookup(): void
{
$this->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, '<html>captive portal</html>')]);
$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');
}
}
|