diff options
| author | Murilo Ijanc <[email protected]> | 2026-07-26 14:58:55 -0300 |
|---|---|---|
| committer | Murilo Ijanc <[email protected]> | 2026-07-26 14:58:55 -0300 |
| commit | 64764fb9fac0227f8e0443e7c7519d73aec275b9 (patch) | |
| tree | cb4fc56eeb6ecce53854f9c530609cf01ab3349f /tests/ServerTestCase.php | |
| download | php-64764fb9fac0227f8e0443e7c7519d73aec275b9.tar.gz | |
Initial importv0.1.0
Diffstat (limited to 'tests/ServerTestCase.php')
| -rw-r--r-- | tests/ServerTestCase.php | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/tests/ServerTestCase.php b/tests/ServerTestCase.php new file mode 100644 index 0000000..d000778 --- /dev/null +++ b/tests/ServerTestCase.php @@ -0,0 +1,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; + } +} |