diff --git a/src/Factory/CryptFactory.php b/src/Factory/CryptFactory.php index 13d056e1..952e9b51 100644 --- a/src/Factory/CryptFactory.php +++ b/src/Factory/CryptFactory.php @@ -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])) { diff --git a/src/Factory/RequestDataMapperFactory.php b/src/Factory/RequestDataMapperFactory.php index 72e81a54..9dd1ec31 100644 --- a/src/Factory/RequestDataMapperFactory.php +++ b/src/Factory/RequestDataMapperFactory.php @@ -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)); } } diff --git a/src/Factory/ResponseDataMapperFactory.php b/src/Factory/ResponseDataMapperFactory.php index 8a7f45da..141d6db9 100644 --- a/src/Factory/ResponseDataMapperFactory.php +++ b/src/Factory/ResponseDataMapperFactory.php @@ -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])) { @@ -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)); } } diff --git a/src/Factory/SerializerFactory.php b/src/Factory/SerializerFactory.php index fb4d1d20..008ed86d 100644 --- a/src/Factory/SerializerFactory.php +++ b/src/Factory/SerializerFactory.php @@ -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) { diff --git a/tests/Unit/Factory/AccountFactoryTest.php b/tests/Unit/Factory/AccountFactoryTest.php index c6172b2f..b6a46593 100644 --- a/tests/Unit/Factory/AccountFactoryTest.php +++ b/tests/Unit/Factory/AccountFactoryTest.php @@ -3,7 +3,7 @@ * @license MIT */ -namespace Factory; +namespace Mews\Pos\Tests\Unit\Factory; use Mews\Pos\Factory\AccountFactory; use Mews\Pos\PosInterface; diff --git a/tests/Unit/Factory/CryptFactoryTest.php b/tests/Unit/Factory/CryptFactoryTest.php new file mode 100644 index 00000000..02ad7ffa --- /dev/null +++ b/tests/Unit/Factory/CryptFactoryTest.php @@ -0,0 +1,47 @@ +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], + ]; + } +} diff --git a/tests/Unit/Factory/PosFactoryTest.php b/tests/Unit/Factory/PosFactoryTest.php new file mode 100644 index 00000000..ede92a87 --- /dev/null +++ b/tests/Unit/Factory/PosFactoryTest.php @@ -0,0 +1,198 @@ +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, + ]; + } + } +} diff --git a/tests/Unit/Factory/RequestDataMapperFactoryTest.php b/tests/Unit/Factory/RequestDataMapperFactoryTest.php new file mode 100644 index 00000000..00219bf0 --- /dev/null +++ b/tests/Unit/Factory/RequestDataMapperFactoryTest.php @@ -0,0 +1,65 @@ +createMock(\Psr\EventDispatcher\EventDispatcherInterface::class); + $crypt = $this->createMock(\Mews\Pos\Crypt\CryptInterface::class); + $currencies = []; + $mapper = RequestDataMapperFactory::createGatewayRequestMapper( + $gatewayClass, + $eventDispatcher, + $crypt, + $currencies + ); + $this->assertInstanceOf($mapperClass, $mapper); + } + + public function testCreateGatewayRequestMapperUnsupported(): void + { + $eventDispatcher = $this->createMock(\Psr\EventDispatcher\EventDispatcherInterface::class); + $crypt = $this->createMock(\Mews\Pos\Crypt\CryptInterface::class); + $currencies = []; + $this->expectException(\DomainException::class); + RequestDataMapperFactory::createGatewayRequestMapper( + \stdClass::class, + $eventDispatcher, + $crypt, + $currencies + ); + } + + public static function createGatewayRequestMapperDataProvider(): array + { + return [ + [\Mews\Pos\Gateways\AkbankPos::class, \Mews\Pos\DataMapper\RequestDataMapper\AkbankPosRequestDataMapper::class], + [\Mews\Pos\Gateways\EstPos::class, \Mews\Pos\DataMapper\RequestDataMapper\EstPosRequestDataMapper::class], + [\Mews\Pos\Gateways\EstV3Pos::class, \Mews\Pos\DataMapper\RequestDataMapper\EstV3PosRequestDataMapper::class], + [\Mews\Pos\Gateways\GarantiPos::class, \Mews\Pos\DataMapper\RequestDataMapper\GarantiPosRequestDataMapper::class], + [\Mews\Pos\Gateways\InterPos::class, \Mews\Pos\DataMapper\RequestDataMapper\InterPosRequestDataMapper::class], + [\Mews\Pos\Gateways\KuveytPos::class, \Mews\Pos\DataMapper\RequestDataMapper\KuveytPosRequestDataMapper::class], + [\Mews\Pos\Gateways\PayFlexCPV4Pos::class, \Mews\Pos\DataMapper\RequestDataMapper\PayFlexCPV4PosRequestDataMapper::class], + [\Mews\Pos\Gateways\PayForPos::class, \Mews\Pos\DataMapper\RequestDataMapper\PayForPosRequestDataMapper::class], + [\Mews\Pos\Gateways\PosNet::class, \Mews\Pos\DataMapper\RequestDataMapper\PosNetRequestDataMapper::class], + [\Mews\Pos\Gateways\PosNetV1Pos::class, \Mews\Pos\DataMapper\RequestDataMapper\PosNetV1PosRequestDataMapper::class], + [\Mews\Pos\Gateways\ToslaPos::class, \Mews\Pos\DataMapper\RequestDataMapper\ToslaPosRequestDataMapper::class], + [\Mews\Pos\Gateways\VakifKatilimPos::class, \Mews\Pos\DataMapper\RequestDataMapper\VakifKatilimPosRequestDataMapper::class], + ]; + } +} diff --git a/tests/Unit/Factory/ResponseDataMapperFactoryTest.php b/tests/Unit/Factory/ResponseDataMapperFactoryTest.php new file mode 100644 index 00000000..2294ac0c --- /dev/null +++ b/tests/Unit/Factory/ResponseDataMapperFactoryTest.php @@ -0,0 +1,64 @@ +createMock(RequestDataMapperInterface::class); + $logger = $this->createMock(LoggerInterface::class); + $mapper = ResponseDataMapperFactory::createGatewayResponseMapper( + $gatewayClass, + $requestDataMapper, + $logger + ); + $this->assertInstanceOf($mapperClass, $mapper); + } + + public function testCreateGatewayResponseMapperUnsupported(): void + { + $requestDataMapper = $this->createMock(RequestDataMapperInterface::class); + $logger = $this->createMock(LoggerInterface::class); + $this->expectException(\DomainException::class); + ResponseDataMapperFactory::createGatewayResponseMapper( + \stdClass::class, + $requestDataMapper, + $logger + ); + } + + public static function createGatewayResponseMapperDataProvider(): array + { + return [ + [\Mews\Pos\Gateways\AkbankPos::class, \Mews\Pos\DataMapper\ResponseDataMapper\AkbankPosResponseDataMapper::class], + [\Mews\Pos\Gateways\EstPos::class, \Mews\Pos\DataMapper\ResponseDataMapper\EstPosResponseDataMapper::class], + [\Mews\Pos\Gateways\EstV3Pos::class, \Mews\Pos\DataMapper\ResponseDataMapper\EstPosResponseDataMapper::class], + [\Mews\Pos\Gateways\GarantiPos::class, \Mews\Pos\DataMapper\ResponseDataMapper\GarantiPosResponseDataMapper::class], + [\Mews\Pos\Gateways\InterPos::class, \Mews\Pos\DataMapper\ResponseDataMapper\InterPosResponseDataMapper::class], + [\Mews\Pos\Gateways\KuveytPos::class, \Mews\Pos\DataMapper\ResponseDataMapper\KuveytPosResponseDataMapper::class], + [\Mews\Pos\Gateways\PayFlexCPV4Pos::class, \Mews\Pos\DataMapper\ResponseDataMapper\PayFlexCPV4PosResponseDataMapper::class], + [\Mews\Pos\Gateways\PayFlexV4Pos::class, \Mews\Pos\DataMapper\ResponseDataMapper\PayFlexV4PosResponseDataMapper::class], + [\Mews\Pos\Gateways\PayForPos::class, \Mews\Pos\DataMapper\ResponseDataMapper\PayForPosResponseDataMapper::class], + [\Mews\Pos\Gateways\PosNet::class, \Mews\Pos\DataMapper\ResponseDataMapper\PosNetResponseDataMapper::class], + [\Mews\Pos\Gateways\PosNetV1Pos::class, \Mews\Pos\DataMapper\ResponseDataMapper\PosNetV1PosResponseDataMapper::class], + [\Mews\Pos\Gateways\ToslaPos::class, \Mews\Pos\DataMapper\ResponseDataMapper\ToslaPosResponseDataMapper::class], + [\Mews\Pos\Gateways\VakifKatilimPos::class, \Mews\Pos\DataMapper\ResponseDataMapper\VakifKatilimPosResponseDataMapper::class], + ]; + } +} diff --git a/tests/Unit/Factory/SerializerFactoryTest.php b/tests/Unit/Factory/SerializerFactoryTest.php new file mode 100644 index 00000000..ba8a3897 --- /dev/null +++ b/tests/Unit/Factory/SerializerFactoryTest.php @@ -0,0 +1,50 @@ +assertInstanceOf($serializerClass, $serializer); + } + + public function testCreateGatewaySerializerUnsupported(): void + { + $this->expectException(\DomainException::class); + SerializerFactory::createGatewaySerializer(\stdClass::class); + } + + public function createGatewaySerializerDataProvider(): array + { + return [ + [\Mews\Pos\Gateways\AkbankPos::class, \Mews\Pos\Serializer\AkbankPosSerializer::class], + [\Mews\Pos\Gateways\EstPos::class, \Mews\Pos\Serializer\EstPosSerializer::class], + [\Mews\Pos\Gateways\EstV3Pos::class, \Mews\Pos\Serializer\EstPosSerializer::class], + [\Mews\Pos\Gateways\GarantiPos::class, \Mews\Pos\Serializer\GarantiPosSerializer::class], + [\Mews\Pos\Gateways\InterPos::class, \Mews\Pos\Serializer\InterPosSerializer::class], + [\Mews\Pos\Gateways\KuveytPos::class, \Mews\Pos\Serializer\KuveytPosSerializer::class], + [\Mews\Pos\Gateways\PayFlexCPV4Pos::class, \Mews\Pos\Serializer\PayFlexCPV4PosSerializer::class], + [\Mews\Pos\Gateways\PayFlexV4Pos::class, \Mews\Pos\Serializer\PayFlexV4PosSerializer::class], + [\Mews\Pos\Gateways\PayForPos::class, \Mews\Pos\Serializer\PayForPosSerializer::class], + [\Mews\Pos\Gateways\PosNet::class, \Mews\Pos\Serializer\PosNetSerializer::class], + [\Mews\Pos\Gateways\PosNetV1Pos::class, \Mews\Pos\Serializer\PosNetV1PosSerializer::class], + [\Mews\Pos\Gateways\ToslaPos::class, \Mews\Pos\Serializer\ToslaPosSerializer::class], + [\Mews\Pos\Gateways\VakifKatilimPos::class, \Mews\Pos\Serializer\VakifKatilimPosSerializer::class], + ]; + } +}