feat: read BusProNet base data from the MyE&P API
This commit is contained in:
1 parent
1505f9b50b
commit
2e9328fc51
37 files changed
+2302
-148
No files matched your search
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\BusProNet\DataProvider;
|
||||
|
||||
use App\BusProNet\DataProvider\PickupDataProvider;
|
||||
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 PickupDataProviderTest extends TestCase
|
||||
{
|
||||
public function testPickupsAreKeyedByBusProId(): void
|
||||
{
|
||||
$provider = $this->provider([$this->pickupsResponse()]);
|
||||
|
||||
$pickups = $provider->getAll();
|
||||
|
||||
$this->assertEquals([4, 27], array_keys($pickups));
|
||||
$this->assertEquals('Karlsruhe', $pickups[4]->getCity());
|
||||
$this->assertEquals('A5 - Autohof Bruchsal', $pickups[4]->getStreet());
|
||||
$this->assertEquals(4, $pickups[4]->getBusProId());
|
||||
}
|
||||
|
||||
public function testGetResolvesASingleBoardingPoint(): void
|
||||
{
|
||||
$provider = $this->provider([$this->pickupsResponse()]);
|
||||
|
||||
$this->assertEquals('Karlsruhe', $provider->get(4)->getCity());
|
||||
$this->assertNull($provider->get(999));
|
||||
}
|
||||
|
||||
public function testAPickupWithoutAnIdIsSkipped(): void
|
||||
{
|
||||
$provider = $this->provider([new MockResponse(json_encode([
|
||||
['city' => 'Nirgendwo'],
|
||||
['id' => 27, 'city' => 'Stuttgart'],
|
||||
]))]);
|
||||
|
||||
$this->assertEquals([27], array_keys($provider->getAll()));
|
||||
}
|
||||
|
||||
public function testAFailingRequestServesTheLastKnownPickups(): void
|
||||
{
|
||||
$pool = new ArrayAdapter();
|
||||
$this->provider([$this->pickupsResponse()], $pool)->getAll();
|
||||
|
||||
$pool->deleteItem('myep_pickups');
|
||||
$failing = $this->provider([new MockResponse('', ['http_code' => 503])], $pool);
|
||||
|
||||
$this->assertCount(2, $failing->getAll());
|
||||
}
|
||||
|
||||
private function pickupsResponse(): MockResponse
|
||||
{
|
||||
return new MockResponse(json_encode([
|
||||
['id' => 4, 'code' => '298', 'city' => 'Karlsruhe', 'street' => 'A5 - Autohof Bruchsal'],
|
||||
['id' => 27, 'code' => '301', 'city' => 'Stuttgart', 'street' => 'Flughafen'],
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<MockResponse> $responses
|
||||
*/
|
||||
private function provider(array $responses, ?ArrayAdapter $pool = null): PickupDataProvider
|
||||
{
|
||||
$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 PickupDataProvider(
|
||||
new ApiClient($httpClient, $tokens, $logger),
|
||||
new ResponseMapper($logger),
|
||||
new StaleWhileErrorCache($pool, $logger),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user