Files
myep/tests/Service/VoucherValidatorTest.php

80 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Service;
use App\BusProNet\Client\VoucherClient;
use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Model\Notification;
use App\BusProNet\Model\PromoVoucher;
use App\BusProNet\Model\PurchaseVoucher;
use App\Service\VoucherValidator;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
class VoucherValidatorTest extends TestCase
{
public function testAcceptedPromoCodeIsCached(): void
{
$client = $this->createMock(VoucherClient::class);
$client->expects(self::once())->method('validatePromoVoucher')
->willReturn(new PromoVoucher('R-TEST26', 'Testen', PromoVoucher::APPLICABILITY_PER_PERSON, 9.99, 0.0, 0.0));
$validator = new VoucherValidator($client, new ArrayAdapter(), new NullLogger());
self::assertTrue($validator->validatePromoVoucher('R-TEST26', 12497, 532.8)->isValid);
self::assertTrue($validator->validatePromoVoucher('R-TEST26', 12497, 532.8)->isValid);
}
public function testRejectedPromoCodeIsAskedAgain(): void
{
// The code is fixed in BusPro between the two attempts
$client = $this->createMock(VoucherClient::class);
$client->expects(self::exactly(2))->method('validatePromoVoucher')
->willReturnOnConsecutiveCalls(
new Notification(633, 'Aktionsgutschein vom Buchungszeitraum ungültig'),
new PromoVoucher('R-TEST26', 'Testen', PromoVoucher::APPLICABILITY_PER_PERSON, 9.99, 0.0, 0.0),
);
$validator = new VoucherValidator($client, new ArrayAdapter(), new NullLogger());
$rejected = $validator->validatePromoVoucher('R-TEST26', 12497, 532.8);
self::assertFalse($rejected->isValid);
self::assertSame(633, $rejected->errorCode);
self::assertTrue($validator->validatePromoVoucher('R-TEST26', 12497, 532.8)->isValid);
}
public function testFailedPromoCheckIsAskedAgain(): void
{
$client = $this->createMock(VoucherClient::class);
$client->expects(self::exactly(2))->method('validatePromoVoucher')
->willReturnOnConsecutiveCalls(
self::throwException(new ApiClientException('Connection refused')),
new PromoVoucher('R-TEST26', 'Testen', PromoVoucher::APPLICABILITY_PER_PERSON, 9.99, 0.0, 0.0),
);
$validator = new VoucherValidator($client, new ArrayAdapter(), new NullLogger());
self::assertFalse($validator->validatePromoVoucher('R-TEST26', 12497, 532.8)->isValid);
self::assertTrue($validator->validatePromoVoucher('R-TEST26', 12497, 532.8)->isValid);
}
public function testRejectedPurchaseCodeIsAskedAgain(): void
{
$client = $this->createMock(VoucherClient::class);
$client->expects(self::exactly(2))->method('validatePurchaseVoucher')
->willReturnOnConsecutiveCalls(
new Notification(995, 'Gutschein nicht gefunden'),
new PurchaseVoucher('80190', '1F7V6PADRZ', 1, 100.0, PurchaseVoucher::TYPE_PURCHASE),
);
$validator = new VoucherValidator($client, new ArrayAdapter(), new NullLogger());
self::assertFalse($validator->validatePurchaseVoucher('1F7V6PADRZ')->isValid);
self::assertTrue($validator->validatePurchaseVoucher('1F7V6PADRZ')->isValid);
}
}