Skip to content

Commit

Permalink
PAYOSWXP-47: unit-test: make payment-method-filter-service more flexibel
Browse files Browse the repository at this point in the history
… to test multiple payment-handlers with one test
  • Loading branch information
rommelfreddy authored and janteuber committed Mar 22, 2024
1 parent c6d55f7 commit 3e5173e
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 53 deletions.
105 changes: 70 additions & 35 deletions tests/Components/PaymentFilter/AbstractPaymentFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ abstract class AbstractPaymentFilterTest extends TestCase
use PayoneTestBehavior;
use ConfigurationHelper;

public function testItHidesPaymentMethodForNotAllowedCountry(): void
/**
* @dataProvider dataProviderPaymentHandlerClasses
*/
public function testItHidesPaymentMethodForNotAllowedCountry(string $paymentHandlerClass): void
{
$methods = $this->getPaymentMethods();
if (!$this->getDisallowedBillingCountry()) {
static::assertTrue(true);

return;
}

$methods = $this->getPaymentMethods($paymentHandlerClass);

$country = new CountryEntity();
$country->setIso($this->getDisallowedBillingCountry());
Expand All @@ -39,15 +48,24 @@ public function testItHidesPaymentMethodForNotAllowedCountry(): void
$this->getAllowedCurrency()
);

$result = $this->getFilterService()->filterPaymentMethods($methods, $filterContext);
$result = $this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);

static::assertNotInPaymentCollection($this->getPaymentHandlerClass(), $result);
static::assertNotInPaymentCollection($paymentHandlerClass, $result);
static::assertInPaymentCollection(PaymentHandlerMock::class, $result);
}

public function testItHidesPaymentMethodForNotAllowedCurrency(): void
/**
* @dataProvider dataProviderPaymentHandlerClasses
*/
public function testItHidesPaymentMethodForNotAllowedCurrency(string $paymentHandlerClass): void
{
$methods = $this->getPaymentMethods();
if (!$this->getDisallowedCurrency()) {
static::assertTrue(true);

return;
}

$methods = $this->getPaymentMethods($paymentHandlerClass);

$country = new CountryEntity();
$country->setIso($this->getAllowedBillingCountry());
Expand All @@ -62,19 +80,21 @@ public function testItHidesPaymentMethodForNotAllowedCurrency(): void
$this->getDisallowedCurrency()
);

$result = $this->getFilterService()->filterPaymentMethods($methods, $filterContext);
$result = $this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);

static::assertNotInPaymentCollection($this->getPaymentHandlerClass(), $result);
static::assertNotInPaymentCollection($paymentHandlerClass, $result);
static::assertInPaymentCollection(PaymentHandlerMock::class, $result);
}

/**
* @dataProvider notAllowedValues
* @testdox It hides payment method for not allowed value $notAllowedValue on checkout
*/
public function testItHidesPaymentMethodForNotAllowedValueOnCheckout(float $notAllowedValue): void
public function testItHidesPaymentMethodForNotAllowedValueOnCheckout(float $notAllowedValue, ?string $paymentHandlerClass = null): void
{
$methods = $this->getPaymentMethods();
$paymentHandlerClass ??= $this->getPaymentHandlerClasses()[0];

$methods = $this->getPaymentMethods($paymentHandlerClass);

$country = new CountryEntity();
$country->setIso($this->getAllowedBillingCountry());
Expand All @@ -97,19 +117,20 @@ public function testItHidesPaymentMethodForNotAllowedValueOnCheckout(float $notA
$cart
);

$result = $this->getFilterService()->filterPaymentMethods($methods, $filterContext);
$result = $this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);

static::assertNotInPaymentCollection($this->getPaymentHandlerClass(), $result);
static::assertNotInPaymentCollection($paymentHandlerClass, $result);
static::assertInPaymentCollection(PaymentHandlerMock::class, $result);
}

/**
* @dataProvider notAllowedValues
* @testdox It hides payment method for not allowed value $notAllowedValue on edit order page
*/
public function testItHidesPaymentMethodForNotAllowedValueOnEditOrderPage(float $notAllowedValue): void
public function testItHidesPaymentMethodForNotAllowedValueOnEditOrderPage(float $notAllowedValue, ?string $paymentHandlerClass = null): void
{
$methods = $this->getPaymentMethods();
$paymentHandlerClass ??= $this->getPaymentHandlerClasses()[0];
$methods = $this->getPaymentMethods($paymentHandlerClass);

$country = new CountryEntity();
$country->setIso($this->getAllowedBillingCountry());
Expand All @@ -131,15 +152,18 @@ public function testItHidesPaymentMethodForNotAllowedValueOnEditOrderPage(float
$order
);

$result = $this->getFilterService()->filterPaymentMethods($methods, $filterContext);
$result = $this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);

static::assertNotInPaymentCollection($this->getPaymentHandlerClass(), $result);
static::assertNotInPaymentCollection($paymentHandlerClass, $result);
static::assertInPaymentCollection(PaymentHandlerMock::class, $result);
}

public function testItNotHidesPaymentMethodForAllowedConditionsOnCheckout(): void
/**
* @dataProvider dataProviderPaymentHandlerClasses
*/
public function testItNotHidesPaymentMethodForAllowedConditionsOnCheckout(string $paymentHandlerClass): void
{
$methods = $this->getPaymentMethods();
$methods = $this->getPaymentMethods($paymentHandlerClass);

$country = new CountryEntity();
$country->setIso($this->getAllowedBillingCountry());
Expand All @@ -162,15 +186,18 @@ public function testItNotHidesPaymentMethodForAllowedConditionsOnCheckout(): voi
$cart
);

$result = $this->getFilterService()->filterPaymentMethods($methods, $filterContext);
$result = $this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);

static::assertInPaymentCollection($this->getPaymentHandlerClass(), $result);
static::assertInPaymentCollection($paymentHandlerClass, $result);
static::assertInPaymentCollection(PaymentHandlerMock::class, $result);
}

public function testItNotHidesPaymentMethodForAllowedConditionsOnEditOrderPage(): void
/**
* @dataProvider dataProviderPaymentHandlerClasses
*/
public function testItNotHidesPaymentMethodForAllowedConditionsOnEditOrderPage(string $paymentHandlerClass): void
{
$methods = $this->getPaymentMethods();
$methods = $this->getPaymentMethods($paymentHandlerClass);

$country = new CountryEntity();
$country->setIso($this->getAllowedBillingCountry());
Expand All @@ -192,9 +219,9 @@ public function testItNotHidesPaymentMethodForAllowedConditionsOnEditOrderPage()
$order
);

$result = $this->getFilterService()->filterPaymentMethods($methods, $filterContext);
$result = $this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);

static::assertInPaymentCollection($this->getPaymentHandlerClass(), $result);
static::assertInPaymentCollection($paymentHandlerClass, $result);
static::assertInPaymentCollection(PaymentHandlerMock::class, $result);
}

Expand All @@ -217,13 +244,13 @@ public function notAllowedValues(): \Generator
];
}

abstract protected function getFilterService(): PaymentFilterServiceInterface;
abstract protected function getFilterService(?string $paymentHandlerClass = null): PaymentFilterServiceInterface;

abstract protected function getDisallowedBillingCountry(): string;
abstract protected function getDisallowedBillingCountry(): ?string;

abstract protected function getAllowedBillingCountry(): string;

abstract protected function getDisallowedCurrency(): CurrencyEntity;
abstract protected function getDisallowedCurrency(): ?CurrencyEntity;

abstract protected function getAllowedCurrency(): CurrencyEntity;

Expand All @@ -234,37 +261,45 @@ abstract protected function getTooHighValue(): ?float;
abstract protected function getAllowedValue(): float;

/**
* @return class-string
* @return array<<array<class-string>>
*/
abstract protected function getPaymentHandlerClass(): string;
abstract protected function getPaymentHandlerClasses(): array;

/**
* @return array<<class-string>
*/
final protected function dataProviderPaymentHandlerClasses(): array
{
return array_map(static fn ($handler) => [$handler], $this->getPaymentHandlerClasses());
}

protected function getPaymentMethods(): PaymentMethodCollection
protected function getPaymentMethods(string $paymentHandlerClass): PaymentMethodCollection
{
$paymentMethod1 = new PaymentMethodEntity();
$paymentMethod1->setId(Uuid::randomHex());
$paymentMethod1->setHandlerIdentifier(PaymentHandlerMock::class);

$paymentMethod2 = new PaymentMethodEntity();
$paymentMethod2->setId(Uuid::randomHex());
$paymentMethod2->setHandlerIdentifier($this->getPaymentHandlerClass());
$paymentMethod2->setHandlerIdentifier($paymentHandlerClass);

return new PaymentMethodCollection([
$paymentMethod1,
$paymentMethod2,
]);
}

protected static function assertInPaymentCollection(string $paymentHandler, PaymentMethodCollection $paymentMethods): void
protected static function assertInPaymentCollection(string $paymentHandler, PaymentMethodCollection $paymentMethods, string $message = ''): void
{
static::assertSame(1, $paymentMethods->filter(
static fn (PaymentMethodEntity $paymentMethod) => $paymentMethod->getHandlerIdentifier() === $paymentHandler
)->count());
)->count(), $message);
}

protected static function assertNotInPaymentCollection(string $paymentHandler, PaymentMethodCollection $paymentMethods): void
protected static function assertNotInPaymentCollection(string $paymentHandler, PaymentMethodCollection $paymentMethods, string $message = ''): void
{
static::assertSame(0, $paymentMethods->filter(
static fn (PaymentMethodEntity $paymentMethod) => $paymentMethod->getHandlerIdentifier() === $paymentHandler
)->count());
)->count(), $message);
}
}
6 changes: 3 additions & 3 deletions tests/Components/PaymentFilter/KlarnaPaymentFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class KlarnaPaymentFilterTest extends AbstractPaymentFilterTest
{
protected function getFilterService(): PaymentFilterServiceInterface
protected function getFilterService(?string $paymentHandlerClass = null): PaymentFilterServiceInterface
{
return $this->getContainer()->get('payone.payment_filter_method.klarna');
}
Expand Down Expand Up @@ -58,8 +58,8 @@ protected function getAllowedValue(): float
return 100.0;
}

protected function getPaymentHandlerClass(): string
protected function getPaymentHandlerClasses(): array
{
return PayoneKlarnaInvoicePaymentHandler::class;
return [PayoneKlarnaInvoicePaymentHandler::class];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class PostfinancePaymentFilterTest extends AbstractPaymentFilterTest
{
protected function getFilterService(): PaymentFilterServiceInterface
protected function getFilterService(?string $paymentHandlerClass = null): PaymentFilterServiceInterface
{
return $this->getContainer()->get('payone.payment_filter_method.postfinance');
}
Expand Down Expand Up @@ -58,8 +58,8 @@ protected function getAllowedValue(): float
return 100.0;
}

protected function getPaymentHandlerClass(): string
protected function getPaymentHandlerClasses(): array
{
return PayonePostfinanceCardPaymentHandler::class;
return [PayonePostfinanceCardPaymentHandler::class];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Przelewy24PaymentFilterTest extends AbstractPaymentFilterTest
{
protected function getFilterService(): PaymentFilterServiceInterface
protected function getFilterService(?string $paymentHandlerClass = null): PaymentFilterServiceInterface
{
return $this->getContainer()->get('payone.payment_filter_method.przelewy24');
}
Expand Down Expand Up @@ -58,8 +58,8 @@ protected function getAllowedValue(): float
return 100.0;
}

protected function getPaymentHandlerClass(): string
protected function getPaymentHandlerClasses(): array
{
return PayonePrzelewy24PaymentHandler::class;
return [PayonePrzelewy24PaymentHandler::class];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class SecuredDirectDebitPaymentFilterTest extends AbstractPaymentFilterTest
{
protected function getFilterService(): PaymentFilterServiceInterface
protected function getFilterService(?string $paymentHandlerClass = null): PaymentFilterServiceInterface
{
return $this->getContainer()->get('payone.payment_filter_method.secured_direct_debit');
}
Expand Down Expand Up @@ -58,8 +58,8 @@ protected function getAllowedValue(): float
return 100.0;
}

protected function getPaymentHandlerClass(): string
protected function getPaymentHandlerClasses(): array
{
return PayoneSecuredDirectDebitPaymentHandler::class;
return [PayoneSecuredDirectDebitPaymentHandler::class];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class SecuredInstallmentPaymentFilterTest extends AbstractPaymentFilterTest
{
protected function getFilterService(): PaymentFilterServiceInterface
protected function getFilterService(?string $paymentHandlerClass = null): PaymentFilterServiceInterface
{
return $this->getContainer()->get('payone.payment_filter_method.secured_installment');
}
Expand Down Expand Up @@ -58,8 +58,8 @@ protected function getAllowedValue(): float
return 300.0;
}

protected function getPaymentHandlerClass(): string
protected function getPaymentHandlerClasses(): array
{
return PayoneSecuredInstallmentPaymentHandler::class;
return [PayoneSecuredInstallmentPaymentHandler::class];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class SecuredInvoicePaymentFilterTest extends AbstractPaymentFilterTest
{
protected function getFilterService(): PaymentFilterServiceInterface
protected function getFilterService(?string $paymentHandlerClass = null): PaymentFilterServiceInterface
{
return $this->getContainer()->get('payone.payment_filter_method.secured_invoice');
}
Expand Down Expand Up @@ -58,8 +58,8 @@ protected function getAllowedValue(): float
return 100.0;
}

protected function getPaymentHandlerClass(): string
protected function getPaymentHandlerClasses(): array
{
return PayoneSecuredInvoicePaymentHandler::class;
return [PayoneSecuredInvoicePaymentHandler::class];
}
}

0 comments on commit 3e5173e

Please sign in to comment.