mivita/tests/Unit/Dhl/DhlConfigCachingTest.php

47 lines
1.4 KiB
PHP

<?php
use App\Http\Controllers\SettingController;
beforeEach(function () {
SettingController::flushDhlConfigCache();
});
afterEach(function () {
SettingController::flushDhlConfigCache();
});
it('returns the cached DHL configuration without re-reading settings', function () {
$cached = [
'base_url' => 'https://api-eu.dhl.com',
'api_key' => 'cached-api-key',
'username' => 'cached-user',
'password' => 'cached-password',
'account_numbers' => ['V01PAK' => '63144073550101'],
];
$reflection = new ReflectionClass(SettingController::class);
$property = $reflection->getProperty('cachedDhlConfig');
$property->setAccessible(true);
$property->setValue(null, $cached);
$controller = new SettingController;
expect($controller->getDhlConfig())->toBe($cached);
$secondCall = $controller->getDhlConfig();
expect($secondCall)->toBe($cached);
});
it('flushes the DHL configuration cache on demand', function () {
$reflection = new ReflectionClass(SettingController::class);
$property = $reflection->getProperty('cachedDhlConfig');
$property->setAccessible(true);
$property->setValue(null, ['api_key' => 'cached-api-key']);
expect($property->getValue())->toBe(['api_key' => 'cached-api-key']);
SettingController::flushDhlConfigCache();
expect($property->getValue())->toBeNull();
});