95 lines
3.2 KiB
PHP
95 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\BusProNet\DataProvider;
|
|
|
|
use App\BusProNet\DataProvider\CountryDataProvider;
|
|
use App\MyEp\Api\AccessTokenProvider;
|
|
use App\MyEp\Api\ApiClient;
|
|
use App\MyEp\Api\ResponseMapper;
|
|
use App\MyEp\Cache\StaleWhileErrorCache;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\NullLogger;
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
|
use Symfony\Component\HttpClient\MockHttpClient;
|
|
use Symfony\Component\HttpClient\Response\MockResponse;
|
|
|
|
class CountryDataProviderTest extends TestCase
|
|
{
|
|
public function testCountriesAreKeyedByTheRawToken(): void
|
|
{
|
|
$provider = $this->provider([$this->countriesResponse()]);
|
|
|
|
$countries = $provider->getAll();
|
|
|
|
// the token, not an ISO code: this is the vocabulary a hotel's country and a
|
|
// teamer's nationality join against
|
|
$this->assertEquals(['D', 'A', 'CH'], array_keys($countries));
|
|
$this->assertEquals('Deutschland', $countries['D']->getName());
|
|
$this->assertEquals('deutsch', $countries['D']->getNationality());
|
|
}
|
|
|
|
public function testGetResolvesByTokenAndToleratesNull(): void
|
|
{
|
|
$provider = $this->provider([$this->countriesResponse()]);
|
|
|
|
$this->assertEquals('Österreich', $provider->get('A')->getName());
|
|
$this->assertNull($provider->get('XX'));
|
|
$this->assertNull($provider->get(null));
|
|
}
|
|
|
|
public function testACountryWithoutATokenIsSkipped(): void
|
|
{
|
|
$provider = $this->provider([new MockResponse(json_encode([
|
|
['id' => 1, 'name' => 'Ohne Token'],
|
|
['id' => 2, 'name' => 'Schweiz', 'token' => 'CH'],
|
|
]))]);
|
|
|
|
$this->assertEquals(['CH'], array_keys($provider->getAll()));
|
|
}
|
|
|
|
public function testAFailingRequestServesTheLastKnownCountries(): void
|
|
{
|
|
$pool = new ArrayAdapter();
|
|
$this->provider([$this->countriesResponse()], $pool)->getAll();
|
|
|
|
$pool->deleteItem('myep_countries');
|
|
$failing = $this->provider([new MockResponse('', ['http_code' => 503])], $pool);
|
|
|
|
$this->assertCount(3, $failing->getAll());
|
|
}
|
|
|
|
private function countriesResponse(): MockResponse
|
|
{
|
|
return new MockResponse(json_encode([
|
|
['id' => 1, 'name' => 'Deutschland', 'token' => 'D', 'nationality' => 'deutsch'],
|
|
['id' => 2, 'name' => 'Österreich', 'token' => 'A', 'nationality' => 'österreichisch'],
|
|
['id' => 3, 'name' => 'Schweiz', 'token' => 'CH', 'nationality' => 'schweizerisch'],
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* @param list<MockResponse> $responses
|
|
*/
|
|
private function provider(array $responses, ?ArrayAdapter $pool = null): CountryDataProvider
|
|
{
|
|
$pool ??= new ArrayAdapter();
|
|
$logger = new NullLogger();
|
|
|
|
$httpClient = new MockHttpClient(array_merge(
|
|
[new MockResponse(json_encode(['access_token' => 'token', 'expires_in' => 600]))],
|
|
$responses,
|
|
));
|
|
|
|
$tokens = new AccessTokenProvider($httpClient, new ArrayAdapter(), $logger, [
|
|
'client_id' => 'id',
|
|
'client_secret' => 'secret',
|
|
]);
|
|
|
|
return new CountryDataProvider(
|
|
new ApiClient($httpClient, $tokens, $logger),
|
|
new ResponseMapper($logger),
|
|
new StaleWhileErrorCache($pool, $logger),
|
|
);
|
|
}
|
|
}
|