87 lines
3.1 KiB
PHP
87 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Service;
|
|
|
|
use App\BusProNet\ApiClient;
|
|
use App\BusProNet\Model\Notification;
|
|
use App\BusProNet\Model\VoucherPaymentMethod;
|
|
use App\BusProNet\Model\VoucherTemplate;
|
|
use App\BusProNet\Model\VoucherTemplates;
|
|
use App\Exception\VoucherShopUnavailableException;
|
|
use App\Service\VoucherTemplateProvider;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\NullLogger;
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
|
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
|
|
|
|
class VoucherTemplateProviderTest extends TestCase
|
|
{
|
|
public function testTemplatesAreCached(): void
|
|
{
|
|
$templates = new VoucherTemplates(10, 5000, [new VoucherTemplate(3, 'K', 'Allgemein')]);
|
|
$apiClient = $this->createMock(ApiClient::class);
|
|
$apiClient->expects(self::once())->method('getVoucherTemplates')->willReturn($templates);
|
|
|
|
$provider = $this->createProvider($apiClient);
|
|
|
|
self::assertEquals($templates, $provider->getTemplates());
|
|
self::assertEquals($templates, $provider->getTemplates());
|
|
}
|
|
|
|
public function testARejectionIsNotCached(): void
|
|
{
|
|
$templates = new VoucherTemplates(10, 5000, [new VoucherTemplate(3, 'K', 'Allgemein')]);
|
|
$apiClient = $this->createMock(ApiClient::class);
|
|
$apiClient->expects(self::exactly(2))->method('getVoucherTemplates')
|
|
->willReturnOnConsecutiveCalls(new Notification(104, 'User fehlerhaft'), $templates);
|
|
|
|
$provider = $this->createProvider($apiClient);
|
|
|
|
try {
|
|
$provider->getTemplates();
|
|
self::fail('Expected a VoucherShopUnavailableException');
|
|
} catch (VoucherShopUnavailableException) {
|
|
}
|
|
|
|
self::assertEquals($templates, $provider->getTemplates());
|
|
}
|
|
|
|
public function testNoTemplatesOnSaleMeansNoShop(): void
|
|
{
|
|
$apiClient = $this->createStub(ApiClient::class);
|
|
$apiClient->method('getVoucherTemplates')->willReturn(new VoucherTemplates(10, 5000, []));
|
|
|
|
$this->expectException(VoucherShopUnavailableException::class);
|
|
|
|
$this->createProvider($apiClient)->getTemplates();
|
|
}
|
|
|
|
public function testThePaymentMethodIsTransfer(): void
|
|
{
|
|
$apiClient = $this->createStub(ApiClient::class);
|
|
$apiClient->method('getVoucherPaymentMethods')->willReturn([
|
|
new VoucherPaymentMethod(1, 'Lastschrift', 'LAST'),
|
|
new VoucherPaymentMethod(2, 'Überweisung', 'UEBERW'),
|
|
]);
|
|
|
|
self::assertSame(2, $this->createProvider($apiClient)->getPaymentMethod()->id);
|
|
}
|
|
|
|
public function testWithoutTransferThereIsNoShop(): void
|
|
{
|
|
$apiClient = $this->createStub(ApiClient::class);
|
|
$apiClient->method('getVoucherPaymentMethods')->willReturn([new VoucherPaymentMethod(1, 'Lastschrift', 'LAST')]);
|
|
|
|
$this->expectException(VoucherShopUnavailableException::class);
|
|
|
|
$this->createProvider($apiClient)->getPaymentMethod();
|
|
}
|
|
|
|
private function createProvider(ApiClient $apiClient): VoucherTemplateProvider
|
|
{
|
|
return new VoucherTemplateProvider($apiClient, new TagAwareAdapter(new ArrayAdapter()), new NullLogger());
|
|
}
|
|
}
|