Bicycle/tests/Unit/CurrencyRateClientTest.php
2026-08-15 10:39:22 +03:00

63 lines
2.2 KiB
PHP

<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use Services\CurrencyRate\Client;
/**
* Сеть ЦБ РФ недоступна из dev-контейнера (изолированная песочница) — fetch() гарантированно
* уходит в graceful null, это и проверяем (см. Services\CurrencyRate\Client::fetch()). Кэш
* проверяется отдельно, через подложенный файл, без реального похода в сеть.
*/
class CurrencyRateClientTest extends TestCase
{
private string $cache_path;
protected function setUp(): void
{
$this->cache_path = sys_get_temp_dir() . '/currency_rate_test_' . uniqid() . '.json';
}
protected function tearDown(): void
{
if (is_file($this->cache_path)) {
unlink($this->cache_path);
}
}
public function testRubAlwaysReturnsOne(): void
{
$client = new Client(['url' => 'https://unreachable.invalid/', 'timeout' => 1, 'cache_path' => $this->cache_path]);
$this->assertSame(1.0, $client->rate('rub'));
$this->assertSame(1.0, $client->rate('RUB'));
}
public function testUnreachableSourceReturnsNullWithoutThrowing(): void
{
$client = new Client(['url' => 'https://unreachable.invalid/', 'timeout' => 1, 'cache_path' => $this->cache_path]);
$this->assertNull($client->rate('EUR'));
}
public function testReadsFromFreshCacheWithoutNetwork(): void
{
file_put_contents($this->cache_path, json_encode(['EUR' => 104.5]));
// url заведомо невалиден — если бы клиент реально пошёл в сеть, тест бы не прошёл (null).
$client = new Client(['url' => 'not-a-url', 'timeout' => 1, 'cache_path' => $this->cache_path, 'ttl' => 86400]);
$this->assertSame(104.5, $client->rate('EUR'));
}
public function testUnknownCodeReturnsNull(): void
{
file_put_contents($this->cache_path, json_encode(['EUR' => 104.5]));
$client = new Client(['url' => 'not-a-url', 'timeout' => 1, 'cache_path' => $this->cache_path, 'ttl' => 86400]);
$this->assertNull($client->rate('XXX'));
}
}