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
|
<?php
declare(strict_types=1);
namespace Cepis\Tests;
use Cepis\Client;
use PHPUnit\Framework\TestCase;
/**
* A fake server and a client whose backoff is near zero.
*
* The backoff is tiny because what is under test is the retry logic, not
* `usleep`'s ability to sleep.
*/
abstract class ServerTestCase extends TestCase
{
/**
* A CEP as the API returns it, used as the fixture across the suite. The
* field names stay Portuguese because they mirror the API's JSON.
*/
protected const SE_ADDRESS = [
'cep' => '01001000',
'logradouro' => 'Praça da Sé',
'complemento' => 'lado ímpar',
'unidade' => '',
'bairro' => 'Sé',
'localidade' => 'São Paulo',
'uf' => 'SP',
'estado' => 'São Paulo',
'regiao' => 'Sudeste',
'ibge' => '3550308',
'gia' => '1004',
'ddd' => '11',
'siafi' => '7107',
];
protected const CSV_HEADER = 'cep,logradouro,complemento,unidade,bairro,localidade,uf,'
. 'estado,regiao,ibge,gia,ddd,siafi';
protected TestServer $server;
protected Client $client;
protected function setUp(): void
{
$this->server = new TestServer();
$this->client = new Client(
baseUrl: $this->server->url,
timeout: 5.0,
backoffBase: 0.001,
backoffMax: 0.01,
);
}
protected function tearDown(): void
{
$this->server->stop();
}
/**
* @param array<string, mixed> $body
* @param array<string, string> $headers
*
* @return array<string, mixed>
*/
protected static function json(array $body, int $status = 200, array $headers = []): array
{
return [
'status' => $status,
'body' => json_encode($body, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
'headers' => $headers,
'content_type' => 'application/json; charset=utf-8',
];
}
/**
* @param array<string, string> $headers
*
* @return array<string, mixed>
*/
protected static function raw(int $status, string $body, array $headers = []): array
{
return ['status' => $status, 'body' => $body, 'headers' => $headers];
}
/**
* @param list<string> $rows
*
* @return array<string, mixed>
*/
protected static function csv(array $rows): array
{
return [
'status' => 200,
'body' => self::CSV_HEADER . "\n" . implode("\n", $rows) . "\n",
'headers' => [],
'content_type' => 'text/csv; charset=utf-8',
];
}
protected static function monotonic(): float
{
return hrtime(true) / 1_000_000_000;
}
}
|