Skip to content

Commit

Permalink
tests - test coverage for factory classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mustapayev committed Oct 12, 2024
1 parent 16f032d commit 1055b66
Show file tree
Hide file tree
Showing 10 changed files with 443 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/Factory/CryptFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ class CryptFactory
public static function createGatewayCrypt(string $gatewayClass, LoggerInterface $logger): CryptInterface
{
$classMappings = [
ToslaPos::class => ToslaPosCrypt::class,
AkbankPos::class => AkbankPosCrypt::class,
EstV3Pos::class => EstV3PosCrypt::class,
EstPos::class => EstPosCrypt::class,
EstV3Pos::class => EstV3PosCrypt::class,
GarantiPos::class => GarantiPosCrypt::class,
InterPos::class => InterPosCrypt::class,
KuveytPos::class => KuveytPosCrypt::class,
VakifKatilimPos::class => KuveytPosCrypt::class,
PayFlexCPV4Pos::class => PayFlexCPV4Crypt::class,
PayForPos::class => PayForPosCrypt::class,
PosNet::class => PosNetCrypt::class,
PosNetV1Pos::class => PosNetV1PosCrypt::class,
PayFlexCPV4Pos::class => PayFlexCPV4Crypt::class,
ToslaPos::class => ToslaPosCrypt::class,
VakifKatilimPos::class => KuveytPosCrypt::class,
];

if (isset($classMappings[$gatewayClass])) {
Expand Down
10 changes: 5 additions & 5 deletions src/Factory/RequestDataMapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,24 @@ class RequestDataMapperFactory
public static function createGatewayRequestMapper(string $gatewayClass, EventDispatcherInterface $eventDispatcher, CryptInterface $crypt, array $currencies = []): RequestDataMapperInterface
{
$classMappings = [
ToslaPos::class => ToslaPosRequestDataMapper::class,
AkbankPos::class => AkbankPosRequestDataMapper::class,
EstPos::class => EstPosRequestDataMapper::class,
EstV3Pos::class => EstV3PosRequestDataMapper::class,
GarantiPos::class => GarantiPosRequestDataMapper::class,
InterPos::class => InterPosRequestDataMapper::class,
KuveytPos::class => KuveytPosRequestDataMapper::class,
VakifKatilimPos::class => VakifKatilimPosRequestDataMapper::class,
PayFlexCPV4Pos::class => PayFlexCPV4PosRequestDataMapper::class,
PayFlexV4Pos::class => PayFlexV4PosRequestDataMapper::class,
PayForPos::class => PayForPosRequestDataMapper::class,
PosNet::class => PosNetRequestDataMapper::class,
PosNetV1Pos::class => PosNetV1PosRequestDataMapper::class,
PayFlexCPV4Pos::class => PayFlexCPV4PosRequestDataMapper::class,
PayFlexV4Pos::class => PayFlexV4PosRequestDataMapper::class,
ToslaPos::class => ToslaPosRequestDataMapper::class,
VakifKatilimPos::class => VakifKatilimPosRequestDataMapper::class,
];
if (isset($classMappings[$gatewayClass])) {
return new $classMappings[$gatewayClass]($eventDispatcher, $crypt, $currencies);
}

throw new DomainException('unsupported gateway');
throw new DomainException(\sprintf('Request data mapper not found for the gateway %s', $gatewayClass));
}
}
12 changes: 6 additions & 6 deletions src/Factory/ResponseDataMapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ class ResponseDataMapperFactory
public static function createGatewayResponseMapper(string $gatewayClass, RequestDataMapperInterface $requestDataMapper, LoggerInterface $logger): ResponseDataMapperInterface
{
$classMappings = [
ToslaPos::class => ToslaPosResponseDataMapper::class,
AkbankPos::class => AkbankPosResponseDataMapper::class,
EstV3Pos::class => EstPosResponseDataMapper::class,
EstPos::class => EstPosResponseDataMapper::class,
EstV3Pos::class => EstPosResponseDataMapper::class,
GarantiPos::class => GarantiPosResponseDataMapper::class,
InterPos::class => InterPosResponseDataMapper::class,
KuveytPos::class => KuveytPosResponseDataMapper::class,
VakifKatilimPos::class => VakifKatilimPosResponseDataMapper::class,
PayFlexCPV4Pos::class => PayFlexCPV4PosResponseDataMapper::class,
PayFlexV4Pos::class => PayFlexV4PosResponseDataMapper::class,
PayForPos::class => PayForPosResponseDataMapper::class,
PosNet::class => PosNetResponseDataMapper::class,
PosNetV1Pos::class => PosNetV1PosResponseDataMapper::class,
PayFlexV4Pos::class => PayFlexV4PosResponseDataMapper::class,
PayFlexCPV4Pos::class => PayFlexCPV4PosResponseDataMapper::class,
ToslaPos::class => ToslaPosResponseDataMapper::class,
VakifKatilimPos::class => VakifKatilimPosResponseDataMapper::class,
];

if (isset($classMappings[$gatewayClass])) {
Expand All @@ -74,6 +74,6 @@ public static function createGatewayResponseMapper(string $gatewayClass, Request
);
}

throw new DomainException('unsupported gateway');
throw new DomainException(\sprintf('Response data mapper not found for the gateway %s', $gatewayClass));
}
}
6 changes: 3 additions & 3 deletions src/Factory/SerializerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ public static function createGatewaySerializer(string $gatewayClass): Serializer
{
/** @var SerializerInterface[] $serializers */
$serializers = [
ToslaPosSerializer::class,
AkbankPosSerializer::class,
EstPosSerializer::class,
GarantiPosSerializer::class,
InterPosSerializer::class,
KuveytPosSerializer::class,
VakifKatilimPosSerializer::class,
PayFlexV4PosSerializer::class,
PayFlexCPV4PosSerializer::class,
PayFlexV4PosSerializer::class,
PayForPosSerializer::class,
PosNetSerializer::class,
PosNetV1PosSerializer::class,
ToslaPosSerializer::class,
VakifKatilimPosSerializer::class,
];

foreach ($serializers as $serializer) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Factory/AccountFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @license MIT
*/

namespace Factory;
namespace Mews\Pos\Tests\Unit\Factory;

use Mews\Pos\Factory\AccountFactory;
use Mews\Pos\PosInterface;
Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/Factory/CryptFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* @license MIT
*/

namespace Mews\Pos\Tests\Unit\Factory;

use Mews\Pos\Factory\CryptFactory;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

/**
* @covers \Mews\Pos\Factory\CryptFactory
*/
class CryptFactoryTest extends TestCase
{

/**
* @dataProvider createGatewayCryptDataProvider
*/
public function testCreateGatewayCrypt(string $gatewayClass, string $serializerClass): void
{
$logger = $this->createMock(LoggerInterface::class);
$crypt = CryptFactory::createGatewayCrypt($gatewayClass, $logger);
$this->assertInstanceOf($serializerClass, $crypt);
}

public static function createGatewayCryptDataProvider(): array
{
return [
[\Mews\Pos\Gateways\AkbankPos::class, \Mews\Pos\Crypt\AkbankPosCrypt::class],
[\Mews\Pos\Gateways\EstPos::class, \Mews\Pos\Crypt\EstPosCrypt::class],
[\Mews\Pos\Gateways\EstV3Pos::class, \Mews\Pos\Crypt\EstV3PosCrypt::class],
[\Mews\Pos\Gateways\GarantiPos::class, \Mews\Pos\Crypt\GarantiPosCrypt::class],
[\Mews\Pos\Gateways\InterPos::class, \Mews\Pos\Crypt\InterPosCrypt::class],
[\Mews\Pos\Gateways\KuveytPos::class, \Mews\Pos\Crypt\KuveytPosCrypt::class],
[\Mews\Pos\Gateways\PayFlexV4Pos::class, \Mews\Pos\Crypt\NullCrypt::class],
[\Mews\Pos\Gateways\PayFlexCPV4Pos::class, \Mews\Pos\Crypt\PayFlexCPV4Crypt::class],
[\Mews\Pos\Gateways\PayForPos::class, \Mews\Pos\Crypt\PayForPosCrypt::class],
[\Mews\Pos\Gateways\PosNet::class, \Mews\Pos\Crypt\PosNetCrypt::class],
[\Mews\Pos\Gateways\PosNetV1Pos::class, \Mews\Pos\Crypt\PosNetV1PosCrypt::class],
[\Mews\Pos\Gateways\ToslaPos::class, \Mews\Pos\Crypt\ToslaPosCrypt::class],
[\Mews\Pos\Gateways\VakifKatilimPos::class, \Mews\Pos\Crypt\KuveytPosCrypt::class],
[\stdClass::class, \Mews\Pos\Crypt\NullCrypt::class],
];
}
}
198 changes: 198 additions & 0 deletions tests/Unit/Factory/PosFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

namespace Mews\Pos\Tests\Unit\Factory;

use Mews\Pos\Entity\Account\AbstractPosAccount;
use Mews\Pos\Exceptions\BankClassNullException;
use Mews\Pos\Exceptions\BankNotFoundException;
use Mews\Pos\Factory\PosFactory;
use PHPUnit\Framework\TestCase;

/**
* @covers \Mews\Pos\Factory\PosFactory
*/
class PosFactoryTest extends TestCase
{

/**
* @dataProvider createPosGatewayDataProvider
*/
public function testCreatePosGateway(array $config, string $configKey, bool $cardTypeMapping, string $expectedGatewayClass): void
{
$account = $this->createMock(AbstractPosAccount::class);
$account->expects(self::atLeastOnce())
->method('getBank')
->willReturn($configKey);

$eventDispatcher = $this->createMock(\Psr\EventDispatcher\EventDispatcherInterface::class);
$httpClient = $this->createMock(\Mews\Pos\Client\HttpClient::class);
$logger = $this->createMock(\Psr\Log\LoggerInterface::class);

$gateway = PosFactory::createPosGateway(
$account,
$config,
$eventDispatcher,
$httpClient,
$logger
);
$this->assertInstanceOf($expectedGatewayClass, $gateway);

$this->assertSame($account, $gateway->getAccount());
$this->assertNotEmpty($gateway->getCurrencies());
if ($cardTypeMapping) {
$this->assertNotEmpty($gateway->getCardTypeMapping());
} else {
$this->assertEmpty($gateway->getCardTypeMapping());
}
}


public function testCreatePosGatewayWithOnlyRequiredParameters(): void
{
$gatewayClass = \Mews\Pos\Gateways\AkbankPos::class;
$config = [
'banks' => [
'akbank' => [
'name' => 'Akbank',
'class' => $gatewayClass,
'gateway_endpoints' => [
'payment_api' => 'https://apipre.akbank.com/api/v1/payment/virtualpos',
'gateway_3d' => 'https://virtualpospaymentgatewaypre.akbank.com/securepay',
'gateway_3d_host' => 'https://virtualpospaymentgatewaypre.akbank.com/payhosting',
],
],
],
];
$account = $this->createMock(AbstractPosAccount::class);
$account->expects(self::atLeastOnce())
->method('getBank')
->willReturn('akbank');

$eventDispatcher = $this->createMock(\Psr\EventDispatcher\EventDispatcherInterface::class);

$gateway = PosFactory::createPosGateway(
$account,
$config,
$eventDispatcher,
);
$this->assertInstanceOf($gatewayClass, $gateway);
}

/**
* @dataProvider createPosGatewayDataExceptionProvider
*/
public function testCreatePosGatewayFail(array $config, string $configKey, string $expectedExceptionClass): void
{
$account = $this->createMock(AbstractPosAccount::class);
$account->expects(self::atLeastOnce())
->method('getBank')
->willReturn($configKey);

$eventDispatcher = $this->createMock(\Psr\EventDispatcher\EventDispatcherInterface::class);

$this->expectException($expectedExceptionClass);
PosFactory::createPosGateway(
$account,
$config,
$eventDispatcher,
);
}

public static function createPosGatewayDataExceptionProvider(): \Generator
{
yield 'missing_gateway_class_in_config' => [
'config' => [
'banks' => [
'akbank' => [
'name' => 'Akbank',
'gateway_endpoints' => [
'payment_api' => 'https://apipre.akbank.com/api/v1/payment/virtualpos',
'gateway_3d' => 'https://virtualpospaymentgatewaypre.akbank.com/securepay',
'gateway_3d_host' => 'https://virtualpospaymentgatewaypre.akbank.com/payhosting',
],
],
],
],
'config_key' => 'akbank',
'expected_exception_class' => BankClassNullException::class,
];

yield 'invalid_gateway_class' => [
'config' => [
'banks' => [
'akbank' => [
'name' => 'Akbank',
'class' => \stdClass::class,
'gateway_endpoints' => [
'payment_api' => 'https://apipre.akbank.com/api/v1/payment/virtualpos',
'gateway_3d' => 'https://virtualpospaymentgatewaypre.akbank.com/securepay',
'gateway_3d_host' => 'https://virtualpospaymentgatewaypre.akbank.com/payhosting',
],
],
],
],
'config_key' => 'akbank',
'expected_exception_class' => \InvalidArgumentException::class,
];

yield 'non_existing_config_key' => [
'config' => [
'banks' => [
'estpos' => [
'name' => 'Akbank',
'class' => \stdClass::class,
'gateway_endpoints' => [
'payment_api' => 'https://apipre.akbank.com/api/v1/payment/virtualpos',
'gateway_3d' => 'https://virtualpospaymentgatewaypre.akbank.com/securepay',
'gateway_3d_host' => 'https://virtualpospaymentgatewaypre.akbank.com/payhosting',
],
],
],
],
'config_key' => 'akbank',
'expected_exception_class' => BankNotFoundException::class,
];
}

public static function createPosGatewayDataProvider(): \Generator
{
$gatewayClasses = [
\Mews\Pos\Gateways\AkbankPos::class => false,
\Mews\Pos\Gateways\EstPos::class => false,
\Mews\Pos\Gateways\EstV3Pos::class => false,
\Mews\Pos\Gateways\GarantiPos::class => false,
\Mews\Pos\Gateways\InterPos::class => true,
\Mews\Pos\Gateways\KuveytPos::class => true,
\Mews\Pos\Gateways\PayFlexCPV4Pos::class => true,
\Mews\Pos\Gateways\PayFlexV4Pos::class => true,
\Mews\Pos\Gateways\PayForPos::class => false,
\Mews\Pos\Gateways\PosNet::class => false,
\Mews\Pos\Gateways\PosNetV1Pos::class => false,
\Mews\Pos\Gateways\ToslaPos::class => false,
\Mews\Pos\Gateways\VakifKatilimPos::class => false,
];

foreach ($gatewayClasses as $gatewayClass => $cardTypeMapping) {
$configKey = 'abcdse';
$config = [
'banks' => [
$configKey => [
'name' => 'Akbank',
'class' => $gatewayClass,
'gateway_endpoints' => [
'payment_api' => 'https://apipre.akbank.com/api/v1/payment/virtualpos',
'gateway_3d' => 'https://virtualpospaymentgatewaypre.akbank.com/securepay',
'gateway_3d_host' => 'https://virtualpospaymentgatewaypre.akbank.com/payhosting',
],
],
],
];
yield [
$config,
$configKey,
$cardTypeMapping,
$gatewayClass,
];
}
}
}
Loading

0 comments on commit 1055b66

Please sign in to comment.